]> git.proxmox.com Git - pve-storage.git/blobdiff - PVE/API2/Disks.pm
fix #3610: properly build ZFS detail tree
[pve-storage.git] / PVE / API2 / Disks.pm
index 3be68735e2d1c8cdfa692f560054433b6f0c1b1f..6c20931d5d4cecca85c937005c076e50f9779981 100644 (file)
@@ -3,16 +3,40 @@ package PVE::API2::Disks;
 use strict;
 use warnings;
 
-use PVE::SafeSyslog;
-use PVE::Diskmanage;
+use File::Basename;
 use HTTP::Status qw(:constants);
+
+use PVE::Diskmanage;
 use PVE::JSONSchema qw(get_standard_option);
+use PVE::SafeSyslog;
 
-use PVE::RESTHandler;
+use PVE::API2::Disks::Directory;
+use PVE::API2::Disks::LVM;
+use PVE::API2::Disks::LVMThin;
+use PVE::API2::Disks::ZFS;
 
+use PVE::RESTHandler;
 use base qw(PVE::RESTHandler);
 
-use Data::Dumper;
+__PACKAGE__->register_method ({
+   subclass => "PVE::API2::Disks::LVM",
+   path => 'lvm',
+});
+
+__PACKAGE__->register_method ({
+   subclass => "PVE::API2::Disks::LVMThin",
+   path => 'lvmthin',
+});
+
+__PACKAGE__->register_method ({
+   subclass => "PVE::API2::Disks::Directory",
+   path => 'directory',
+});
+
+__PACKAGE__->register_method ({
+   subclass => "PVE::API2::Disks::ZFS",
+   path => 'zfs',
+});
 
 __PACKAGE__->register_method ({
     name => 'index',
@@ -42,7 +66,12 @@ __PACKAGE__->register_method ({
            { name => 'list' },
            { name => 'initgpt' },
            { name => 'smart' },
-           ];
+           { name => 'lvm' },
+           { name => 'lvmthin' },
+           { name => 'directory' },
+           { name => 'wipedisk' },
+           { name => 'zfs' },
+       ];
 
        return $result;
     }});
@@ -55,12 +84,33 @@ __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,
        properties => {
            node => get_standard_option('pve-node'),
+           'include-partitions' => {
+               description => "Also include partitions.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+           skipsmart => {
+               description => "Skip smart checks.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+           type => {
+               description => "Only list specific types of disks.",
+               type => 'string',
+               enum => ['unused', 'journal_disks'],
+               optional => 1,
+           },
        },
     },
     returns => {
@@ -81,18 +131,43 @@ __PACKAGE__->register_method ({
                serial =>  { type => 'string', optional => 1 },
                wwn => { type => 'string', optional => 1},
                health => { type => 'string', optional => 1},
+               parent => {
+                   type => 'string',
+                   description => 'For partitions only. The device path of ' .
+                       'the disk the partition resides on.',
+                   optional => 1
+               },
            },
        },
     },
     code => sub {
        my ($param) = @_;
 
-       my $disks = PVE::Diskmanage::get_disks();
+       my $skipsmart = $param->{skipsmart} // 0;
+       my $include_partitions = $param->{'include-partitions'} // 0;
 
+       my $disks = PVE::Diskmanage::get_disks(
+           undef,
+           $skipsmart,
+           $include_partitions
+       );
+
+       my $type = $param->{type} // '';
        my $result = [];
 
        foreach my $disk (sort keys %$disks) {
            my $entry = $disks->{$disk};
+           if ($type eq 'journal_disks') {
+               next if $entry->{osdid} >= 0;
+               if (my $usage = $entry->{used}) {
+                   next if !($usage eq 'partitions' && $entry->{gpt}
+                       || $usage eq 'LVM');
+               }
+           } elsif ($type eq 'unused') {
+               next if $entry->{used};
+           } elsif ($type ne '') {
+               die "internal error"; # should not happen
+           }
            push @$result, $entry;
        }
        return $result;
@@ -124,21 +199,24 @@ __PACKAGE__->register_method ({
            },
        },
     },
-    returns => { type => 'object' },
+    returns => {
+       type => 'object',
+       properties => {
+           health => { type => 'string' },
+           type => { type => 'string', optional => 1 },
+           attributes => { type => 'array', optional => 1},
+           text => { type => 'string', optional => 1 },
+       },
+    },
     code => sub {
        my ($param) = @_;
 
        my $disk = PVE::Diskmanage::verify_blockdev_path($param->{disk});
 
-       my $result = {};
-
-       if ($param->{healthonly}) {
-           $result = { health => PVE::Diskmanage::get_smart_health($disk) };
-       } else {
-           $result = PVE::Diskmanage::get_smart_data($disk);
-       }
+       my $result = PVE::Diskmanage::get_smart_data($disk, $param->{healthonly});
 
        $result->{health} = 'UNKNOWN' if !defined $result->{health};
+       $result = { health => $result->{health} } if $param->{healthonly};
 
        return $result;
     }});
@@ -191,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;