fstests: add _require_hardlinks for all necessary tests
[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 export OVL_XATTR_METACOPY="trusted.overlay.metacopy"
14
15 # helper function to do the actual overlayfs mount operation
16 _overlay_mount_dirs()
17 {
18         local lowerdir=$1
19         local upperdir=$2
20         local workdir=$3
21         shift 3
22
23         $MOUNT_PROG -t overlay -o lowerdir=$lowerdir -o upperdir=$upperdir \
24                     -o workdir=$workdir `_common_dev_mount_options $*`
25 }
26
27 # Mount with same options/mnt/dev of scratch mount, but optionally
28 # with different lower/upper/work dirs
29 _overlay_scratch_mount_dirs()
30 {
31         local lowerdir=$1
32         local upperdir=$2
33         local workdir=$3
34         shift 3
35
36         _overlay_mount_dirs $lowerdir $upperdir $workdir \
37                                 $* $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT
38 }
39
40 _overlay_mkdirs()
41 {
42         local dir=$1
43
44         mkdir -p $dir/$OVL_UPPER
45         mkdir -p $dir/$OVL_LOWER
46         mkdir -p $dir/$OVL_WORK
47         mkdir -p $dir/$OVL_MNT
48 }
49
50 # Given a base fs dir, set up overlay directories and mount on the given mnt.
51 # The dir is used as the mount device so it can be seen from df or mount
52 _overlay_mount()
53 {
54         local dir=$1
55         local mnt=$2
56         shift 2
57
58         _supports_filetype $dir || _notrun "upper fs needs to support d_type"
59
60         _overlay_mkdirs $dir
61
62         _overlay_mount_dirs $dir/$OVL_LOWER $dir/$OVL_UPPER $dir/$OVL_WORK \
63                                 $* $dir $mnt
64 }
65
66 _overlay_base_mount()
67 {
68         local devname=$1
69         local mntname=$2
70         local dev=$3
71         local mnt=$4
72         shift 4
73
74         if [ -z "$dev" -o -z "$mnt" ] || \
75                 _check_mounted_on $devname $dev $mntname $mnt; then
76                 # no base fs or already mounted
77                 return 0
78         elif [ $? -ne 1 ]; then
79                 # base fs mounted but not on mount point
80                 return 1
81         fi
82
83         _mount $* $dev $mnt
84 }
85
86 _overlay_base_test_mount()
87 {
88         _overlay_base_mount OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
89                         "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
90                         $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
91 }
92
93 _overlay_test_mount()
94 {
95         _overlay_base_test_mount && \
96                 _overlay_mount $OVL_BASE_TEST_DIR $TEST_DIR $*
97 }
98
99 _overlay_base_scratch_mount()
100 {
101         _overlay_base_mount OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
102                         "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
103                         $OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
104 }
105
106 _overlay_scratch_mount()
107 {
108         if echo "$*" | grep -q remount; then
109                 $MOUNT_PROG $SCRATCH_MNT $*
110                 return
111         fi
112
113         _overlay_base_scratch_mount && \
114                 _overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
115 }
116
117 _overlay_base_unmount()
118 {
119         local dev=$1
120         local mnt=$2
121
122         [ -n "$dev" -a -n "$mnt" ] || return 0
123
124         $UMOUNT_PROG $mnt
125 }
126
127 _overlay_test_unmount()
128 {
129         $UMOUNT_PROG $TEST_DIR
130         _overlay_base_unmount "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR"
131 }
132
133 _overlay_scratch_unmount()
134 {
135         $UMOUNT_PROG $SCRATCH_MNT
136         _overlay_base_unmount "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT"
137 }
138
139 # Check that a specific overlayfs feature is supported
140 _check_overlay_feature()
141 {
142         local feature=$1
143         local dev=$2
144         local mnt=$3
145
146         # overalyfs features (e.g. redirect_dir, index) are
147         # configurable from Kconfig (the build default), by module
148         # parameter (the system default) and per mount by mount
149         # option ${feature}=[on|off].
150         local default=`_get_fs_module_param ${feature}`
151         [ "$default" = Y ] || [ "$default" = N ] || \
152                 _notrun "feature '${feature}' not supported by ${FSTYP}"
153
154         # Check options to be sure. For example, Overlayfs will fallback to
155         # index=off if underlying fs does not support file handles.
156         # Overlayfs only displays mount option if it differs from the default.
157         # Overlayfs may enable the feature, but fallback to read-only mount.
158         ((( [ "$default" = N ] && _fs_options $dev | grep -q "${feature}=on" ) || \
159           ( [ "$default" = Y ] && ! _fs_options $dev | grep -q "${feature}=off" )) && \
160             touch $mnt/foo 2>/dev/null ) || \
161                 _notrun "${FSTYP} feature '${feature}' cannot be enabled on ${dev}"
162 }
163
164 # Require a set of overlayfs features
165 _require_scratch_overlay_features()
166 {
167         local features=( $* )
168         local opts="rw"
169
170         for feature in ${features[*]}; do
171                 # If the module parameter does not exist then there is no
172                 # point in checking the mount option.
173                 _get_fs_module_param ${feature} > /dev/null 2>&1 || \
174                         _notrun "feature '${feature}' not supported by overlay"
175                 opts+=",${feature}=on"
176         done
177
178         _scratch_mkfs > /dev/null 2>&1
179         _try_scratch_mount -o $opts || \
180                 _notrun "overlay features '${features[*]}' cannot be enabled on ${SCRATCH_DEV}"
181
182         for feature in ${features[*]}; do
183                 _check_overlay_feature ${feature} $SCRATCH_DEV $SCRATCH_MNT
184         done
185
186         _scratch_unmount
187 }
188
189 # Helper function to check underlying dirs of overlay filesystem
190 _overlay_fsck_dirs()
191 {
192         local lowerdir=$1
193         local upperdir=$2
194         local workdir=$3
195         shift 3
196
197         [[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0
198
199         $FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
200                            -o workdir=$workdir $*
201 }
202
203 # Run fsck and check for expected return value
204 _overlay_fsck_expect()
205 {
206         # The first arguments is the expected fsck program exit code, the
207         # remaining arguments are the input parameters of the fsck program.
208         local expect_ret=$1
209         local lowerdir=$2
210         local upperdir=$3
211         local workdir=$4
212         shift 4
213
214         _overlay_fsck_dirs $lowerdir $upperdir $workdir $* >> \
215                         $seqres.full 2>&1
216         fsck_ret=$?
217
218         [[ "$fsck_ret" == "$expect_ret" ]] || \
219                 echo "expect fsck.overlay to return $expect_ret, but got $fsck_ret"
220 }
221
222 _overlay_check_dirs()
223 {
224         local lowerdir=$1
225         local upperdir=$2
226         local workdir=$3
227         shift 3
228         local err=0
229
230         _overlay_fsck_dirs $lowerdir $upperdir $workdir \
231                            $FSCK_OPTIONS $* >>$tmp.fsck 2>&1
232         if [ $? -ne 0 ]; then
233                 _log_err "_overlay_check_fs: overlayfs on $lowerdir,$upperdir,$workdir is inconsistent"
234
235                 echo "*** fsck.overlay output ***"      >>$seqres.full
236                 cat $tmp.fsck                           >>$seqres.full
237                 echo "*** end fsck.overlay output"      >>$seqres.full
238
239                 echo "*** mount output ***"             >>$seqres.full
240                 _mount                                  >>$seqres.full
241                 echo "*** end mount output"             >>$seqres.full
242
243                 err=1
244         fi
245         rm -f $tmp.fsck
246
247         return $err
248 }
249
250 # Check the same mnt/dev of _check_overlay_scratch_fs but non-default
251 # underlying scratch dirs of overlayfs, it needs lower/upper/work dirs
252 # provided as arguments, and it's useful for non-default setups such
253 # as multiple lower layers
254 _overlay_check_scratch_dirs()
255 {
256         local lowerdir=$1
257         local upperdir=$2
258         local workdir=$3
259         shift 3
260
261         # Need to umount overlay for scratch dir check
262         local ovl_mounted=`_is_dir_mountpoint $SCRATCH_MNT`
263         [ -z "$ovl_mounted" ] || $UMOUNT_PROG $SCRATCH_MNT
264
265         # Check dirs with extra overlay options
266         _overlay_check_dirs $lowerdir $upperdir $workdir $*
267         local ret=$?
268
269         if [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
270                 # overlay was mounted, remount with extra mount options
271                 _overlay_scratch_mount_dirs $lowerdir $upperdir \
272                                             $workdir $*
273                 ret=$?
274         fi
275
276         return $ret
277 }
278
279 _overlay_check_fs()
280 {
281         # The first arguments is overlay mount point use for checking
282         # overlay filesystem is mounted or not, the remaining arquments
283         # use for mounting overlay base filesystem if it was not mounted.
284         # We shift one to aligns arguments for _overlay_base_mount.
285         local ovl_mnt=$1
286         shift 1
287
288         local base_dev=$3
289         local base_mnt=$4
290
291         [ "$FSTYP" = overlay ] || return 0
292
293         # Base fs needs to be mounted to check overlay dirs
294         local base_fstype=""
295         local ovl_mounted=""
296
297         [ -z "$base_dev" ] || \
298                 base_fstype=`_fs_type $base_dev`
299
300         # If base_dev is set but base_fstype is empty, base fs is not
301         # mounted, we need to mount base fs. Otherwise, we need to
302         # check and umount overlayfs if it was mounted.
303         if [ -n "$base_dev" -a -z "$base_fstype" ]; then
304                 _overlay_base_mount $*
305         else
306                 # Check and umount overlay for dir check
307                 ovl_mounted=`_is_dir_mountpoint $ovl_mnt`
308                 [ -z "$ovl_mounted" ] || $UMOUNT_PROG $ovl_mnt
309         fi
310
311         _overlay_check_dirs $base_mnt/$OVL_LOWER $base_mnt/$OVL_UPPER \
312                             $base_mnt/$OVL_WORK
313         local ret=$?
314
315         if [ -n "$base_dev" -a -z "$base_fstype" ]; then
316                 _overlay_base_unmount "$base_dev" "$base_mnt"
317         elif [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
318                 # overlay was mounted, remount besides extra mount options
319                 _overlay_mount $base_mnt $ovl_mnt
320                 ret=$?
321         fi
322
323         if [ $ret != 0 ]; then
324                 status=1
325                 if [ "$iam" != "check" ]; then
326                         exit 1
327                 fi
328                 return 1
329         fi
330
331         return 0
332 }
333
334 _check_overlay_test_fs()
335 {
336         _overlay_check_fs "$TEST_DIR" \
337                 OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
338                 "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
339                 $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
340 }
341
342 _check_overlay_scratch_fs()
343 {
344         _overlay_check_fs "$SCRATCH_MNT" \
345                 OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
346                 "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
347                 $OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
348 }
349
350 _repair_overlay_scratch_fs()
351 {
352         _overlay_fsck_dirs $OVL_BASE_SCRATCH_MNT/$OVL_LOWER \
353                 $OVL_BASE_SCRATCH_MNT/$OVL_UPPER \
354                 $OVL_BASE_SCRATCH_MNT/$OVL_WORK -y
355         local res=$?
356         case $res in
357         $FSCK_OK|$FSCK_NONDESTRUCT)
358                 res=0
359                 ;;
360         *)
361                 _dump_err2 "fsck.overlay failed, err=$res"
362                 ;;
363         esac
364         return $res
365 }