From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 1DF0F766F9
 for <pve-devel@lists.proxmox.com>; Fri, 23 Apr 2021 12:15:20 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 0F37327704
 for <pve-devel@lists.proxmox.com>; Fri, 23 Apr 2021 12:15:18 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id 86DF127657
 for <pve-devel@lists.proxmox.com>; Fri, 23 Apr 2021 12:15:15 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 97B8B45B0F
 for <pve-devel@lists.proxmox.com>; Fri, 23 Apr 2021 12:15:05 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri, 23 Apr 2021 12:14:55 +0200
Message-Id: <20210423101501.27300-6-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210423101501.27300-1-f.ebner@proxmox.com>
References: <20210423101501.27300-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH storage 05/11] api: add wipedisk call
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 23 Apr 2021 10:15:20 -0000

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@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