Skip to contentSkip to conversation

device: add wipe_known_sbs wrapper fn

PATCH New Peter Rajnoha 1 participant 1 message Nov 15, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 device: add wipe_known_sbs wrapper fn

The wipe_known_sbs fn wraps the _wipe_sb fn that is called each known superblock. Just a cleanup - makes the code more readable, not repeating the same sequence when used anywhere in the code. We're going to reuse this code...

---
 lib/device/dev-type.c   | 42 ++++++++++++++++++++++++++++++++++++++++++
 lib/device/dev-type.h   |  4 ++++
 lib/metadata/metadata.c | 46 ++++------------------------------------------
 3 files changed, 50 insertions(+), 42 deletions(-)
lib/device/dev-type.c +42 -0
index a98cf45..4157b33 100644
@@ -443,6 +443,48 @@ out:
 	return ret;
 }
 
+static int _wipe_sb(struct device *dev, const char *type, const char *name,
+		    int wipe_len, int yes, force_t force,
+		    int (*func)(struct device *dev, uint64_t *signature))
+{
+	int wipe;
+	uint64_t superblock;
+
+	wipe = func(dev, &superblock);
+	if (wipe == -1) {
+		log_error("Fatal error while trying to detect %s on %s.",
+			  type, name);
+		return 0;
+	}
+
+	if (wipe == 0)
+		return 1;
+
+	/* Specifying --yes => do not ask. */
+	if (!yes && (force == PROMPT) &&
+	    yes_no_prompt("WARNING: %s detected on %s. Wipe it? [y/n] ",
+			  type, name) != 'y')
+		return_0;
+
+	log_print_unless_silent("Wiping %s on %s.", type, name);
+	if (!dev_set(dev, superblock, wipe_len, 0)) {
+		log_error("Failed to wipe %s on %s.", type, name);
+		return 0;
+	}
+
+	return 1;
+}
+
+int wipe_known_sbs(struct device *dev, const char *name, int yes, force_t force)
+{
+	if (!_wipe_sb(dev, "software RAID md superblock", name, 4, yes, force, dev_is_md) ||
+	    !_wipe_sb(dev, "swap signature", name, 10, yes, force, dev_is_swap) ||
+	    !_wipe_sb(dev, "LUKS signature", name, 8, yes, force, dev_is_luks))
+		return 0;
+
+	return 1;
+}
+
 #ifdef __linux__
 
 static unsigned long _dev_topology_attribute(struct dev_types *dt,
lib/device/dev-type.h +4 -0
index 2fa32db..d92b430 100644
@@ -16,6 +16,7 @@
 #define _LVM_DEV_TYPE_H
 
 #include "device.h"
+#include "display.h"
 
 #define NUMBER_OF_MAJORS 4096
 
@@ -58,6 +59,9 @@ int dev_is_md(struct device *dev, uint64_t *sb);
 int dev_is_swap(struct device *dev, uint64_t *signature);
 int dev_is_luks(struct device *dev, uint64_t *signature);
 
+/* Signature wiping. */
+int wipe_known_sbs(struct device *dev, const char *name, int yes, force_t force);
+
 /* Type-specific device properties */
 unsigned long dev_md_stripe_width(struct dev_types *dt, struct device *dev);
 
lib/metadata/metadata.c +4 -42

58 lines not on this page · raw message

Signature · 1 line
1.8.4.2