xref: /OK3568_Linux_fs/yocto/poky/meta/lib/oe/overlayfs.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
3*4882a593Smuzhiyun#
4*4882a593Smuzhiyun# This file contains common functions for overlayfs and its QA check
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun# this function is based on https://github.com/systemd/systemd/blob/main/src/basic/unit-name.c
7*4882a593Smuzhiyundef escapeSystemdUnitName(path):
8*4882a593Smuzhiyun    escapeMap = {
9*4882a593Smuzhiyun        '/': '-',
10*4882a593Smuzhiyun        '-': "\\x2d",
11*4882a593Smuzhiyun        '\\': "\\x5d"
12*4882a593Smuzhiyun    }
13*4882a593Smuzhiyun    return "".join([escapeMap.get(c, c) for c in path.strip('/')])
14*4882a593Smuzhiyun
15*4882a593Smuzhiyundef strForBash(s):
16*4882a593Smuzhiyun    return s.replace('\\', '\\\\')
17*4882a593Smuzhiyun
18*4882a593Smuzhiyundef allOverlaysUnitName(d):
19*4882a593Smuzhiyun    return d.getVar('PN') + '-overlays.service'
20*4882a593Smuzhiyun
21*4882a593Smuzhiyundef mountUnitName(unit):
22*4882a593Smuzhiyun    return escapeSystemdUnitName(unit) + '.mount'
23*4882a593Smuzhiyun
24*4882a593Smuzhiyundef helperUnitName(unit):
25*4882a593Smuzhiyun    return escapeSystemdUnitName(unit) + '-create-upper-dir.service'
26*4882a593Smuzhiyun
27*4882a593Smuzhiyundef unitFileList(d):
28*4882a593Smuzhiyun    fileList = []
29*4882a593Smuzhiyun    overlayMountPoints = d.getVarFlags("OVERLAYFS_MOUNT_POINT")
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun    if not overlayMountPoints:
32*4882a593Smuzhiyun        bb.fatal("A recipe uses overlayfs class but there is no OVERLAYFS_MOUNT_POINT set in your MACHINE configuration")
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun    # check that we have required mount points set first
35*4882a593Smuzhiyun    requiredMountPoints = d.getVarFlags('OVERLAYFS_WRITABLE_PATHS')
36*4882a593Smuzhiyun    for mountPoint in requiredMountPoints:
37*4882a593Smuzhiyun        if mountPoint not in overlayMountPoints:
38*4882a593Smuzhiyun            bb.fatal("Missing required mount point for OVERLAYFS_MOUNT_POINT[%s] in your MACHINE configuration" % mountPoint)
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun    for mountPoint in overlayMountPoints:
41*4882a593Smuzhiyun        mountPointList = d.getVarFlag('OVERLAYFS_WRITABLE_PATHS', mountPoint)
42*4882a593Smuzhiyun        if not mountPointList:
43*4882a593Smuzhiyun            bb.debug(1, "No mount points defined for %s flag, don't add to file list", mountPoint)
44*4882a593Smuzhiyun            continue
45*4882a593Smuzhiyun        for path in mountPointList.split():
46*4882a593Smuzhiyun            fileList.append(mountUnitName(path))
47*4882a593Smuzhiyun            fileList.append(helperUnitName(path))
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun    fileList.append(allOverlaysUnitName(d))
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun    return fileList
52*4882a593Smuzhiyun
53