btrfs-progs: introduce TEST_TOP and INTERNAL_BIN for tests
[platform/upstream/btrfs-progs.git] / tests / misc-tests / 004-shrink-fs / test.sh
1 #!/bin/bash
2 #
3 # Test getting the minimum size a filesystem can be resized to and verify we
4 # are able to resize (shrink) it to that size.
5 #
6
7 source "$TEST_TOP/common"
8
9 check_prereq mkfs.btrfs
10 check_prereq btrfs
11
12 setup_root_helper
13
14 # Optionally take id of the device to shrink
15 shrink_test()
16 {
17         min_size=$(run_check_stdout $SUDO_HELPER $TOP/btrfs inspect-internal min-dev-size ${1:+--id $1} $TEST_MNT)
18         min_size=$(echo $min_size | cut -d ' ' -f 1)
19         echo "min size = ${min_size}" >> $RESULTS
20         if [ -z "$min_size" ]; then
21                 _fail "Failed to parse minimum size"
22         fi
23         run_check $SUDO_HELPER $TOP/btrfs filesystem resize $min_size $TEST_MNT
24 }
25
26 run_check truncate -s 20G $IMAGE
27 run_check $TOP/mkfs.btrfs -f $IMAGE
28 run_check $SUDO_HELPER mount $IMAGE $TEST_MNT
29 run_check $SUDO_HELPER chmod a+rw $TEST_MNT
30
31 # Create 7 data block groups, each with a size of 1Gb.
32 for ((i = 1; i <= 7; i++)); do
33         run_check fallocate -l 1G $TEST_MNT/foo$i
34 done
35
36 # Make sure they are persisted (all the chunk, device and block group items
37 # added to the chunk/dev/extent trees).
38 run_check $TOP/btrfs filesystem sync $TEST_MNT
39
40 # Now remove 3 of those 1G files. This will result in 3 block groups becoming
41 # unused, which will be automatically deleted by the cleaner kthread, and this
42 # will result in 3 holes (unallocated space) in the device (each with a size
43 # of 1Gb).
44
45 run_check rm -f $TEST_MNT/foo2
46 run_check rm -f $TEST_MNT/foo4
47 run_check rm -f $TEST_MNT/foo6
48
49 # Sync once to wake up the cleaner kthread which will delete the unused block
50 # groups - it could have been sleeping when they became unused. Then wait a bit
51 # to allow the cleaner kthread to delete them and then finally ensure the
52 # transaction started by the cleaner kthread is committed.
53 run_check $TOP/btrfs filesystem sync $TEST_MNT
54 sleep 3
55 run_check $TOP/btrfs filesystem sync $TEST_MNT
56
57 # Now attempt to get the minimum size we can resize the filesystem to and verify
58 # the resize operation succeeds. This size closely matches the sum of the size
59 # of all the allocated device extents.
60 for ((i = 1; i <= 3; i++)); do
61         shrink_test
62 done
63
64 # Now convert metadata and system chunks to the single profile and check we are
65 # still able to get a correct minimum size and shrink to that size.
66 run_check $SUDO_HELPER $TOP/btrfs balance start -mconvert=single \
67         -sconvert=single -f $TEST_MNT
68 for ((i = 1; i <= 3; i++)); do
69         shrink_test 1
70 done
71
72 run_check $SUDO_HELPER umount $TEST_MNT