Blame SOURCES/0025-pvcreate-overwrite-partition-header-with-f.patch

38b7b2
From a5c37afdca97d6565ea02bc4bc7d52f360823cd3 Mon Sep 17 00:00:00 2001
38b7b2
From: David Teigland <teigland@redhat.com>
38b7b2
Date: Wed, 8 Sep 2021 16:30:11 -0500
38b7b2
Subject: [PATCH 25/54] pvcreate: overwrite partition header with -f
38b7b2
38b7b2
$ pvcreate /dev/sdc
38b7b2
  Cannot use /dev/sdc: device is partitioned
38b7b2
$ pvcreate -f /dev/sdc
38b7b2
  Physical volume "/dev/sdc" successfully created.
38b7b2
---
38b7b2
 lib/commands/toolcontext.h       |  1 +
38b7b2
 lib/filters/filter-partitioned.c |  3 +++
38b7b2
 man/pvcreate.8_des               |  7 ++++---
38b7b2
 test/shell/test-partition.sh     | 12 ++++++++++--
38b7b2
 tools/toollib.c                  | 10 ++++++++++
38b7b2
 5 files changed, 28 insertions(+), 5 deletions(-)
38b7b2
38b7b2
diff --git a/lib/commands/toolcontext.h b/lib/commands/toolcontext.h
38b7b2
index 356c79f8a..b83883fb8 100644
38b7b2
--- a/lib/commands/toolcontext.h
38b7b2
+++ b/lib/commands/toolcontext.h
38b7b2
@@ -201,6 +201,7 @@ struct cmd_context {
38b7b2
 	unsigned ignore_device_name_mismatch:1; /* skip updating devices file names */
38b7b2
 	unsigned backup_disabled:1;		/* skip repeated debug message */
38b7b2
 	unsigned event_activation:1;		/* whether event_activation is set */
38b7b2
+	unsigned filter_partitioned_skip:1;	/* don't use filter-partitioned */
38b7b2
 
38b7b2
 	/*
38b7b2
 	 * Devices and filtering.
38b7b2
diff --git a/lib/filters/filter-partitioned.c b/lib/filters/filter-partitioned.c
38b7b2
index 642553ef2..8f468a567 100644
38b7b2
--- a/lib/filters/filter-partitioned.c
38b7b2
+++ b/lib/filters/filter-partitioned.c
38b7b2
@@ -27,6 +27,9 @@ static int _passes_partitioned_filter(struct cmd_context *cmd, struct dev_filter
38b7b2
 	if (cmd->filter_nodata_only)
38b7b2
 		return 1;
38b7b2
 
38b7b2
+	if (cmd->filter_partitioned_skip)
38b7b2
+		return 1;
38b7b2
+
38b7b2
 	dev->filtered_flags &= ~DEV_FILTERED_PARTITIONED;
38b7b2
 
38b7b2
 	ret = dev_is_partitioned(cmd, dev);
38b7b2
diff --git a/man/pvcreate.8_des b/man/pvcreate.8_des
38b7b2
index 69bd133aa..4048eb71c 100644
38b7b2
--- a/man/pvcreate.8_des
38b7b2
+++ b/man/pvcreate.8_des
38b7b2
@@ -7,9 +7,10 @@ Use \fBvgcreate\fP(8) to create a new VG on the PV, or \fBvgextend\fP(8)
38b7b2
 to add the PV to an existing VG.  Use \fBpvremove\fP(8) to remove the LVM
38b7b2
 disk label from the device.
38b7b2
 .P
38b7b2
-The force option will create a PV without confirmation.  Repeating the
38b7b2
-force option (\fB-ff\fP) will forcibly create a PV, overriding checks that
38b7b2
-normally prevent it, e.g. if the PV is already in a VG.
38b7b2
+The force option will create a PV without confirmation, and will overwrite
38b7b2
+partition headers.  Repeating the force option (\fB-ff\fP) will override other
38b7b2
+checks that would normally prevent a pvcreate, e.g. if the PV is already in a
38b7b2
+VG.
38b7b2
 .P
38b7b2
 .B Metadata location, size, and alignment
38b7b2
 .P
38b7b2
diff --git a/test/shell/test-partition.sh b/test/shell/test-partition.sh
38b7b2
index 0e92f00db..3a45f9089 100644
38b7b2
--- a/test/shell/test-partition.sh
38b7b2
+++ b/test/shell/test-partition.sh
38b7b2
@@ -16,7 +16,6 @@
38b7b2
 #
38b7b2
 
38b7b2
 
38b7b2
-
38b7b2
 SKIP_WITH_LVMPOLLD=1
38b7b2
 
38b7b2
 LVM_TEST_CONFIG_DEVICES="types = [\"device-mapper\", 142]"
38b7b2
@@ -25,7 +24,7 @@ LVM_TEST_CONFIG_DEVICES="types = [\"device-mapper\", 142]"
38b7b2
 
38b7b2
 which sfdisk || skip
38b7b2
 
38b7b2
-aux prepare_pvs 1 30
38b7b2
+aux prepare_pvs 2 30
38b7b2
 
38b7b2
 pvs "$dev1"
38b7b2
 
38b7b2
@@ -33,3 +32,12 @@ pvs "$dev1"
38b7b2
 echo "1 2" | sfdisk --force "$dev1"
38b7b2
 
38b7b2
 not pvs "$dev1"
38b7b2
+
38b7b2
+wipefs -a "$dev2"
38b7b2
+echo "1 2" | sfdisk --force "$dev2"
38b7b2
+partprobe
38b7b2
+not pvcreate "$dev2"
38b7b2
+pvcreate -f "$dev2"
38b7b2
+pvs "$dev2"
38b7b2
+pvremove "$dev2"
38b7b2
+
38b7b2
diff --git a/tools/toollib.c b/tools/toollib.c
38b7b2
index d6f48aad2..80d3de57c 100644
38b7b2
--- a/tools/toollib.c
38b7b2
+++ b/tools/toollib.c
38b7b2
@@ -5243,6 +5243,10 @@ int pvcreate_each_device(struct cmd_context *cmd,
38b7b2
 	if (cmd->enable_devices_file && !pp->is_remove)
38b7b2
 		cmd->filter_deviceid_skip = 1;
38b7b2
 
38b7b2
+	/* pvcreate -f overwrites partitions */
38b7b2
+	if (pp->force && !pp->is_remove)
38b7b2
+		cmd->filter_partitioned_skip = 1;
38b7b2
+
38b7b2
 	log_debug("Scanning and filtering device args (%u).", dm_list_size(&scan_devs));
38b7b2
 	label_scan_devs(cmd, cmd->filter, &scan_devs);
38b7b2
 
38b7b2
@@ -5257,6 +5261,7 @@ int pvcreate_each_device(struct cmd_context *cmd,
38b7b2
 		}
38b7b2
 	}
