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_scratch_overlay_feature()
144 # overalyfs features (e.g. redirect_dir, index) are
145 # configurable from Kconfig (the build default), by module
146 # parameter (the system default) and per mount by mount
147 # option ${feature}=[on|off].
148 local default=`_get_fs_module_param ${feature}`
149 [ "$default" = Y ] || [ "$default" = N ] || \
150 _notrun "feature '${feature}' not supported by ${FSTYP}"
152 # Check options to be sure. For example, Overlayfs will fallback to
153 # index=off if underlying fs does not support file handles.
154 # Overlayfs only displays mount option if it differs from the default.
155 # Overlayfs may enable the feature, but fallback to read-only mount.
156 ((( [ "$default" = N ] && _fs_options $SCRATCH_DEV | grep -q "${feature}=on" ) || \
157 ( [ "$default" = Y ] && ! _fs_options $SCRATCH_DEV | grep -q "${feature}=off" )) && \
158 touch $SCRATCH_MNT/foo 2>/dev/null ) || \
159 _notrun "${FSTYP} feature '${feature}' cannot be enabled on ${SCRATCH_DEV}"
162 # Require a set of overlayfs features
163 _require_scratch_overlay_features()
165 local features=( $* )
168 for feature in ${features[*]}; do
169 # If the module parameter does not exist then there is no
170 # point in checking the mount option.
171 _get_fs_module_param ${feature} > /dev/null 2>&1 || \
172 _notrun "feature '${feature}' not supported by overlay"
173 opts+=",${feature}=on"
176 _scratch_mkfs > /dev/null 2>&1
177 _try_scratch_mount -o $opts || \
178 _notrun "overlay features '${features[*]}' cannot be enabled on ${SCRATCH_DEV}"
180 for feature in ${features[*]}; do
181 __check_scratch_overlay_feature ${feature}
187 # Helper function to check underlying dirs of overlay filesystem
195 [[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0
197 $FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
198 -o workdir=$workdir $*
201 # Run fsck and check for expected return value
202 _overlay_fsck_expect()
204 # The first arguments is the expected fsck program exit code, the
205 # remaining arguments are the input parameters of the fsck program.
212 _overlay_fsck_dirs $lowerdir $upperdir $workdir $* >> \
216 [[ "$fsck_ret" == "$expect_ret" ]] || \
217 echo "expect fsck.overlay to return $expect_ret, but got $fsck_ret"
220 _overlay_check_dirs()
228 _overlay_fsck_dirs $lowerdir $upperdir $workdir \
229 $FSCK_OPTIONS $* >>$tmp.fsck 2>&1
230 if [ $? -ne 0 ]; then
231 _log_err "_overlay_check_fs: overlayfs on $lowerdir,$upperdir,$workdir is inconsistent"
233 echo "*** fsck.overlay output ***" >>$seqres.full
234 cat $tmp.fsck >>$seqres.full
235 echo "*** end fsck.overlay output" >>$seqres.full
237 echo "*** mount output ***" >>$seqres.full
238 _mount >>$seqres.full
239 echo "*** end mount output" >>$seqres.full
248 # Check the same mnt/dev of _check_overlay_scratch_fs but non-default
249 # underlying scratch dirs of overlayfs, it needs lower/upper/work dirs
250 # provided as arguments, and it's useful for non-default setups such
251 # as multiple lower layers
252 _overlay_check_scratch_dirs()
259 # Need to umount overlay for scratch dir check
260 local ovl_mounted=`_is_dir_mountpoint $SCRATCH_MNT`
261 [ -z "$ovl_mounted" ] || $UMOUNT_PROG $SCRATCH_MNT
263 # Check dirs with extra overlay options
264 _overlay_check_dirs $lowerdir $upperdir $workdir $*
267 if [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
268 # overlay was mounted, remount with extra mount options
269 _overlay_scratch_mount_dirs $lowerdir $upperdir \
279 # The first arguments is overlay mount point use for checking
280 # overlay filesystem is mounted or not, the remaining arquments
281 # use for mounting overlay base filesystem if it was not mounted.
282 # We shift one to aligns arguments for _overlay_base_mount.
289 [ "$FSTYP" = overlay ] || return 0
291 # Base fs needs to be mounted to check overlay dirs
295 [ -z "$base_dev" ] || \
296 base_fstype=`_fs_type $base_dev`
298 # If base_dev is set but base_fstype is empty, base fs is not
299 # mounted, we need to mount base fs. Otherwise, we need to
300 # check and umount overlayfs if it was mounted.
301 if [ -n "$base_dev" -a -z "$base_fstype" ]; then
302 _overlay_base_mount $*
304 # Check and umount overlay for dir check
305 ovl_mounted=`_is_dir_mountpoint $ovl_mnt`
306 [ -z "$ovl_mounted" ] || $UMOUNT_PROG $ovl_mnt
309 _overlay_check_dirs $base_mnt/$OVL_LOWER $base_mnt/$OVL_UPPER \
313 if [ -n "$base_dev" -a -z "$base_fstype" ]; then
314 _overlay_base_unmount "$base_dev" "$base_mnt"
315 elif [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
316 # overlay was mounted, remount besides extra mount options
317 _overlay_mount $base_mnt $ovl_mnt
321 if [ $ret != 0 ]; then
323 if [ "$iam" != "check" ]; then
332 _check_overlay_test_fs()
334 _overlay_check_fs "$TEST_DIR" \
335 OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
336 "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
337 $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
340 _check_overlay_scratch_fs()
342 _overlay_check_fs "$SCRATCH_MNT" \
343 OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
344 "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
345 $OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
348 _repair_overlay_scratch_fs()
350 _overlay_fsck_dirs $OVL_BASE_SCRATCH_MNT/$OVL_LOWER \
351 $OVL_BASE_SCRATCH_MNT/$OVL_UPPER \
352 $OVL_BASE_SCRATCH_MNT/$OVL_WORK -y
355 $FSCK_OK|$FSCK_NONDESTRUCT)
359 _dump_err2 "fsck.overlay failed, err=$res"