aboutsummaryrefslogtreecommitdiffstats
path: root/common/overlay
blob: faa9339a6477f71c30ca8a0e7e9f9afcbedf9e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#
# overlayfs specific common functions.
#
. ./common/module

# Export overlayfs xattrs and constant value
export OVL_XATTR_OPAQUE="trusted.overlay.opaque"
export OVL_XATTR_REDIRECT="trusted.overlay.redirect"
export OVL_XATTR_IMPURE="trusted.overlay.impure"
export OVL_XATTR_ORIGIN="trusted.overlay.origin"
export OVL_XATTR_NLINK="trusted.overlay.nlink"
export OVL_XATTR_UPPER="trusted.overlay.upper"
export OVL_XATTR_METACOPY="trusted.overlay.metacopy"

if [ -n "$OVL_BASE_FSTYP" ];then
	_source_specific_fs $OVL_BASE_FSTYP
fi

# helper function to do the actual overlayfs mount operation
# accepts "-" as upperdir for non-upper overlayfs
_overlay_mount_dirs()
{
	local lowerdir=$1
	local upperdir=$2
	local workdir=$3
	shift 3
	local diropts="-olowerdir=$lowerdir"

	[ -n "$upperdir" ] && [ "$upperdir" != "-" ] && \
		diropts+=",upperdir=$upperdir,workdir=$workdir"

	$MOUNT_PROG -t overlay $diropts `_common_dev_mount_options $*`
}

# Mount with mnt/dev of scratch mount and custom mount options
_overlay_scratch_mount_opts()
{
	$MOUNT_PROG -t overlay $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
}

# Mount with same options/mnt/dev of scratch mount, but optionally
# with different lower/upper/work dirs
_overlay_scratch_mount_dirs()
{
	local lowerdir=$1
	local upperdir=$2
	local workdir=$3
	shift 3

	_overlay_mount_dirs $lowerdir $upperdir $workdir \
				$* $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT
}

_overlay_mkdirs()
{
	local dir=$1

	mkdir -p $dir/$OVL_UPPER
	mkdir -p $dir/$OVL_LOWER
	mkdir -p $dir/$OVL_WORK
	mkdir -p $dir/$OVL_MNT
}

# Given a base fs dir, set up overlay directories and mount on the given mnt.
# The dir is used as the mount device so it can be seen from df or mount
_overlay_mount()
{
	local dir=$1
	local mnt=$2
	shift 2

	_supports_filetype $dir || _notrun "upper fs needs to support d_type"

	_overlay_mkdirs $dir

	_overlay_mount_dirs $dir/$OVL_LOWER $dir/$OVL_UPPER $dir/$OVL_WORK \
				$* $dir $mnt
}

_overlay_base_mount()
{
	local devname=$1
	local mntname=$2
	local dev=$3
	local mnt=$4
	shift 4

	if [ -z "$dev" -o -z "$mnt" ] || \
		_check_mounted_on $devname $dev $mntname $mnt; then
		_idmapped_mount $dev $mnt
		# no base fs or already mounted
		return 0
	elif [ $? -ne 1 ]; then
		# base fs mounted but not on mount point
		return 1
	fi

	if [ $OVL_BASE_FSTYP ]; then
		_mount -t $OVL_BASE_FSTYP $* $dev $mnt
	else
		_mount $* $dev $mnt
	fi

	_idmapped_mount $dev $mnt
}

_overlay_base_test_mount()
{
	_overlay_base_mount OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
			"$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
			$TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
}

_overlay_test_mount()
{
	_overlay_base_test_mount && \
		_overlay_mount $OVL_BASE_TEST_DIR $TEST_DIR $*
}

_overlay_base_scratch_mount()
{
	_overlay_base_mount OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
			"$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
			$OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
}

_overlay_scratch_mount()
{
	if echo "$*" | grep -q remount; then
		$MOUNT_PROG $SCRATCH_MNT $*
		return
	fi

	_overlay_base_scratch_mount && \
		_overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
}

_overlay_base_unmount()
{
	local dev=$1
	local mnt=$2

	[ -n "$dev" -a -n "$mnt" ] || return 0

	$UMOUNT_PROG $mnt
}

_overlay_test_unmount()
{
	$UMOUNT_PROG $TEST_DIR
	_overlay_base_unmount "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR"
}

