Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Perform a bind mount, copying existing files as we do so to ensure the |
| 4 | # overlaid path has the necessary content. |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 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. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 8 | |
| 9 | if [ $# -lt 2 ]; then |
| 10 | echo >&2 "Usage: $0 spec mountpoint [OPTIONS]" |
| 11 | exit 1 |
| 12 | fi |
| 13 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 14 | # e.g. /var/volatile/lib |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | spec=$1 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 16 | |
| 17 | # e.g. /var/lib |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | mountpoint=$2 |
| 19 | |
| 20 | if [ $# -gt 2 ]; then |
| 21 | options=$3 |
| 22 | else |
| 23 | options= |
| 24 | fi |
| 25 | |
| 26 | [ -n "$options" ] && options=",$options" |
| 27 | |
| 28 | mkdir -p "${spec%/*}" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 29 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | if [ -d "$mountpoint" ]; then |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 31 | |
| 32 | if [ -d "$spec" ]; then |
| 33 | specdir_existed=yes |
| 34 | else |
| 35 | specdir_existed=no |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | mkdir "$spec" |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 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 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 44 | fi |
| 45 | |
| 46 | # Fast version of calculating `dirname ${spec}`/.`basename ${spec}`-work |
| 47 | overlay_workdir="${spec%/*}/.${spec##*/}-work" |
| 48 | mkdir "${overlay_workdir}" |
| 49 | |
| 50 | # Try to mount using overlay, which is must faster than copying files. |
| 51 | # If that fails, fall back to slower copy. |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 52 | if command -v selinuxenabled > /dev/null 2>&1; then |
| 53 | if selinuxenabled; then |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 54 | mountcontext=",rootcontext=$(matchpathcon -n "$mountpoint")" |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 55 | fi |
| 56 | fi |
Andrew Geissler | 9aee500 | 2022-03-30 16:27:02 +0000 | [diff] [blame^] | 57 | if [ "$MOUNT_COPYBIND_AVOID_OVERLAYFS" = 1 ] || ! mount -t overlay overlay -olowerdir="$mountpoint",upperdir="$spec",workdir="$overlay_workdir""$mountcontext" "$mountpoint" > /dev/null 2>&1; then |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 58 | |
| 59 | if [ "$specdir_existed" != "yes" ]; then |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 60 | cp -aPR "$mountpoint"/. "$spec/" |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 61 | fi |
| 62 | |
| 63 | mount -o "bind$options" "$spec" "$mountpoint" |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 64 | # restore the selinux context. |
| 65 | if command -v selinuxenabled > /dev/null 2>&1; then |
| 66 | if selinuxenabled; then |
| 67 | restorecon -R "$mountpoint" |
| 68 | fi |
| 69 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | fi |
| 71 | elif [ -f "$mountpoint" ]; then |
| 72 | if [ ! -f "$spec" ]; then |
Brad Bishop | f3fd288 | 2019-06-21 08:06:37 -0400 | [diff] [blame] | 73 | cp -aP "$mountpoint" "$spec" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | fi |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 75 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 76 | mount -o "bind$options" "$spec" "$mountpoint" |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 77 | # restore the selinux context. |
| 78 | if command -v selinuxenabled > /dev/null 2>&1; then |
| 79 | if selinuxenabled; then |
| 80 | restorecon -R "$mountpoint" |
| 81 | fi |
| 82 | fi |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 83 | fi |