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