]> git.proxmox.com Git - pve-storage.git/commitdiff
add API for LVMThin management
authorDominik Csapak <d.csapak@proxmox.com>
Mon, 30 Jul 2018 08:26:05 +0000 (10:26 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 2 Aug 2018 09:29:47 +0000 (11:29 +0200)
like the LVM API, but return an array for the list,
because we do not have nested data here

and create a vg and thin lv with the name given and use the full size

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
PVE/API2/Disks.pm
PVE/API2/Disks/LVMThin.pm [new file with mode: 0644]
PVE/API2/Disks/Makefile

index 86d8fcf4c8bcd93ee3cefc279af71700c6df9fb7..c49d4391c459125decb3624875a4772d39b602a0 100644 (file)
@@ -9,6 +9,7 @@ use HTTP::Status qw(:constants);
 use PVE::JSONSchema qw(get_standard_option);
 
 use PVE::API2::Disks::LVM;
+use PVE::API2::Disks::LVMThin;
 
 use PVE::RESTHandler;
 
@@ -19,6 +20,11 @@ __PACKAGE__->register_method ({
    path => 'lvm',
 });
 
+__PACKAGE__->register_method ({
+   subclass => "PVE::API2::Disks::LVMThin",
+   path => 'lvmthin',
+});
+
 __PACKAGE__->register_method ({
     name => 'index',
     path => '',
@@ -48,6 +54,7 @@ __PACKAGE__->register_method ({
            { name => 'initgpt' },
            { name => 'smart' },
            { name => 'lvm' },
+           { name => 'lvmthin' },
            ];
 
        return $result;
diff --git a/PVE/API2/Disks/LVMThin.pm b/PVE/API2/Disks/LVMThin.pm
new file mode 100644 (file)
index 0000000..d07e050
--- /dev/null
@@ -0,0 +1,141 @@
+package PVE::API2::Disks::LVMThin;
+
+use strict;
+use warnings;
+
+use PVE::Storage::LvmThinPlugin;
+use PVE::Diskmanage;
+use PVE::JSONSchema qw(get_standard_option);
+use PVE::API2::Storage::Config;
+use PVE::Storage;
+use PVE::Tools qw(run_command lock_file);
+
+use PVE::RPCEnvironment;
+use PVE::RESTHandler;
+
+use base qw(PVE::RESTHandler);
+
+__PACKAGE__->register_method ({
+    name => 'index',
+    path => '',
+    method => 'GET',
+    proxyto => 'node',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
+    },
+    description => "List LVM thinpools",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+       },
+    },
+    returns => {
+       type => 'array',
+       items => {
+           type => 'object',
+           properties => {
+               lv => {
+                   type => 'string',
+                   description => 'The name of the thinpool.',
+               },
+               lv_size => {
+                   type => 'integer',
+                   description => 'The size of the thinpool in bytes.',
+               },
+               used => {
+                   type => 'integer',
+                   description => 'The used bytes of the thinpool.',
+               },
+               metadata_size => {
+                   type => 'integer',
+                   description => 'The size of the metadata lv in bytes.',
+               },
+               metadata_used => {
+                   type => 'integer',
+                   description => 'The used bytes of the metadata lv.',
+               },
+           },
+       },
+    },
+    code => sub {
+       my ($param) = @_;
+       return PVE::Storage::LvmThinPlugin::list_thinpools(undef);
+    }});
+
+__PACKAGE__->register_method ({
+    name => 'create',
+    path => '',
+    method => 'POST',
+    proxyto => 'node',
+    protected => 1,
+    permissions => {
+       check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
+    },
+    description => "Create an LVM thinpool",
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           name => get_standard_option('pve-storage-id'),
+           device => {
+               type => 'string',
+               description => 'The block device you want to create the thinpool on.',
+           },
+           add_storage => {
+               description => "Configure storage using the thinpool.",
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+           },
+       },
+    },
+    returns => { type => 'string' },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $user = $rpcenv->get_user();
+
+       my $name = $param->{name};
+       my $dev = $param->{device};
+       my $node = $param->{node};
+
+       $dev = PVE::Diskmanage::verify_blockdev_path($dev);
+       die "device $dev is already in use\n" if PVE::Diskmanage::disk_is_used($dev);
+
+       my $cfg = PVE::Storage::config();
+
+       if (my $scfg = PVE::Storage::storage_config($cfg, $name, 1)) {
+           die "storage ID '$name' already defined\n";
+       }
+
+       my $worker = sub {
+           lock_file('/run/lock/pve-diskmanage.lck', undef, sub {
+               PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
+
+               # create thinpool with size 100%, let lvm handle the metadata size
+               run_command(['/sbin/lvcreate', '--type', 'thin-pool', '-l100%FREE', '-n', $name, $name]);
+
+               if ($param->{add_storage}) {
+                   my $storage_params = {
+                       type => 'lvmthin',
+                       vgname => $name,
+                       thinpool => $name,
+                       storage => $name,
+                       content => 'rootdir,images',
+                       nodes => $node,
+                   };
+
+                   PVE::API2::Storage::Config->create($storage_params);
+               }
+           });
+
+           die $@ if $@;
+       };
+
+       return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker);
+    }});
+
+1;
index 7b41f89dea19d4b4174aa5a9a3fdd0a90209ee71..bc2af8fe5c5e43f3ca9db8c8df6a1aa49354d80f 100644 (file)
@@ -1,5 +1,6 @@
 
-SOURCES= LVM.pm
+SOURCES= LVM.pm\
+        LVMThin.pm
 
 .PHONY: install
 install: