btrfs-progs: introduce TEST_TOP and INTERNAL_BIN for tests
[platform/upstream/btrfs-progs.git] / tests / misc-tests / 020-fix-superblock-corruption / test.sh
1 #!/bin/bash
2 #
3 # Corrupt primary superblock and restore it using backup superblock.
4
5 source "$TEST_TOP/common"
6
7 check_prereq btrfs-select-super
8 check_prereq btrfs
9
10 setup_root_helper
11 prepare_test_dev
12
13 FIRST_SUPERBLOCK_OFFSET=65536
14
15 test_superblock_restore()
16 {
17         run_check $SUDO_HELPER "$TOP/mkfs.btrfs" -f "$TEST_DEV"
18
19         # Corrupt superblock checksum
20         run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_DEV" \
21         seek="$FIRST_SUPERBLOCK_OFFSET" bs=1 count=4 conv=notrunc
22
23         # Run btrfs check to detect corruption
24         run_mayfail "$TOP/btrfs" check "$TEST_DEV" && \
25                 _fail "btrfs check should detect corruption"
26
27         # Copy backup superblock to primary
28         run_check "$TOP/btrfs-select-super" -s 1 "$TEST_DEV"
29
30         # Perform btrfs check
31         run_check "$TOP/btrfs" check "$TEST_DEV"
32 }
33
34 test_superblock_restore