]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
mke2fs: check for pre-existing file system
authorTheodore Ts'o <tytso@mit.edu>
Sat, 26 Apr 2014 22:42:31 +0000 (18:42 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 5 May 2014 02:22:45 +0000 (22:22 -0400)
Warn the system administrator if there is an existing file system on
the block device, and give the administrator an opportunity to abort
the mkfs operation.

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

index 6b099e10ab2ce7740c92db49bf638262fe367397..97b601f9fe95a676b4aa55adaa892473224ee2c3 100644 (file)
@@ -1375,7 +1375,7 @@ out:
 
 static void PRS(int argc, char *argv[])
 {
-       int             b, c;
+       int             b, c, flags;
        int             cluster_size = 0;
        char            *tmp, **cpp;
        int             blocksize = 0;
@@ -1753,8 +1753,11 @@ profile_error:
        profile_get_integer(profile, "options", "proceed_delay", 0, 0,
                            &proceed_delay);
 
-       if (!check_plausibility(device_name, CREATE_FILE,
-                               &is_device) && !force)
+       /* The isatty() test is so we don't break existing scripts */
+       flags = CREATE_FILE;
+       if (isatty(0) && isatty(1))
+               flags |= CHECK_FS_EXIST;
+       if (!check_plausibility(device_name, flags, &is_device) && !force)
                proceed_question(proceed_delay);
 
        check_mount(device_name, force, _("filesystem"));
index afb0058c29fd0f132a934002da139735bbf2fd4d..be16ebebd6fe99a6a015dabf40ed90a0b8ff78ec 100644 (file)
@@ -113,6 +113,9 @@ int check_plausibility(const char *device, int flags, int *ret_is_dev)
        int fd, is_dev = 0;
        ext2fs_struct_stat s;
        int fl = O_RDONLY;
+       blkid_cache cache = NULL;
+       char *fs_type = NULL;
+       char *fs_label = NULL;
 
        if (flags & CREATE_FILE)
                fl |= O_CREAT;
@@ -148,6 +151,32 @@ int check_plausibility(const char *device, int flags, int *ret_is_dev)
                return 0;
        }
 
+       if ((flags & CHECK_FS_EXIST) && blkid_get_cache(&cache, NULL) >= 0) {
+               fs_type = blkid_get_tag_value(cache, "TYPE", device);
+               if (fs_type)
+                       fs_label = blkid_get_tag_value(cache, "LABEL", device);
+               blkid_put_cache(cache);
+       }
+
+       if (fs_type) {
+               if (fs_label)
+                       printf(_("%s contains a %s file system "
+                                "labelled '%s'\n"), device, fs_type, fs_label);
+               else
+                       printf(_("%s contains a %s file system\n"), device,
+                              fs_type);
+               free(fs_type);
+               free(fs_label);
+               return 0;
+       }
+
+       /*
+        * We should eventually replace this with a test for the
+        * presence of a partition table.  Unfortunately the blkid
+        * library doesn't test for partition tabels, and checking for
+        * valid GPT and MBR and possibly others isn't quite trivial.
+        */
+
 #ifdef HAVE_LINUX_MAJOR_H
 #ifndef MAJOR
 #define MAJOR(dev)     ((dev)>>8)
index 9de3fbf6166d7ce75d671ef945a8be92dcfbbed4..745568e21b9470bc8b146b9ec135cbb0e51df71f 100644 (file)
@@ -20,6 +20,7 @@ extern char   *journal_location_string;
  */
 #define CHECK_BLOCK_DEV        0x0001
 #define CREATE_FILE    0x0002
+#define CHECK_FS_EXIST 0x0004
 
 #ifndef HAVE_STRCASECMP
 extern int strcasecmp (char *s1, char *s2);