blob: 8d9b59c9bf124cf9642e3ef0491f73ea4f4fcef1 [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
34
35REQUIRED_DISTRO_FEATURES += "systemd overlayfs"
36
37inherit systemd features_check
38
39python do_create_overlayfs_units() {
40 CreateDirsUnitTemplate = """[Unit]
41Description=Overlayfs directories setup
42Requires={DATA_MOUNT_UNIT}
43After={DATA_MOUNT_UNIT}
44DefaultDependencies=no
45
46[Service]
47Type=oneshot
48ExecStart=mkdir -p {DATA_MOUNT_POINT}/workdir{LOWERDIR} && mkdir -p {DATA_MOUNT_POINT}/upper{LOWERDIR}
49RemainAfterExit=true
50StandardOutput=journal
51
52[Install]
53WantedBy=multi-user.target
54"""
55 MountUnitTemplate = """[Unit]
56Description=Overlayfs mount unit
57Requires={CREATE_DIRS_SERVICE}
58After={CREATE_DIRS_SERVICE}
59
60[Mount]
61What=overlay
62Where={LOWERDIR}
63Type=overlay
64Options=lowerdir={LOWERDIR},upperdir={DATA_MOUNT_POINT}/upper{LOWERDIR},workdir={DATA_MOUNT_POINT}/workdir{LOWERDIR}
65
66[Install]
67WantedBy=multi-user.target
68"""
69
70 def prepareUnits(data, lower):
71 from oe.overlayfs import mountUnitName, helperUnitName
72
73 args = {
74 'DATA_MOUNT_POINT': data,
75 'DATA_MOUNT_UNIT': mountUnitName(data),
76 'CREATE_DIRS_SERVICE': helperUnitName(lower),
77 'LOWERDIR': lower,
78 }
79
80 with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f:
81 f.write(MountUnitTemplate.format(**args))
82
83 with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f:
84 f.write(CreateDirsUnitTemplate.format(**args))
85
86 overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT")
87 for mountPoint in overlayMountPoints:
88 for lower in d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint).split():
89 prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower)
90}
91
92# we need to generate file names early during parsing stage
93python () {
94 from oe.overlayfs import strForBash, unitFileList
95
96 unitList = unitFileList(d)
97 for unit in unitList:
98 d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit);
99 d.appendVar('FILES:' + d.getVar('PN'), ' ' + strForBash(unit))
100
101 d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList]))
102}
103
104do_install:append() {
105 install -d ${D}${systemd_system_unitdir}
106 for unit in ${OVERLAYFS_UNIT_LIST}; do
107 install -m 0444 ${WORKDIR}/${unit} ${D}${systemd_system_unitdir}
108 done
109}
110
111addtask create_overlayfs_units before do_install