blob: f7069edd416d8a366d1892ab295a96be4a769c88 [file] [log] [blame]
Patrick Williams0ca19cc2021-08-16 14:03:13 -05001# 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#
Andrew Geisslerd5838332022-05-27 11:33:10 -050019# 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.
Patrick Williams0ca19cc2021-08-16 14:03:13 -050022#
Andrew Geisslerd5838332022-05-27 11:33:10 -050023# 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:
Patrick Williams0ca19cc2021-08-16 14:03:13 -050031#
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#
Andrew Geisslerd5838332022-05-27 11:33:10 -050041# 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#
Patrick Williams0ca19cc2021-08-16 14:03:13 -050045# Note: the class does not support /etc directory itself, because systemd depends on it
Andrew Geissler595f6302022-01-24 19:11:47 +000046# For /etc directory use overlayfs-etc class
Patrick Williams0ca19cc2021-08-16 14:03:13 -050047
48REQUIRED_DISTRO_FEATURES += "systemd overlayfs"
49
50inherit systemd features_check
51
Andrew Geissler595f6302022-01-24 19:11:47 +000052OVERLAYFS_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
Patrick Williams0ca19cc2021-08-16 14:03:13 -050056python do_create_overlayfs_units() {
Andrew Geissler595f6302022-01-24 19:11:47 +000057 from oe.overlayfs import mountUnitName
Patrick Williams0ca19cc2021-08-16 14:03:13 -050058
Andrew Geissler595f6302022-01-24 19:11:47 +000059 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()
Patrick Williams0ca19cc2021-08-16 14:03:13 -050065
66 def prepareUnits(data, lower):
Andrew Geissler595f6302022-01-24 19:11:47 +000067 from oe.overlayfs import helperUnitName
Patrick Williams0ca19cc2021-08-16 14:03:13 -050068
69 args = {
70 'DATA_MOUNT_POINT': data,
71 'DATA_MOUNT_UNIT': mountUnitName(data),
72 'CREATE_DIRS_SERVICE': helperUnitName(lower),
73 'LOWERDIR': lower,
74 }
75
Andrew Geissler595f6302022-01-24 19:11:47 +000076 bb.debug(1, "Generate systemd unit %s" % mountUnitName(lower))
Patrick Williams0ca19cc2021-08-16 14:03:13 -050077 with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f:
78 f.write(MountUnitTemplate.format(**args))
79
Andrew Geissler595f6302022-01-24 19:11:47 +000080 bb.debug(1, "Generate helper systemd unit %s" % helperUnitName(lower))
Patrick Williams0ca19cc2021-08-16 14:03:13 -050081 with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f:
82 f.write(CreateDirsUnitTemplate.format(**args))
83
Andrew Geissler595f6302022-01-24 19:11:47 +000084 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 = []
Patrick Williams0ca19cc2021-08-16 14:03:13 -050096 overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT")
97 for mountPoint in overlayMountPoints:
Andrew Geissler595f6302022-01-24 19:11:47 +000098 bb.debug(1, "Process variable flag %s" % mountPoint)
Patrick Williams0ca19cc2021-08-16 14:03:13 -050099 for lower in d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint).split():
Andrew Geissler595f6302022-01-24 19:11:47 +0000100 bb.debug(1, "Prepare mount unit for %s with data mount point %s" %
101 (lower, d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint)))
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500102 prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower)
Andrew Geissler595f6302022-01-24 19:11:47 +0000103 mountUnitList.append(mountUnitName(lower))
104
105 # set up one unit, which depends on all mount units, so users can set
106 # only one dependency in their units to make sure software starts
107 # when all overlays are mounted
108 prepareGlobalUnit(mountUnitList)
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500109}
110
111# we need to generate file names early during parsing stage
112python () {
113 from oe.overlayfs import strForBash, unitFileList
114
115 unitList = unitFileList(d)
116 for unit in unitList:
Andrew Geissler595f6302022-01-24 19:11:47 +0000117 d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000118 d.appendVar('FILES:' + d.getVar('PN'), ' ' +
119 d.getVar('systemd_system_unitdir') + '/' + strForBash(unit))
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500120
121 d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList]))
122}
123
124do_install:append() {
125 install -d ${D}${systemd_system_unitdir}
126 for unit in ${OVERLAYFS_UNIT_LIST}; do
127 install -m 0444 ${WORKDIR}/${unit} ${D}${systemd_system_unitdir}
128 done
129}
130
131addtask create_overlayfs_units before do_install