btrfs-progs: convert-test: trigger chunk allocation after convert
[platform/upstream/btrfs-progs.git] / tests / common.convert
1 #!/bin/bash
2 # helpers for btrfs-convert tests
3
4 populate_fs() {
5
6         for dataset_type in 'small' 'hardlink' 'fast_symlink' 'brokenlink' 'perm' 'sparse' 'acls' 'fifo' 'slow_symlink'; do
7                 generate_dataset "$dataset_type"
8         done
9 }
10
11 # verbose message before the test, same arguments as convert_test
12 convert_test_preamble() {
13         local features
14         local msg
15
16         features="$1"
17         msg="$2"
18         shift 3
19         echo "    [TEST/conv]     $msg, btrfs" "${features:-defaults}"
20         echo "creating ext image with: $@" >> "$RESULTS"
21 }
22
23 #  prepare TEST_DEV before conversion, create filesystem and mount it, image
24 #  size is 512MB
25 #  $@: free form, command to create the filesystem, with appended -F
26 convert_test_prep_fs() {
27         # TEST_DEV not removed as the file might have special permissions, eg.
28         # when test image is on NFS and would not be writable for root
29         run_check truncate -s 0 "$TEST_DEV"
30         # 256MB is the smallest acceptable btrfs image.
31         run_check truncate -s 512M "$TEST_DEV"
32         run_check "$@" -F "$TEST_DEV"
33
34         # create a file to check btrfs-convert can convert regular file correct
35         run_check_mount_test_dev
36
37         # create a file inside the fs before convert, to make sure there is
38         # data covering btrfs backup superblock range (64M)
39         run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
40                 of="$TEST_MNT/convert_space_holder"
41 }
42
43 # generate md5 checksums of files on $TEST_MNT
44 # $1: path where the checksums will be stored
45 convert_test_gen_checksums() {
46         _assert_path "$1"
47
48         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
49                 count=1 >/dev/null 2>&1
50         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -exec md5sum {} \+ > "$1"
51 }
52 # list $TEST_MNT data set file permissions.
53 # $1: path where the permissions will be stored
54 convert_test_perm() {
55         local PERMTMP
56
57         _assert_path "$1"
58         PERMTMP="$1"
59         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
60
61         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
62                 count=1 >/dev/null 2>&1
63         run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -fprint "$FILES_LIST"
64         # Fix directory entries order
65         sort "$FILES_LIST" -o "$FILES_LIST"
66         for file in `cat "$FILES_LIST"` ;do
67                 run_check_stdout $SUDO_HELPER getfacl --absolute-names "$file" >> "$PERMTMP"
68         done
69         rm -- "$FILES_LIST"
70 }
71 # list acls of files on $TEST_MNT
72 # $1: path where the acls will be stored
73 convert_test_acl() {
74         local ACLSTMP
75         ACLTMP="$1"
76         FILES_LIST=$(mktemp --tmpdir btrfs-progs-convert.fileslistXXXXXX)
77
78         run_check_stdout $SUDO_HELPER find "$TEST_MNT/acls" -type f -fprint "$FILES_LIST"
79         # Fix directory entries order
80         sort "$FILES_LIST" -o "$FILES_LIST"
81         for file in `cat "$FILES_LIST"`;do
82                 run_check_stdout $SUDO_HELPER getfattr --absolute-names -d "$file" >> "$ACLTMP"
83         done
84         rm -- "$FILES_LIST"
85 }
86
87 # do conversion with given features and nodesize, fsck afterwards
88 # $1: features, argument of -O, can be empty
89 # $2: nodesize, argument of -N, can be empty
90 convert_test_do_convert() {
91         run_check "$TOP/btrfs-convert" ${1:+-O "$1"} ${2:+-N "$2"} "$TEST_DEV"
92         run_check "$TOP/btrfs" check "$TEST_DEV"
93         run_check "$TOP/btrfs" inspect-internal dump-super -Ffa "$TEST_DEV"
94 }
95
96 # post conversion check, verify file permissions.
97 # $1: file with ext permissions.
98 convert_test_post_check_permissions() {
99         local EXT_PERMTMP
100         local BTRFS_PERMTMP
101
102         _assert_path "$1"
103         EXT_PERMTMP="$1"
104         BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
105         convert_test_perm "$BTRFS_PERMTMP"
106
107         btrfs_perm=`md5sum "$BTRFS_PERMTMP" | cut -f1 -d' '`
108         ext_perm=`md5sum "$EXT_PERMTMP" | cut -f1 -d' '`
109
110         if [ "$btrfs_perm" != "$ext_perm" ];
111         then
112                 btrfs_perm_file=`md5sum "$BTRFS_PERMTMP" | cut -f2 -d' '`
113                 ext_perm_file=`md5sum "$EXT_PERMTMP" | cut -f2 -d' '`
114                 _fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
115         fi
116
117         rm -- "$BTRFS_PERMTMP"
118 }
119 # post conversion check, compare BTRFS file acls against EXT.
120 # $1: file with ext acls.
121 convert_test_post_check_acl() {
122         local EXT_ACLTMP
123         local BTRFS_ACLTMP
124
125         _assert_path "$1"
126         EXT_ACLTMP="$1"
127         BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
128         convert_test_acl "$BTRFS_ACLTMP"
129
130         btrfs_acl=`md5sum "$BTRFS_ACLTMP" | cut -f1 -d' '`
131         ext_acl=`md5sum "$EXT_ACLTMP" | cut -f1 -d' '`
132
133         if [ "$btrfs_acl" != "$ext_acl" ]
134         then
135                 btrfs_acl_file=`md5sum "$BTRFS_ACLTMP" | cut -f2 -d' '`
136                 ext_acl_file=`md5sum "$EXT_ACLTMP" | cut -f2 -d' '`
137                 _fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
138         fi
139
140         rm -- "$BTRFS_ACLTMP"
141 }
142
143 # post conversion checks, verify md5sums
144 convert_test_post_check_checksums() {
145         _assert_path "$1"
146         run_check_stdout $SUDO_HELPER md5sum -c "$1" |
147                 grep -q 'FAILED' && _fail "file validation failed"
148 }
149
150 # post conversion checks, all three in one call, on an unmounted image
151 # $1: file with checksums
152 # $2: file with permissions.
153 # $3: file with acl entries.
154 convert_test_post_checks_all() {
155         _assert_path "$1"
156         _assert_path "$2"
157         _assert_path "$3"
158
159         run_check_mount_test_dev
160         convert_test_post_check_checksums "$1"
161         convert_test_post_check_permissions "$2"
162         convert_test_post_check_acl "$3"
163
164         # Create a large file to trigger data chunk allocation
165         generate_dataset "large"
166         run_check_umount_test_dev
167 }
168
169 # do rollback and fsck
170 convert_test_post_rollback() {
171         run_check "$TOP/btrfs-convert" --rollback "$TEST_DEV"
172         run_check fsck -n -t ext2,ext3,ext4 "$TEST_DEV"
173 }
174
175 # simple wrapper for a convert test
176 # $1: btrfs features, argument to -O
177 # $2: description of the test "ext2 8k nodesize"
178 # $3: nodesize value
179 # $4 + rest: command to create the ext2 image
180 convert_test() {
181         local features
182         local nodesize
183         local msg
184         local CHECKSUMTMP
185         local EXT_PERMTMP
186         local EXT_ACLTMP
187
188         features="$1"
189         msg="$2"
190         nodesize="$3"
191         shift 3
192         convert_test_preamble "$features" "$msg" "$nodesize" "$@"
193         convert_test_prep_fs "$@"
194         populate_fs
195         CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert.XXXXXXXXXX)
196         EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert.permXXXXXX)
197         EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert.aclsXXXXXXX)
198         convert_test_gen_checksums "$CHECKSUMTMP"
199         convert_test_perm "$EXT_PERMTMP"
200         convert_test_acl "$EXT_ACLTMP"
201
202         run_check_umount_test_dev
203
204         convert_test_do_convert "$features" "$nodesize"
205         convert_test_post_checks_all "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
206         rm -- "$CHECKSUMTMP"
207         rm -- "$EXT_PERMTMP"
208         rm -- "$EXT_ACLTMP"
209
210         convert_test_post_rollback
211 }