]> git.proxmox.com Git - pve-storage.git/blame - PVE/API2/Disks/LVM.pm
rename check_available to assert_sid_unused
[pve-storage.git] / PVE / API2 / Disks / LVM.pm
CommitLineData
8b6842ca
DC
1package PVE::API2::Disks::LVM;
2
3use strict;
4use warnings;
5
6use PVE::Storage::LVMPlugin;
7use PVE::Diskmanage;
8use PVE::JSONSchema qw(get_standard_option);
9use PVE::API2::Storage::Config;
10use PVE::Tools qw(lock_file);
11
12use PVE::RPCEnvironment;
13use PVE::RESTHandler;
14
15use base qw(PVE::RESTHandler);
16
17__PACKAGE__->register_method ({
18 name => 'index',
19 path => '',
20 method => 'GET',
21 proxyto => 'node',
22 protected => 1,
23 permissions => {
24 check => ['perm', '/', ['Sys.Audit', 'Datastore.Audit'], any => 1],
25 },
26 description => "List LVM Volume Groups",
27 parameters => {
28 additionalProperties => 0,
29 properties => {
30 node => get_standard_option('pve-node'),
31 },
32 },
33 returns => {
34 type => 'object',
35 properties => {
36 leaf => {
37 type => 'boolean',
38 },
39 children => {
40 type => 'array',
41 items => {
42 type => "object",
43 properties => {
44 leaf => {
45 type => 'boolean',
46 },
47 name => {
48 type => 'string',
49 description => 'The name of the volume group',
50 },
51 size => {
52 type => 'integer',
53 description => 'The size of the volume group in bytes',
54 },
55 free => {
56 type => 'integer',
57 description => 'The free bytes in the volume group',
58 },
59 children => {
60 optional => 1,
61 type => 'array',
62 description => 'The underlying physical volumes',
63 items => {
64 type => 'object',
65 properties => {
66 leaf => {
67 type => 'boolean',
68 },
69 name => {
70 type => 'string',
71 description => 'The name of the physical volume',
72 },
73 size => {
74 type => 'integer',
75 description => 'The size of the physical volume in bytes',
76 },
77 free => {
78 type => 'integer',
79 description => 'The free bytes in the physical volume',
80 },
81 },
82 },
83 },
84 },
85 },
86 },
87 },
88 },
89 code => sub {
90 my ($param) = @_;
91
92 my $result = [];
93
94 my $vgs = PVE::Storage::LVMPlugin::lvm_vgs(1);
95
96 foreach my $vg_name (sort keys %$vgs) {
97 my $vg = $vgs->{$vg_name};
98 $vg->{name} = $vg_name;
99 $vg->{leaf} = 0;
100 foreach my $pv (@{$vg->{pvs}}) {
101 $pv->{leaf} = 1;
102 }
103 $vg->{children} = delete $vg->{pvs};
104 push @$result, $vg;
105 }
106
107 return {
108 leaf => 0,
109 children => $result,
110 };
111 }});
112
113__PACKAGE__->register_method ({
114 name => 'create',
115 path => '',
116 method => 'POST',
117 proxyto => 'node',
118 protected => 1,
119 permissions => {
120 check => ['perm', '/', ['Sys.Modify', 'Datastore.Allocate']],
121 },
122 description => "Create an LVM Volume Group",
123 parameters => {
124 additionalProperties => 0,
125 properties => {
126 node => get_standard_option('pve-node'),
127 name => get_standard_option('pve-storage-id'),
128 device => {
129 type => 'string',
130 description => 'The block device you want to create the volume group on',
131 },
132 add_storage => {
133 description => "Configure storage using the Volume Group",
134 type => 'boolean',
135 optional => 1,
136 default => 0,
137 },
138 },
139 },
140 returns => { type => 'string' },
141 code => sub {
142 my ($param) = @_;
143
144 my $rpcenv = PVE::RPCEnvironment::get();
145 my $user = $rpcenv->get_user();
146
147 my $name = $param->{name};
148 my $dev = $param->{device};
149 my $node = $param->{node};
150
151 $dev = PVE::Diskmanage::verify_blockdev_path($dev);
76c1e57b 152 PVE::Diskmanage::check_unused($dev);
9280153e 153 PVE::Storage::assert_sid_unused($name) if $param->{add_storage};
8b6842ca
DC
154
155 my $worker = sub {
e39e8ee2 156 PVE::Diskmanage::locked_disk_action(sub {
8b6842ca
DC
157 PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name);
158
159 if ($param->{add_storage}) {
160 my $storage_params = {
161 type => 'lvm',
162 vgname => $name,
163 storage => $name,
164 content => 'rootdir,images',
165 shared => 0,
166 nodes => $node,
167 };
168
169 PVE::API2::Storage::Config->create($storage_params);
170 }
171 });
8b6842ca
DC
172 };
173
174 return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker);
175 }});
176
1771;