_overlay_scratch_unmount()
{
	$UMOUNT_PROG $SCRATCH_MNT
	_overlay_base_unmount "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT"
}

# Check that a specific overlayfs feature is supported
_check_overlay_feature()
{
	local feature=$1
	local dev=$2
	local mnt=$3

	# overalyfs features (e.g. redirect_dir, index) are
	# configurable from Kconfig (the build default), by module
	# parameter (the system default) and per mount by mount
	# option ${feature}=[on|off].
	local default=`_get_fs_module_param ${feature}`
	[ "$default" = Y ] || [ "$default" = N ] || \
		_notrun "feature '${feature}' not supported by ${FSTYP}"

	# Check options to be sure. For example, Overlayfs will fallback to
	# index=off if underlying fs does not support file handles.
	# Overlayfs only displays mount option if it differs from the default.
	# Overlayfs may enable the feature, but fallback to read-only mount.
	((( [ "$default" = N ] && _fs_options $dev | grep -q "${feature}=on" ) || \
	  ( [ "$default" = Y ] && ! _fs_options $dev | grep -q "${feature}=off" )) && \
	    touch $mnt/foo 2>/dev/null ) || \
	        _notrun "${FSTYP} feature '${feature}' cannot be enabled on ${dev}"
}

# Require a set of overlayfs features
_require_scratch_overlay_features()
{
	local features=( $* )
	local opts="rw"

	for feature in ${features[*]}; do
		# If the module parameter does not exist then there is no
		# point in checking the mount option.
		_get_fs_module_param ${feature} > /dev/null 2>&1 || \
			_notrun "feature '${feature}' not supported by overlay"
		opts+=",${feature}=on"
	done

	_scratch_mkfs > /dev/null 2>&1
	_try_scratch_mount -o $opts || \
	        _notrun "overlay features '${features[*]}' cannot be enabled on ${SCRATCH_DEV}"

	for feature in ${features[*]}; do
		_check_overlay_feature ${feature} $SCRATCH_DEV $SCRATCH_MNT
	done

	_scratch_unmount
}

_check_scratch_overlay_xattr_escapes()
{
	local testfile=$1

	touch $testfile
	! ($GETFATTR_PROG -n trusted.overlay.foo $testfile 2>&1 | grep -E -q "not (permitted|supported)")
}

_require_scratch_overlay_xattr_escapes()
{
	_scratch_mkfs > /dev/null 2>&1
	_scratch_mount

        _check_scratch_overlay_xattr_escapes $SCRATCH_MNT/file || \
                  _notrun "xattr escaping is not supported by overlay"

	_scratch_unmount
}

_require_scratch_overlay_verity()
{
	local lowerdirs="$OVL_BASE_SCRATCH_MNT/$OVL_UPPER:$OVL_BASE_SCRATCH_MNT/$OVL_LOWER"

	_require_scratch_verity "$OVL_BASE_FSTYP" "$OVL_BASE_SCRATCH_MNT"

	_scratch_mkfs > /dev/null 2>&1
	_overlay_scratch_mount_dirs "$lowerdirs" "-" "-" \
		-o ro,redirect_dir=follow,metacopy=on,verity=on > /dev/null 2>&1 || \
	        _notrun "overlay verity not supported on ${SCRATCH_DEV}"

	_scratch_unmount
}

# Check kernel support for <lowerdirs>::<lowerdatadir> format
_require_scratch_overlay_lowerdata_layers()
{
	local lowerdirs="$OVL_BASE_SCRATCH_MNT/$OVL_UPPER::$OVL_BASE_SCRATCH_MNT/$OVL_LOWER"

	_scratch_mkfs > /dev/null 2>&1
	_overlay_scratch_mount_dirs "$lowerdirs" "-" "-" \
		-o ro,redirect_dir=follow,metacopy=on > /dev/null 2>&1 || \
	        _notrun "overlay data-only layers not supported on ${SCRATCH_DEV}"

	_scratch_unmount
}

# Check kernel support for lowerdir+=<lowerdir>,datadir+=<lowerdatadir> format
_require_scratch_overlay_lowerdir_add_layers()
{
	local lowerdir="$OVL_BASE_SCRATCH_MNT/$OVL_UPPER"
	local datadir="$OVL_BASE_SCRATCH_MNT/$OVL_LOWER"

	_scratch_mkfs > /dev/null 2>&1
	_overlay_scratch_mount_opts \
		-o"lowerdir+=$lowerdir,datadir+=$datadir" \
		-o"redirect_dir=follow,metacopy=on" > /dev/null 2>&1 || \
	        _notrun "overlay lowerdir+,datadir+ not supported on ${SCRATCH_DEV}"

	_scratch_unmount
}

