1175e480141565058c054b685ca91ab404f29908
[platform/upstream/btrfs-progs.git] / tests / misc-tests / 028-superblock-recover / test.sh
1 #!/bin/bash
2 # Test that any superblock is correctly detected and fixed by btrfs rescue
3
4 source "$TOP/tests/common"
5
6 check_prereq btrfs
7 check_prereq mkfs.btrfs
8 check_prereq btrfs-select-super
9
10 setup_root_helper
11 prepare_test_dev 260G
12
13 # Create the test file system.
14 run_check $SUDO_HELPER "$TOP"/mkfs.btrfs -f "$TEST_DEV"
15
16 function check_corruption {
17         local sb_offset=$1
18         local source_sb=$2
19
20         # First we ensure we can mount it successfully
21         run_check_mount_test_dev
22         run_check_umount_test_dev
23
24         # Now corrupt 1k of the superblock at sb_offset
25         run_check $SUDO_HELPER dd bs=1K count=1 seek=$(($sb_offset + 1)) if=/dev/zero of="$TEST_DEV" conv=notrunc
26
27         # if corrupting one of the sb copies, copy it over the initial superblock
28         if [ ! -z $source_sb ]; then
29                 local shift_val=$((16 << $source_sb * 12 ))
30                 run_check $SUDO_HELPER dd bs=1K count=4 seek=64 skip=$shift_val if="$TEST_DEV" of="$TEST_DEV" conv=notrunc
31         fi
32
33         # we can't use our mount helper, the following works for file image and
34         # block device as TEST_DEV
35         run_mustfail "mounted fs with corrupted superblock" \
36                 $SUDO_HELPER mount "$TEST_DEV" "$TEST_MNT"
37
38         # Now run btrfs rescue which should fix the superblock. It uses 2
39         # to signal success of recovery use mayfail to ignore that retval
40         # but still log the output of the command
41         run_mayfail $SUDO_HELPER "$TOP"/btrfs rescue super-recover -yv "$TEST_DEV"
42         if [ $? != 2 ]; then
43                 _fail "couldn't rescue super"
44         fi
45
46         run_check_mount_test_dev
47         run_check_umount_test_dev
48 }
49
50 # Corrupting first superblock
51 check_corruption 64
52
53 # Corrupting second superblock
54 check_corruption 65536 1
55
56 # Corrupting third superblock
57 check_corruption 268435456 2