btrfs-progs: tests: scan results for commands not found
[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 ext 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
63         fstype="$1"
64         shift
65         # TEST_DEV not removed as the file might have special permissions, eg.
66         # when test image is on NFS and would not be writable for root
67         run_check truncate -s 0 "$TEST_DEV"
68         # 256MB is the smallest acceptable btrfs image.
69         run_check truncate -s 512M "$TEST_DEV"
70         run_check "$@" -F "$TEST_DEV"
71
72         # create a file to check btrfs-convert can convert regular file correct
73         run_check_mount_convert_dev "$fstype"
74
75         # create a file inside the fs before convert, to make sure there is
76         # data covering btrfs backup superblock range (64M)
77         run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
78                 of="$TEST_MNT/convert_space_holder"
79 }
80
81 # generate md5 checksums of files on $TEST_MNT
82 # $1: path where the checksums will be stored
83 convert_test_gen_checksums() {
84         _assert_path "$1"
85
86         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
87                 count=1 >/dev/null 2>&1
88         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -exec md5sum {} \+ > "$1"
89 }
90 # list $TEST_MNT data set file permissions.
91 # $1: path where the permissions will be stored
92 convert_test_perm() {
93         local PERMTMP
94
95         _assert_path "$1"
96         PERMTMP="$1"
97         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
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' -fprint "$FILES_LIST"
102         # Fix directory entries order
103         sort "$FILES_LIST" -o "$FILES_LIST"
104         for file in `cat "$FILES_LIST"` ;do
105                 run_check_stdout $SUDO_HELPER getfacl --absolute-names "$file" >> "$PERMTMP"
106         done
107         rm -- "$FILES_LIST"
108 }
109 # list acls of files on $TEST_MNT
110 # $1: path where the acls will be stored
111 convert_test_acl() {
112         local ACLSTMP
113         ACLTMP="$1"
114         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
115
116         run_check_stdout $SUDO_HELPER find "$TEST_MNT/acls" -type f -fprint "$FILES_LIST"
117         # Fix directory entries order
118         sort "$FILES_LIST" -o "$FILES_LIST"
119         for file in `cat "$FILES_LIST"`;do
120                 run_check_stdout $SUDO_HELPER getfattr --absolute-names -d "$file" >> "$ACLTMP"
121         done
122         rm -- "$FILES_LIST"
123 }
124
125 # do conversion with given features and nodesize, fsck afterwards
126 # $1: features, argument of -O, can be empty
127 # $2: nodesize, argument of -N, can be empty
128 convert_test_do_convert() {
129         run_check "$TOP/btrfs-convert" ${1:+-O "$1"} ${2:+-N "$2"} "$TEST_DEV"
130         run_check "$TOP/btrfs" check "$TEST_DEV"
131         run_check "$TOP/btrfs" inspect-internal dump-super -Ffa "$TEST_DEV"
132 }
133
134 # post conversion check, verify file permissions.
135 # $1: file with ext permissions.
136 convert_test_post_check_permissions() {
137         local EXT_PERMTMP
138         local BTRFS_PERMTMP
139
140         _assert_path "$1"
141         EXT_PERMTMP="$1"
142         BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
143         convert_test_perm "$BTRFS_PERMTMP"
144
145         btrfs_perm=`md5sum "$BTRFS_PERMTMP" | cut -f1 -d' '`
146         ext_perm=`md5sum "$EXT_PERMTMP" | cut -f1 -d' '`
147
148         if [ "$btrfs_perm" != "$ext_perm" ];
149         then
150                 btrfs_perm_file=`md5sum "$BTRFS_PERMTMP" | cut -f2 -d' '`
151                 ext_perm_file=`md5sum "$EXT_PERMTMP" | cut -f2 -d' '`
152                 _fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
153         fi
154
155         rm -- "$BTRFS_PERMTMP"
156 }
157 # post conversion check, compare BTRFS file acls against EXT.
158 # $1: file with ext acls.
159 convert_test_post_check_acl() {
160         local EXT_ACLTMP
161         local BTRFS_ACLTMP
162
163         _assert_path "$1"
164         EXT_ACLTMP="$1"
165         BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
166         convert_test_acl "$BTRFS_ACLTMP"
167
168         btrfs_acl=`md5sum "$BTRFS_ACLTMP" | cut -f1 -d' '`
169         ext_acl=`md5sum "$EXT_ACLTMP" | cut -f1 -d' '`
170
171         if [ "$btrfs_acl" != "$ext_acl" ]
172         then
173                 btrfs_acl_file=`md5sum "$BTRFS_ACLTMP" | cut -f2 -d' '`
174                 ext_acl_file=`md5sum "$EXT_ACLTMP" | cut -f2 -d' '`
175                 _fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
176         fi
177
178         rm -- "$BTRFS_ACLTMP"
179 }
180
181 # post conversion checks, verify md5sums
182 convert_test_post_check_checksums() {
183         _assert_path "$1"
184         run_check_stdout $SUDO_HELPER md5sum -c "$1" |
185                 grep -q 'FAILED' && _fail "file validation failed"
186 }
187
188 # post conversion checks, all three in one call, on an unmounted image
189 # $1: file with checksums
190 # $2: file with permissions.
191 # $3: file with acl entries.
192 convert_test_post_checks_all() {
193         _assert_path "$1"
194         _assert_path "$2"
195         _assert_path "$3"
196
197         run_check_mount_test_dev
198         convert_test_post_check_checksums "$1"
199         convert_test_post_check_permissions "$2"
200         convert_test_post_check_acl "$3"
201
202         # Create a large file to trigger data chunk allocation
203         generate_dataset "large"
204         run_check_umount_test_dev
205 }
206
207 # do rollback and fsck
208 convert_test_post_rollback() {
209         run_check "$TOP/btrfs-convert" --rollback "$TEST_DEV"
210         run_check fsck -n -t ext2,ext3,ext4 "$TEST_DEV"
211 }
212
213 # simple wrapper for a convert test
214 # $1: type of the converted filesystem
215 # $2: btrfs features, argument to -O
216 # $3: description of the test "ext2 8k nodesize"
217 # $4: nodesize value
218 # $5 + rest: command to create the ext2 image
219 convert_test() {
220         local fstype
221         local features
222         local nodesize
223         local msg
224         local CHECKSUMTMP
225         local EXT_PERMTMP
226         local EXT_ACLTMP
227
228         fstype="$1"
229         features="$2"
230         msg="$3"
231         nodesize="$4"
232         shift 4
233         convert_test_preamble "$features" "$msg" "$nodesize" "$@"
234         convert_test_prep_fs "$fstype" "$@"
235         populate_fs
236         CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX)
237         EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
238         EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
239         convert_test_gen_checksums "$CHECKSUMTMP"
240         convert_test_perm "$EXT_PERMTMP"
241         convert_test_acl "$EXT_ACLTMP"
242
243         run_check_umount_test_dev
244
245         convert_test_do_convert "$features" "$nodesize"
246         convert_test_post_checks_all "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
247         rm -- "$CHECKSUMTMP"
248         rm -- "$EXT_PERMTMP"
249         rm -- "$EXT_ACLTMP"
250
251         convert_test_post_rollback
252 }