38b7b2
 	cmd->filter_deviceid_skip = 0;
38b7b2
+	cmd->filter_partitioned_skip = 0;
38b7b2
 
38b7b2
 	/*
38b7b2
 	 * Can the command continue if some specified devices were not found?
38b7b2
@@ -5469,6 +5474,9 @@ do_command:
38b7b2
 	if (cmd->enable_devices_file && !pp->is_remove)
38b7b2
 		cmd->filter_deviceid_skip = 1;
38b7b2
 
38b7b2
+	if (pp->force && !pp->is_remove)
38b7b2
+		cmd->filter_partitioned_skip = 1;
38b7b2
+
38b7b2
 	log_debug("Rescanning and filtering device args with exclusive open");
38b7b2
 	if (!label_scan_devs_excl(cmd, cmd->filter, &rescan_devs)) {
38b7b2
 		log_debug("Failed to rescan devs excl");
38b7b2
@@ -5482,7 +5490,9 @@ do_command:
38b7b2
 			dm_list_add(&pp->arg_fail, &pd->list);
38b7b2
 		}
38b7b2
 	}
38b7b2
+
38b7b2
 	cmd->filter_deviceid_skip = 0;
38b7b2
+	cmd->filter_partitioned_skip = 0;
38b7b2
 
38b7b2
 	if (dm_list_empty(&pp->arg_process) && dm_list_empty(&remove_duplicates)) {
38b7b2
 		log_debug("No devices to process.");
38b7b2
-- 
38b7b2
2.34.3
38b7b2