blob: ddc43576159b3da65bacaa1714a7c08955b24157 [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.
Andrew Geissler9aee5002022-03-30 16:27:02 +00005# 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 Williamsc124f4f2015-09-15 14:41:29 -05008
9if [ $# -lt 2 ]; then
10 echo >&2 "Usage: $0 spec mountpoint [OPTIONS]"
11 exit 1
12fi
13
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080014# e.g. /var/volatile/lib
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015spec=$1
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080016
17# e.g. /var/lib
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018mountpoint=$2
19
20if [ $# -gt 2 ]; then
21 options=$3
22else
23 options=
24fi
25
26[ -n "$options" ] && options=",$options"
27
28mkdir -p "${spec%/*}"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080029
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030if [ -d "$mountpoint" ]; then
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080031
32 if [ -d "$spec" ]; then
33 specdir_existed=yes
34 else
35 specdir_existed=no
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036 mkdir "$spec"
Andrew Geisslereff27472021-10-29 15:35:00 -050037 # 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 Bishop1a4b7ee2018-12-16 17:11:34 -080044 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 Geisslereff27472021-10-29 15:35:00 -050052 if command -v selinuxenabled > /dev/null 2>&1; then
53 if selinuxenabled; then
Andrew Geissler9aee5002022-03-30 16:27:02 +000054 mountcontext=",rootcontext=$(matchpathcon -n "$mountpoint")"
Andrew Geisslereff27472021-10-29 15:35:00 -050055 fi
56 fi
Andrew Geissler9aee5002022-03-30 16:27:02 +000057 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 Bishop1a4b7ee2018-12-16 17:11:34 -080058
59 if [ "$specdir_existed" != "yes" ]; then
Brad Bishopf3fd2882019-06-21 08:06:37 -040060 cp -aPR "$mountpoint"/. "$spec/"
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080061 fi
62
63 mount -o "bind$options" "$spec" "$mountpoint"
Andrew Geisslereff27472021-10-29 15:35:00 -050064 # 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 Williamsc124f4f2015-09-15 14:41:29 -050070 fi
71elif [ -f "$mountpoint" ]; then
72 if [ ! -f "$spec" ]; then
Brad Bishopf3fd2882019-06-21 08:06:37 -040073 cp -aP "$mountpoint" "$spec"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050074 fi
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075
Brad Bishop1a4b7ee2018-12-16 17:11:34 -080076 mount -o "bind$options" "$spec" "$mountpoint"
Andrew Geisslereff27472021-10-29 15:35:00 -050077 # 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 Bishop1a4b7ee2018-12-16 17:11:34 -080083fi