overlay: Add tests for overlay metadata only copy up feature
[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         _overlay_base_scratch_mount && \
109                 _overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
110 }
111
112 _overlay_base_unmount()
113 {
114         local dev=$1
115         local mnt=$2
116
117         [ -n "$dev" -a -n "$mnt" ] || return 0
118
119         $UMOUNT_PROG $mnt
120 }
121
122 _overlay_test_unmount()
123 {
124         $UMOUNT_PROG $TEST_DIR
125         _overlay_base_unmount "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR"
126 }
127
128 _overlay_scratch_unmount()
129 {
130         $UMOUNT_PROG $SCRATCH_MNT
131         _overlay_base_unmount "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT"
132 }
133
134 # Check that a specific overlayfs feature is supported
135 __check_scratch_overlay_feature()
136 {
137         local feature=$1
138
139         # overalyfs features (e.g. redirect_dir, index) are
140         # configurable from Kconfig (the build default), by module
141         # parameter (the system default) and per mount by mount
142         # option ${feature}=[on|off].
143         #
144         # If the module parameter does not exist then there is no
145         # point in checking the mount option.
146         local default=`_get_fs_module_param ${feature}`
147         [ "$default" = Y ] || [ "$default" = N ] || \
148                 _notrun "feature '${feature}' not supported by ${FSTYP}"
149
150         # Check options to be sure. For example, Overlayfs will fallback to
151         # index=off if underlying fs does not support file handles.
152         # Overlayfs only displays mount option if it differs from the default.
153         # Overlayfs may enable the feature, but fallback to read-only mount.
154         ((( [ "$default" = N ] && _fs_options $SCRATCH_DEV | grep -q "${feature}=on" ) || \
155           ( [ "$default" = Y ] && ! _fs_options $SCRATCH_DEV | grep -q "${feature}=off" )) && \
156             touch $SCRATCH_MNT/foo 2>/dev/null ) || \
157                 _notrun "${FSTYP} feature '${feature}' cannot be enabled on ${SCRATCH_DEV}"
158 }
159
160 # Require a set of overlayfs features
161 _require_scratch_overlay_features()
162 {
163         local features=( $* )
164         local opts="rw"
165
166         for feature in ${features[*]}; do
167                 opts+=",${feature}=on"
168         done
169
170         _scratch_mkfs > /dev/null 2>&1
171         _try_scratch_mount -o $opts || \
172                 _notrun "overlay options '$opts' cannot be enabled on ${SCRATCH_DEV}"
173
174         for feature in ${features[*]}; do
175                 __check_scratch_overlay_feature ${feature}
176         done
177
178         _scratch_unmount
179 }
180
181 # Helper function to check underlying dirs of overlay filesystem
182 _overlay_fsck_dirs()
183 {
184         local lowerdir=$1
185         local upperdir=$2
186         local workdir=$3
187         shift 3
188
189         [[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0
190
191         $FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
192                            -o workdir=$workdir $*
193 }
194
195 _overlay_check_dirs()
196 {
197         local lowerdir=$1
198         local upperdir=$2
199         local workdir=$3
200         shift 3
201         local err=0
202
203         _overlay_fsck_dirs $lowerdir $upperdir $workdir \
204                            $FSCK_OPTIONS $* >>$tmp.fsck 2>&1
205         if [ $? -ne 0 ]; then
206                 _log_err "_overlay_check_fs: overlayfs on $lowerdir,$upperdir,$workdir is inconsistent"
207
208                 echo "*** fsck.overlay output ***"      >>$seqres.full
209                 cat $tmp.fsck                           >>$seqres.full
210                 echo "*** end fsck.overlay output"      >>$seqres.full
211
212                 echo "*** mount output ***"             >>$seqres.full
213                 _mount                                  >>$seqres.full
214                 echo "*** end mount output"             >>$seqres.full
215
216                 err=1
217         fi
218         rm -f $tmp.fsck
219
220         return $err
221 }
222
223 # Check the same mnt/dev of _check_overlay_scratch_fs but non-default
224 # underlying scratch dirs of overlayfs, it needs lower/upper/work dirs
225 # provided as arguments, and it's useful for non-default setups such
226 # as multiple lower layers
227 _overlay_check_scratch_dirs()
228 {
229         local lowerdir=$1
230         local upperdir=$2
231         local workdir=$3
232         shift 3
233
234         # Need to umount overlay for scratch dir check
235         local ovl_mounted=`_is_dir_mountpoint $SCRATCH_MNT`
236         [ -z "$ovl_mounted" ] || $UMOUNT_PROG $SCRATCH_MNT
237
238         # Check dirs with extra overlay options
239         _overlay_check_dirs $lowerdir $upperdir $workdir $*
240         local ret=$?
241
242         if [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
243                 # overlay was mounted, remount with extra mount options
244                 _overlay_scratch_mount_dirs $lowerdir $upperdir \
245                                             $workdir $*
246                 ret=$?
247         fi
248
249         return $ret
250 }
251
252 _overlay_check_fs()
253 {
254         # The first arguments is overlay mount point use for checking
255         # overlay filesystem is mounted or not, the remaining arquments
256         # use for mounting overlay base filesystem if it was not mounted.
257         # We shift one to aligns arguments for _overlay_base_mount.
258         local ovl_mnt=$1
259         shift 1
260
261         local base_dev=$3
262         local base_mnt=$4
263
264         [ "$FSTYP" = overlay ] || return 0
265
266         # Base fs needs to be mounted to check overlay dirs
267         local base_fstype=""
268         local ovl_mounted=""
269
270         [ -z "$base_dev" ] || \
271                 base_fstype=`_fs_type $base_dev`
272
273         # If base_dev is set but base_fstype is empty, base fs is not
274         # mounted, we need to mount base fs. Otherwise, we need to
275         # check and umount overlayfs if it was mounted.
276         if [ -n "$base_dev" -a -z "$base_fstype" ]; then
277                 _overlay_base_mount $*
278         else
279                 # Check and umount overlay for dir check
280                 ovl_mounted=`_is_dir_mountpoint $ovl_mnt`
281                 [ -z "$ovl_mounted" ] || $UMOUNT_PROG $ovl_mnt
282         fi
283
284         _overlay_check_dirs $base_mnt/$OVL_LOWER $base_mnt/$OVL_UPPER \
285                             $base_mnt/$OVL_WORK
286         local ret=$?
287
288         if [ -n "$base_dev" -a -z "$base_fstype" ]; then
289                 _overlay_base_unmount "$base_dev" "$base_mnt"
290         elif [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
291                 # overlay was mounted, remount besides extra mount options
292                 _overlay_mount $base_mnt $ovl_mnt
293                 ret=$?
294         fi
295
296         if [ $ret != 0 ]; then
297                 status=1
298                 if [ "$iam" != "check" ]; then
299                         exit 1
300                 fi
301                 return 1
302         fi
303
304         return 0
305 }
306
307 _check_overlay_test_fs()
308 {
309         _overlay_check_fs "$TEST_DIR" \
310                 OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
311                 "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
312                 $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
313 }
314
315 _check_overlay_scratch_fs()
316 {
317         _overlay_check_fs "$SCRATCH_MNT" \
318                 OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
319                 "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
320                 $OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
321 }