overlayfs-etc.bbclass 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #
  2. # Copyright OpenEmbedded Contributors
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. # Class for setting up /etc in overlayfs
  7. #
  8. # In order to have /etc directory in overlayfs a special handling at early boot stage is required
  9. # The idea is to supply a custom init script that mounts /etc before launching actual init program,
  10. # because the latter already requires /etc to be mounted
  11. #
  12. # The configuration must be machine specific. You should at least set these three variables:
  13. # OVERLAYFS_ETC_MOUNT_POINT ?= "/data"
  14. # OVERLAYFS_ETC_FSTYPE ?= "ext4"
  15. # OVERLAYFS_ETC_DEVICE ?= "/dev/mmcblk0p2"
  16. #
  17. # To control more mount options you should consider setting mount options:
  18. # OVERLAYFS_ETC_MOUNT_OPTIONS ?= "defaults"
  19. #
  20. # The class provides two options for /sbin/init generation
  21. # 1. Default option is to rename original /sbin/init to /sbin/init.orig and place generated init under
  22. # original name, i.e. /sbin/init. It has an advantage that you won't need to change any kernel
  23. # parameters in order to make it work, but it poses a restriction that package-management can't
  24. # be used, becaause updating init manager would remove generated script
  25. # 2. If you are would like to keep original init as is, you can set
  26. # OVERLAYFS_ETC_USE_ORIG_INIT_NAME = "0"
  27. # Then generated init will be named /sbin/preinit and you would need to extend you kernel parameters
  28. # manually in your bootloader configuration.
  29. #
  30. # Regardless which mode you choose, update and migration strategy of configuration files under /etc
  31. # overlay is out of scope of this class
  32. ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "overlayfs-etc", "create_overlayfs_etc_preinit", "", d)}'
  33. IMAGE_FEATURES_CONFLICTS_overlayfs-etc = "${@ 'package-management' if bb.utils.to_boolean(d.getVar('OVERLAYFS_ETC_USE_ORIG_INIT_NAME'), True) else ''}"
  34. OVERLAYFS_ETC_MOUNT_POINT ??= ""
  35. OVERLAYFS_ETC_FSTYPE ??= ""
  36. OVERLAYFS_ETC_DEVICE ??= ""
  37. OVERLAYFS_ETC_USE_ORIG_INIT_NAME ??= "1"
  38. OVERLAYFS_ETC_MOUNT_OPTIONS ??= "defaults"
  39. OVERLAYFS_ETC_INIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-etc-preinit.sh.in"
  40. OVERLAYFS_ETC_EXPOSE_LOWER ??= "0"
  41. python create_overlayfs_etc_preinit() {
  42. overlayEtcMountPoint = d.getVar("OVERLAYFS_ETC_MOUNT_POINT")
  43. overlayEtcFsType = d.getVar("OVERLAYFS_ETC_FSTYPE")
  44. overlayEtcDevice = d.getVar("OVERLAYFS_ETC_DEVICE")
  45. if not overlayEtcMountPoint:
  46. bb.fatal("OVERLAYFS_ETC_MOUNT_POINT must be set in your MACHINE configuration")
  47. if not overlayEtcDevice:
  48. bb.fatal("OVERLAYFS_ETC_DEVICE must be set in your MACHINE configuration")
  49. if not overlayEtcFsType:
  50. bb.fatal("OVERLAYFS_ETC_FSTYPE should contain a valid file system type on {0}".format(overlayEtcDevice))
  51. with open(d.getVar("OVERLAYFS_ETC_INIT_TEMPLATE"), "r") as f:
  52. PreinitTemplate = f.read()
  53. useOrigInit = oe.types.boolean(d.getVar('OVERLAYFS_ETC_USE_ORIG_INIT_NAME'))
  54. preinitPath = oe.path.join(d.getVar("IMAGE_ROOTFS"), d.getVar("base_sbindir"), "preinit")
  55. initBaseName = oe.path.join(d.getVar("base_sbindir"), "init")
  56. origInitNameSuffix = ".orig"
  57. exposeLower = oe.types.boolean(d.getVar('OVERLAYFS_ETC_EXPOSE_LOWER'))
  58. args = {
  59. 'OVERLAYFS_ETC_MOUNT_POINT': overlayEtcMountPoint,
  60. 'OVERLAYFS_ETC_MOUNT_OPTIONS': d.getVar('OVERLAYFS_ETC_MOUNT_OPTIONS'),
  61. 'OVERLAYFS_ETC_FSTYPE': overlayEtcFsType,
  62. 'OVERLAYFS_ETC_DEVICE': overlayEtcDevice,
  63. 'SBIN_INIT_NAME': initBaseName + origInitNameSuffix if useOrigInit else initBaseName,
  64. 'OVERLAYFS_ETC_EXPOSE_LOWER': "true" if exposeLower else "false"
  65. }
  66. if useOrigInit:
  67. # rename original /sbin/init
  68. origInit = oe.path.join(d.getVar("IMAGE_ROOTFS"), initBaseName)
  69. bb.debug(1, "rootfs path %s, init path %s, test %s" % (d.getVar('IMAGE_ROOTFS'), origInit, d.getVar("IMAGE_ROOTFS")))
  70. bb.utils.rename(origInit, origInit + origInitNameSuffix)
  71. preinitPath = origInit
  72. with open(preinitPath, 'w') as f:
  73. f.write(PreinitTemplate.format(**args))
  74. os.chmod(preinitPath, 0o755)
  75. }