diff options
Diffstat (limited to 'meta/recipes-core/volatile-binds/files/mount-copybind')
-rwxr-xr-x | meta/recipes-core/volatile-binds/files/mount-copybind | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/meta/recipes-core/volatile-binds/files/mount-copybind b/meta/recipes-core/volatile-binds/files/mount-copybind index e32e675308..5885880534 100755 --- a/meta/recipes-core/volatile-binds/files/mount-copybind +++ b/meta/recipes-core/volatile-binds/files/mount-copybind | |||
@@ -2,6 +2,9 @@ | |||
2 | # | 2 | # |
3 | # Perform a bind mount, copying existing files as we do so to ensure the | 3 | # Perform a bind mount, copying existing files as we do so to ensure the |
4 | # overlaid path has the necessary content. | 4 | # overlaid path has the necessary content. |
5 | # If the target is a directory and overlayfs is available (and the environment | ||
6 | # variable MOUNT_COPYBIND_AVOID_OVERLAYFS=1 is not set), then an overlay mount | ||
7 | # will be attempted first. | ||
5 | 8 | ||
6 | if [ $# -lt 2 ]; then | 9 | if [ $# -lt 2 ]; then |
7 | echo >&2 "Usage: $0 spec mountpoint [OPTIONS]" | 10 | echo >&2 "Usage: $0 spec mountpoint [OPTIONS]" |
@@ -31,21 +34,42 @@ if [ -d "$mountpoint" ]; then | |||
31 | else | 34 | else |
32 | specdir_existed=no | 35 | specdir_existed=no |
33 | mkdir "$spec" | 36 | mkdir "$spec" |
37 | # If the $spec directory is created we need to take care that | ||
38 | # the selinux context is correct | ||
39 | if command -v selinuxenabled > /dev/null 2>&1; then | ||
40 | if selinuxenabled; then | ||
41 | restorecon "$spec" | ||
42 | fi | ||
43 | fi | ||
34 | fi | 44 | fi |
35 | 45 | ||
36 | # Fast version of calculating `dirname ${spec}`/.`basename ${spec}`-work | 46 | # Fast version of calculating `dirname ${spec}`/.`basename ${spec}`-work |
37 | overlay_workdir="${spec%/*}/.${spec##*/}-work" | 47 | overlay_workdir="${spec%/*}/.${spec##*/}-work" |
38 | mkdir "${overlay_workdir}" | 48 | if [ "$MOUNT_COPYBIND_AVOID_OVERLAYFS" != 1 ]; then |
49 | mkdir "${overlay_workdir}" | ||
50 | fi | ||
39 | 51 | ||
40 | # Try to mount using overlay, which is must faster than copying files. | 52 | # Try to mount using overlay, which is much faster than copying files. |
41 | # If that fails, fall back to slower copy. | 53 | # If that fails, fall back to slower copy. |
42 | if ! mount -t overlay overlay -olowerdir="$mountpoint",upperdir="$spec",workdir="$overlay_workdir" "$mountpoint" > /dev/null 2>&1; then | 54 | if command -v selinuxenabled > /dev/null 2>&1; then |
55 | if selinuxenabled; then | ||
56 | mountcontext=",rootcontext=$(matchpathcon -n "$mountpoint")" | ||
57 | fi | ||
58 | fi | ||
59 | if [ "$MOUNT_COPYBIND_AVOID_OVERLAYFS" = 1 ] || ! mount -t overlay overlay -olowerdir="$mountpoint",upperdir="$spec",workdir="$overlay_workdir""$mountcontext" "$mountpoint" > /dev/null 2>&1; then | ||
60 | rm -rf "$overlay_workdir" | ||
43 | 61 | ||
44 | if [ "$specdir_existed" != "yes" ]; then | 62 | if [ "$specdir_existed" != "yes" ]; then |
45 | cp -aPR "$mountpoint"/. "$spec/" | 63 | cp -aPR "$mountpoint"/. "$spec/" |
46 | fi | 64 | fi |
47 | 65 | ||
48 | mount -o "bind$options" "$spec" "$mountpoint" | 66 | mount -o "bind$options" "$spec" "$mountpoint" |
67 | # restore the selinux context. | ||
68 | if command -v selinuxenabled > /dev/null 2>&1; then | ||
69 | if selinuxenabled; then | ||
70 | restorecon -R "$mountpoint" | ||
71 | fi | ||
72 | fi | ||
49 | fi | 73 | fi |
50 | elif [ -f "$mountpoint" ]; then | 74 | elif [ -f "$mountpoint" ]; then |
51 | if [ ! -f "$spec" ]; then | 75 | if [ ! -f "$spec" ]; then |
@@ -53,4 +77,10 @@ elif [ -f "$mountpoint" ]; then | |||
53 | fi | 77 | fi |
54 | 78 | ||
55 | mount -o "bind$options" "$spec" "$mountpoint" | 79 | mount -o "bind$options" "$spec" "$mountpoint" |
80 | # restore the selinux context. | ||
81 | if command -v selinuxenabled > /dev/null 2>&1; then | ||
82 | if selinuxenabled; then | ||
83 | restorecon -R "$mountpoint" | ||
84 | fi | ||
85 | fi | ||
56 | fi | 86 | fi |