Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 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 | # 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 Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 34 | # For /etc directory use overlayfs-etc class |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 35 | |
| 36 | REQUIRED_DISTRO_FEATURES += "systemd overlayfs" |
| 37 | |
| 38 | inherit systemd features_check |
| 39 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 40 | OVERLAYFS_CREATE_DIRS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-create-dirs.service.in" |
| 41 | OVERLAYFS_MOUNT_UNIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-unit.mount.in" |
| 42 | OVERLAYFS_ALL_OVERLAYS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-all-overlays.service.in" |
| 43 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 44 | python do_create_overlayfs_units() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 45 | from oe.overlayfs import mountUnitName |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 46 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 47 | 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 Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 53 | |
| 54 | def prepareUnits(data, lower): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 55 | from oe.overlayfs import helperUnitName |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 56 | |
| 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 Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 64 | bb.debug(1, "Generate systemd unit %s" % mountUnitName(lower)) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 65 | with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f: |
| 66 | f.write(MountUnitTemplate.format(**args)) |
| 67 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 68 | bb.debug(1, "Generate helper systemd unit %s" % helperUnitName(lower)) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 69 | with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f: |
| 70 | f.write(CreateDirsUnitTemplate.format(**args)) |
| 71 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 72 | 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 Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 84 | overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT") |
| 85 | for mountPoint in overlayMountPoints: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 86 | bb.debug(1, "Process variable flag %s" % mountPoint) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 87 | for lower in d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint).split(): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 88 | bb.debug(1, "Prepare mount unit for %s with data mount point %s" % |
| 89 | (lower, d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint))) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 90 | prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 91 | 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 Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | # we need to generate file names early during parsing stage |
| 100 | python () { |
| 101 | from oe.overlayfs import strForBash, unitFileList |
| 102 | |
| 103 | unitList = unitFileList(d) |
| 104 | for unit in unitList: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 105 | d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 106 | d.appendVar('FILES:' + d.getVar('PN'), ' ' + strForBash(unit)) |
| 107 | |
| 108 | d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList])) |
| 109 | } |
| 110 | |
| 111 | do_install:append() { |
| 112 | install -d ${D}${systemd_system_unitdir} |
| 113 | for unit in ${OVERLAYFS_UNIT_LIST}; do |
| 114 | install -m 0444 ${WORKDIR}/${unit} ${D}${systemd_system_unitdir} |
| 115 | done |
| 116 | } |
| 117 | |
| 118 | addtask create_overlayfs_units before do_install |