3 # Common routines for all tests
8 echo "$*" | tee -a $RESULTS
12 # log a message to the results file
15 echo "$*" | tee -a $RESULTS
26 echo "############### $@" >> $RESULTS 2>&1
27 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD: $@" > /dev/tty; fi
28 if [ "$1" = 'root_helper' ]; then
29 "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
31 $INSTRUMENT "$@" >> $RESULTS 2>&1 || _fail "failed: $@"
35 # same as run_check but the stderr+stdout output is duplicated on stdout and
36 # can be processed further
39 echo "############### $@" >> $RESULTS 2>&1
40 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(stdout): $@" > /dev/tty; fi
41 if [ "$1" = 'root_helper' ]; then
42 "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
44 $INSTRUMENT "$@" 2>&1 | tee -a $RESULTS || _fail "failed: $@"
48 # same as run_check but does not fail the test, output is logged
51 echo "############### $@" >> $RESULTS 2>&1
52 if [ "$TEST_LOG" = 'tty' ]; then echo "CMD(mayfail): $@" > /dev/tty; fi
53 if [ "$1" = 'root_helper' ]; then
56 $INSTRUMENT "$@" >> $RESULTS 2>&1
59 echo "failed (ignored): $@" >> $RESULTS
66 if ! [ -f $TOP/$1 ]; then
67 _fail "Failed prerequisities: $1";
76 echo "testing image $(basename $image)" >> $RESULTS
77 $TOP/btrfs check $image >> $RESULTS 2>&1
78 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
80 run_check $TOP/btrfs check --repair $image
81 run_check $TOP/btrfs check $image
84 # Extract a usable image from packed formats
85 # - raw btrfs filesystem images, suffix .raw
86 # - dtto compressed by XZ, suffix .raw.xz
87 # - meta-dump images with suffix .img
88 # - dtto compressed by XZ, suffix .img.xz
100 xz --decompress --keep "$image" || \
101 _fail "failed to decompress image $image" >&2
103 rm -f $image.restored
107 cp --sparse=auto $image $image.restored
110 xz --decompress --keep "$image" || \
111 _fail "failed to decompress image $image" >&2
113 mv "$image" "$image".restored
117 if ! [ -f $image.restored ]; then
118 echo "restoring image $(basename $image)" >> $RESULTS
119 $TOP/btrfs-image -r $image $image.restored \
121 || _fail "failed to restore image $image" >&2
124 [ -f "$cleanme" ] && rm -f "$cleanme"
126 echo "$image.restored"
129 # Process all image dumps in a given directory
136 for image in $(find $dir \( -iname '*.img' -o \
137 -iname '*.img.xz' -o \
139 -iname '*.raw.xz' \) | sort)
141 extracted=$(extract_image "$image")
142 check_image "$extracted"
147 # some tests need to mount the recovered image and do verifications call
148 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
149 # needs to fail otherwise; using sudo by default for now
151 NEED_SUDO_VALIDATE=unknown
153 export NEED_SUDO_VALIDATE
156 if [ $UID -eq 0 ]; then
159 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
160 sudo -v -n &>/dev/null || \
161 _not_run "Need to validate sudo credentials"
163 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
164 sudo -n /bin/true &> /dev/null || \
165 _not_run "Need to validate sudo user settings"
169 _not_run "Need to validate root privileges"
176 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
180 # Test for old sudo or special settings, which make sudo -v fail even
181 # if user setting is NOPASSWD
182 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
184 # Newer sudo or default sudo setting
185 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
187 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
188 _not_run "Need to validate root privileges"
190 SUDO_HELPER=root_helper
198 [[ "$TEST_DEV" ]] && return
199 [[ "$size" ]] || size='2G'
201 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
203 TEST_DEV="$TOP/tests/test.img"
205 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
208 run_check_mount_test_dev()
213 if [[ -b "$TEST_DEV" ]]; then
215 elif [[ -f "$TEST_DEV" ]]; then
218 _fail "Invalid \$TEST_DEV: $TEST_DEV"
221 [[ -d "$TEST_MNT" ]] || {
222 _fail "Invalid \$TEST_MNT: $TEST_MNT"
225 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
228 run_check_umount_test_dev()
231 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
236 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
238 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }