btrfs-progs: tests: add quotation around variables in common.convert
[platform/upstream/btrfs-progs.git] / tests / common.convert
1 #!/bin/bash
2 # helpers for btrfs-convert tests
3
4 # how many files to create.
5 DATASET_SIZE=50
6
7 generate_dataset() {
8
9         dataset_type="$1"
10         dirpath=$TEST_MNT/$dataset_type
11         run_check $SUDO_HELPER mkdir -p "$dirpath"
12
13         case $dataset_type in
14                 small)
15                         for num in $(seq 1 "$DATASET_SIZE"); do
16                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
17                                 count=1 >/dev/null 2>&1
18                         done
19                         ;;
20
21                 hardlink)
22                         for num in $(seq 1 "$DATASET_SIZE"); do
23                                 run_check $SUDO_HELPER touch $dirpath/$dataset_type.$num
24                                 run_check $SUDO_HELPER ln "$dirpath/$dataset_type.$num" "$dirpath/hlink.$num"
25                         done
26                         ;;
27
28                 fast_symlink)
29                         for num in $(seq 1 "$DATASET_SIZE"); do
30                                 run_check $SUDO_HELPER touch $dirpath/$dataset_type.$num
31                                 run_check cd "$dirpath" && \
32                                         $SUDO_HELPER ln -s "$dataset_type.$num" "$dirpath/slink.$num" && \
33                                         cd /
34                         done
35                         ;;
36
37                 brokenlink)
38                         for num in $(seq 1 "$DATASET_SIZE"); do
39                                 run_check $SUDO_HELPER ln -s "$dirpath/$dataset_type.$num" "$dirpath/blink.$num"
40                         done
41                         ;;
42
43                 perm)
44                         for modes in 777 775 755 750 700 666 664 644 640 600 444 440 400 000            \
45                                 1777 1775 1755 1750 1700 1666 1664 1644 1640 1600 1444 1440 1400 1000   \
46                                 2777 2775 2755 2750 2700 2666 2664 2644 2640 2600 2444 2440 2400 2000   \
47                                 4777 4775 4755 4750 4700 4666 4664 4644 4640 4600 4444 4440 4400 4000; do
48                                 if [[ "$modes" == *9* ]] || [[ "$modes" == *8* ]]
49                                 then
50                                         continue;
51                                 else
52                                         run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$modes"
53                                         run_check $SUDO_HELPER chmod "$modes" "$dirpath/$dataset_type.$modes"
54                                 fi
55                         done
56                         ;;
57
58                 sparse)
59                         for num in $(seq 1 "$DATASET_SIZE"); do
60                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
61                                 count=1 >/dev/null 2>&1
62                                 run_check $SUDO_HELPER truncate -s 500K "$dirpath/$dataset_type.$num"
63                                 run_check $SUDO_HELPER dd if=/dev/urandom of="$dirpath/$dataset_type.$num" bs=10K \
64                                 oflag=append conv=notrunc count=1 >/dev/null 2>&1
65                                 run_check $SUDO_HELPER truncate -s 800K "$dirpath/$dataset_type.$num"
66                         done
67                         ;;
68
69                 acls)
70                         for num in $(seq 1 "$DATASET_SIZE"); do
71                                 run_check $SUDO_HELPER touch "$dirpath/$dataset_type.$num"
72                                 run_check $SUDO_HELPER setfacl -m "u:root:x" "$dirpath/$dataset_type.$num"
73                                 run_check $SUDO_HELPER setfattr -n user.foo -v "bar$num" "$dirpath/$dataset_type.$num"
74                         done
75                         ;;
76
77                 fifo)
78                         for num in $(seq 1 "$DATASET_SIZE"); do
79                                 run_check $SUDO_HELPER mkfifo "$dirpath/$dataset_type.$num"
80                         done
81                         ;;
82
83                 slow_symlink)
84                         long_filename=`date +%s | sha256sum | cut -f1 -d'-'`
85                         run_check $SUDO_HELPER touch "$dirpath/$long_filename"
86                         for num in $(seq 1 "$DATASET_SIZE"); do
87                                 run_check $SUDO_HELPER ln -s "$dirpath/$long_filename" "$dirpath/slow_slink.$num"
88                         done
89                         ;;
90         esac
91 }
92
93 populate_fs() {
94
95         for dataset_type in 'small' 'hardlink' 'fast_symlink' 'brokenlink' 'perm' 'sparse' 'acls' 'fifo' 'slow_symlink'; do
96                 generate_dataset "$dataset_type"
97         done
98 }
99
100 # verbose message before the test, same arguments as convert_test
101 convert_test_preamble() {
102         local features
103         local msg
104
105         features="$1"
106         msg="$2"
107         shift 3
108         echo "    [TEST/conv]     $msg, btrfs" "${features:-defaults}"
109         echo "creating ext image with: $@" >> "$RESULTS"
110 }
111
112 #  prepare TEST_DEV before conversion, create filesystem and mount it, image
113 #  size is 512MB
114 #  $@: free form, command to create the filesystem, with appended -F
115 convert_test_prep_fs() {
116         # TEST_DEV not removed as the file might have special permissions, eg.
117         # when test image is on NFS and would not be writable for root
118         run_check truncate -s 0 "$TEST_DEV"
119         # 256MB is the smallest acceptable btrfs image.
120         run_check truncate -s 512M "$TEST_DEV"
121         run_check "$@" -F "$TEST_DEV"
122
123         # create a file to check btrfs-convert can convert regular file correct
124         run_check_mount_test_dev
125
126         # create a file inside the fs before convert, to make sure there is
127         # data covering btrfs backup superblock range (64M)
128         run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
129                 of="$TEST_MNT/convert_space_holder"
130 }
131
132 # generate md5 checksums of files on $TEST_MNT
133 # $1: path where the checksums will be stored
134 convert_test_gen_checksums() {
135         _assert_path "$1"
136
137         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
138                 count=1 >/dev/null 2>&1
139         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -exec md5sum {} \+ > "$1"
140 }
141 # list $TEST_MNT data set file permissions.
142 # $1: path where the permissions will be stored
143 convert_test_perm() {
144         local PERMTMP
145
146         _assert_path "$1"
147         PERMTMP="$1"
148         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
149
150         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
151                 count=1 >/dev/null 2>&1
152         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -fprint "$FILES_LIST"
153         # Fix directory entries order
154         sort "$FILES_LIST" -o "$FILES_LIST"
155         for file in `cat "$FILES_LIST"` ;do
156                 run_check_stdout $SUDO_HELPER getfacl --absolute-names "$file" >> "$PERMTMP"
157         done
158         rm -- "$FILES_LIST"
159 }
160 # list acls of files on $TEST_MNT
161 # $1: path where the acls will be stored
162 convert_test_acl() {
163         local ACLSTMP
164         ACLTMP="$1"
165         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
166
167         run_check_stdout $SUDO_HELPER find "$TEST_MNT/acls" -type f -fprint "$FILES_LIST"
168         # Fix directory entries order
169         sort "$FILES_LIST" -o "$FILES_LIST"
170         for file in `cat "$FILES_LIST"`;do
171                 run_check_stdout $SUDO_HELPER getfattr --absolute-names -d "$file" >> "$ACLTMP"
172         done
173         rm -- "$FILES_LIST"
174 }
175
176 # do conversion with given features and nodesize, fsck afterwards
177 # $1: features, argument of -O, can be empty
178 # $2: nodesize, argument of -N, can be empty
179 convert_test_do_convert() {
180         run_check "$TOP/btrfs-convert" ${1:+-O "$1"} ${2:+-N "$2"} "$TEST_DEV"
181         run_check "$TOP/btrfs" check $TEST_DEV
182         run_check "$TOP/btrfs" inspect-internal dump-super -Ffa "$TEST_DEV"
183 }
184
185 # post conversion check, verify file permissions.
186 # $1: file with ext permissions.
187 convert_test_post_check_permissions() {
188         local EXT_PERMTMP
189         local BTRFS_PERMTMP
190
191         _assert_path "$1"
192         EXT_PERMTMP="$1"
193         BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
194         convert_test_perm "$BTRFS_PERMTMP"
195
196         btrfs_perm=`md5sum "$BTRFS_PERMTMP" | cut -f1 -d' '`
197         ext_perm=`md5sum "$EXT_PERMTMP" | cut -f1 -d' '`
198
199         if [ "$btrfs_perm" != "$ext_perm" ];
200         then
201                 btrfs_perm_file=`md5sum "$BTRFS_PERMTMP" | cut -f2 -d' '`
202                 ext_perm_file=`md5sum "$EXT_PERMTMP" | cut -f2 -d' '`
203                 _fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
204         fi
205
206         rm -- "$BTRFS_PERMTMP"
207 }
208 # post conversion check, compare BTRFS file acls against EXT.
209 # $1: file with ext acls.
210 convert_test_post_check_acl() {
211         local EXT_ACLTMP
212         local BTRFS_ACLTMP
213
214         _assert_path "$1"
215         EXT_ACLTMP="$1"
216         BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
217         convert_test_acl "$BTRFS_ACLTMP"
218
219         btrfs_acl=`md5sum "$BTRFS_ACLTMP" | cut -f1 -d' '`
220         ext_acl=`md5sum "$EXT_ACLTMP" | cut -f1 -d' '`
221
222         if [ "$btrfs_acl" != "$ext_acl" ]
223         then
224                 btrfs_acl_file=`md5sum "$BTRFS_ACLTMP" | cut -f2 -d' '`
225                 ext_acl_file=`md5sum "$EXT_ACLTMP" | cut -f2 -d' '`
226                 _fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
227         fi
228
229         rm -- "$BTRFS_ACLTMP"
230 }
231
232 # post conversion checks, verify md5sums
233 convert_test_post_check_checksums() {
234         _assert_path "$1"
235         run_check_stdout $SUDO_HELPER md5sum -c "$1" |
236                 grep -q 'FAILED' && _fail "file validation failed"
237 }
238
239 # post conversion checks, all three in one call, on an unmounted image
240 # $1: file with checksums
241 # $2: file with permissions.
242 # $3: file with acl entries.
243 convert_test_post_checks_all() {
244         _assert_path "$1"
245         _assert_path "$2"
246         _assert_path "$3"
247
248         run_check_mount_test_dev
249         convert_test_post_check_checksums "$1"
250         convert_test_post_check_permissions "$2"
251         convert_test_post_check_acl "$3"
252         run_check_umount_test_dev
253 }
254
255 # do rollback and fsck
256 convert_test_post_rollback() {
257         run_check "$TOP/btrfs-convert" --rollback "$TEST_DEV"
258         run_check fsck -n -t ext2,ext3,ext4 "$TEST_DEV"
259 }
260
261 # simple wrapper for a convert test
262 # $1: btrfs features, argument to -O
263 # $2: description of the test "ext2 8k nodesize"
264 # $3: nodesize value
265 # $4 + rest: command to create the ext2 image
266 convert_test() {
267         local features
268         local nodesize
269         local msg
270         local CHECKSUMTMP
271         local EXT_PERMTMP
272         local EXT_ACLTMP
273
274         features="$1"
275         msg="$2"
276         nodesize="$3"
277         shift 3
278         convert_test_preamble "$features" "$msg" "$nodesize" "$@"
279         convert_test_prep_fs "$@"
280         populate_fs
281         CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX)
282         EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
283         EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
284         convert_test_gen_checksums "$CHECKSUMTMP"
285         convert_test_perm "$EXT_PERMTMP"
286         convert_test_acl "$EXT_ACLTMP"
287
288         run_check_umount_test_dev
289
290         convert_test_do_convert "$features" "$nodesize"
291         convert_test_post_checks_all "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
292         rm -- "$CHECKSUMTMP"
293         rm -- "$EXT_PERMTMP"
294         rm -- "$EXT_ACLTMP"
295
296         convert_test_post_rollback
297 }