rootfs-postcommands.bbclass 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # Zap the root password if debug-tweaks feature is not enabled
  2. ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'empty-root-password' ], "", "zap_empty_root_password ; ",d)}'
  3. # Allow dropbear/openssh to accept logins from accounts with an empty password string if debug-tweaks is enabled
  4. ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'allow-empty-password' ], "ssh_allow_empty_password; ", "",d)}'
  5. # Enable postinst logging if debug-tweaks is enabled
  6. ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains_any("IMAGE_FEATURES", [ 'debug-tweaks', 'post-install-logging' ], "postinst_enable_logging; ", "",d)}'
  7. # Create /etc/timestamp during image construction to give a reasonably sane default time setting
  8. ROOTFS_POSTPROCESS_COMMAND += "rootfs_update_timestamp ; "
  9. # Tweak the mount options for rootfs in /etc/fstab if read-only-rootfs is enabled
  10. ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "read-only-rootfs", "read_only_rootfs_hook; ", "",d)}'
  11. # Generates test data file with data store variables expanded in json format
  12. ROOTFS_POSTPROCESS_COMMAND += "write_image_test_data ; "
  13. # Write manifest
  14. IMAGE_MANIFEST = "${IMGDEPLOYDIR}/${IMAGE_NAME}.rootfs.manifest"
  15. ROOTFS_POSTUNINSTALL_COMMAND =+ "write_image_manifest ; "
  16. # Set default postinst log file
  17. POSTINST_LOGFILE ?= "${localstatedir}/log/postinstall.log"
  18. # Set default target for systemd images
  19. SYSTEMD_DEFAULT_TARGET ?= '${@bb.utils.contains("IMAGE_FEATURES", "x11-base", "graphical.target", "multi-user.target", d)}'
  20. ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("DISTRO_FEATURES", "systemd", "set_systemd_default_target; systemd_create_users;", "", d)}'
  21. ROOTFS_POSTPROCESS_COMMAND += 'empty_var_volatile;'
  22. # Disable DNS lookups, the SSH_DISABLE_DNS_LOOKUP can be overridden to allow
  23. # distros to choose not to take this change
  24. SSH_DISABLE_DNS_LOOKUP ?= " ssh_disable_dns_lookup ; "
  25. ROOTFS_POSTPROCESS_COMMAND_append_qemuall = "${SSH_DISABLE_DNS_LOOKUP}"
  26. # Sort the user and group entries in /etc by ID in order to make the content
  27. # deterministic. Package installs are not deterministic, causing the ordering
  28. # of entries to change between builds. In case that this isn't desired,
  29. # the command can be overridden.
  30. #
  31. # Note that useradd-staticids.bbclass has to be used to ensure that
  32. # the numeric IDs of dynamically created entries remain stable.
  33. #
  34. # We want this to run as late as possible, in particular after
  35. # systemd_sysusers_create and set_user_group. Using _append is not
  36. # enough for that, set_user_group is added that way and would end
  37. # up running after us.
  38. SORT_PASSWD_POSTPROCESS_COMMAND ??= " sort_passwd; "
  39. python () {
  40. d.appendVar('ROOTFS_POSTPROCESS_COMMAND', '${SORT_PASSWD_POSTPROCESS_COMMAND}')
  41. }
  42. systemd_create_users () {
  43. for conffile in ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd.conf ${IMAGE_ROOTFS}/usr/lib/sysusers.d/systemd-remote.conf; do
  44. [ -e $conffile ] || continue
  45. grep -v "^#" $conffile | sed -e '/^$/d' | while read type name id comment; do
  46. if [ "$type" = "u" ]; then
  47. useradd_params="--shell /sbin/nologin"
  48. [ "$id" != "-" ] && useradd_params="$useradd_params --uid $id"
  49. [ "$comment" != "-" ] && useradd_params="$useradd_params --comment $comment"
  50. useradd_params="$useradd_params --system $name"
  51. eval useradd --root ${IMAGE_ROOTFS} $useradd_params || true
  52. elif [ "$type" = "g" ]; then
  53. groupadd_params=""
  54. [ "$id" != "-" ] && groupadd_params="$groupadd_params --gid $id"
  55. groupadd_params="$groupadd_params --system $name"
  56. eval groupadd --root ${IMAGE_ROOTFS} $groupadd_params || true
  57. elif [ "$type" = "m" ]; then
  58. group=$id
  59. if [ ! `grep -q "^${group}:" ${IMAGE_ROOTFS}${sysconfdir}/group` ]; then
  60. eval groupadd --root ${IMAGE_ROOTFS} --system $group
  61. fi
  62. if [ ! `grep -q "^${name}:" ${IMAGE_ROOTFS}${sysconfdir}/passwd` ]; then
  63. eval useradd --root ${IMAGE_ROOTFS} --shell /sbin/nologin --system $name
  64. fi
  65. eval usermod --root ${IMAGE_ROOTFS} -a -G $group $name
  66. fi
  67. done
  68. done
  69. }
  70. #
  71. # A hook function to support read-only-rootfs IMAGE_FEATURES
  72. #
  73. read_only_rootfs_hook () {
  74. # Tweak the mount option and fs_passno for rootfs in fstab
  75. sed -i -e '/^[#[:space:]]*\/dev\/root/{s/defaults/ro/;s/\([[:space:]]*[[:digit:]]\)\([[:space:]]*\)[[:digit:]]$/\1\20/}' ${IMAGE_ROOTFS}/etc/fstab
  76. # If we're using openssh and the /etc/ssh directory has no pre-generated keys,
  77. # we should configure openssh to use the configuration file /etc/ssh/sshd_config_readonly
  78. # and the keys under /var/run/ssh.
  79. if [ -d ${IMAGE_ROOTFS}/etc/ssh ]; then
  80. if [ -e ${IMAGE_ROOTFS}/etc/ssh/ssh_host_rsa_key ]; then
  81. echo "SYSCONFDIR=/etc/ssh" >> ${IMAGE_ROOTFS}/etc/default/ssh
  82. echo "SSHD_OPTS=" >> ${IMAGE_ROOTFS}/etc/default/ssh
  83. else
  84. echo "SYSCONFDIR=/var/run/ssh" >> ${IMAGE_ROOTFS}/etc/default/ssh
  85. echo "SSHD_OPTS='-f /etc/ssh/sshd_config_readonly'" >> ${IMAGE_ROOTFS}/etc/default/ssh
  86. fi
  87. fi
  88. # Also tweak the key location for dropbear in the same way.
  89. if [ -d ${IMAGE_ROOTFS}/etc/dropbear ]; then
  90. if [ -e ${IMAGE_ROOTFS}/etc/dropbear/dropbear_rsa_host_key ]; then
  91. echo "DROPBEAR_RSAKEY_DIR=/etc/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
  92. else
  93. echo "DROPBEAR_RSAKEY_DIR=/var/lib/dropbear" >> ${IMAGE_ROOTFS}/etc/default/dropbear
  94. fi
  95. fi
  96. if ${@bb.utils.contains("DISTRO_FEATURES", "sysvinit", "true", "false", d)}; then
  97. # Change the value of ROOTFS_READ_ONLY in /etc/default/rcS to yes
  98. if [ -e ${IMAGE_ROOTFS}/etc/default/rcS ]; then
  99. sed -i 's/ROOTFS_READ_ONLY=no/ROOTFS_READ_ONLY=yes/' ${IMAGE_ROOTFS}/etc/default/rcS
  100. fi
  101. # Run populate-volatile.sh at rootfs time to set up basic files
  102. # and directories to support read-only rootfs.
  103. if [ -x ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh ]; then
  104. ${IMAGE_ROOTFS}/etc/init.d/populate-volatile.sh
  105. fi
  106. fi
  107. }
  108. #
  109. # This function is intended to disallow empty root password if 'debug-tweaks' is not in IMAGE_FEATURES.
  110. #
  111. zap_empty_root_password () {
  112. if [ -e ${IMAGE_ROOTFS}/etc/shadow ]; then
  113. sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/shadow
  114. fi
  115. if [ -e ${IMAGE_ROOTFS}/etc/passwd ]; then
  116. sed -i 's%^root::%root:*:%' ${IMAGE_ROOTFS}/etc/passwd
  117. fi
  118. }
  119. #
  120. # allow dropbear/openssh to accept root logins and logins from accounts with an empty password string
  121. #
  122. ssh_allow_empty_password () {
  123. for config in sshd_config sshd_config_readonly; do
  124. if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config ]; then
  125. sed -i 's/^[#[:space:]]*PermitRootLogin.*/PermitRootLogin yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
  126. sed -i 's/^[#[:space:]]*PermitEmptyPasswords.*/PermitEmptyPasswords yes/' ${IMAGE_ROOTFS}${sysconfdir}/ssh/$config
  127. fi
  128. done
  129. if [ -e ${IMAGE_ROOTFS}${sbindir}/dropbear ] ; then
  130. if grep -q DROPBEAR_EXTRA_ARGS ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear 2>/dev/null ; then
  131. if ! grep -q "DROPBEAR_EXTRA_ARGS=.*-B" ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear ; then
  132. sed -i 's/^DROPBEAR_EXTRA_ARGS="*\([^"]*\)"*/DROPBEAR_EXTRA_ARGS="\1 -B"/' ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
  133. fi
  134. else
  135. printf '\nDROPBEAR_EXTRA_ARGS="-B"\n' >> ${IMAGE_ROOTFS}${sysconfdir}/default/dropbear
  136. fi
  137. fi
  138. if [ -d ${IMAGE_ROOTFS}${sysconfdir}/pam.d ] ; then
  139. sed -i 's/nullok_secure/nullok/' ${IMAGE_ROOTFS}${sysconfdir}/pam.d/*
  140. fi
  141. }
  142. ssh_disable_dns_lookup () {
  143. if [ -e ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config ]; then
  144. sed -i -e 's:#UseDNS yes:UseDNS no:' ${IMAGE_ROOTFS}${sysconfdir}/ssh/sshd_config
  145. fi
  146. }
  147. python sort_passwd () {
  148. import rootfspostcommands
  149. rootfspostcommands.sort_passwd(d.expand('${IMAGE_ROOTFS}${sysconfdir}'))
  150. }
  151. #
  152. # Enable postinst logging if debug-tweaks is enabled
  153. #
  154. postinst_enable_logging () {
  155. mkdir -p ${IMAGE_ROOTFS}${sysconfdir}/default
  156. echo "POSTINST_LOGGING=1" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
  157. echo "LOGFILE=${POSTINST_LOGFILE}" >> ${IMAGE_ROOTFS}${sysconfdir}/default/postinst
  158. }
  159. #
  160. # Modify systemd default target
  161. #
  162. set_systemd_default_target () {
  163. if [ -d ${IMAGE_ROOTFS}${sysconfdir}/systemd/system -a -e ${IMAGE_ROOTFS}${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ]; then
  164. ln -sf ${systemd_unitdir}/system/${SYSTEMD_DEFAULT_TARGET} ${IMAGE_ROOTFS}${sysconfdir}/systemd/system/default.target
  165. fi
  166. }
  167. # If /var/volatile is not empty, we have seen problems where programs such as the
  168. # journal make assumptions based on the contents of /var/volatile. The journal
  169. # would then write to /var/volatile before it was mounted, thus hiding the
  170. # items previously written.
  171. #
  172. # This change is to attempt to fix those types of issues in a way that doesn't
  173. # affect users that may not be using /var/volatile.
  174. empty_var_volatile () {
  175. if [ -e ${IMAGE_ROOTFS}/etc/fstab ]; then
  176. match=`awk '$1 !~ "#" && $2 ~ /\/var\/volatile/{print $2}' ${IMAGE_ROOTFS}/etc/fstab 2> /dev/null`
  177. if [ -n "$match" ]; then
  178. find ${IMAGE_ROOTFS}/var/volatile -mindepth 1 -delete
  179. fi
  180. fi
  181. }
  182. # Turn any symbolic /sbin/init link into a file
  183. remove_init_link () {
  184. if [ -h ${IMAGE_ROOTFS}/sbin/init ]; then
  185. LINKFILE=${IMAGE_ROOTFS}`readlink ${IMAGE_ROOTFS}/sbin/init`
  186. rm ${IMAGE_ROOTFS}/sbin/init
  187. cp $LINKFILE ${IMAGE_ROOTFS}/sbin/init
  188. fi
  189. }
  190. make_zimage_symlink_relative () {
  191. if [ -L ${IMAGE_ROOTFS}/boot/zImage ]; then
  192. (cd ${IMAGE_ROOTFS}/boot/ && for i in `ls zImage-* | sort`; do ln -sf $i zImage; done)
  193. fi
  194. }
  195. insert_feed_uris () {
  196. echo "Building feeds for [${DISTRO}].."
  197. for line in ${FEED_URIS}
  198. do
  199. # strip leading and trailing spaces/tabs, then split into name and uri
  200. line_clean="`echo "$line"|sed 's/^[ \t]*//;s/[ \t]*$//'`"
  201. feed_name="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\1/p'`"
  202. feed_uri="`echo "$line_clean" | sed -n 's/\(.*\)##\(.*\)/\2/p'`"
  203. echo "Added $feed_name feed with URL $feed_uri"
  204. # insert new feed-sources
  205. echo "src/gz $feed_name $feed_uri" >> ${IMAGE_ROOTFS}/etc/opkg/${feed_name}-feed.conf
  206. done
  207. }
  208. python write_image_manifest () {
  209. from oe.rootfs import image_list_installed_packages
  210. from oe.utils import format_pkg_list
  211. deploy_dir = d.getVar('IMGDEPLOYDIR')
  212. link_name = d.getVar('IMAGE_LINK_NAME')
  213. manifest_name = d.getVar('IMAGE_MANIFEST')
  214. if not manifest_name:
  215. return
  216. pkgs = image_list_installed_packages(d)
  217. with open(manifest_name, 'w+') as image_manifest:
  218. image_manifest.write(format_pkg_list(pkgs, "ver"))
  219. image_manifest.write("\n")
  220. if os.path.exists(manifest_name):
  221. manifest_link = deploy_dir + "/" + link_name + ".manifest"
  222. if os.path.lexists(manifest_link):
  223. os.remove(manifest_link)
  224. os.symlink(os.path.basename(manifest_name), manifest_link)
  225. }
  226. # Can be use to create /etc/timestamp during image construction to give a reasonably
  227. # sane default time setting
  228. rootfs_update_timestamp () {
  229. date -u +%4Y%2m%2d%2H%2M%2S >${IMAGE_ROOTFS}/etc/timestamp
  230. }
  231. # Prevent X from being started
  232. rootfs_no_x_startup () {
  233. if [ -f ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm ]; then
  234. chmod a-x ${IMAGE_ROOTFS}/etc/init.d/xserver-nodm
  235. fi
  236. }
  237. rootfs_trim_schemas () {
  238. for schema in ${IMAGE_ROOTFS}/etc/gconf/schemas/*.schemas
  239. do
  240. # Need this in case no files exist
  241. if [ -e $schema ]; then
  242. oe-trim-schemas $schema > $schema.new
  243. mv $schema.new $schema
  244. fi
  245. done
  246. }
  247. rootfs_check_host_user_contaminated () {
  248. contaminated="${WORKDIR}/host-user-contaminated.txt"
  249. HOST_USER_UID="$(PSEUDO_UNLOAD=1 id -u)"
  250. HOST_USER_GID="$(PSEUDO_UNLOAD=1 id -g)"
  251. find "${IMAGE_ROOTFS}" -wholename "${IMAGE_ROOTFS}/home" -prune \
  252. -user "$HOST_USER_UID" -o -group "$HOST_USER_GID" >"$contaminated"
  253. if [ -s "$contaminated" ]; then
  254. echo "WARNING: Paths in the rootfs are owned by the same user or group as the user running bitbake. See the logfile for the specific paths."
  255. cat "$contaminated" | sed "s,^, ,"
  256. fi
  257. }
  258. # Make any absolute links in a sysroot relative
  259. rootfs_sysroot_relativelinks () {
  260. sysroot-relativelinks.py ${SDK_OUTPUT}/${SDKTARGETSYSROOT}
  261. }
  262. # Generated test data json file
  263. python write_image_test_data() {
  264. from oe.data import export2json
  265. testdata = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_NAME'))
  266. testdata_link = "%s/%s.testdata.json" % (d.getVar('DEPLOY_DIR_IMAGE'), d.getVar('IMAGE_LINK_NAME'))
  267. bb.utils.mkdirhier(os.path.dirname(testdata))
  268. export2json(d, testdata)
  269. if os.path.lexists(testdata_link):
  270. os.remove(testdata_link)
  271. os.symlink(os.path.basename(testdata), testdata_link)
  272. }