{
test $sector_size_ = 512 || skip_ 'FS test with sector size != 512'
}
+
+# Use this for a test that requires an actual hardware device, e.g., a real
+# disk, a USB key, or a CD-RW. The envvars $DEVICE_TO_ERASE and
+# $DEVICE_TO_ERASE_SIZE must be set properly. Otherwise, skip the test.
+require_erasable_()
+{
+ # Skip quietly if both envvars are not specified.
+ test -n "$DEVICE_TO_ERASE" && test -n "$DEVICE_TO_ERASE_SIZE" \
+ || skip_ 'This test requires an erasable device and you have not properly' \
+ 'set the $DEVICE_TO_ERASE and $DEVICE_TO_ERASE_SIZE envvars.'
+
+ # Since testing a drive with parted destroys all data on that drive,
+ # we have rather draconian safety requirements that should help avoid
+ # accidents. If $dev_ is the name of the device,
+ # - running "parted -s $dev_ print" must succeed, and
+ # - its output must include a line matching /^Disk $dev_: $DEV_SIZE$/
+ # - Neither $dev_ nor any $dev_[0-9]* may be mounted.
+ dev_=$DEVICE_TO_ERASE
+ sz=$DEVICE_TO_ERASE_SIZE
+ parted_output=$(parted -s $dev_ print) || fail_ "no such device: $dev_"
+ parted -s $dev_ print|grep "^Disk $dev_: $sz$" \
+ > /dev/null || fail_ "actual device size is not $sz"
+ # Try to see if $dev_ or any of its partitions is mounted.
+ # This is not reliable. FIXME: find a better way.
+ # Maybe expose parted's own test for whether a disk is in use.
+ # The following assume that $dev_ is canonicalized, e.g., that $dev_
+ # contains no "//" or "/./" components.
+
+ # Prefer df --local, if it works, so we don't waste time
+ # enumerating lots of automounted file systems.
+ ( df --local / > /dev/null 2>&1 ) && df='df --local' || df=df
+ $df | grep "^$dev_" && fail_ "$dev_ is already mounted"
+ $df | grep "^$dev_[0-9]" && fail_ "a partition of $dev_ is already mounted"
+}
#!/bin/sh
+# partitioning (parted -s DEV mklabel) a busy disk must fail.
# Copyright (C) 2007-2010 Free Software Foundation, Inc.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-test_description='partitioning (parted -s DEV mklabel) a busy disk must fail.'
-
-privileges_required_=1
-erasable_device_required_=1
-
-: ${srcdir=.}
-. $srcdir/test-lib.sh
+. "${srcdir=.}/init.sh"; path_prepend_ ../parted
+require_erasable_
+require_root_
require_512_byte_sector_size_
+
dev=$DEVICE_TO_ERASE
-test_expect_success \
- "setup: create a fat32 file system on $dev" \
- 'dd if=/dev/zero "of=$dev" bs=1k count=1 2> /dev/null &&
- parted -s "$dev" mklabel msdos > out 2>&1 &&
- parted -s "$dev" mkpartfs primary fat32 1 40 >> out 2>&1'
-test_expect_success 'expect no output' 'compare out /dev/null'
+# setup: create a fat32 file system on $dev
+dd if=/dev/zero "of=$dev" bs=1k count=1 2> /dev/null || fail=1
+parted -s "$dev" mklabel msdos > out 2>&1 || fail=1
+parted -s "$dev" mkpartfs primary fat32 1 40 >> out 2>&1 || fail=1
+compare out /dev/null || fail=1
mount_point="`pwd`/mnt"
# device, ${dev}1 (i.e., /dev/sdd1) is not created immediately, and
# without some delay, this mount command would fail. Using a flash card
# as $dev, the loop below typically iterates 7-20 times.
-test_expect_success \
- 'create mount point dir. and mount the just-created partition on it' \
- 'mkdir $mount_point &&
- i=0; while :; do test -e "${dev}1" && break; test $i = 90 && break;
- i=$(expr $i + 1); done;
- mount "${dev}1" $mount_point'
-
-test_expect_failure \
- 'now that a partition is mounted, mklabel attempt must fail' \
- 'parted -s "$dev" mklabel msdos > out 2>&1'
-test_expect_success \
- 'create expected output file' \
- 'echo "Error: Partition(s) on $dev are being used." > exp'
-test_expect_success \
- 'check for expected failure diagnostic' \
- 'compare out exp'
+
+# create mount point dir. and mount the just-created partition on it
+mkdir $mount_point || fail=1
+i=0; while :; do test -e "${dev}1" && break; test $i = 90 && break;
+ i=$(expr $i + 1); done;
+mount "${dev}1" $mount_point || fail=1
+
+# now that a partition is mounted, mklabel attempt must fail
+parted -s "$dev" mklabel msdos > out 2>&1; test $? = 1 || fail=1
+
+# create expected output file
+echo "Error: Partition(s) on $dev are being used." > exp
+compare out exp || fail=1
# ==================================================
-# Now, test it in interactive mode.
-test_expect_success 'create input file' 'echo c > in'
-test_expect_failure \
- 'as above, this mklabel attempt must fail' \
- 'parted ---pretend-input-tty "$dev" mklabel msdos < in > out 2>&1'
+# Repeat the test in interactive mode.
+# create input file
+echo c > in
+
+# as above, this mklabel attempt must fail
+parted ---pretend-input-tty "$dev" mklabel msdos < in > out 2>&1
+test $? = 1 || fail=1
-fail=0
cat <<EOF > exp || fail=1
Warning: Partition(s) on $dev are being used.
Ignore/Cancel? c
EOF
-test_expect_success 'create expected output file' 'test $fail = 0'
# Transform the actual output, removing ^M ...^M.
-test_expect_success \
- 'normalize the actual output' \
- 'mv out o2 && sed -e "s,\r *\r,,;s, $,," \
- -e "s,^.*/lt-parted: ,parted: ," o2 > out'
+# normalize the actual output
+mv out o2 && sed -e 's,\r *\r,,;s, $,,;s/^.*Warning/Warning/' \
+ -e 's,^.*/lt-parted: ,parted: ,' o2 > out
-test_expect_success \
- 'check for expected failure diagnostic' \
- 'compare out exp'
+# check for expected failure diagnostic
+compare out exp || fail=1
-test_done
+Exit $fail