a8b0e9369c86e1dc937527ef02dfdcb28dac76c3
[xfstests-dev.git] / common / overlay
1 #
2 # overlayfs specific common functions.
3 #
4 . ./common/module
5
6 # Export overlayfs xattrs and constant value
7 export OVL_XATTR_OPAQUE="trusted.overlay.opaque"
8 export OVL_XATTR_REDIRECT="trusted.overlay.redirect"
9 export OVL_XATTR_IMPURE="trusted.overlay.impure"
10 export OVL_XATTR_ORIGIN="trusted.overlay.origin"
11 export OVL_XATTR_NLINK="trusted.overlay.nlink"
12 export OVL_XATTR_UPPER="trusted.overlay.upper"
13
14 # helper function to do the actual overlayfs mount operation
15 _overlay_mount_dirs()
16 {
17         local lowerdir=$1
18         local upperdir=$2
19         local workdir=$3
20         shift 3
21
22         $MOUNT_PROG -t overlay -o lowerdir=$lowerdir -o upperdir=$upperdir \
23                     -o workdir=$workdir `_common_dev_mount_options $*`
24 }
25
26 # Mount with same options/mnt/dev of scratch mount, but optionally
27 # with different lower/upper/work dirs
28 _overlay_scratch_mount_dirs()
29 {
30         local lowerdir=$1
31         local upperdir=$2
32         local workdir=$3
33         shift 3
34
35         _overlay_mount_dirs $lowerdir $upperdir $workdir \
36                                 $* $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT
37 }
38
39 _overlay_mkdirs()
40 {
41         local dir=$1
42
43         mkdir -p $dir/$OVL_UPPER
44         mkdir -p $dir/$OVL_LOWER
45         mkdir -p $dir/$OVL_WORK
46         mkdir -p $dir/$OVL_MNT
47 }
48
49 # Given a base fs dir, set up overlay directories and mount on the given mnt.
50 # The dir is used as the mount device so it can be seen from df or mount
51 _overlay_mount()
52 {
53         local dir=$1
54         local mnt=$2
55         shift 2
56
57         _supports_filetype $dir || _notrun "upper fs needs to support d_type"
58
59         _overlay_mkdirs $dir
60
61         _overlay_mount_dirs $dir/$OVL_LOWER $dir/$OVL_UPPER $dir/$OVL_WORK \
62                                 $* $dir $mnt
63 }
64
65 _overlay_base_mount()
66 {
67         local devname=$1
68         local mntname=$2
69         local dev=$3
70         local mnt=$4
71         shift 4
72
73         if [ -z "$dev" -o -z "$mnt" ] || \
74                 _check_mounted_on $devname $dev $mntname $mnt; then
75                 # no base fs or already mounted
76                 return 0
77         elif [ $? -ne 1 ]; then
78                 # base fs mounted but not on mount point
79                 return 1
80         fi
81
82         _mount $* $dev $mnt
83 }
84
85 _overlay_base_test_mount()
86 {
87         _overlay_base_mount OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
88                         "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
89                         $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
90 }
91
92 _overlay_test_mount()
93 {
94         _overlay_base_test_mount && \
95                 _overlay_mount $OVL_BASE_TEST_DIR $TEST_DIR $*
96 }
97
98 _overlay_base_scratch_mount()
99 {
100         _overlay_base_mount OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
101                         "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
102                         $OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
103 }
104
105 _overlay_scratch_mount()
106 {
107         _overlay_base_scratch_mount && \
108                 _overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
109 }
110
111 _overlay_base_unmount()
112 {
113         local dev=$1
114         local mnt=$2
115
116         [ -n "$dev" -a -n "$mnt" ] || return 0
117
118         $UMOUNT_PROG $mnt
119 }
120
121 _overlay_test_unmount()
122 {
123         $UMOUNT_PROG $TEST_DIR
124         _overlay_base_unmount "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR"
125 }
126
127 _overlay_scratch_unmount()
128 {
129         $UMOUNT_PROG $SCRATCH_MNT
130         _overlay_base_unmount "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT"
131 }
132
133 # Check that a specific overlayfs feature is supported
134 __check_scratch_overlay_feature()
135 {
136         local feature=$1
137
138         # overalyfs features (e.g. redirect_dir, index) are
139         # configurable from Kconfig (the build default), by module
140         # parameter (the system default) and per mount by mount
141         # option ${feature}=[on|off].
142         #
143         # If the module parameter does not exist then there is no
144         # point in checking the mount option.
145         local default=`_get_fs_module_param ${feature}`
146         [ "$default" = Y ] || [ "$default" = N ] || \
147                 _notrun "feature '${feature}' not supported by ${FSTYP}"
148
149         # Check options to be sure. For example, Overlayfs will fallback to
150         # index=off if underlying fs does not support file handles.
151         # Overlayfs only displays mount option if it differs from the default.
152         # Overlayfs may enable the feature, but fallback to read-only mount.
153         ((( [ "$default" = N ] && _fs_options $SCRATCH_DEV | grep -q "${feature}=on" ) || \
154           ( [ "$default" = Y ] && ! _fs_options $SCRATCH_DEV | grep -q "${feature}=off" )) && \
155             touch $SCRATCH_MNT/foo 2>/dev/null ) || \
156                 _notrun "${FSTYP} feature '${feature}' cannot be enabled on ${SCRATCH_DEV}"
157 }
158
159 # Require a set of overlayfs features
160 _require_scratch_overlay_features()
161 {
162         local features=( $* )
163         local opts="rw"
164
165         for feature in ${features[*]}; do
166                 opts+=",${feature}=on"
167         done
168
169         _scratch_mkfs > /dev/null 2>&1
170         _scratch_mount -o $opts || \
171                 _notrun "overlay options '$opts' cannot be enabled on ${SCRATCH_DEV}"
172
173         for feature in ${features[*]}; do
174                 __check_scratch_overlay_feature ${feature}
175         done
176
177         _scratch_unmount
178 }
179
180 # Helper function to check underlying dirs of overlay filesystem
181 _overlay_fsck_dirs()
182 {
183         local lowerdir=$1
184         local upperdir=$2
185         local workdir=$3
186         shift 3
187
188         [[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0
189
190         $FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
191                            -o workdir=$workdir $*
192 }