2 # Test that any superblock is correctly detected and fixed by btrfs rescue
4 source "$TEST_TOP/common"
7 check_prereq mkfs.btrfs
8 check_prereq btrfs-select-super
13 # Create the test file system.
14 run_check $SUDO_HELPER "$TOP"/mkfs.btrfs -f "$TEST_DEV"
16 function check_corruption {
20 # First we ensure we can mount it successfully
21 run_check_mount_test_dev
22 run_check_umount_test_dev
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
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
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"
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"
43 _fail "couldn't rescue super"
46 run_check_mount_test_dev
47 run_check_umount_test_dev
50 # Corrupting first superblock
53 # Corrupting second superblock
54 check_corruption 65536 1
56 # Corrupting third superblock
57 check_corruption 268435456 2