blob: 29fced2ca781b040dd4b8d28a7eb26a3b354f655 [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#
19# The class assumes you have a data.mount systemd unit defined in your
20# systemd-machine-units recipe and installed to the image.
21#
22# Then you can specify writable directories on a recipe base
23#
24# OVERLAYFS_WRITABLE_PATHS[data] = "/usr/share/my-custom-application"
25#
26# To support several mount points you can use a different variable flag. Assume we
27# want to have a writable location on the file system, but not interested where the data
28# survive a reboot. Then we could have a mnt-overlay.mount unit for a tmpfs file system:
29#
30# OVERLAYFS_MOUNT_POINT[mnt-overlay] = "/mnt/overlay"
31# OVERLAYFS_WRITABLE_PATHS[mnt-overlay] = "/usr/share/another-application"
32#
33# Note: the class does not support /etc directory itself, because systemd depends on it
Andrew Geissler595f6302022-01-24 19:11:47 +000034# For /etc directory use overlayfs-etc class
Patrick Williams0ca19cc2021-08-16 14:03:13 -050035
36REQUIRED_DISTRO_FEATURES += "systemd overlayfs"
37
38inherit systemd features_check
39
Andrew Geissler595f6302022-01-24 19:11:47 +000040OVERLAYFS_CREATE_DIRS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-create-dirs.service.in"
41OVERLAYFS_MOUNT_UNIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-unit.mount.in"
42OVERLAYFS_ALL_OVERLAYS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-all-overlays.service.in"
43
Patrick Williams0ca19cc2021-08-16 14:03:13 -050044python do_create_overlayfs_units() {
Andrew Geissler595f6302022-01-24 19:11:47 +000045 from oe.overlayfs import mountUnitName
Patrick Williams0ca19cc2021-08-16 14:03:13 -050046
Andrew Geissler595f6302022-01-24 19:11:47 +000047 with open(d.getVar("OVERLAYFS_CREATE_DIRS_TEMPLATE"), "r") as f:
48 CreateDirsUnitTemplate = f.read()
49 with open(d.getVar("OVERLAYFS_MOUNT_UNIT_TEMPLATE"), "r") as f:
50 MountUnitTemplate = f.read()
51 with open(d.getVar("OVERLAYFS_ALL_OVERLAYS_TEMPLATE"), "r") as f:
52 AllOverlaysTemplate = f.read()
Patrick Williams0ca19cc2021-08-16 14:03:13 -050053
54 def prepareUnits(data, lower):
Andrew Geissler595f6302022-01-24 19:11:47 +000055 from oe.overlayfs import helperUnitName
Patrick Williams0ca19cc2021-08-16 14:03:13 -050056
57 args = {
58 'DATA_MOUNT_POINT': data,
59 'DATA_MOUNT_UNIT': mountUnitName(data),
60 'CREATE_DIRS_SERVICE': helperUnitName(lower),
61 'LOWERDIR': lower,
62 }
63
Andrew Geissler595f6302022-01-24 19:11:47 +000064 bb.debug(1, "Generate systemd unit %s" % mountUnitName(lower))
Patrick Williams0ca19cc2021-08-16 14:03:13 -050065 with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f:
66 f.write(MountUnitTemplate.format(**args))
67
Andrew Geissler595f6302022-01-24 19:11:47 +000068 bb.debug(1, "Generate helper systemd unit %s" % helperUnitName(lower))
Patrick Williams0ca19cc2021-08-16 14:03:13 -050069 with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f:
70 f.write(CreateDirsUnitTemplate.format(**args))
71
Andrew Geissler595f6302022-01-24 19:11:47 +000072 def prepareGlobalUnit(dependentUnits):
73 from oe.overlayfs import allOverlaysUnitName
74 args = {
75 'ALL_OVERLAYFS_UNITS': " ".join(dependentUnits),
76 'PN': d.getVar('PN')
77 }
78
79 bb.debug(1, "Generate systemd unit with all overlays %s" % allOverlaysUnitName(d))
80 with open(os.path.join(d.getVar('WORKDIR'), allOverlaysUnitName(d)), 'w') as f:
81 f.write(AllOverlaysTemplate.format(**args))
82
83 mountUnitList = []
Patrick Williams0ca19cc2021-08-16 14:03:13 -050084 overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT")
85 for mountPoint in overlayMountPoints:
Andrew Geissler595f6302022-01-24 19:11:47 +000086 bb.debug(1, "Process variable flag %s" % mountPoint)
Patrick Williams0ca19cc2021-08-16 14:03:13 -050087 for lower in d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint).split():
Andrew Geissler595f6302022-01-24 19:11:47 +000088 bb.debug(1, "Prepare mount unit for %s with data mount point %s" %
89 (lower, d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint)))
Patrick Williams0ca19cc2021-08-16 14:03:13 -050090 prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower)
Andrew Geissler595f6302022-01-24 19:11:47 +000091 mountUnitList.append(mountUnitName(lower))
92
93 # set up one unit, which depends on all mount units, so users can set
94 # only one dependency in their units to make sure software starts
95 # when all overlays are mounted
96 prepareGlobalUnit(mountUnitList)
Patrick Williams0ca19cc2021-08-16 14:03:13 -050097}
98
99# we need to generate file names early during parsing stage
100python () {
101 from oe.overlayfs import strForBash, unitFileList
102
103 unitList = unitFileList(d)
104 for unit in unitList:
Andrew Geissler595f6302022-01-24 19:11:47 +0000105 d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit)
Andrew Geissler9aee5002022-03-30 16:27:02 +0000106 d.appendVar('FILES:' + d.getVar('PN'), ' ' +
107 d.getVar('systemd_system_unitdir') + '/' + strForBash(unit))
Patrick Williams0ca19cc2021-08-16 14:03:13 -0500108
109 d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList]))
110}
111
112do_install:append() {
113 install -d ${D}${systemd_system_unitdir}
114 for unit in ${OVERLAYFS_UNIT_LIST}; do
115 install -m 0444 ${WORKDIR}/${unit} ${D}${systemd_system_unitdir}
116 done
117}
118
119addtask create_overlayfs_units before do_install