]>
Commit | Line | Data |
---|---|---|
8b6842ca DC |
1 | package PVE::API2::Disks::LVM; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use PVE::Storage::LVMPlugin; | |
7 | use PVE::Diskmanage; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | use PVE::API2::Storage::Config; | |
10 | use PVE::Tools qw(lock_file); | |
11 | ||
12 | use PVE::RPCEnvironment; | |
13 | use PVE::RESTHandler; | |
14 | ||
15 | use 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); | |
152 | die "device $dev is already in use\n" if PVE::Diskmanage::disk_is_used($dev); | |
153 | ||
154 | my $worker = sub { | |
e39e8ee2 | 155 | PVE::Diskmanage::locked_disk_action(sub { |
8b6842ca DC |
156 | PVE::Storage::LVMPlugin::lvm_create_volume_group($dev, $name); |
157 | ||
158 | if ($param->{add_storage}) { | |
159 | my $storage_params = { | |
160 | type => 'lvm', | |
161 | vgname => $name, | |
162 | storage => $name, | |
163 | content => 'rootdir,images', | |
164 | shared => 0, | |
165 | nodes => $node, | |
166 | }; | |
167 | ||
168 | PVE::API2::Storage::Config->create($storage_params); | |
169 | } | |
170 | }); | |
8b6842ca DC |
171 | }; |
172 | ||
173 | return $rpcenv->fork_worker('lvmcreate', $name, $user, $worker); | |
174 | }}); | |
175 | ||
176 | 1; |