# Helper function to check underlying dirs of overlay filesystem
_overlay_fsck_dirs()
{
	local lowerdir=$1
	local upperdir=$2
	local workdir=$3
	shift 3

	[[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0

	$FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
			   -o workdir=$workdir $*
}

# Run fsck and check for expected return value
_overlay_fsck_expect()
{
	# The first arguments is the expected fsck program exit code, the
	# remaining arguments are the input parameters of the fsck program.
	local expect_ret=$1
	local lowerdir=$2
	local upperdir=$3
	local workdir=$4
	shift 4

	_overlay_fsck_dirs $lowerdir $upperdir $workdir $* >> \
			$seqres.full 2>&1
	fsck_ret=$?

	[[ "$fsck_ret" == "$expect_ret" ]] || \
		echo "expect fsck.overlay to return $expect_ret, but got $fsck_ret"
}

_overlay_check_dirs()
{
	local lowerdir=$1
	local upperdir=$2
	local workdir=$3
	shift 3
	local err=0

	_overlay_fsck_dirs $lowerdir $upperdir $workdir \
			   $FSCK_OPTIONS $* >>$tmp.fsck 2>&1
	if [ $? -ne 0 ]; then
		_log_err "_overlay_check_fs: overlayfs on $lowerdir,$upperdir,$workdir is inconsistent"

		echo "*** fsck.overlay output ***"	>>$seqres.full
		cat $tmp.fsck				>>$seqres.full
		echo "*** end fsck.overlay output"	>>$seqres.full

		echo "*** mount output ***"		>>$seqres.full
		_mount					>>$seqres.full
		echo "*** end mount output"		>>$seqres.full

		err=1
	fi
	rm -f $tmp.fsck

	return $err
}

# Check the same mnt/dev of _check_overlay_scratch_fs but non-default
# underlying scratch dirs of overlayfs, it needs lower/upper/work dirs
# provided as arguments, and it's useful for non-default setups such
# as multiple lower layers
_overlay_check_scratch_dirs()
{
	local lowerdir=$1
	local upperdir=$2
	local workdir=$3
	shift 3

	# Need to umount overlay for scratch dir check
	local ovl_mounted=`_is_dir_mountpoint $SCRATCH_MNT`
	[ -z "$ovl_mounted" ] || $UMOUNT_PROG $SCRATCH_MNT

	# Check dirs with extra overlay options
	_overlay_check_dirs $lowerdir $upperdir $workdir $*
	local ret=$?

	if [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
		# overlay was mounted, remount with extra mount options
		_overlay_scratch_mount_dirs $lowerdir $upperdir \
					    $workdir $*
		ret=$?
	fi

	return $ret
}

_overlay_check_fs()
{
	# The first arguments is overlay mount point use for checking
	# overlay filesystem is mounted or not, the remaining arquments
	# use for mounting overlay base filesystem if it was not mounted.
	# We shift one to aligns arguments for _overlay_base_mount.
	local ovl_mnt=$1
	shift 1

	local base_dev=$3
	local base_mnt=$4

	[ "$FSTYP" = overlay ] || return 0

	# Base fs needs to be mounted to check overlay dirs
	local base_fstype=""
	local ovl_mounted=""

	[ -z "$base_dev" ] || \
		base_fstype=`_fs_type $base_dev`

	# If base_dev is set but base_fstype is empty, base fs is not
	# mounted, we need to mount base fs. Otherwise, we need to
	# check and umount overlayfs if it was mounted.
	if [ -n "$base_dev" -a -z "$base_fstype" ]; then
		_overlay_base_mount $*
	else
		# Check and umount overlay for dir check
		ovl_mounted=`_is_dir_mountpoint $ovl_mnt`
		[ -z "$ovl_mounted" ] || $UMOUNT_PROG $ovl_mnt
	fi

	_overlay_check_dirs $base_mnt/$OVL_LOWER $base_mnt/$OVL_UPPER \
			    $base_mnt/$OVL_WORK
	local ret=$?

	if [ -n "$base_dev" -a -z "$base_fstype" ]; then
		_overlay_base_unmount "$base_dev" "$base_mnt"
	elif [ $ret -eq 0 -a -n "$ovl_mounted" ]; then
		# overlay was mounted, remount besides extra mount options
		_overlay_mount $base_mnt $ovl_mnt
		ret=$?
	fi

	if [ $ret != 0 ]; then
		status=1
		if [ "$iam" != "check" ]; then
			exit 1
		fi
		return 1
	fi

	return 0
}

_check_overlay_test_fs()
{
	_overlay_check_fs "$TEST_DIR" \
		OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
		"$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
		$TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
}

_check_overlay_scratch_fs()
{
	_overlay_check_fs "$SCRATCH_MNT" \
		OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
		"$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
		$OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
}

_repair_overlay_scratch_fs()
{
	_overlay_fsck_dirs $OVL_BASE_SCRATCH_MNT/$OVL_LOWER \
		$OVL_BASE_SCRATCH_MNT/$OVL_UPPER \
		$OVL_BASE_SCRATCH_MNT/$OVL_WORK -y
	local res=$?
	case $res in
	$FSCK_OK|$FSCK_NONDESTRUCT)
		res=0
		;;
	*)
		_dump_err2 "fsck.overlay failed, err=$res"
		;;
	esac
	return $res
}

# This test requires that unionmount testsuite is installed at
# $UNIONMOUNT_TESTSUITE and that it supports configuring layers and overlay
# mount paths via UNIONMOUNT_* environment variables.
_require_unionmount_testsuite()
{
	[ -x "$UNIONMOUNT_TESTSUITE/run" ] || \
		_notrun "unionmount testsuite required."

	# Verify that UNIONMOUNT_* vars are supported
	local usage=`UNIONMOUNT_BASEDIR=_ "$UNIONMOUNT_TESTSUITE/run" 2>&1`
	echo $usage | grep -wq "UNIONMOUNT_BASEDIR" || \
		_notrun "newer version of unionmount testsuite required."

	[ -n "$OVERLAY_MOUNT_OPTIONS" ] || return
	# If custom overlay mount options are used
	# verify that UNIONMOUNT_MNTOPTIONS var is supported
	local usage=`UNIONMOUNT_MNTOPTIONS=_ "$UNIONMOUNT_TESTSUITE/run" 2>&1`
	echo $usage | grep -wq "UNIONMOUNT_MNTOPTIONS" || \
		_notrun "newer version of unionmount testsuite required to support OVERLAY_MOUNT_OPTIONS."
}

_unionmount_testsuite_run()
{
	[ "$FSTYP" = overlay ] || \
		_notrun "Filesystem $FSTYP not supported with unionmount testsuite."

	# Provide the mounted base fs for upper and lower dirs and the
	# overlay mount point.
	# unionmount testsuite will perform the overlay mount.
	# test fs is used for lower layer in non-samefs runs.
	# scratch fs is used for upper layer in non-samefs runs and
	# for both layers in samefs runs.
	if (echo $* | grep -qv samefs) ; then
		_overlay_base_test_mount
		export UNIONMOUNT_LOWERDIR=$OVL_BASE_TEST_DIR/union
	fi
	export UNIONMOUNT_BASEDIR=$OVL_BASE_SCRATCH_MNT/union
	export UNIONMOUNT_MNTOPTIONS="$OVERLAY_MOUNT_OPTIONS"

	_scratch_mkfs
	rm -rf $UNIONMOUNT_BASEDIR $UNIONMOUNT_LOWERDIR
	mkdir -p $UNIONMOUNT_BASEDIR $UNIONMOUNT_LOWERDIR

	cd $UNIONMOUNT_TESTSUITE
	echo "run $* ..." > $seqres.full
	./run $* >> $seqres.full || \
		echo "unionmount testsuite failed! see $seqres.full for details."
}

_unionmount_testsuite_cleanup()
{
	cd /
	rm -f $tmp.*

	[ -n "$UNIONMOUNT_BASEDIR" ] || return 0

	# Cleanup overlay mount after unionmount testsuite run
	cd $UNIONMOUNT_TESTSUITE
	echo "run --clean-up ..." >> $seqres.full
	./run --clean-up >> $seqres.full 2>&1
}