]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Disks/LVMThin.pm
refactor disk/storage checks for Disk API
[pve-storage.git] / PVE / API2 / Disks / LVMThin.pm
CommitLineData
0ea9f384
DC
1package PVE::API2::Disks::LVMThin;
2
3use strict;
4use warnings;
5
6use PVE::Storage::LvmThinPlugin;
7use PVE::Diskmanage;
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::API2::Storage::Config;
10use PVE::Storage;
11use PVE::Tools qw(run_command lock_file);
12
13use PVE::RPCEnvironment;
14use PVE::RESTHandler;
15
16use base qw(PVE::RESTHandler);
17
18__PACKAGE__->register_method ({
19 name => 'index',
20 path => '',
21 method => 'GET',
22 proxyto => 'node',
23 protected => 1,
24 permissions => {
25 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
26 },
27 description => "List LVM thinpools",
28 parameters => {
29 additionalProperties => 0,
30 properties => {
31 node => get_standard_option('pve-node'),
32 },
33 },
34 returns => {
35 type => 'array',
36 items => {
37 type => 'object',
38 properties => {
39 lv => {
40 type => 'string',
41 description => 'The name of the thinpool.',
42 },
43 lv_size => {
44 type => 'integer',
45 description => 'The size of the thinpool in bytes.',
46 },
47 used => {
48 type => 'integer',
49 description => 'The used bytes of the thinpool.',
50 },
51 metadata_size => {
52 type => 'integer',
53 description => 'The size of the metadata lv in bytes.',
54 },
55 metadata_used => {
56 type => 'integer',
57 description => 'The used bytes of the metadata lv.',
58 },
59 },
60 },
61 },
62 code => sub {
63 my ($param) = @_;
64 return PVE::Storage::LvmThinPlugin::list_thinpools(undef);
65 }});
66
67__PACKAGE__->register_method ({
68 name => 'create',
69 path => '',
70 method => 'POST',
71 proxyto => 'node',
72 protected => 1,
73 permissions => {
74 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
75 },
76 description => "Create an LVM thinpool",
77 parameters => {
78 additionalProperties => 0,
79 properties => {
80 node => get_standard_option('pve-node'),
81 name => get_standard_option('pve-storage-id'),
82 device => {
83 type => 'string',
84 description => 'The block device you want to create the thinpool on.',
85 },
86 add_storage => {
87 description => "Configure storage using the thinpool.",
88 type => 'boolean',
89 optional => 1,
90 default => 0,
91 },
92 },
93 },
94 returns => { type => 'string' },
95 code => sub {
96 my ($param) = @_;
97
98 my $rpcenv = PVE::RPCEnvironment::get();
99 my $user = $rpcenv->get_user();
100
101 my $name = $param->{name};
102 my $dev = $param->{device};
103 my $node = $param->{node};
104
105 $dev = PVE::Diskmanage::verify_blockdev_path($dev);
76c1e57b
DC
106 PVE::Diskmanage::check_unused($dev);
107 PVE::Storage::check_available($name);
0ea9f384
DC
108
109 my $worker = sub {
e39e8ee2 110 PVE::Diskmanage::locked_disk_action(sub {
0ea9f384
DC
111 PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
112
113 # create thinpool with size 100%, let lvm handle the metadata size
114 run_command(['/sbin/lvcreate', '--type', 'thin-pool', '-l100%FREE', '-n', $name, $name]);
115
116 if ($param->{add_storage}) {
117 my $storage_params = {
118 type => 'lvmthin',
119 vgname => $name,
120 thinpool => $name,
121 storage => $name,
122 content => 'rootdir,images',
123 nodes => $node,
124 };
125
126 PVE::API2::Storage::Config->create($storage_params);
127 }
128 });
0ea9f384
DC
129 };
130
5e35281d 131 return $rpcenv->fork_worker('lvmthincreate', $name, $user, $worker);
0ea9f384
DC
132 }});
133
1341;