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
54 "$@" >> $RESULTS 2>&1 || _log "failed (ignored): $@"
56 $INSTRUMENT "$@" >> $RESULTS 2>&1 || _log "failed (ignored): $@"
62 if ! [ -f $TOP/$1 ]; then
63 _fail "Failed prerequisities: $1";
72 echo "testing image $(basename $image)" >> $RESULTS
73 $TOP/btrfs check $image >> $RESULTS 2>&1
74 [ $? -eq 0 ] && _fail "btrfs check should have detected corruption"
76 run_check $TOP/btrfs check --repair $image
77 run_check $TOP/btrfs check $image
80 # Extract a usable image from packed formats
81 # - raw btrfs filesystem images, suffix .raw
82 # - dtto compressed by XZ, suffix .raw.xz
83 # - meta-dump images with suffix .img
84 # - dtto compressed by XZ, suffix .img.xz
96 xz --decompress --keep "$image" || \
97 _fail "failed to decompress image $image" >&2
103 cp --sparse=auto $image $image.restored
106 xz --decompress --keep "$image" || \
107 _fail "failed to decompress image $image" >&2
109 mv "$image" "$image".restored
113 if ! [ -f $image.restored ]; then
114 echo "restoring image $(basename $image)" >> $RESULTS
115 $TOP/btrfs-image -r $image $image.restored \
117 || _fail "failed to restore image $image" >&2
120 [ -f "$cleanme" ] && rm -f "$cleanme"
122 echo "$image.restored"
125 # Process all image dumps in a given directory
132 for image in $(find $dir \( -iname '*.img' -o \
133 -iname '*.img.xz' -o \
135 -iname '*.raw.xz' \) | sort)
137 extracted=$(extract_image "$image")
138 check_image "$extracted"
143 # some tests need to mount the recovered image and do verifications call
144 # 'setup_root_helper' and then check for have_root_helper == 1 if the test
145 # needs to fail otherwise; using sudo by default for now
147 NEED_SUDO_VALIDATE=unknown
149 export NEED_SUDO_VALIDATE
152 if [ $UID -eq 0 ]; then
155 if [ "$NEED_SUDO_VALIDATE" = 'yes' ]; then
156 sudo -v -n &>/dev/null || \
157 _not_run "Need to validate sudo credentials"
159 elif [ "$NEED_SUDO_VALIDATE" = 'no' ]; then
160 sudo -n /bin/true &> /dev/null || \
161 _not_run "Need to validate sudo user settings"
165 _not_run "Need to validate root privileges"
172 if [ $UID -eq 0 -o -n "$SUDO_HELPER" ]; then
176 # Test for old sudo or special settings, which make sudo -v fail even
177 # if user setting is NOPASSWD
178 sudo -n /bin/true &>/dev/null && NEED_SUDO_VALIDATE=no
180 # Newer sudo or default sudo setting
181 sudo -v -n &>/dev/null && NEED_SUDO_VALIDATE=yes
183 if [ "$NEED_SUDO_VALIDATE" = 'unknown' ]; then
184 _not_run "Need to validate root privileges"
186 SUDO_HELPER=root_helper
194 [[ "$TEST_DEV" ]] && return
195 [[ "$size" ]] || size='2G'
197 echo "\$TEST_DEV not given, use $TOP/test/test.img as fallback" >> \
199 TEST_DEV="$TOP/tests/test.img"
201 truncate -s "$size" "$TEST_DEV" || _not_run "create file for loop device failed"
204 run_check_mount_test_dev()
209 if [[ -b "$TEST_DEV" ]]; then
211 elif [[ -f "$TEST_DEV" ]]; then
214 _fail "Invalid \$TEST_DEV: $TEST_DEV"
217 [[ -d "$TEST_MNT" ]] || {
218 _fail "Invalid \$TEST_MNT: $TEST_MNT"
221 run_check $SUDO_HELPER mount $loop_opt "$@" "$TEST_DEV" "$TEST_MNT"
224 run_check_umount_test_dev()
227 run_check $SUDO_HELPER umount "$@" "$TEST_DEV"
232 TEST_MNT="${TEST_MNT:-$TOP/tests/mnt}"
234 mkdir -p "$TEST_MNT" || { echo "Failed mkdir -p $TEST_MNT"; exit 1; }