xref: /OK3568_Linux_fs/yocto/poky/meta/classes/overlayfs-etc.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1# 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' if bb.utils.to_boolean(d.getVar('OVERLAYFS_ETC_USE_ORIG_INIT_NAME'), True) else ''}"
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"
37OVERLAYFS_ETC_EXPOSE_LOWER ??= "0"
38
39python create_overlayfs_etc_preinit() {
40    overlayEtcMountPoint = d.getVar("OVERLAYFS_ETC_MOUNT_POINT")
41    overlayEtcFsType = d.getVar("OVERLAYFS_ETC_FSTYPE")
42    overlayEtcDevice = d.getVar("OVERLAYFS_ETC_DEVICE")
43
44    if not overlayEtcMountPoint:
45        bb.fatal("OVERLAYFS_ETC_MOUNT_POINT must be set in your MACHINE configuration")
46    if not overlayEtcDevice:
47        bb.fatal("OVERLAYFS_ETC_DEVICE must be set in your MACHINE configuration")
48    if not overlayEtcFsType:
49        bb.fatal("OVERLAYFS_ETC_FSTYPE should contain a valid file system type on {0}".format(overlayEtcDevice))
50
51    with open(d.getVar("OVERLAYFS_ETC_INIT_TEMPLATE"), "r") as f:
52        PreinitTemplate = f.read()
53
54    useOrigInit = oe.types.boolean(d.getVar('OVERLAYFS_ETC_USE_ORIG_INIT_NAME'))
55    preinitPath = oe.path.join(d.getVar("IMAGE_ROOTFS"), d.getVar("base_sbindir"), "preinit")
56    initBaseName = oe.path.join(d.getVar("base_sbindir"), "init")
57    origInitNameSuffix = ".orig"
58    exposeLower = oe.types.boolean(d.getVar('OVERLAYFS_ETC_EXPOSE_LOWER'))
59
60    args = {
61        'OVERLAYFS_ETC_MOUNT_POINT': overlayEtcMountPoint,
62        'OVERLAYFS_ETC_MOUNT_OPTIONS': d.getVar('OVERLAYFS_ETC_MOUNT_OPTIONS'),
63        'OVERLAYFS_ETC_FSTYPE': overlayEtcFsType,
64        'OVERLAYFS_ETC_DEVICE': overlayEtcDevice,
65        'SBIN_INIT_NAME': initBaseName + origInitNameSuffix if useOrigInit else initBaseName,
66        'OVERLAYFS_ETC_EXPOSE_LOWER': "true" if exposeLower else "false"
67    }
68
69    if useOrigInit:
70        # rename original /sbin/init
71        origInit = oe.path.join(d.getVar("IMAGE_ROOTFS"), initBaseName)
72        bb.debug(1, "rootfs path %s, init path %s, test %s" % (d.getVar('IMAGE_ROOTFS'), origInit, d.getVar("IMAGE_ROOTFS")))
73        bb.utils.rename(origInit, origInit + origInitNameSuffix)
74        preinitPath = origInit
75
76    with open(preinitPath, 'w') as f:
77        f.write(PreinitTemplate.format(**args))
78    os.chmod(preinitPath, 0o755)
79}
80