btrfs-progs: tests: fix source path for testsuite
[platform/upstream/btrfs-progs.git] / tests / common.convert
1 #!/bin/bash
2 # helpers for btrfs-convert tests
3
4 # mount image of converted filesystem of a given type
5 # $1: type of the filesystem
6 run_check_mount_convert_dev()
7 {
8         local fstype
9         local loop_opt
10
11         setup_root_helper
12
13         fstype="$1"
14         shift
15         if [ -z "$fstype" ]; then
16                 _fail "Missing source filesystem type"
17         fi
18         if [ "$fstype" = 'btrfs' ]; then
19                 _fail "Incorrect type for converted filesystem: btrfs"
20         fi
21
22         if [[ -b "$TEST_DEV" ]]; then
23                 loop_opt=""
24         elif [[ -f "$TEST_DEV" ]]; then
25                 loop_opt="-o loop"
26         else
27                 _fail "Invalid \$TEST_DEV: $TEST_DEV"
28         fi
29
30         [[ -d "$TEST_MNT" ]] || {
31                 _fail "Invalid \$TEST_MNT: $TEST_MNT"
32         }
33
34         run_check $SUDO_HELPER mount $loop_opt -t "$fstype" "$@" "$TEST_DEV" "$TEST_MNT"
35 }
36
37 populate_fs() {
38
39         for dataset_type in 'small' 'hardlink' 'fast_symlink' 'brokenlink' 'perm' 'sparse' 'acls' 'fifo' 'slow_symlink'; do
40                 generate_dataset "$dataset_type"
41         done
42 }
43
44 # verbose message before the test, same arguments as convert_test
45 convert_test_preamble() {
46         local features
47         local msg
48
49         features="$1"
50         msg="$2"
51         shift 3
52         echo "    [TEST/conv]     $msg, btrfs" "${features:-defaults}"
53         echo "creating test image with: $@" >> "$RESULTS"
54 }
55
56 #  prepare TEST_DEV before conversion, create filesystem and mount it, image
57 #  size is 512MB
58 #  $1: type of the filesystem
59 #  $2+: free form, command to create the filesystem, with appended -F
60 convert_test_prep_fs() {
61         local fstype
62         local force
63         local mountopts
64
65         fstype="$1"
66         shift
67         # TEST_DEV not removed as the file might have special permissions, eg.
68         # when test image is on NFS and would not be writable for root
69         run_check truncate -s 0 "$TEST_DEV"
70         # 256MB is the smallest acceptable btrfs image.
71         run_check truncate -s 512M "$TEST_DEV"
72         force=
73         mountopts=
74         case "$fstype" in
75         ext[234])
76                 force=-F ;;
77         reiserfs)
78                 force=-ff
79                 mountopts="-o acl,user_xattr,attrs" ;;
80         *)
81                 _fail "unknown filesystem to convert: $fstype"
82         esac
83         run_check "$@" $force "$TEST_DEV"
84
85         # create a file to check btrfs-convert can convert regular file correct
86         run_check_mount_convert_dev "$fstype" $mountopts
87
88         # create a file inside the fs before convert, to make sure there is
89         # data covering btrfs backup superblock range (64M)
90         run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
91                 of="$TEST_MNT/convert_space_holder"
92 }
93
94 # generate md5 checksums of files on $TEST_MNT
95 # $1: path where the checksums will be stored
96 convert_test_gen_checksums() {
97         _assert_path "$1"
98
99         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
100                 count=1 >/dev/null 2>&1
101         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -exec md5sum {} \+ > "$1"
102 }
103 # list $TEST_MNT data set file permissions.
104 # $1: path where the permissions will be stored
105 convert_test_perm() {
106         local PERMTMP
107
108         _assert_path "$1"
109         PERMTMP="$1"
110         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
111
112         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
113                 count=1 >/dev/null 2>&1
114         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -fprint "$FILES_LIST"
115         # Fix directory entries order
116         sort "$FILES_LIST" -o "$FILES_LIST"
117         for file in `cat "$FILES_LIST"` ;do
118                 run_check_stdout $SUDO_HELPER getfacl --absolute-names "$file" >> "$PERMTMP"
119         done
120         rm -- "$FILES_LIST"
121 }
122 # list acls of files on $TEST_MNT
123 # $1: path where the acls will be stored
124 convert_test_acl() {
125         local ACLSTMP
126         ACLTMP="$1"
127         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
128
129         run_check_stdout $SUDO_HELPER find "$TEST_MNT/acls" -type f -fprint "$FILES_LIST"
130         # Fix directory entries order
131         sort "$FILES_LIST" -o "$FILES_LIST"
132         for file in `cat "$FILES_LIST"`;do
133                 run_check_stdout $SUDO_HELPER getfattr --absolute-names -d "$file" >> "$ACLTMP"
134         done
135         rm -- "$FILES_LIST"
136 }
137
138 # do conversion with given features and nodesize, fsck afterwards
139 # $1: features, argument of -O, can be empty
140 # $2: nodesize, argument of -N, can be empty
141 convert_test_do_convert() {
142         run_check "$TOP/btrfs-convert" ${1:+-O "$1"} ${2:+-N "$2"} "$TEST_DEV"
143         run_check "$TOP/btrfs" check "$TEST_DEV"
144         run_check "$TOP/btrfs" inspect-internal dump-super -Ffa "$TEST_DEV"
145 }
146
147 # post conversion check, verify file permissions.
148 # $1: file with ext permissions.
149 convert_test_post_check_permissions() {
150         local EXT_PERMTMP
151         local BTRFS_PERMTMP
152
153         _assert_path "$1"
154         EXT_PERMTMP="$1"
155         BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
156         convert_test_perm "$BTRFS_PERMTMP"
157
158         btrfs_perm=`md5sum "$BTRFS_PERMTMP" | cut -f1 -d' '`
159         ext_perm=`md5sum "$EXT_PERMTMP" | cut -f1 -d' '`
160
161         if [ "$btrfs_perm" != "$ext_perm" ];
162         then
163                 btrfs_perm_file=`md5sum "$BTRFS_PERMTMP" | cut -f2 -d' '`
164                 ext_perm_file=`md5sum "$EXT_PERMTMP" | cut -f2 -d' '`
165                 _fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
166         fi
167
168         rm -- "$BTRFS_PERMTMP"
169 }
170 # post conversion check, compare BTRFS file acls against EXT.
171 # $1: file with ext acls.
172 convert_test_post_check_acl() {
173         local EXT_ACLTMP
174         local BTRFS_ACLTMP
175
176         _assert_path "$1"
177         EXT_ACLTMP="$1"
178         BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
179         convert_test_acl "$BTRFS_ACLTMP"
180
181         btrfs_acl=`md5sum "$BTRFS_ACLTMP" | cut -f1 -d' '`
182         ext_acl=`md5sum "$EXT_ACLTMP" | cut -f1 -d' '`
183
184         if [ "$btrfs_acl" != "$ext_acl" ]
185         then
186                 btrfs_acl_file=`md5sum "$BTRFS_ACLTMP" | cut -f2 -d' '`
187                 ext_acl_file=`md5sum "$EXT_ACLTMP" | cut -f2 -d' '`
188                 _fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
189         fi
190
191         rm -- "$BTRFS_ACLTMP"
192 }
193
194 # post conversion checks, verify md5sums
195 convert_test_post_check_checksums() {
196         _assert_path "$1"
197         run_check_stdout $SUDO_HELPER md5sum -c "$1" |
198                 grep -q 'FAILED' && _fail "file validation failed"
199 }
200
201 # post conversion checks, all three in one call, on an unmounted image
202 # $1: file with checksums
203 # $2: file with permissions.
204 # $3: file with acl entries.
205 convert_test_post_checks_all() {
206         _assert_path "$1"
207         _assert_path "$2"
208         _assert_path "$3"
209
210         run_check_mount_test_dev
211         convert_test_post_check_checksums "$1"
212         convert_test_post_check_permissions "$2"
213         convert_test_post_check_acl "$3"
214
215         # Create a large file to trigger data chunk allocation
216         generate_dataset "large"
217         run_check_umount_test_dev
218 }
219
220 # do rollback and fsck
221 # $1: filesystem name or alias (ext2 includes ext3 and ext4),
222 convert_test_post_rollback() {
223         local types
224
225         run_check "$TOP/btrfs-convert" --rollback "$TEST_DEV"
226         if [ -z "$1" ]; then
227                 _fail "missing filesystem type to check"
228         fi
229         case "$1" in
230                 ext[234]) types=ext2,ext3,ext4 ;;
231                 reiserfs) types=reiserfs ;;
232                 *) _fail "unknown filesystem type to check: $1" ;;
233         esac
234
235         run_check fsck -n -t "$types" "$TEST_DEV"
236 }
237
238 # simple wrapper for a convert test
239 # $1: type of the converted filesystem
240 # $2: btrfs features, argument to -O
241 # $3: description of the test "ext2 8k nodesize"
242 # $4: nodesize value
243 # $5 + rest: command to create the ext2 image
244 convert_test() {
245         local fstype
246         local features
247         local nodesize
248         local msg
249         local CHECKSUMTMP
250         local EXT_PERMTMP
251         local EXT_ACLTMP
252
253         fstype="$1"
254         features="$2"
255         msg="$3"
256         nodesize="$4"
257         shift 4
258         convert_test_preamble "$features" "$msg" "$nodesize" "$@"
259         convert_test_prep_fs "$fstype" "$@"
260         populate_fs
261         CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX)
262         EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
263         EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
264         convert_test_gen_checksums "$CHECKSUMTMP"
265         convert_test_perm "$EXT_PERMTMP"
266         convert_test_acl "$EXT_ACLTMP"
267
268         run_check_umount_test_dev
269
270         convert_test_do_convert "$features" "$nodesize"
271         convert_test_post_checks_all "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
272         rm -- "$CHECKSUMTMP"
273         rm -- "$EXT_PERMTMP"
274         rm -- "$EXT_ACLTMP"
275
276         convert_test_post_rollback "$fstype"
277 }
278
279 load_module_reiserfs()
280 {
281         $SUDO_HELPER modprobe reiserfs
282 }
283
284 check_kernel_support_reiserfs()
285 {
286         if ! grep -iq 'reiserfs' /proc/filesystems; then
287                 echo "WARNING: reiserfs filesystem not listed in /proc/filesystems, some tests might be skipped"
288                 return 1
289         fi
290         return 0
291 }