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 |
| 34 | |
| 35 | REQUIRED_DISTRO_FEATURES += "systemd overlayfs" |
| 36 | |
| 37 | inherit systemd features_check |
| 38 | |
| 39 | python do_create_overlayfs_units() { |
| 40 | CreateDirsUnitTemplate = """[Unit] |
| 41 | Description=Overlayfs directories setup |
| 42 | Requires={DATA_MOUNT_UNIT} |
| 43 | After={DATA_MOUNT_UNIT} |
| 44 | DefaultDependencies=no |
| 45 | |
| 46 | [Service] |
| 47 | Type=oneshot |
| 48 | ExecStart=mkdir -p {DATA_MOUNT_POINT}/workdir{LOWERDIR} && mkdir -p {DATA_MOUNT_POINT}/upper{LOWERDIR} |
| 49 | RemainAfterExit=true |
| 50 | StandardOutput=journal |
| 51 | |
| 52 | [Install] |
| 53 | WantedBy=multi-user.target |
| 54 | """ |
| 55 | MountUnitTemplate = """[Unit] |
| 56 | Description=Overlayfs mount unit |
| 57 | Requires={CREATE_DIRS_SERVICE} |
| 58 | After={CREATE_DIRS_SERVICE} |
| 59 | |
| 60 | [Mount] |
| 61 | What=overlay |
| 62 | Where={LOWERDIR} |
| 63 | Type=overlay |
| 64 | Options=lowerdir={LOWERDIR},upperdir={DATA_MOUNT_POINT}/upper{LOWERDIR},workdir={DATA_MOUNT_POINT}/workdir{LOWERDIR} |
| 65 | |
| 66 | [Install] |
| 67 | WantedBy=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 |
| 93 | python () { |
| 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 | |
| 104 | do_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 | |
| 111 | addtask create_overlayfs_units before do_install |