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