return 0;
}
+/**
+ * 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
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;
*
* 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();
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;
}
/**
/* 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"));
display_in_cyl_units : 1, /* for obscure labels */
display_details : 1, /* expert display mode */
protect_bootbits : 1, /* don't zeroize fll irst sector */
+ wipe_device : 1, /* wipe device before write */
listonly : 1; /* list partition, nothing else */
+ char *collision; /* name of already existing FS/PT */
+
int sizeunit; /* SIZE fields, FDISK_SIZEUNIT_* */
/* alignment */
struct fdisk_script *script; /* what we want to follow */
};
+int fdisk_wipe_collisions(struct fdisk_context *cxt);
+
/* partition.c */
int fdisk_partition_next_partno(struct fdisk_partition *pa,
struct fdisk_context *cxt,