summaryrefslogtreecommitdiffstats
path: root/libfdisk/src/context.c
diff options
context:
space:
mode:
authorKarel Zak2016-02-18 12:42:06 +0100
committerKarel Zak2016-02-18 12:58:12 +0100
commit6cbb7371b349c63c6f3ba59a14acb0afc8ca3bde (patch)
tree5aaf4727f1285dc7eec602a51c7144b8c160abaf /libfdisk/src/context.c
parentunshare: fix busyloop and reduce racing probability (diff)
downloadkernel-qcow2-util-linux-6cbb7371b349c63c6f3ba59a14acb0afc8ca3bde.tar.gz
kernel-qcow2-util-linux-6cbb7371b349c63c6f3ba59a14acb0afc8ca3bde.tar.xz
kernel-qcow2-util-linux-6cbb7371b349c63c6f3ba59a14acb0afc8ca3bde.zip
libfdisk: add API to control signatures wiping
Now libfdisk warns if another filesystem/RAID/PT signature is detected on the device. It seems better and user-friendly to make it possible to wipe the signatures when we write a new disk label to the device. This patch adds to the library API fdisk_enable_wipe() fdisk_has_wipe() fdisk_get_collision() to control this new feature. The device modification is done by libblkid (the same we use in some mkfs-like utils). Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libfdisk/src/context.c')
-rw-r--r--libfdisk/src/context.c104
1 files changed, 89 insertions, 15 deletions
diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c
index 1f6118cd0..73d72ecb8 100644
--- a/libfdisk/src/context.c
+++ b/libfdisk/src/context.c
@@ -335,6 +335,50 @@ int fdisk_enable_bootbits_protection(struct fdisk_context *cxt, int enable)
}
/**
+ * fdisk_enable_wipe
+ * @cxt: fdisk context
+ * @enable: 1 or 0
+ *
+ * The library removes all filesystem/RAID signatures before write PT. This is
+ * no-op if any collision has not been detected by fdisk_assign_device(). See
+ * fdisk_has_collision(). The default is not wipe a device.
+ *
+ * Returns: 0 on success, < 0 on error.
+ */
+int fdisk_enable_wipe(struct fdisk_context *cxt, int enable)
+{
+ if (!cxt)
+ return -EINVAL;
+ cxt->wipe_device = enable ? 1 : 0;
+ return 0;
+}
+
+/**
+ * fdisk_has_wipe
+ * @cxt: fdisk context
+ *
+ * Returns the current wipe setting. See fdisk_enable_wipe().
+ *
+ * Returns: 0 on success, < 0 on error.
+ */
+int fdisk_has_wipe(struct fdisk_context *cxt)
+{
+ return cxt && cxt->wipe_device;
+}
+
+
+/**
+ * fdisk_get_collision
+ * @cxt: fdisk context
+ *
+ * Returns: name of the filesystem or RAID detected on the device or NULL.
+ */
+const char *fdisk_get_collision(struct fdisk_context *cxt)
+{
+ return cxt->collision;
+}
+
+/**
* fdisk_get_npartitions:
* @cxt: context
*
@@ -427,6 +471,9 @@ static void reset_context(struct fdisk_context *cxt)
free(cxt->dev_path);
cxt->dev_path = NULL;
+ free(cxt->collision);
+ cxt->collision = NULL;
+
cxt->dev_fd = -1;
cxt->firstsector = NULL;
cxt->firstsector_bufsz = 0;
@@ -445,18 +492,15 @@ static void reset_context(struct fdisk_context *cxt)
*
* Returns: 0 if nothing found, < 0 on error, 1 if found a signature
*/
-static int warn_wipe(struct fdisk_context *cxt)
+static int check_collisions(struct fdisk_context *cxt)
{
#ifdef HAVE_LIBBLKID
- blkid_probe pr;
-#endif
int rc = 0;
+ blkid_probe pr;
assert(cxt);
+ assert(cxt->dev_fd >= 0);
- if (fdisk_has_label(cxt) || cxt->dev_fd < 0)
- return -EINVAL;
-#ifdef HAVE_LIBBLKID
DBG(CXT, ul_debugobj(cxt, "wipe check: initialize libblkid prober"));
pr = blkid_new_probe();
@@ -478,18 +522,48 @@ static int warn_wipe(struct fdisk_context *cxt)
if (blkid_probe_lookup_value(pr, "TYPE", &name, 0) == 0 ||
blkid_probe_lookup_value(pr, "PTTYPE", &name, 0) == 0) {
- fdisk_warnx(cxt, _(
- "%s: device contains a valid '%s' signature; it is "
- "strongly recommended to wipe the device with "
- "wipefs(8) if this is unexpected, in order to "
- "avoid possible collisions"), cxt->dev_path, name);
- rc = 1;
+ cxt->collision = strdup(name);
+ if (!cxt->collision)
+ rc = -ENOMEM;
}
}
blkid_free_probe(pr);
-#endif
return rc;
+#elif
+ return 0;
+#endif
+}
+
+int fdisk_wipe_collisions(struct fdisk_context *cxt)
+{
+#ifdef HAVE_LIBBLKID
+ blkid_probe pr;
+ int rc;
+
+ assert(cxt);
+ assert(cxt->dev_fd >= 0);
+
+ DBG(CXT, ul_debugobj(cxt, "wipe: 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_MAGIC);
+ blkid_probe_enable_partitions(pr, 1);
+ blkid_probe_set_partitions_flags(pr, BLKID_PARTS_MAGIC);
+
+ while (blkid_do_probe(pr) == 0)
+ blkid_do_wipe(pr, FALSE);
+
+ blkid_free_probe(pr);
+#endif
+ return 0;
}
/**
@@ -564,8 +638,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))
- warn_wipe(cxt);
+ if (!fdisk_is_listonly(cxt) && !fdisk_has_label(cxt) && check_collisions(cxt) < 0)
+ goto fail;
DBG(CXT, ul_debugobj(cxt, "initialized for %s [%s]",
fname, readonly ? "READ-ONLY" : "READ-WRITE"));