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