btrfs-progs: mkfs: do not truncate the image when --rootdir is set
authorDavid Sterba <dsterba@suse.com>
Thu, 5 Nov 2015 13:22:10 +0000 (14:22 +0100)
committerDavid Sterba <dsterba@suse.com>
Thu, 5 Nov 2015 13:49:02 +0000 (14:49 +0100)
With the rootdir option we try to guess the final size of the image and
fill it with zeros, preceded by truncation. After patch

"Btrfs-progs: Do not force mixed block group creation unless '-M' option
 is specified"

the misc test 002 will fail, because of the non-mixed mode. I think we
should not touch the image size (no change for block devices) and try to
fit into whatever is provided by user.

Signed-off-by: David Sterba <dsterba@suse.com>
mkfs.c
tests/mkfs-tests/004-rootdir-keeps-size/test.sh [new file with mode: 0755]

diff --git a/mkfs.c b/mkfs.c
index 72ee290..0ea39f3 100644 (file)
--- a/mkfs.c
+++ b/mkfs.c
@@ -926,7 +926,7 @@ fail_no_dir:
 static int open_target(char *output_name)
 {
        int output_fd;
-       output_fd = open(output_name, O_CREAT | O_RDWR | O_TRUNC,
+       output_fd = open(output_name, O_CREAT | O_RDWR,
                         S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
 
        return output_fd;
diff --git a/tests/mkfs-tests/004-rootdir-keeps-size/test.sh b/tests/mkfs-tests/004-rootdir-keeps-size/test.sh
new file mode 100755 (executable)
index 0000000..a78a3de
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash
+# make sure that mkfs.btrfs --rootsize does not change size of the image
+
+source $TOP/tests/common
+
+check_prereq mkfs.btrfs
+prepare_test_dev
+
+test_mkfs_with_size() {
+       local size
+       local imgsize
+       local tmp
+
+       size="$1"
+       run_check truncate -s$size $TEST_DEV
+       imgsize=$(run_check_stdout stat --format=%s $TEST_DEV)
+       run_check $SUDO_HELPER $TOP/mkfs.btrfs -f \
+               --rootdir $TOP/Documentation \
+               $TEST_DEV
+       tmp=$(run_check_stdout stat --format=%s $TEST_DEV)
+       if ! [ "$imgsize" = "$tmp" ]; then
+               _fail "image size changed from $imgsize to $tmp"
+       fi
+}
+
+test_mkfs_with_size 128M
+test_mkfs_with_size 256M
+test_mkfs_with_size 512M
+test_mkfs_with_size 1G
+test_mkfs_with_size 2G