]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: check for collisions when create new label
authorKarel Zak <kzak@redhat.com>
Tue, 14 Feb 2017 12:07:54 +0000 (13:07 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 14 Feb 2017 12:07:54 +0000 (13:07 +0100)
We need to be sure that when create a new disklabel than the old label
will be removed.

Addresses: https://github.com/karelzak/util-linux/issues/410
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/context.c
libfdisk/src/fdiskP.h
libfdisk/src/label.c
libfdisk/src/wipe.c

index 5b0fed928d2c84ed0db48ac9dc5b03f93385c16e..77bc19c943c82e60954ccf9ab809e88bb007bade 100644 (file)
@@ -500,55 +500,6 @@ static void reset_context(struct fdisk_context *cxt)
        fdisk_free_wipe_areas(cxt);
 }
 
-/*
- * This function prints a warning if the device is not wiped (e.g. wipefs(8).
- * Please don't call this function if there is already a PT.
- *
- * Returns: 0 if nothing found, < 0 on error, 1 if found a signature
- */
-static int check_collisions(struct fdisk_context *cxt)
-{
-#ifdef HAVE_LIBBLKID
-       int rc = 0;
-       blkid_probe pr;
-
-       assert(cxt);
-       assert(cxt->dev_fd >= 0);
-
-       DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober"));
-
-       pr = blkid_new_probe();
-       if (!pr)
-               return -ENOMEM;
-       rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0);
-       if (rc)
-               return rc;
-
-       blkid_probe_enable_superblocks(pr, 1);
-       blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE);
-       blkid_probe_enable_partitions(pr, 1);
-
-       /* we care about the first found FS/raid, so don't call blkid_do_probe()
-        * in loop or don't use blkid_do_fullprobe() ... */
-       rc = blkid_do_probe(pr);
-       if (rc == 0) {
-               const char *name = NULL;
-
-               if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 ||
-                   blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) {
-                       cxt->collision = strdup(name);
-                       if (!cxt->collision)
-                               rc = -ENOMEM;
-               }
-       }
-
-       blkid_free_probe(pr);
-       return rc;
-#else
-       return 0;
-#endif
-}
-
 /**
  * fdisk_assign_device:
  * @cxt: context
@@ -621,7 +572,8 @@ int fdisk_assign_device(struct fdisk_context *cxt,
 
        /* warn about obsolete stuff on the device if we aren't in
         * list-only mode and there is not PT yet */
-       if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt) && check_collisions(cxt) < 0)
+       if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt)
+           && fdisk_check_collisions(cxt) < 0)
                goto fail;
 
        DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s]",
index 99d7fdc9c19ec44018866b7765e63d29b64aa6af..ca20ffb8f3804806275fee7828a0869acd607322 100644 (file)
@@ -477,5 +477,6 @@ void fdisk_free_wipe_areas(struct fdisk_context *cxt);
 int fdisk_set_wipe_area(struct fdisk_context *cxt, uint64_t start, uint64_t size, int enable);
 int fdisk_do_wipe(struct fdisk_context *cxt);
 int fdisk_has_wipe_area(struct fdisk_context *cxt, uint64_t start, uint64_t size);
+int fdisk_check_collisions(struct fdisk_context *cxt);
 
 #endif /* _LIBFDISK_PRIVATE_H */
index 52f9ec5ea7639634a46f186f6102718ea7264af2..1319284b03b8061c762672bd68804dda536f26be 100644 (file)
@@ -367,6 +367,10 @@ int fdisk_create_disklabel(struct fdisk_context *cxt, const char *name)
        lb = fdisk_get_label(cxt, name);
        if (!lb || lb->disabled)
                return -EINVAL;
+
+       if (!haslabel || (lb && cxt->label != lb))
+               fdisk_check_collisions(cxt);
+
        if (!lb->op->create)
                return -ENOSYS;
 
index e773f756db09a811c5b194227a796a0fefb6a42f..c58015d0e840cb03c8831b2519ae2e4f6465f97b 100644 (file)
@@ -143,3 +143,53 @@ int fdisk_do_wipe(struct fdisk_context *cxt)
 #endif
        return 0;
 }
+
+
+/*
+ * Please don't call this function if there is already a PT.
+ *
+ * Returns: 0 if nothing found, < 0 on error, 1 if found a signature
+ */
+int fdisk_check_collisions(struct fdisk_context *cxt)
+{
+#ifdef HAVE_LIBBLKID
+       int rc = 0;
+       blkid_probe pr;
+
+       assert(cxt);
+       assert(cxt->dev_fd >= 0);
+
+       DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober"));
+
+       pr = blkid_new_probe();
+       if (!pr)
+               return -ENOMEM;
+       rc = blkid_probe_set_device(pr, cxt->dev_fd, 0, 0);
+       if (rc)
+               return rc;
+
+       blkid_probe_enable_superblocks(pr, 1);
+       blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_TYPE);
+       blkid_probe_enable_partitions(pr, 1);
+
+       /* we care about the first found FS/raid, so don't call blkid_do_probe()
+        * in loop or don't use blkid_do_fullprobe() ... */
+       rc = blkid_do_probe(pr);
+       if (rc == 0) {
+               const char *name = NULL;
+
+               if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 ||
+                   blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) {
+                       cxt->collision = strdup(name);
+                       if (!cxt->collision)
+                               rc = -ENOMEM;
+               }
+       }
+
+       blkid_free_probe(pr);
+       return rc;
+#else
+       return 0;
+#endif
+}
+