commit bc3177141d93e0d87ebd3a144f32b5c64acd7407
parent 8f9e9f9d2afcec6a81f1efd5fba765aa4d04c44c
Author: Jaromil <jaromil@dyne.org>
Date:   Wed, 12 Jun 2013 03:01:28 +0200
fix to tomb resize
also included into test script
Diffstat:
2 files changed, 39 insertions(+), 44 deletions(-)
diff --git a/extras/test/dig_forge_lock_open_close.sh b/extras/test/dig_forge_lock_open_close.sh
@@ -1,48 +1,46 @@
-#/usr/bin/env zsh 
+#!/usr/bin/zsh
 
 T="../../tomb"
 source utils.sh
+source ${T} source
 
-rm /tmp/test.tomb{,.key} -f || exit 1
-
-sudo -k
+notice() { print; yes "${@}"; print; }
+error() { _warning "     ${@}"; }
+tt() {
+	start_loops=(`sudo losetup -a |cut -d: -f1`)
+	${T} ${=@}
+	res=$?
+	loops=(`sudo losetup -a |cut -d: -f1`)
+	{ test "${#start_loops}" = "${#loops}" } || { error "loop device limit change to ${#loops}" }
+	print "     Tomb command returns $res"
+	return $res
+}
 
-${T} dig -s 10 /tmp/test.tomb
 
-sudo losetup -a
 
-${T} --ignore-swap --unsecure-dev-mode --tomb-pwd f00za --use-urandom \
-	forge /tmp/test.tomb.key
+rm /tmp/test.tomb{,.key} -f || exit 1
 
-sudo losetup -a
+notice "Testing creation"
 
-${T} --ignore-swap --unsecure-dev-mode --tomb-pwd f00za \
-	lock /tmp/test.tomb -k /tmp/test.tomb.key
+tt dig -s 10 /tmp/test.tomb
 
-sudo losetup -a
+tt --ignore-swap --unsecure-dev-mode --tomb-pwd f00za --use-urandom forge /tmp/test.tomb.key
 
-# sanity_tomb /tmp/asd.tomb
-echo
-echo trying to open with wrong password
-echo
 
-${T} --unsecure-dev-mode --tomb-pwd wrongpassword \
-	open /tmp/test.tomb
+tt --ignore-swap --unsecure-dev-mode --tomb-pwd f00za lock /tmp/test.tomb -k /tmp/test.tomb.key
 
-sudo losetup -a
-
-echo
-echo trying to open with good password
-echo
+# sanity_tomb /tmp/asd.tomb
+notice "Testing open with wrong password"
 
-${T} --unsecure-dev-mode --tomb-pwd f00za \
-	open /tmp/test.tomb
+tt --unsecure-dev-mode --tomb-pwd wrongpassword open /tmp/test.tomb
 
-sudo losetup -a
+notice "Testing open with good password"
 
-${T} --unsecure-dev-mode close test 
+tt --unsecure-dev-mode --tomb-pwd f00za open /tmp/test.tomb
 
-sudo losetup -a
+tt --unsecure-dev-mode close test
 
-rm /tmp/test.tomb{,.key} -f || exit 1
+notice "Testing resize to 20MiB"
+tt --unsecure-dev-mode --tomb-pwd f00za -k /tmp/test.tomb.key resize /tmp/test.tomb -s 20
 
+# rm /tmp/test.tomb{,.key} -f || exit 1
diff --git a/tomb b/tomb
@@ -214,7 +214,7 @@ EOF
     fi # are we root already
 
     # make sure necessary kernel modules are loaded
-    modprobe dm_mod 2>/dev/null 
+    modprobe dm_mod 2>/dev/null
     modprobe dm_crypt 2>/dev/null
 
     return 0
