1*4882a593Smuzhiyun# Class for generation of overlayfs mount units 2*4882a593Smuzhiyun# 3*4882a593Smuzhiyun# It's often desired in Embedded System design to have a read-only rootfs. 4*4882a593Smuzhiyun# But a lot of different applications might want to have a read-write access to 5*4882a593Smuzhiyun# some parts of a filesystem. It can be especially useful when your update mechanism 6*4882a593Smuzhiyun# overwrites the whole rootfs, but you want your application data to be preserved 7*4882a593Smuzhiyun# between updates. This class provides a way to achieve that by means 8*4882a593Smuzhiyun# of overlayfs and at the same time keeping the base rootfs read-only. 9*4882a593Smuzhiyun# 10*4882a593Smuzhiyun# Usage example. 11*4882a593Smuzhiyun# 12*4882a593Smuzhiyun# Set a mount point for a partition overlayfs is going to use as upper layer 13*4882a593Smuzhiyun# in your machine configuration. Underlying file system can be anything that 14*4882a593Smuzhiyun# is supported by overlayfs. This has to be done in your machine configuration. 15*4882a593Smuzhiyun# QA check fails to catch file existence if you redefine this variable in your recipe! 16*4882a593Smuzhiyun# 17*4882a593Smuzhiyun# OVERLAYFS_MOUNT_POINT[data] ?= "/data" 18*4882a593Smuzhiyun# 19*4882a593Smuzhiyun# Per default the class assumes you have a corresponding fstab entry or systemd 20*4882a593Smuzhiyun# mount unit (data.mount in this case) for this mount point installed on the 21*4882a593Smuzhiyun# image, for instance via a wks script or the systemd-machine-units recipe. 22*4882a593Smuzhiyun# 23*4882a593Smuzhiyun# If the mount point is handled somewhere else, e.g. custom boot or preinit 24*4882a593Smuzhiyun# scripts or in a initramfs, then this QA check can be skipped by adding 25*4882a593Smuzhiyun# mount-configured to the related OVERLAYFS_QA_SKIP flag: 26*4882a593Smuzhiyun# 27*4882a593Smuzhiyun# OVERLAYFS_QA_SKIP[data] = "mount-configured" 28*4882a593Smuzhiyun# 29*4882a593Smuzhiyun# To use the overlayfs, you just have to specify writable directories inside 30*4882a593Smuzhiyun# their recipe: 31*4882a593Smuzhiyun# 32*4882a593Smuzhiyun# OVERLAYFS_WRITABLE_PATHS[data] = "/usr/share/my-custom-application" 33*4882a593Smuzhiyun# 34*4882a593Smuzhiyun# To support several mount points you can use a different variable flag. Assume we 35*4882a593Smuzhiyun# want to have a writable location on the file system, but not interested where the data 36*4882a593Smuzhiyun# survive a reboot. Then we could have a mnt-overlay.mount unit for a tmpfs file system: 37*4882a593Smuzhiyun# 38*4882a593Smuzhiyun# OVERLAYFS_MOUNT_POINT[mnt-overlay] = "/mnt/overlay" 39*4882a593Smuzhiyun# OVERLAYFS_WRITABLE_PATHS[mnt-overlay] = "/usr/share/another-application" 40*4882a593Smuzhiyun# 41*4882a593Smuzhiyun# If your recipe deploys a systemd service, then it should require and be 42*4882a593Smuzhiyun# started after the ${PN}-overlays.service to make sure that all overlays are 43*4882a593Smuzhiyun# mounted beforehand. 44*4882a593Smuzhiyun# 45*4882a593Smuzhiyun# Note: the class does not support /etc directory itself, because systemd depends on it 46*4882a593Smuzhiyun# For /etc directory use overlayfs-etc class 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunREQUIRED_DISTRO_FEATURES += "systemd overlayfs" 49*4882a593Smuzhiyun 50*4882a593Smuzhiyuninherit systemd features_check 51*4882a593Smuzhiyun 52*4882a593SmuzhiyunOVERLAYFS_CREATE_DIRS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-create-dirs.service.in" 53*4882a593SmuzhiyunOVERLAYFS_MOUNT_UNIT_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-unit.mount.in" 54*4882a593SmuzhiyunOVERLAYFS_ALL_OVERLAYS_TEMPLATE ??= "${COREBASE}/meta/files/overlayfs-all-overlays.service.in" 55*4882a593Smuzhiyun 56*4882a593Smuzhiyunpython do_create_overlayfs_units() { 57*4882a593Smuzhiyun from oe.overlayfs import mountUnitName 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun with open(d.getVar("OVERLAYFS_CREATE_DIRS_TEMPLATE"), "r") as f: 60*4882a593Smuzhiyun CreateDirsUnitTemplate = f.read() 61*4882a593Smuzhiyun with open(d.getVar("OVERLAYFS_MOUNT_UNIT_TEMPLATE"), "r") as f: 62*4882a593Smuzhiyun MountUnitTemplate = f.read() 63*4882a593Smuzhiyun with open(d.getVar("OVERLAYFS_ALL_OVERLAYS_TEMPLATE"), "r") as f: 64*4882a593Smuzhiyun AllOverlaysTemplate = f.read() 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun def prepareUnits(data, lower): 67*4882a593Smuzhiyun from oe.overlayfs import helperUnitName 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun args = { 70*4882a593Smuzhiyun 'DATA_MOUNT_POINT': data, 71*4882a593Smuzhiyun 'DATA_MOUNT_UNIT': mountUnitName(data), 72*4882a593Smuzhiyun 'CREATE_DIRS_SERVICE': helperUnitName(lower), 73*4882a593Smuzhiyun 'LOWERDIR': lower, 74*4882a593Smuzhiyun } 75*4882a593Smuzhiyun 76*4882a593Smuzhiyun bb.debug(1, "Generate systemd unit %s" % mountUnitName(lower)) 77*4882a593Smuzhiyun with open(os.path.join(d.getVar('WORKDIR'), mountUnitName(lower)), 'w') as f: 78*4882a593Smuzhiyun f.write(MountUnitTemplate.format(**args)) 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun bb.debug(1, "Generate helper systemd unit %s" % helperUnitName(lower)) 81*4882a593Smuzhiyun with open(os.path.join(d.getVar('WORKDIR'), helperUnitName(lower)), 'w') as f: 82*4882a593Smuzhiyun f.write(CreateDirsUnitTemplate.format(**args)) 83*4882a593Smuzhiyun 84*4882a593Smuzhiyun def prepareGlobalUnit(dependentUnits): 85*4882a593Smuzhiyun from oe.overlayfs import allOverlaysUnitName 86*4882a593Smuzhiyun args = { 87*4882a593Smuzhiyun 'ALL_OVERLAYFS_UNITS': " ".join(dependentUnits), 88*4882a593Smuzhiyun 'PN': d.getVar('PN') 89*4882a593Smuzhiyun } 90*4882a593Smuzhiyun 91*4882a593Smuzhiyun bb.debug(1, "Generate systemd unit with all overlays %s" % allOverlaysUnitName(d)) 92*4882a593Smuzhiyun with open(os.path.join(d.getVar('WORKDIR'), allOverlaysUnitName(d)), 'w') as f: 93*4882a593Smuzhiyun f.write(AllOverlaysTemplate.format(**args)) 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun mountUnitList = [] 96*4882a593Smuzhiyun overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT") 97*4882a593Smuzhiyun for mountPoint in overlayMountPoints: 98*4882a593Smuzhiyun bb.debug(1, "Process variable flag %s" % mountPoint) 99*4882a593Smuzhiyun lowerList = d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint) 100*4882a593Smuzhiyun if not lowerList: 101*4882a593Smuzhiyun bb.note("No mount points defined for %s flag, skipping" % (mountPoint)) 102*4882a593Smuzhiyun continue 103*4882a593Smuzhiyun for lower in lowerList.split(): 104*4882a593Smuzhiyun bb.debug(1, "Prepare mount unit for %s with data mount point %s" % 105*4882a593Smuzhiyun (lower, d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint))) 106*4882a593Smuzhiyun prepareUnits(d.getVarFlag('OVERLAYFS_MOUNT_POINT', mountPoint), lower) 107*4882a593Smuzhiyun mountUnitList.append(mountUnitName(lower)) 108*4882a593Smuzhiyun 109*4882a593Smuzhiyun # set up one unit, which depends on all mount units, so users can set 110*4882a593Smuzhiyun # only one dependency in their units to make sure software starts 111*4882a593Smuzhiyun # when all overlays are mounted 112*4882a593Smuzhiyun prepareGlobalUnit(mountUnitList) 113*4882a593Smuzhiyun} 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun# we need to generate file names early during parsing stage 116*4882a593Smuzhiyunpython () { 117*4882a593Smuzhiyun from oe.overlayfs import strForBash, unitFileList 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun unitList = unitFileList(d) 120*4882a593Smuzhiyun for unit in unitList: 121*4882a593Smuzhiyun d.appendVar('SYSTEMD_SERVICE:' + d.getVar('PN'), ' ' + unit) 122*4882a593Smuzhiyun d.appendVar('FILES:' + d.getVar('PN'), ' ' + 123*4882a593Smuzhiyun d.getVar('systemd_system_unitdir') + '/' + strForBash(unit)) 124*4882a593Smuzhiyun 125*4882a593Smuzhiyun d.setVar('OVERLAYFS_UNIT_LIST', ' '.join([strForBash(s) for s in unitList])) 126*4882a593Smuzhiyun} 127*4882a593Smuzhiyun 128*4882a593Smuzhiyundo_install:append() { 129*4882a593Smuzhiyun install -d ${D}${systemd_system_unitdir} 130*4882a593Smuzhiyun for unit in ${OVERLAYFS_UNIT_LIST}; do 131*4882a593Smuzhiyun install -m 0444 ${WORKDIR}/${unit} ${D}${systemd_system_unitdir} 132*4882a593Smuzhiyun done 133*4882a593Smuzhiyun} 134*4882a593Smuzhiyun 135*4882a593Smuzhiyunaddtask create_overlayfs_units before do_install 136