Whamcloud - gitweb
mke2fs: fix creating a file system image w/o a pre-existing file
authorTheodore Ts'o <tytso@mit.edu>
Tue, 3 Aug 2021 01:08:01 +0000 (21:08 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 3 Aug 2021 01:22:41 +0000 (21:22 -0400)
The mke2fs program should allow creating a file system image when an
explicit file system size is specified, even if the file doesn't yet
exist.  By deferring the call to check_plausible() in commit
942b00cb9d2f ("mke2fs: do not warn about a pre-existing partition
table when using a non-zero offset") this behaviour was broken.

Fix this regression by explicitly creating the file if the file system
size is specified.

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
misc/mke2fs.c

index 306064d..31e8de1 100644 (file)
@@ -1986,6 +1986,25 @@ profile_error:
                retval = ext2fs_get_device_size2(device_name,
                                                 EXT2_BLOCK_SIZE(&fs_param),
                                                 &dev_size);
+       if (retval == ENOENT) {
+               int fd;
+
+               if (!explicit_fssize) {
+                       fprintf(stderr,
+                               _("The file %s does not exist and no "
+                                 "size was specified.\n"), device_name);
+                       exit(1);
+               }
+               fd = ext2fs_open_file(device_name,
+                                     O_CREAT | O_WRONLY, 0666);
+               if (fd < 0) {
+                       retval = errno;
+               } else {
+                       dev_size = 0;
+                       retval = 0;
+                       printf(_("Creating regular file %s\n"), device_name);
+               }
+       }
        if (retval && (retval != EXT2_ET_UNIMPLEMENTED)) {
                com_err(program_name, retval, "%s",
                        _("while trying to determine filesystem size"));