summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/wipe.c
diff options
context:
space:
mode:
authorKarel Zak2017-02-14 13:07:54 +0100
committerKarel Zak2017-02-14 13:07:54 +0100
commit37204dc60c048be91794e349fc41217aea7363b7 (patch)
treeed290a88c6876a99cd3149d39781922e1ab99b3b /libfdisk/src/wipe.c
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>
Diffstat (limited to 'libfdisk/src/wipe.c')
-rw-r--r--libfdisk/src/wipe.c50
1 files changed, 50 insertions, 0 deletions
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
+}
+