summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarel Zak2017-02-14 13:07:54 +0100
committerKarel Zak2017-02-14 13:07:54 +0100
commit37204dc60c048be91794e349fc41217aea7363b7 (patch)
treeed290a88c6876a99cd3149d39781922e1ab99b3b
parentlib/randutils: glibc 2.25 has getrandom(2) declaration (diff)
downloadkernel-qcow2-util-linux-37204dc60c048be91794e349fc41217aea7363b7.tar.gz
kernel-qcow2-util-linux-37204dc60c048be91794e349fc41217aea7363b7.tar.xz
kernel-qcow2-util-linux-37204dc60c048be91794e349fc41217aea7363b7.zip
libfdisk: check for collisions when create new label
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>
-rw-r--r--libfdisk/src/context.c52
-rw-r--r--libfdisk/src/fdiskP.h1
-rw-r--r--libfdisk/src/label.c4
-rw-r--r--libfdisk/src/wipe.c50
4 files changed, 57 insertions, 50 deletions
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 5b0fed928..77bc19c94 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -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]",
diff --git a/libfdisk/src/fdiskP.h b/libfdisk/src/fdiskP.h
index 99d7fdc9c..ca20ffb8f 100644
--- a/libfdisk/src/fdiskP.h
+++ b/libfdisk/src/fdiskP.h
@@ -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 */
diff --git a/libfdisk/src/label.c b/libfdisk/src/label.c
index 52f9ec5ea..1319284b0 100644
--- a/libfdisk/src/label.c
+++ b/libfdisk/src/label.c
@@ -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;
diff --git a/libfdisk/src/wipe.c b/libfdisk/src/wipe.c
index e773f756d..c58015d0e 100644
--- a/libfdisk/src/wipe.c
+++ b/libfdisk/src/wipe.c
@@ -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
+}
+