xref: /OK3568_Linux_fs/yocto/poky/meta/classes/overlayfs.bbclass (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1# Class for generation of overlayfs mount units
2#
3# It's often desired in Embedded System design to have a read-only rootfs.
4# But a lot of different applications might want to have a read-write access to
5# some parts of a filesystem. It can be especially useful when your update mechanism
6# overwrites the whole rootfs, but you want your application data to be preserved
7# between updates. This class provides a way to achieve that by means
8# of overlayfs and at the same time keeping the base rootfs read-only.
9#
10# Usage example.
11#
12# Set a mount point for a partition overlayfs is going to use as upper layer
13# in your machine configuration. Underlying file system can be anything that
14# is supported by overlayfs. This has to be done in your machine configuration.
15# QA check fails to catch file existence if you redefine this variable in your recipe!
16#
17#   OVERLAYFS_MOUNT_POINT[data] ?= "/data"
18#
19# Per default the class assumes you have a corresponding fstab entry or systemd
20# mount unit (data.mount in this case) for this mount point installed on the
21# image, for instance via a wks script or the systemd-machine-units recipe.
22#
23# If the mount point is handled somewhere else, e.g. custom boot or preinit
24# scripts or in a initramfs, then this QA check can be skipped by adding
25# mount-configured to the related OVERLAYFS_QA_SKIP flag:
26#
27#   OVERLAYFS_QA_SKIP[data] = "mount-configured"
28#
29# To use the overlayfs, you just have to specify writable directories inside
30# their recipe:
31#
32#   OVERLAYFS_WRITABLE_PATHS[data] = "/usr/share/my-custom-application"
33#
34# To support several mount points you can use a different variable flag. Assume we
35# want to have a writable location on the file system, but not interested where the data
36# survive a reboot. Then we could have a mnt-overlay.mount unit for a tmpfs file system:
37#
38#   OVERLAYFS_MOUNT_POINT[mnt-overlay] = "/mnt/overlay"
39#   OVERLAYFS_WRITABLE_PATHS[mnt-overlay] = "/usr/share/another-application"
40#
41# If your recipe deploys a systemd service, then it should require and be
42# started after the ${PN}-overlays.service to make sure that all overlays are
43# mounted beforehand.
44#
45# Note: the class does not support /etc directory itself, because systemd depends on it
46# For /etc directory use overlayfs-etc class
47
48REQUIRED_DISTRO_FEATURES += "systemd overlayfs"
49
50inherit systemd features_check
51
52OVERLAYFS_CREATE_DIRS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-create-dirs.service.in"
53OVERLAYFS_MOUNT_UNIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-unit.mount.in"
54OVERLAYFS_ALL_OVERLAYS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-all-overlays.service.in"
55
56python do_create_overlayfs_units() {
57    from oe.overlayfs import mountUnitName
58
59    with open(d.getVar("OVERLAYFS_CREATE_DIRS_TEMPLATE"), "r") as f:
60        CreateDirsUnitTemplate = f.read()
61    with open(d.getVar("OVERLAYFS_MOUNT_UNIT_TEMPLATE"), "r") as f:
62        MountUnitTemplate = f.read()
63    with open(d.getVar("OVERLAYFS_ALL_OVERLAYS_TEMPLATE"), "r") as f:
64        AllOverlaysTemplate = f.read()
65
66    def prepareUnits(data, lower):
67        from oe.overlayfs import helperUnitName
68
69        args = {
70            'DATA_MOUNT_POINT': data,
71            'DATA_MOUNT_UNIT': mountUnitName(data),
72            'CREATE_DIRS_SERVICE': helperUnitName(lower),
73            'LOWERDIR': lower,
74        }
75
76        bb.debug(1, "Generate systemd unit %s" % mountUnitName(lower))
77        with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f:
78            f.write(MountUnitTemplate.format(**args))
79
80        bb.debug(1, "Generate helper systemd unit %s" % helperUnitName(lower))
81        with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f:
82            f.write(CreateDirsUnitTemplate.format(**args))
83
84    def prepareGlobalUnit(dependentUnits):
85        from oe.overlayfs import allOverlaysUnitName
86        args = {
87            'ALL_OVERLAYFS_UNITS': " ".join(dependentUnits),
88            'PN': d.getVar('PN')
89        }
90
91        bb.debug(1, "Generate systemd unit with all overlays %s" % allOverlaysUnitName(d))
92        with open(os.path.join(d.getVar('WORKDIR'), allOverlaysUnitName(d)), 'w') as f:
93            f.write(AllOverlaysTemplate.format(**args))
94
95    mountUnitList = []
96    overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT")
97    for mountPoint in overlayMountPoints:
98        bb.debug(1, "Process variable flag %s" % mountPoint)
99        lowerList = d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint)
100        if not lowerList:
101            bb.note("No mount points defined for %s flag, skipping" % (mountPoint))
102            continue
103        for lower in lowerList.split():
104            bb.debug(1, "Prepare mount unit for %s with data mount point %s" %
105                     (lower, d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint)))
106            prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower)
107            mountUnitList.append(mountUnitName(lower))
108
109    # set up one unit, which depends on all mount units, so users can set
110    # only one dependency in their units to make sure software starts
111    # when all overlays are mounted
112    prepareGlobalUnit(mountUnitList)
113}
114
115# we need to generate file names early during parsing stage
116python () {
117    from oe.overlayfs import strForBash, unitFileList
118
119    unitList = unitFileList(d)
120    for unit in unitList:
121        d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit)
122        d.appendVar('FILES:' + d.getVar('PN'), ' ' +
123                d.getVar('systemd_system_unitdir') + '/' + strForBash(unit))
124
125    d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList]))
126}
127
128do_install:append() {
129    install -d ${D}${systemd_system_unitdir}
130    for unit in ${OVERLAYFS_UNIT_LIST}; do
131        install -m 0444 ${WORKDIR}/${unit} ${D}${systemd_system_unitdir}
132    done
133}
134
135addtask create_overlayfs_units before do_install
136