16f13a8fb0cb479cc6b322a25fab53e167c5aef6
[xfstests-dev.git] / common / overlay
1 #
2 # overlayfs specific common functions.
3 #
4 . ./common/module
5
6 # helper function to do the actual overlayfs mount operation
7 _overlay_mount_dirs()
8 {
9         local lowerdir=$1
10         local upperdir=$2
11         local workdir=$3
12         shift 3
13
14         $MOUNT_PROG -t overlay -o lowerdir=$lowerdir -o upperdir=$upperdir \
15                     -o workdir=$workdir `_common_dev_mount_options $*`
16 }
17
18 # Mount with same options/mnt/dev of scratch mount, but optionally
19 # with different lower/upper/work dirs
20 _overlay_scratch_mount_dirs()
21 {
22         local lowerdir=$1
23         local upperdir=$2
24         local workdir=$3
25         shift 3
26
27         _overlay_mount_dirs $lowerdir $upperdir $workdir \
28                                 $* $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT
29 }
30
31 _overlay_mkdirs()
32 {
33         local dir=$1
34
35         mkdir -p $dir/$OVL_UPPER
36         mkdir -p $dir/$OVL_LOWER
37         mkdir -p $dir/$OVL_WORK
38         mkdir -p $dir/$OVL_MNT
39 }
40
41 # Given a base fs dir, set up overlay directories and mount on the given mnt.
42 # The dir is used as the mount device so it can be seen from df or mount
43 _overlay_mount()
44 {
45         local dir=$1
46         local mnt=$2
47         shift 2
48
49         _supports_filetype $dir || _notrun "upper fs needs to support d_type"
50
51         _overlay_mkdirs $dir
52
53         _overlay_mount_dirs $dir/$OVL_LOWER $dir/$OVL_UPPER $dir/$OVL_WORK \
54                                 $* $dir $mnt
55 }
56
57 _overlay_base_mount()
58 {
59         local devname=$1
60         local mntname=$2
61         local dev=$3
62         local mnt=$4
63         shift 4
64
65         if [ -z "$dev" -o -z "$mnt" ] || \
66                 _check_mounted_on $devname $dev $mntname $mnt; then
67                 # no base fs or already mounted
68                 return 0
69         elif [ $? -ne 1 ]; then
70                 # base fs mounted but not on mount point
71                 return 1
72         fi
73
74         _mount $* $dev $mnt
75 }
76
77 _overlay_base_test_mount()
78 {
79         _overlay_base_mount OVL_BASE_TEST_DEV OVL_BASE_TEST_DIR \
80                         "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR" \
81                         $TEST_FS_MOUNT_OPTS $SELINUX_MOUNT_OPTIONS
82 }
83
84 _overlay_test_mount()
85 {
86         _overlay_base_test_mount && \
87                 _overlay_mount $OVL_BASE_TEST_DIR $TEST_DIR $*
88 }
89
90 _overlay_base_scratch_mount()
91 {
92         _overlay_base_mount OVL_BASE_SCRATCH_DEV OVL_BASE_SCRATCH_MNT \
93                         "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT" \
94                         $OVL_BASE_MOUNT_OPTIONS $SELINUX_MOUNT_OPTIONS
95 }
96
97 _overlay_scratch_mount()
98 {
99         _overlay_base_scratch_mount && \
100                 _overlay_mount $OVL_BASE_SCRATCH_MNT $SCRATCH_MNT $*
101 }
102
103 _overlay_base_unmount()
104 {
105         local dev=$1
106         local mnt=$2
107
108         [ -n "$dev" -a -n "$mnt" ] || return 0
109
110         $UMOUNT_PROG $mnt
111 }
112
113 _overlay_test_unmount()
114 {
115         $UMOUNT_PROG $TEST_DIR
116         _overlay_base_unmount "$OVL_BASE_TEST_DEV" "$OVL_BASE_TEST_DIR"
117 }
118
119 _overlay_scratch_unmount()
120 {
121         $UMOUNT_PROG $SCRATCH_MNT
122         _overlay_base_unmount "$OVL_BASE_SCRATCH_DEV" "$OVL_BASE_SCRATCH_MNT"
123 }
124
125 # Check that a specific overlayfs feature is supported
126 __check_scratch_overlay_feature()
127 {
128         local feature=$1
129
130         # overalyfs features (e.g. redirect_dir, index) are
131         # configurable from Kconfig (the build default), by module
132         # parameter (the system default) and per mount by mount
133         # option ${feature}=[on|off].
134         #
135         # If the module parameter does not exist then there is no
136         # point in checking the mount option.
137         local default=`_get_fs_module_param ${feature}`
138         [ "$default" = Y ] || [ "$default" = N ] || \
139                 _notrun "feature '${feature}' not supported by ${FSTYP}"
140
141         # Check options to be sure. For example, Overlayfs will fallback to
142         # index=off if underlying fs does not support file handles.
143         # Overlayfs only displays mount option if it differs from the default.
144         # Overlayfs may enable the feature, but fallback to read-only mount.
145         ((( [ "$default" = N ] && _fs_options $SCRATCH_DEV | grep -q "${feature}=on" ) || \
146           ( [ "$default" = Y ] && ! _fs_options $SCRATCH_DEV | grep -q "${feature}=off" )) && \
147             touch $SCRATCH_MNT/foo 2>/dev/null ) || \
148                 _notrun "${FSTYP} feature '${feature}' cannot be enabled on ${SCRATCH_DEV}"
149 }
150
151 # Require a set of overlayfs features
152 _require_scratch_overlay_features()
153 {
154         local features=( $* )
155         local opts="rw"
156
157         for feature in ${features[*]}; do
158                 opts+=",${feature}=on"
159         done
160
161         _scratch_mkfs > /dev/null 2>&1
162         _scratch_mount -o $opts || \
163                 _notrun "overlay options '$opts' cannot be enabled on ${SCRATCH_DEV}"
164
165         for feature in ${features[*]}; do
166                 __check_scratch_overlay_feature ${feature}
167         done
168
169         _scratch_unmount
170 }
171
172 # Helper function to check underlying dirs of overlay filesystem
173 _overlay_fsck_dirs()
174 {
175         local lowerdir=$1
176         local upperdir=$2
177         local workdir=$3
178         shift 3
179
180         [[ ! -x "$FSCK_OVERLAY_PROG" ]] && return 0
181
182         $FSCK_OVERLAY_PROG -o lowerdir=$lowerdir -o upperdir=$upperdir \
183                            -o workdir=$workdir $*
184 }