2 # overlayfs specific common functions.
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"
15 # helper function to do the actual overlayfs mount operation
23 $MOUNT_PROG -t overlay -o lowerdir=$lowerdir -o upperdir=$upperdir \
24 -o workdir=$workdir `_common_dev_mount_options $*`
27 # Mount with same options/mnt/dev of scratch mount, but optionally
28 # with different lower/upper/work dirs
29 _overlay_scratch_mount_dirs()
36 _overlay_mount_dirs $lowerdir $upperdir $workdir \
37 $* $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT
44 mkdir -p $dir/$OVL_UPPER
45 mkdir -p $dir/$OVL_LOWER
46 mkdir -p $dir/$OVL_WORK
47 mkdir -p $dir/$OVL_MNT
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
58 _supports_filetype $dir || _notrun "upper fs needs to support d_type"
62 _overlay_mount_dirs $dir/$OVL_LOWER $dir/$OVL_UPPER $dir/$OVL_WORK \
74 if [ -z "$dev" -o -z "$mnt" ] || \
75 _check_mounted_on $devname $dev $mntname $mnt; then
76 # no base fs or already mounted
78 elif [ $? -ne 1 ]; then
79 # base fs mounted but not on mount point
86 _overlay_base_test_mount()
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
95 _overlay_base_test_mount && \
96 _overlay_mount $OVL_BASE_TEST_DIR $TEST_DIR $*
99 _overlay_base_scratch_mount()
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
106 _overlay_scratch_mount()
108 if echo "$*" | grep -q remount; then
109 $MOUNT_PROG $SCRATCH_MNT $*
113 _overlay_base_scratch_mount && \
114 _overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
117 _overlay_base_unmount()
122 [ -n "$dev" -a -n "$mnt" ] || return 0
127 _overlay_test_unmount()
129 $UMOUNT_PROG $TEST_DIR
130 _overlay_base_unmount "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR"
133 _overlay_scratch_unmount()
135 $UMOUNT_PROG $SCRATCH_MNT
136 _overlay_base_unmount "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT"
139 # Check that a specific overlayfs feature is supported
140 _check_overlay_feature()
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}"
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}"
164 # Require a set of overlayfs features
165 _require_scratch_overlay_features()
167 local features=( $* )
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"
178 _scratch_mkfs > /dev/null 2>&1
179 _try_scratch_mount -o $opts || \
180 _notrun "overlay features '${features[*]}' cannot be enabled on ${SCRATCH_DEV}"
182 for feature in ${features[*]}; do
183 _check_overlay_feature ${feature} $SCRATCH_DEV $SCRATCH_MNT
189 # Helper function to check underlying dirs of overlay filesystem
197 [[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0
199 $FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
200 -o workdir=$workdir $*
203 # Run fsck and check for expected return value
204 _overlay_fsck_expect()
206 # The first arguments is the expected fsck program exit code, the
207 # remaining arguments are the input parameters of the fsck program.
214 _overlay_fsck_dirs $lowerdir $upperdir $workdir $* >> \
218 [[ "$fsck_ret" == "$expect_ret" ]] || \
219 echo "expect fsck.overlay to return $expect_ret, but got $fsck_ret"
222 _overlay_check_dirs()
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"
235 echo "*** fsck.overlay output ***" >>$seqres.full
236 cat $tmp.fsck >>$seqres.full
237 echo "*** end fsck.overlay output" >>$seqres.full
239 echo "*** mount output ***" >>$seqres.full
240 _mount >>$seqres.full
241 echo "*** end mount output" >>$seqres.full
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()
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
265 # Check dirs with extra overlay options
266 _overlay_check_dirs $lowerdir $upperdir $workdir $*
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 \
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.
291 [ "$FSTYP" = overlay ] || return 0
293 # Base fs needs to be mounted to check overlay dirs
297 [ -z "$base_dev" ] || \
298 base_fstype=`_fs_type $base_dev`
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 $*
306 # Check and umount overlay for dir check
307 ovl_mounted=`_is_dir_mountpoint $ovl_mnt`
308 [ -z "$ovl_mounted" ] || $UMOUNT_PROG $ovl_mnt
311 _overlay_check_dirs $base_mnt/$OVL_LOWER $base_mnt/$OVL_UPPER \
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
323 if [ $ret != 0 ]; then
325 if [ "$iam" != "check" ]; then
334 _check_overlay_test_fs()
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
342 _check_overlay_scratch_fs()
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
350 _repair_overlay_scratch_fs()
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
357 $FSCK_OK|$FSCK_NONDESTRUCT)
361 _dump_err2 "fsck.overlay failed, err=$res"
367 # This test requires that unionmount testsuite is installed at
368 # $UNIONMOUNT_TESTSUITE and that it supports configuring layers and overlay
369 # mount paths via UNIONMOUNT_* environment variables.
370 _require_unionmount_testsuite()
372 [ -x "$UNIONMOUNT_TESTSUITE/run" ] || \
373 _notrun "unionmount testsuite required."
375 # Verify that UNIONMOUNT_* vars are supported
376 local usage=`UNIONMOUNT_BASEDIR=_ "$UNIONMOUNT_TESTSUITE/run" 2>&1`
377 echo $usage | grep -wq "UNIONMOUNT_BASEDIR" || \
378 _notrun "newer version of unionmount testsuite required."
380 [ -n "$OVERLAY_MOUNT_OPTIONS" ] || return
381 # If custom overlay mount options are used
382 # verify that UNIONMOUNT_MNTOPTIONS var is supported
383 local usage=`UNIONMOUNT_MNTOPTIONS=_ "$UNIONMOUNT_TESTSUITE/run" 2>&1`
384 echo $usage | grep -wq "UNIONMOUNT_MNTOPTIONS" || \
385 _notrun "newer version of unionmount testsuite required to support OVERLAY_MOUNT_OPTIONS."
388 _unionmount_testsuite_run()
390 [ "$FSTYP" = overlay ] || \
391 _notrun "Filesystem $FSTYP not supported with unionmount testsuite."
393 # Provide the mounted base fs for upper and lower dirs and the
394 # overlay mount point.
395 # unionmount testsuite will perform the overlay mount.
396 # test fs is used for lower layer in non-samefs runs.
397 # scratch fs is used for upper layer in non-samefs runs and
398 # for both layers in samefs runs.
399 if (echo $* | grep -qv samefs) ; then
400 _overlay_base_test_mount
401 export UNIONMOUNT_LOWERDIR=$OVL_BASE_TEST_DIR/union
403 export UNIONMOUNT_BASEDIR=$OVL_BASE_SCRATCH_MNT/union
404 export UNIONMOUNT_MNTOPTIONS="$OVERLAY_MOUNT_OPTIONS"
407 rm -rf $UNIONMOUNT_BASEDIR $UNIONMOUNT_LOWERDIR
408 mkdir -p $UNIONMOUNT_BASEDIR $UNIONMOUNT_LOWERDIR
410 cd $UNIONMOUNT_TESTSUITE
411 echo "run $* ..." > $seqres.full
412 ./run $* >> $seqres.full || \
413 echo "unionmount testsuite failed! see $seqres.full for details."
416 _unionmount_testsuite_cleanup()
421 [ -n "$UNIONMOUNT_BASEDIR" ] || return 0
423 # Cleanup overlay mount after unionmount testsuite run
424 cd $UNIONMOUNT_TESTSUITE
425 echo "run --clean-up ..." >> $seqres.full
426 ./run --clean-up >> $seqres.full 2>&1