btrfs-progs: tests: add test case for shrunk device

Create a filesystem on a file backed loop block device, then shrink the
file (and its loop block device), then make sure btrfs check can detect
such shrunk device.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2022-08-30 14:49:43 +08:00 committed by David Sterba
parent 0a348d3c81
commit 192d436503

View File

@ -0,0 +1,31 @@
#!/bin/bash
#
# Make sure "btrfs check" can detect device smaller than its total_bytes
# in device item
#
source "$TEST_TOP/common"
setup_root_helper
file="img"
# Allocate an initial 1G file for testing.
truncate -s0 "$file"
truncate -s1g "$file"
dev=$(run_check_stdout $SUDO_HELPER losetup --find --show "$file")
run_check "$TOP/mkfs.btrfs" -f "$dev"
# The original device size from prepare_loopdevs is 2G.
# Since the fs is empty, shrinking it to 996m will not cause any
# lose of metadata.
run_check $SUDO_HELPER losetup -d "$dev"
truncate -s 996m "$file"
dev=$(run_check_stdout $SUDO_HELPER losetup --find --show "$file")
run_mustfail "btrfs check should detect errors in device size" \
"$TOP/btrfs" check "$dev"
losetup -d "$dev"
rm -- "$file"