blob: aad022c6e4c4c4329dfdfe00727fbed46e25e028 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/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
6if [ $# -lt 2 ]; then
7 echo >&2 "Usage: $0 spec mountpoint [OPTIONS]"
8 exit 1
9fi
10
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080011# e.g. /var/volatile/lib
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012spec=$1
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080013
14# e.g. /var/lib
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015mountpoint=$2
16
17if [ $# -gt 2 ]; then
18 options=$3
19else
20 options=
21fi
22
23[ -n "$options" ] && options=",$options"
24
25mkdir -p "${spec%/*}"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080026
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027if [ -d "$mountpoint" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080028
29 if [ -d "$spec" ]; then
30 specdir_existed=yes
31 else
32 specdir_existed=no
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 mkdir "$spec"
Andrew Geisslereff27472021-10-29 15:35:00 -050034 # 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 Bishop1a4b7ee2018-12-16 17:11:34 -080041 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 Geisslereff27472021-10-29 15:35:00 -050049 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 Bishop1a4b7ee2018-12-16 17:11:34 -080055
56 if [ "$specdir_existed" != "yes" ]; then
Brad Bishopf3fd2882019-06-21 08:06:37 -040057 cp -aPR "$mountpoint"/. "$spec/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080058 fi
59
60 mount -o "bind$options" "$spec" "$mountpoint"
Andrew Geisslereff27472021-10-29 15:35:00 -050061 # 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 Williamsc124f4f2015-09-15 14:41:29 -050067 fi
68elif [ -f "$mountpoint" ]; then
69 if [ ! -f "$spec" ]; then
Brad Bishopf3fd2882019-06-21 08:06:37 -040070 cp -aP "$mountpoint" "$spec"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080073 mount -o "bind$options" "$spec" "$mountpoint"
Andrew Geisslereff27472021-10-29 15:35:00 -050074 # 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 Bishop1a4b7ee2018-12-16 17:11:34 -080080fi