]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Disks.pm
prune: mark renamed and protected backups differently
[pve-storage.git] / PVE / API2 / Disks.pm
index d2ee81dd613fec3c95619504e9becbe082ee4036..25c9ded4eb7a273beaf0631cb8af92e40ce36742 100644 (file)
@@ -3,11 +3,13 @@ package PVE::API2::Disks;
 use strict;
 use warnings;
 
+use File::Basename;
 use HTTP::Status qw(:constants);
 
 use PVE::Diskmanage;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::SafeSyslog;
+use PVE::Tools qw(run_command);
 
 use PVE::API2::Disks::Directory;
 use PVE::API2::Disks::LVM;
@@ -68,6 +70,7 @@ __PACKAGE__->register_method ({
            { name => 'lvm' },
            { name => 'lvmthin' },
            { name => 'directory' },
+           { name => 'wipedisk' },
            { name => 'zfs' },
        ];
 
@@ -82,7 +85,10 @@ __PACKAGE__->register_method ({
     protected => 1,
     proxyto => 'node',
     permissions => {
-       check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
+       check => ['or',
+           ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
+           ['perm', '/nodes/{node}', ['Sys.Audit', 'Datastore.Audit'], any => 1],
+       ],
     },
     parameters => {
        additionalProperties => 0,
@@ -254,6 +260,7 @@ __PACKAGE__->register_method ({
 
        my $authuser = $rpcenv->get_user();
 
+       die "$disk is a partition\n" if PVE::Diskmanage::is_partition($disk);
        die "disk $disk already in use\n" if PVE::Diskmanage::disk_is_used($disk);
        my $worker = sub {
            PVE::Diskmanage::init_disk($disk, $param->{uuid});
@@ -264,4 +271,52 @@ __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);
+
+           # FIXME: Remove once we depend on systemd >= v249.
+           # Work around udev bug https://github.com/systemd/systemd/issues/18525 to ensure the
+           # udev database is updated.
+           eval { run_command(['udevadm', 'trigger', $disk]); };
+           warn $@ if $@;
+       };
+
+       my $basename = basename($disk); # avoid '/' in the ID
+
+       return $rpcenv->fork_worker('wipedisk', $basename, $authuser, $worker);
+    }});
+
 1;