[pve-devel] [PATCH storage 05/11] api: add wipedisk call

Fabian Ebner f.ebner at proxmox.com
Fri Apr 23 12:14:55 CEST 2021


Try to detect active mounts and holders early, because it's cheap. The wipefs
command in the worker will detect even more situations where wiping alone is
not enough for the device to show up as unused, or could otherwise be
problematic.

Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 PVE/API2/Disks.pm | 42 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)

diff --git a/PVE/API2/Disks.pm b/PVE/API2/Disks.pm
index 33bca76..6c20931 100644
--- a/PVE/API2/Disks.pm
+++ b/PVE/API2/Disks.pm
@@ -3,6 +3,7 @@ package PVE::API2::Disks;
 use strict;
 use warnings;
 
+use File::Basename;
 use HTTP::Status qw(:constants);
 
 use PVE::Diskmanage;
@@ -68,6 +69,7 @@ __PACKAGE__->register_method ({
 	    { name => 'lvm' },
 	    { name => 'lvmthin' },
 	    { name => 'directory' },
+	    { name => 'wipedisk' },
 	    { name => 'zfs' },
 	];
 
@@ -267,4 +269,44 @@ __PACKAGE__->register_method ({
 	return $rpcenv->fork_worker('diskinit', $diskid, $authuser, $worker);
     }});
 
+__PACKAGE__->register_method ({
+    name => 'wipe_disk',
+    path => 'wipedisk',
+    method => 'PUT',
+    description => "Wipe a disk or partition.",
+    proxyto => 'node',
+    protected => 1,
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    disk => {
+		type => 'string',
+		description => "Block device name",
+		pattern => '^/dev/[a-zA-Z0-9\/]+$',
+	    },
+	},
+    },
+    returns => { type => 'string' },
+    code => sub {
+	my ($param) = @_;
+
+	my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
+
+	my $mounted = PVE::Diskmanage::is_mounted($disk);
+	die "disk/partition '${mounted}' is mounted\n" if $mounted;
+
+	my $held = PVE::Diskmanage::has_holder($disk);
+	die "disk/partition '${held}' has a holder\n" if $held;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
+
+	my $worker = sub { PVE::Diskmanage::wipe_blockdev($disk); };
+
+	my $basename = basename($disk); # avoid '/' in the ID
+
+	return $rpcenv->fork_worker('wipedisk', $basename, $authuser, $worker);
+    }});
+
 1;
-- 
2.20.1






More information about the pve-devel mailing list