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