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