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 | # |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 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. |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 22 | # |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 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: |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 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 | # |
Andrew Geissler | d583833 | 2022-05-27 11:33:10 -0500 | [diff] [blame] | 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 | # |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 45 | # 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] | 46 | # For /etc directory use overlayfs-etc class |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 47 | |
| 48 | REQUIRED_DISTRO_FEATURES += "systemd overlayfs" |
| 49 | |
| 50 | inherit systemd features_check |
| 51 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 52 | OVERLAYFS_CREATE_DIRS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-create-dirs.service.in" |
| 53 | OVERLAYFS_MOUNT_UNIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-unit.mount.in" |
| 54 | OVERLAYFS_ALL_OVERLAYS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-all-overlays.service.in" |
| 55 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 56 | python do_create_overlayfs_units() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 57 | from oe.overlayfs import mountUnitName |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 58 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 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() |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 65 | |
| 66 | def prepareUnits(data, lower): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 67 | from oe.overlayfs import helperUnitName |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 76 | bb.debug(1, "Generate systemd unit %s" % mountUnitName(lower)) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 77 | with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f: |
| 78 | f.write(MountUnitTemplate.format(**args)) |
| 79 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 80 | bb.debug(1, "Generate helper systemd unit %s" % helperUnitName(lower)) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 81 | with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f: |
| 82 | f.write(CreateDirsUnitTemplate.format(**args)) |
| 83 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 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 = [] |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 96 | overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT") |
| 97 | for mountPoint in overlayMountPoints: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 98 | bb.debug(1, "Process variable flag %s" % mountPoint) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 99 | for lower in d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint).split(): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 100 | bb.debug(1, "Prepare mount unit for %s with data mount point %s" % |
| 101 | (lower, d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint))) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 102 | prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 103 | 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 Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | # we need to generate file names early during parsing stage |
| 112 | python () { |
| 113 | from oe.overlayfs import strForBash, unitFileList |
| 114 | |
| 115 | unitList = unitFileList(d) |
| 116 | for unit in unitList: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 117 | d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit) |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame] | 118 | d.appendVar('FILES:' + d.getVar('PN'), ' ' + |
| 119 | d.getVar('systemd_system_unitdir') + '/' + strForBash(unit)) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 120 | |
| 121 | d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList])) |
| 122 | } |
| 123 | |
| 124 | do_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 | |
| 131 | addtask create_overlayfs_units before do_install |