[pve-devel] [PATCH storage 01/11] diskmanage: add wipe_blockdev method
Fabian Ebner
f.ebner at proxmox.com
Fri Apr 23 12:14:51 CEST 2021
based on the wipe_disks method from pve-manager's Ceph/Tools.pm with the
following main differences:
* use wipefs to wipe labels first (to avoid sgdisk complaining about the
backed up GPT structure on a subsequent GPT initialization)
* only take one device as an argument
* do not use an absolute path for 'dd'
* die if one of the command fails
The wipefs command makes checks and complains about e.g. mounted or active
devices. One could supply --force to wipefs, but in many such situations it
does not work as expected, because the device would still be detected as in-use
afterwards, and further manaual steps would be needed.
Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
PVE/Diskmanage.pm | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/PVE/Diskmanage.pm b/PVE/Diskmanage.pm
index b916d2e..612d976 100644
--- a/PVE/Diskmanage.pm
+++ b/PVE/Diskmanage.pm
@@ -7,6 +7,7 @@ use PVE::ProcFSTools;
use Data::Dumper;
use Cwd qw(abs_path);
use Fcntl ':mode';
+use File::Basename;
use File::stat;
use JSON;
@@ -841,4 +842,30 @@ sub append_partition {
return $partition;
}
+# Wipes all labels and the first 200 MiB of a disk/partition (or the whole if it is smaller).
+# Expected to be called with a result of verify_blockdev_path().
+sub wipe_blockdev {
+ my ($devpath) = @_;
+
+ my $wipefs_cmd = ['wipefs', '--all', $devpath];
+
+ my $dd_cmd = ['dd', 'if=/dev/zero', "of=${devpath}", 'bs=1M', 'conv=fdatasync'];
+
+ my $devname = basename($devpath);
+ my $dev_size = PVE::Tools::file_get_contents("/sys/class/block/$devname/size");
+
+ ($dev_size) = $dev_size =~ m|(\d+)|; # untaint $dev_size
+ die "Couldn't get the size of the device $devname\n" if !defined($dev_size);
+
+ my $size = ($dev_size * 512 / 1024 / 1024);
+ my $count = ($size < 200) ? $size : 200;
+
+ push @{$dd_cmd}, "count=${count}";
+
+ print "wiping disk/partition: ${devpath}\n";
+
+ run_command($wipefs_cmd, errmsg => "error wiping labels for '${devpath}'");
+ run_command($dd_cmd, errmsg => "error wiping '${devpath}'");
+}
+
1;
--
2.20.1
More information about the pve-devel
mailing list