blob: 4ced07ba11aa4f6a860e41ec27d5c6565c39b4bf [file] [log] [blame]
Andrew Geissler595f6302022-01-24 19:11:47 +00001# Class for setting up /etc in overlayfs
2#
3# In order to have /etc directory in overlayfs a special handling at early boot stage is required
4# The idea is to supply a custom init script that mounts /etc before launching actual init program,
5# because the latter already requires /etc to be mounted
6#
7# The configuration must be machine specific. You should at least set these three variables:
8# OVERLAYFS_ETC_MOUNT_POINT ?= "/data"
9# OVERLAYFS_ETC_FSTYPE ?= "ext4"
10# OVERLAYFS_ETC_DEVICE ?= "/dev/mmcblk0p2"
11#
12# To control more mount options you should consider setting mount options:
13# OVERLAYFS_ETC_MOUNT_OPTIONS ?= "defaults"
14#
15# The class provides two options for /sbin/init generation
16# 1. Default option is to rename original /sbin/init to /sbin/init.orig and place generated init under
17# original name, i.e. /sbin/init. It has an advantage that you won't need to change any kernel
18# parameters in order to make it work, but it poses a restriction that package-management can't
19# be used, becaause updating init manager would remove generated script
20# 2. If you are would like to keep original init as is, you can set
21# OVERLAYFS_ETC_USE_ORIG_INIT_NAME = "0"
22# Then generated init will be named /sbin/preinit and you would need to extend you kernel parameters
23# manually in your bootloader configuration.
24#
25# Regardless which mode you choose, update and migration strategy of configuration files under /etc
26# overlay is out of scope of this class
27
28ROOTFS_POSTPROCESS_COMMAND += '${@bb.utils.contains("IMAGE_FEATURES", "overlayfs-etc", "create_overlayfs_etc_preinit;", "", d)}'
29IMAGE_FEATURES_CONFLICTS_overlayfs-etc = "package-management"
30
31OVERLAYFS_ETC_MOUNT_POINT ??= ""
32OVERLAYFS_ETC_FSTYPE ??= ""
33OVERLAYFS_ETC_DEVICE ??= ""
34OVERLAYFS_ETC_USE_ORIG_INIT_NAME ??= "1"
35OVERLAYFS_ETC_MOUNT_OPTIONS ??= "defaults"
36OVERLAYFS_ETC_INIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-etc-preinit.sh.in"
37
38python create_overlayfs_etc_preinit() {
39 overlayEtcMountPoint = d.getVar("OVERLAYFS_ETC_MOUNT_POINT")
40 overlayEtcFsType = d.getVar("OVERLAYFS_ETC_FSTYPE")
41 overlayEtcDevice = d.getVar("OVERLAYFS_ETC_DEVICE")
42
43 if not overlayEtcMountPoint:
44 bb.fatal("OVERLAYFS_ETC_MOUNT_POINT must be set in your MACHINE configuration")
45 if not overlayEtcDevice:
46 bb.fatal("OVERLAYFS_ETC_DEVICE must be set in your MACHINE configuration")
47 if not overlayEtcFsType:
48 bb.fatal("OVERLAYFS_ETC_FSTYPE should contain a valid file system type on {0}".format(overlayEtcDevice))
49
50 with open(d.getVar("OVERLAYFS_ETC_INIT_TEMPLATE"), "r") as f:
51 PreinitTemplate = f.read()
52
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
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 }
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
73 with open(preinitPath, 'w') as f:
74 f.write(PreinitTemplate.format(**args))
75 os.chmod(preinitPath, 0o755)
76}