]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::LVMPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use PVE::Tools qw(run_command trim); | |
7 | use PVE::Storage::Plugin; | |
8 | use PVE::JSONSchema qw(get_standard_option); | |
9 | ||
10 | use base qw(PVE::Storage::Plugin); | |
11 | ||
12 | # lvm helper functions | |
13 | ||
14 | sub lvm_pv_info { | |
15 | my ($device) = @_; | |
16 | ||
17 | die "no device specified" if !$device; | |
18 | ||
19 | my $has_label = 0; | |
20 | ||
21 | my $cmd = ['/usr/bin/file', '-L', '-s', $device]; | |
22 | run_command($cmd, outfunc => sub { | |
23 | my $line = shift; | |
24 | $has_label = 1 if $line =~ m/LVM2/; | |
25 | }); | |
26 | ||
27 | return undef if !$has_label; | |
28 | ||
29 | $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k', | |
30 | '--unbuffered', '--nosuffix', '--options', | |
31 | 'pv_name,pv_size,vg_name,pv_uuid', $device]; | |
32 | ||
33 | my $pvinfo; | |
34 | run_command($cmd, outfunc => sub { | |
35 | my $line = shift; | |
36 | ||
37 | $line = trim($line); | |
38 | ||
39 | my ($pvname, $size, $vgname, $uuid) = split(':', $line); | |
40 | ||
41 | die "found multiple pvs entries for device '$device'\n" | |
42 | if $pvinfo; | |
43 | ||
44 | $pvinfo = { | |
45 | pvname => $pvname, | |
46 | size => $size, | |
47 | vgname => $vgname, | |
48 | uuid => $uuid, | |
49 | }; | |
50 | }); | |
51 | ||
52 | return $pvinfo; | |
53 | } | |
54 | ||
55 | sub clear_first_sector { | |
56 | my ($dev) = shift; | |
57 | ||
58 | if (my $fh = IO::File->new($dev, "w")) { | |
59 | my $buf = 0 x 512; | |
60 | syswrite $fh, $buf; | |
61 | $fh->close(); | |
62 | } | |
63 | } | |
64 | ||
65 | sub lvm_create_volume_group { | |
66 | my ($device, $vgname, $shared) = @_; | |
67 | ||
68 | my $res = lvm_pv_info($device); | |
69 | ||
70 | if ($res->{vgname}) { | |
71 | return if $res->{vgname} eq $vgname; # already created | |
72 | die "device '$device' is already used by volume group '$res->{vgname}'\n"; | |
73 | } | |
74 | ||
75 | clear_first_sector($device); # else pvcreate fails | |
76 | ||
77 | # we use --metadatasize 250k, which reseults in "pe_start = 512" | |
78 | # so pe_start is aligned on a 128k boundary (advantage for SSDs) | |
79 | my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device]; | |
80 | ||
81 | run_command($cmd, errmsg => "pvcreate '$device' error"); | |
82 | ||
83 | $cmd = ['/sbin/vgcreate', $vgname, $device]; | |
84 | # push @$cmd, '-c', 'y' if $shared; # we do not use this yet | |
85 | ||
86 | run_command($cmd, errmsg => "vgcreate $vgname $device error"); | |
87 | } | |
88 | ||
89 | sub lvm_vgs { | |
90 | ||
91 | my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b', | |
92 | '--unbuffered', '--nosuffix', '--options', | |
93 | 'vg_name,vg_size,vg_free']; | |
94 | ||
95 | my $vgs = {}; | |
96 | eval { | |
97 | run_command($cmd, outfunc => sub { | |
98 | my $line = shift; | |
99 | ||
100 | $line = trim($line); | |
101 | ||
102 | my ($name, $size, $free) = split (':', $line); | |
103 | ||
104 | $vgs->{$name} = { size => int ($size), free => int ($free) }; | |
105 | }); | |
106 | }; | |
107 | my $err = $@; | |
108 | ||
109 | # just warn (vgs return error code 5 if clvmd does not run) | |
110 | # but output is still OK (list without clustered VGs) | |
111 | warn $err if $err; | |
112 | ||
113 | return $vgs; | |
114 | } | |
115 | ||
116 | sub lvm_lvs { | |
117 | my ($vgname) = @_; | |
118 | ||
119 | my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b', | |
120 | '--unbuffered', '--nosuffix', '--options', | |
121 | 'vg_name,lv_name,lv_size,uuid,tags']; | |
122 | ||
123 | push @$cmd, $vgname if $vgname; | |
124 | ||
125 | my $lvs = {}; | |
126 | run_command($cmd, outfunc => sub { | |
127 | my $line = shift; | |
128 | ||
129 | $line = trim($line); | |
130 | ||
131 | my ($vg, $name, $size, $uuid, $tags) = split(':', $line); | |
132 | ||
133 | return if $name !~ m/^vm-(\d+)-/; | |
134 | my $nid = $1; | |
135 | ||
136 | my $owner; | |
137 | foreach my $tag (split (/,/, $tags)) { | |
138 | if ($tag =~ m/^pve-vm-(\d+)$/) { | |
139 | $owner = $1; | |
140 | last; | |
141 | } | |
142 | } | |
143 | ||
144 | if ($owner) { | |
145 | if ($owner ne $nid) { | |
146 | warn "owner mismatch name = $name, owner = $owner\n"; | |
147 | } | |
148 | ||
149 | $lvs->{$vg}->{$name} = { format => 'raw', size => $size, | |
150 | uuid => $uuid, tags => $tags, | |
151 | vmid => $owner }; | |
152 | } | |
153 | }); | |
154 | ||
155 | return $lvs; | |
156 | } | |
157 | ||
158 | # Configuration | |
159 | ||
1dc01b9f DM |
160 | sub type { |
161 | return 'lvm'; | |
162 | } | |
163 | ||
164 | sub plugindata { | |
165 | return { | |
68b2c18a | 166 | content => [ {images => 1, rootdir => 1}, { images => 1 }], |
1dc01b9f DM |
167 | }; |
168 | } | |
169 | ||
170 | sub properties { | |
171 | return { | |
172 | vgname => { | |
173 | description => "Volume group name.", | |
174 | type => 'string', format => 'pve-storage-vgname', | |
175 | }, | |
176 | base => { | |
177 | description => "Base volume. This volume is automatically activated.", | |
178 | type => 'string', format => 'pve-volume-id', | |
179 | }, | |
180 | saferemove => { | |
181 | description => "Zero-out data when removing LVs.", | |
182 | type => 'boolean', | |
183 | }, | |
399ab2b6 PB |
184 | saferemove_throughput => { |
185 | description => "Wipe throughput (cstream -t parameter value).", | |
186 | type => 'string', | |
187 | }, | |
1dc01b9f DM |
188 | }; |
189 | } | |
190 | ||
191 | sub options { | |
192 | return { | |
193 | vgname => { fixed => 1 }, | |
194 | nodes => { optional => 1 }, | |
195 | shared => { optional => 1 }, | |
196 | disable => { optional => 1 }, | |
197 | saferemove => { optional => 1 }, | |
399ab2b6 | 198 | saferemove_throughput => { optional => 1 }, |
1dc01b9f DM |
199 | content => { optional => 1 }, |
200 | base => { fixed => 1, optional => 1 }, | |
201 | }; | |
202 | } | |
203 | ||
204 | # Storage implementation | |
205 | ||
206 | sub parse_volname { | |
207 | my ($class, $volname) = @_; | |
208 | ||
5dca5c7c | 209 | PVE::Storage::Plugin::parse_lvm_name($volname); |
1dc01b9f DM |
210 | |
211 | if ($volname =~ m/^(vm-(\d+)-\S+)$/) { | |
7800e84d | 212 | return ('images', $1, $2, undef, undef, undef, 'raw'); |
1dc01b9f DM |
213 | } |
214 | ||
215 | die "unable to parse lvm volume name '$volname'\n"; | |
216 | } | |
217 | ||
452e3ee7 | 218 | sub filesystem_path { |
e67069eb DM |
219 | my ($class, $scfg, $volname, $snapname) = @_; |
220 | ||
221 | die "lvm snapshot is not implemented"if defined($snapname); | |
1dc01b9f DM |
222 | |
223 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
224 | ||
225 | my $vg = $scfg->{vgname}; | |
226 | ||
227 | my $path = "/dev/$vg/$name"; | |
228 | ||
5521b580 | 229 | return wantarray ? ($path, $vmid, $vtype) : $path; |
1dc01b9f DM |
230 | } |
231 | ||
5eab0272 DM |
232 | sub create_base { |
233 | my ($class, $storeid, $scfg, $volname) = @_; | |
234 | ||
235 | die "can't create base images in lvm storage\n"; | |
236 | } | |
237 | ||
238 | sub clone_image { | |
f236eaf8 | 239 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
5eab0272 DM |
240 | |
241 | die "can't clone images in lvm storage\n"; | |
242 | } | |
243 | ||
1dc01b9f DM |
244 | sub alloc_image { |
245 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
246 | ||
247 | die "unsupported format '$fmt'" if $fmt ne 'raw'; | |
248 | ||
249 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" | |
250 | if $name && $name !~ m/^vm-$vmid-/; | |
251 | ||
252 | my $vgs = lvm_vgs(); | |
253 | ||
254 | my $vg = $scfg->{vgname}; | |
255 | ||
e8acaa3c | 256 | die "no such volume group '$vg'\n" if !defined ($vgs->{$vg}); |
1dc01b9f DM |
257 | |
258 | my $free = int($vgs->{$vg}->{free}); | |
259 | ||
260 | die "not enough free space ($free < $size)\n" if $free < $size; | |
261 | ||
262 | if (!$name) { | |
263 | my $lvs = lvm_lvs($vg); | |
264 | ||
265 | for (my $i = 1; $i < 100; $i++) { | |
266 | my $tn = "vm-$vmid-disk-$i"; | |
267 | if (!defined ($lvs->{$vg}->{$tn})) { | |
268 | $name = $tn; | |
269 | last; | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" | |
275 | if !$name; | |
276 | ||
277 | my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg]; | |
278 | ||
279 | run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error"); | |
280 | ||
281 | return $name; | |
282 | } | |
283 | ||
284 | sub free_image { | |
32437ed2 | 285 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
1dc01b9f DM |
286 | |
287 | my $vg = $scfg->{vgname}; | |
399ab2b6 | 288 | |
1dc01b9f DM |
289 | # we need to zero out LVM data for security reasons |
290 | # and to allow thin provisioning | |
291 | ||
292 | my $zero_out_worker = sub { | |
399ab2b6 PB |
293 | print "zero-out data on image $volname (/dev/$vg/del-$volname)\n"; |
294 | ||
295 | # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput | |
296 | my $throughput = '-10485760'; | |
297 | if ($scfg->{saferemove_throughput}) { | |
298 | $throughput = $scfg->{saferemove_throughput}; | |
299 | } | |
300 | ||
301 | my $cmd = [ | |
302 | '/usr/bin/cstream', | |
303 | '-i', '/dev/zero', | |
304 | '-o', "/dev/$vg/del-$volname", | |
305 | '-T', '10', | |
306 | '-v', '1', | |
307 | '-b', '1048576', | |
308 | '-t', "$throughput" | |
309 | ]; | |
310 | eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); }; | |
1dc01b9f DM |
311 | warn $@ if $@; |
312 | ||
313 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
314 | my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"]; | |
315 | run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error"); | |
316 | }); | |
399ab2b6 | 317 | print "successfully removed volume $volname ($vg/del-$volname)\n"; |
1dc01b9f DM |
318 | }; |
319 | ||
399ab2b6 PB |
320 | my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"]; |
321 | run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data"); | |
322 | ||
1dc01b9f DM |
323 | if ($scfg->{saferemove}) { |
324 | # avoid long running task, so we only rename here | |
399ab2b6 | 325 | $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"]; |
1dc01b9f DM |
326 | run_command($cmd, errmsg => "lvrename '$vg/$volname' error"); |
327 | return $zero_out_worker; | |
328 | } else { | |
329 | my $tmpvg = $scfg->{vgname}; | |
399ab2b6 | 330 | $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"]; |
1dc01b9f DM |
331 | run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error"); |
332 | } | |
333 | ||
334 | return undef; | |
335 | } | |
336 | ||
337 | sub list_images { | |
338 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
339 | ||
340 | my $vgname = $scfg->{vgname}; | |
341 | ||
342 | $cache->{lvs} = lvm_lvs() if !$cache->{lvs}; | |
343 | ||
344 | my $res = []; | |
345 | ||
346 | if (my $dat = $cache->{lvs}->{$vgname}) { | |
347 | ||
348 | foreach my $volname (keys %$dat) { | |
349 | ||
350 | my $owner = $dat->{$volname}->{vmid}; | |
351 | ||
352 | my $volid = "$storeid:$volname"; | |
353 | ||
354 | if ($vollist) { | |
355 | my $found = grep { $_ eq $volid } @$vollist; | |
356 | next if !$found; | |
357 | } else { | |
358 | next if defined ($vmid) && ($owner ne $vmid); | |
359 | } | |
360 | ||
361 | my $info = $dat->{$volname}; | |
362 | $info->{volid} = $volid; | |
363 | ||
364 | push @$res, $info; | |
365 | } | |
366 | } | |
367 | ||
368 | return $res; | |
369 | } | |
370 | ||
371 | sub status { | |
372 | my ($class, $storeid, $scfg, $cache) = @_; | |
373 | ||
374 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
375 | ||
376 | my $vgname = $scfg->{vgname}; | |
377 | ||
097a2b2f DM |
378 | if (my $info = $cache->{vgs}->{$vgname}) { |
379 | return ($info->{size}, $info->{free}, $info->{size} - $info->{free}, 1); | |
1dc01b9f DM |
380 | } |
381 | ||
382 | return undef; | |
383 | } | |
384 | ||
385 | sub activate_storage { | |
386 | my ($class, $storeid, $scfg, $cache) = @_; | |
387 | ||
388 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
389 | ||
390 | # In LVM2, vgscans take place automatically; | |
391 | # this is just to be sure | |
392 | if ($cache->{vgs} && !$cache->{vgscaned} && | |
393 | !$cache->{vgs}->{$scfg->{vgname}}) { | |
394 | $cache->{vgscaned} = 1; | |
395 | my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes']; | |
396 | eval { run_command($cmd, outfunc => sub {}); }; | |
397 | warn $@ if $@; | |
398 | } | |
399 | ||
400 | # we do not acticate any volumes here ('vgchange -aly') | |
401 | # instead, volumes are activate individually later | |
402 | } | |
403 | ||
404 | sub deactivate_storage { | |
405 | my ($class, $storeid, $scfg, $cache) = @_; | |
406 | ||
407 | my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}]; | |
408 | run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'"); | |
409 | } | |
410 | ||
411 | sub activate_volume { | |
02e797b8 | 412 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
c8943a85 | 413 | #fix me lvmchange is not provided on |
02e797b8 | 414 | my $path = $class->path($scfg, $volname, $snapname); |
1dc01b9f | 415 | |
c8943a85 | 416 | my $lvm_activate_mode = 'ey'; |
1dc01b9f DM |
417 | |
418 | my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path]; | |
419 | run_command($cmd, errmsg => "can't activate LV '$path'"); | |
420 | } | |
421 | ||
422 | sub deactivate_volume { | |
02e797b8 | 423 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f | 424 | |
02e797b8 | 425 | my $path = $class->path($scfg, $volname, $snapname); |
1dc01b9f DM |
426 | return if ! -b $path; |
427 | ||
428 | my $cmd = ['/sbin/lvchange', '-aln', $path]; | |
429 | run_command($cmd, errmsg => "can't deactivate LV '$path'"); | |
430 | } | |
431 | ||
530defb6 AD |
432 | sub volume_resize { |
433 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
434 | ||
435 | $size = ($size/1024/1024) . "M"; | |
436 | ||
437 | my $path = $class->path($scfg, $volname); | |
438 | my $cmd = ['/sbin/lvextend', '-L', $size, $path]; | |
439 | run_command($cmd, errmsg => "error resizing volume '$path'"); | |
440 | ||
441 | return 1; | |
442 | } | |
443 | ||
33818d16 | 444 | sub volume_snapshot { |
f5640e7d | 445 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
33818d16 AD |
446 | |
447 | die "lvm snapshot is not implemented"; | |
448 | } | |
449 | ||
051e85b8 AD |
450 | sub volume_snapshot_rollback { |
451 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
452 | ||
453 | die "lvm snapshot rollback is not implemented"; | |
454 | } | |
455 | ||
f57e796b AD |
456 | sub volume_snapshot_delete { |
457 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
458 | ||
459 | die "lvm snapshot delete is not implemented"; | |
460 | } | |
461 | ||
f7d4064f AD |
462 | sub volume_has_feature { |
463 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
464 | ||
9bb4abf6 AD |
465 | my $features = { |
466 | copy => { base => 1, current => 1}, | |
467 | }; | |
468 | ||
469 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
470 | $class->parse_volname($volname); | |
471 | ||
472 | my $key = undef; | |
473 | if($snapname){ | |
2c5a7097 | 474 | $key = 'snap'; |
9bb4abf6 AD |
475 | }else{ |
476 | $key = $isBase ? 'base' : 'current'; | |
477 | } | |
478 | return 1 if $features->{$feature}->{$key}; | |
479 | ||
f7d4064f AD |
480 | return undef; |
481 | } | |
482 | ||
1dc01b9f | 483 | 1; |