@@ -495,7 +495,7 @@ ask_key_password() {
     local tombpass=""
     if option_is_set --tomb-pwd; then
 	    tombpass=`option_value --tomb-pwd`
-            xxx "ask_key_password takes tombpass from CLI argument: $tombpass"
+	    xxx "ask_key_password takes tombpass from CLI argument: $tombpass"
 
 	    get_lukskey "$tombpass" ${tombkey} >/dev/null
 
@@ -686,7 +686,7 @@ gen_key() {
 	done
     else
 	tombpass="`option_value --tomb-pwd`"
-        xxx "gen_key takes tombpass from CLI argument: $tombpass"
+	xxx "gen_key takes tombpass from CLI argument: $tombpass"
     fi
 
 
@@ -1221,7 +1221,7 @@ mount_tomb() {
     if [ $? != 0 ]; then
 	# is it a LUKS encrypted nest? see cryptsetup(1)
 	_warning "$tombfile is not a valid Luks encrypted storage file"
-        losetup -d ${nstloop}
+	losetup -d ${nstloop}
 	return 1
     fi
     say "this tomb is a valid LUKS encrypted device"
@@ -1248,7 +1248,7 @@ mount_tomb() {
 
     tombpass=`ask_key_password $tombkey`
     { test $? = 0 } || {
-        losetup -d ${nstloop}
+	losetup -d ${nstloop}
 	die "No valid password supplied" }
 
     get_lukskey "${tombpass}" ${tombkey} | \
@@ -1648,7 +1648,7 @@ resize_tomb() {
 
     local tmp_resize=`safe_filename tmbrsz`
     local newtombsize=$opts[-s]
-    local oldtombsize=`stat -c %s "$1" 2>/dev/null`
+    local oldtombsize=$(( `stat -c %s "$1" 2>/dev/null` / 1048576 ))
     local mounted_tomb=`mount -l |
 	awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $1 }'`
 
@@ -1656,9 +1656,6 @@ resize_tomb() {
 	_failure "the tomb $tombname is open, to resize it it needs to be close."
     fi
 
-    # MB to bytes conversion
-    newtombsize=`expr \( $newtombsize \* 1024 \) \* 1024 2> /dev/null`
-
     if ! [ "$newtombsize" ] ; then
 	_failure "You must specify the new size of $tombname"
     elif [[ $newtombsize != <-> ]]; then
@@ -1667,12 +1664,12 @@ resize_tomb() {
 	_failure "the new size must be greater then old tomb size."
     fi
 
-    local delta=`expr $newtombsize \- $oldtombsize`
+    delta="$(( $newtombsize - $oldtombsize ))"
 
     act "Generating ${tombfile} of ${newtombsize}MiB"
 
     xxx "Data dump using ${DD[1]} from /dev/urandom"
-    ${=DD} if=/dev/urandom bs=1048576 count=${tombsize} of="${tmp_resize}"
+    ${=DD} if=/dev/urandom bs=1048576 count=${delta} of="${tmp_resize}"
 
     if [ $? = 0 -a -e "${tmp_resize}" ]; then
 	xxx "OK: `ls -lh ${tmp_resize}`"
@@ -1680,8 +1677,8 @@ resize_tomb() {
 	_failure "Error creating the extra resize $tmp_resize, operation aborted."
     fi
 
-    cat "${tmp_resize}" >> "$1"
-    ${=WIPE} "${tmp_resize}"
+    cat "${tmp_resize}" >> ${tombdir}/${tombfile}
+    rm "${tmp_resize}"
 
 
     tombpass=`ask_key_password $tombkey`
@@ -1693,7 +1690,7 @@ resize_tomb() {
 	_failure "too many tomb opened. Please close any of them to open another tomb"
     fi
 
-    losetup -f "$1"
+    losetup -f ${tombdir}/${tombfile}
 
     local mapdate=`date +%s`
     local mapper="tomb.${tombname}.${mapdate}.`basename $nstloop`"
@@ -1919,7 +1916,7 @@ main() {
     subcommands_opts[askpass]=""
     subcommands_opts[mktemp]=""
     subcommands_opts[source]=""
-    subcommands_opts[resize]="s: -size=s k: -key=k"
+    subcommands_opts[resize]="f -force -ignore-swap s: -size=s k: -key=k -tomb-pwd:"
     subcommands_opts[check]="-ignore-swap"
 #    subcommands_opts[translate]=""