]>
Commit | Line | Data |
---|---|---|
1dc01b9f DM |
1 | package PVE::Storage::LVMPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
074b2cb4 | 5 | |
1dc01b9f | 6 | use IO::File; |
074b2cb4 | 7 | |
1dc01b9f DM |
8 | use PVE::Tools qw(run_command trim); |
9 | use PVE::Storage::Plugin; | |
10 | use PVE::JSONSchema qw(get_standard_option); | |
11 | ||
12 | use base qw(PVE::Storage::Plugin); | |
13 | ||
14 | # lvm helper functions | |
15 | ||
14092a37 TL |
16 | my $ignore_no_medium_warnings = sub { |
17 | my $line = shift; | |
18 | # ignore those, most of the time they're from (virtual) IPMI/iKVM devices | |
19 | # and just spam the log.. | |
20 | if ($line !~ /open failed: No medium found/) { | |
21 | print STDERR "$line\n"; | |
22 | } | |
23 | }; | |
24 | ||
1dc01b9f DM |
25 | sub lvm_pv_info { |
26 | my ($device) = @_; | |
27 | ||
28 | die "no device specified" if !$device; | |
29 | ||
30 | my $has_label = 0; | |
31 | ||
32 | my $cmd = ['/usr/bin/file', '-L', '-s', $device]; | |
33 | run_command($cmd, outfunc => sub { | |
34 | my $line = shift; | |
35 | $has_label = 1 if $line =~ m/LVM2/; | |
36 | }); | |
37 | ||
38 | return undef if !$has_label; | |
39 | ||
40 | $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k', | |
41 | '--unbuffered', '--nosuffix', '--options', | |
42 | 'pv_name,pv_size,vg_name,pv_uuid', $device]; | |
43 | ||
44 | my $pvinfo; | |
45 | run_command($cmd, outfunc => sub { | |
46 | my $line = shift; | |
47 | ||
48 | $line = trim($line); | |
49 | ||
50 | my ($pvname, $size, $vgname, $uuid) = split(':', $line); | |
51 | ||
5c687bd9 | 52 | die "found multiple pvs entries for device '$device'\n" |
1dc01b9f DM |
53 | if $pvinfo; |
54 | ||
55 | $pvinfo = { | |
56 | pvname => $pvname, | |
a3f38a64 | 57 | size => int($size), |
1dc01b9f DM |
58 | vgname => $vgname, |
59 | uuid => $uuid, | |
60 | }; | |
61 | }); | |
62 | ||
63 | return $pvinfo; | |
64 | } | |
65 | ||
66 | sub clear_first_sector { | |
67 | my ($dev) = shift; | |
68 | ||
69 | if (my $fh = IO::File->new($dev, "w")) { | |
70 | my $buf = 0 x 512; | |
71 | syswrite $fh, $buf; | |
5c687bd9 | 72 | $fh->close(); |
1dc01b9f DM |
73 | } |
74 | } | |
75 | ||
76 | sub lvm_create_volume_group { | |
77 | my ($device, $vgname, $shared) = @_; | |
5c687bd9 | 78 | |
1dc01b9f | 79 | my $res = lvm_pv_info($device); |
5c687bd9 | 80 | |
1dc01b9f DM |
81 | if ($res->{vgname}) { |
82 | return if $res->{vgname} eq $vgname; # already created | |
83 | die "device '$device' is already used by volume group '$res->{vgname}'\n"; | |
84 | } | |
85 | ||
86 | clear_first_sector($device); # else pvcreate fails | |
87 | ||
88 | # we use --metadatasize 250k, which reseults in "pe_start = 512" | |
89 | # so pe_start is aligned on a 128k boundary (advantage for SSDs) | |
90 | my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device]; | |
91 | ||
92 | run_command($cmd, errmsg => "pvcreate '$device' error"); | |
93 | ||
94 | $cmd = ['/sbin/vgcreate', $vgname, $device]; | |
95 | # push @$cmd, '-c', 'y' if $shared; # we do not use this yet | |
96 | ||
14c7ede3 | 97 | run_command($cmd, errmsg => "vgcreate $vgname $device error", errfunc => $ignore_no_medium_warnings, outfunc => $ignore_no_medium_warnings); |
1dc01b9f DM |
98 | } |
99 | ||
100 | sub lvm_vgs { | |
8cccb344 | 101 | my ($includepvs) = @_; |
1dc01b9f DM |
102 | |
103 | my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b', | |
8cccb344 DC |
104 | '--unbuffered', '--nosuffix', '--options']; |
105 | ||
106 | my $cols = [qw(vg_name vg_size vg_free lv_count)]; | |
107 | ||
108 | if ($includepvs) { | |
109 | push @$cols, qw(pv_name pv_size pv_free); | |
110 | } | |
111 | ||
112 | push @$cmd, join(',', @$cols); | |
1dc01b9f DM |
113 | |
114 | my $vgs = {}; | |
115 | eval { | |
116 | run_command($cmd, outfunc => sub { | |
117 | my $line = shift; | |
1dc01b9f DM |
118 | $line = trim($line); |
119 | ||
8cccb344 DC |
120 | my ($name, $size, $free, $lvcount, $pvname, $pvsize, $pvfree) = split (':', $line); |
121 | ||
14092a37 TL |
122 | $vgs->{$name} //= { |
123 | size => int ($size), | |
124 | free => int ($free), | |
125 | lvcount => int($lvcount) | |
126 | }; | |
1dc01b9f | 127 | |
8cccb344 DC |
128 | if (defined($pvname) && defined($pvsize) && defined($pvfree)) { |
129 | push @{$vgs->{$name}->{pvs}}, { | |
130 | name => $pvname, | |
131 | size => int($pvsize), | |
132 | free => int($pvfree), | |
133 | }; | |
134 | } | |
14092a37 TL |
135 | }, |
136 | errfunc => $ignore_no_medium_warnings, | |
137 | ); | |
1dc01b9f DM |
138 | }; |
139 | my $err = $@; | |
140 | ||
141 | # just warn (vgs return error code 5 if clvmd does not run) | |
142 | # but output is still OK (list without clustered VGs) | |
143 | warn $err if $err; | |
144 | ||
145 | return $vgs; | |
146 | } | |
147 | ||
3e44cd84 | 148 | sub lvm_list_volumes { |
1dc01b9f DM |
149 | my ($vgname) = @_; |
150 | ||
151 | my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b', | |
152 | '--unbuffered', '--nosuffix', '--options', | |
2c2fd98b | 153 | 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size']; |
1dc01b9f DM |
154 | |
155 | push @$cmd, $vgname if $vgname; | |
156 | ||
157 | my $lvs = {}; | |
158 | run_command($cmd, outfunc => sub { | |
159 | my $line = shift; | |
160 | ||
161 | $line = trim($line); | |
162 | ||
2c2fd98b | 163 | my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size) = split(':', $line); |
3e44cd84 DM |
164 | return if !$vg_name; |
165 | return if !$lv_name; | |
1dc01b9f | 166 | |
3e44cd84 | 167 | my $lv_type = substr($lv_attr, 0, 1); |
1dc01b9f | 168 | |
3e44cd84 | 169 | my $d = { |
a3f38a64 | 170 | lv_size => int($lv_size), |
3e44cd84 DM |
171 | lv_type => $lv_type, |
172 | }; | |
173 | $d->{pool_lv} = $pool_lv if $pool_lv; | |
7a9dd119 | 174 | $d->{tags} = $tags if $tags; |
3e44cd84 DM |
175 | |
176 | if ($lv_type eq 't') { | |
177 | $data_percent ||= 0; | |
178 | $meta_percent ||= 0; | |
179 | $snap_percent ||= 0; | |
2c2fd98b DC |
180 | $d->{metadata_size} = int($meta_size); |
181 | $d->{metadata_used} = int(($meta_percent * $meta_size)/100); | |
faabe9e2 | 182 | $d->{used} = int(($data_percent * $lv_size)/100); |
1dc01b9f | 183 | } |
3e44cd84 | 184 | $lvs->{$vg_name}->{$lv_name} = $d; |
14092a37 TL |
185 | }, |
186 | errfunc => $ignore_no_medium_warnings, | |
187 | ); | |
1dc01b9f DM |
188 | |
189 | return $lvs; | |
190 | } | |
191 | ||
5c687bd9 | 192 | # Configuration |
1dc01b9f | 193 | |
1dc01b9f DM |
194 | sub type { |
195 | return 'lvm'; | |
196 | } | |
197 | ||
198 | sub plugindata { | |
199 | return { | |
68b2c18a | 200 | content => [ {images => 1, rootdir => 1}, { images => 1 }], |
1dc01b9f DM |
201 | }; |
202 | } | |
203 | ||
204 | sub properties { | |
205 | return { | |
206 | vgname => { | |
207 | description => "Volume group name.", | |
208 | type => 'string', format => 'pve-storage-vgname', | |
209 | }, | |
210 | base => { | |
211 | description => "Base volume. This volume is automatically activated.", | |
212 | type => 'string', format => 'pve-volume-id', | |
213 | }, | |
214 | saferemove => { | |
215 | description => "Zero-out data when removing LVs.", | |
216 | type => 'boolean', | |
217 | }, | |
399ab2b6 PB |
218 | saferemove_throughput => { |
219 | description => "Wipe throughput (cstream -t parameter value).", | |
220 | type => 'string', | |
221 | }, | |
7a9dd119 FG |
222 | tagged_only => { |
223 | description => "Only use logical volumes tagged with 'pve-vm-ID'.", | |
224 | type => 'boolean', | |
225 | } | |
1dc01b9f DM |
226 | }; |
227 | } | |
228 | ||
229 | sub options { | |
230 | return { | |
231 | vgname => { fixed => 1 }, | |
0423e8c6 | 232 | nodes => { optional => 1 }, |
1dc01b9f DM |
233 | shared => { optional => 1 }, |
234 | disable => { optional => 1 }, | |
0423e8c6 FG |
235 | saferemove => { optional => 1 }, |
236 | saferemove_throughput => { optional => 1 }, | |
1dc01b9f | 237 | content => { optional => 1 }, |
0423e8c6 | 238 | base => { fixed => 1, optional => 1 }, |
7a9dd119 | 239 | tagged_only => { optional => 1 }, |
9edb99a5 | 240 | bwlimit => { optional => 1 }, |
1dc01b9f DM |
241 | }; |
242 | } | |
243 | ||
244 | # Storage implementation | |
245 | ||
f9602323 TL |
246 | sub on_add_hook { |
247 | my ($class, $storeid, $scfg, %param) = @_; | |
248 | ||
249 | if (my $base = $scfg->{base}) { | |
250 | my ($baseid, $volname) = PVE::Storage::parse_volume_id($base); | |
251 | ||
252 | my $cfg = PVE::Storage::config(); | |
253 | my $basecfg = PVE::Storage::storage_config ($cfg, $baseid, 1); | |
254 | die "base storage ID '$baseid' does not exist\n" if !$basecfg; | |
255 | ||
256 | # we only support iscsi for now | |
257 | die "unsupported base type '$basecfg->{type}'" | |
258 | if $basecfg->{type} ne 'iscsi'; | |
259 | ||
260 | my $path = PVE::Storage::path($cfg, $base); | |
261 | ||
262 | PVE::Storage::activate_storage($cfg, $baseid); | |
263 | ||
264 | lvm_create_volume_group($path, $scfg->{vgname}, $scfg->{shared}); | |
265 | } | |
266 | } | |
267 | ||
1dc01b9f DM |
268 | sub parse_volname { |
269 | my ($class, $volname) = @_; | |
270 | ||
5dca5c7c | 271 | PVE::Storage::Plugin::parse_lvm_name($volname); |
1dc01b9f DM |
272 | |
273 | if ($volname =~ m/^(vm-(\d+)-\S+)$/) { | |
7800e84d | 274 | return ('images', $1, $2, undef, undef, undef, 'raw'); |
1dc01b9f DM |
275 | } |
276 | ||
277 | die "unable to parse lvm volume name '$volname'\n"; | |
278 | } | |
279 | ||
452e3ee7 | 280 | sub filesystem_path { |
e67069eb DM |
281 | my ($class, $scfg, $volname, $snapname) = @_; |
282 | ||
283 | die "lvm snapshot is not implemented"if defined($snapname); | |
1dc01b9f DM |
284 | |
285 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
286 | ||
287 | my $vg = $scfg->{vgname}; | |
5c687bd9 | 288 | |
1dc01b9f DM |
289 | my $path = "/dev/$vg/$name"; |
290 | ||
5521b580 | 291 | return wantarray ? ($path, $vmid, $vtype) : $path; |
1dc01b9f DM |
292 | } |
293 | ||
5eab0272 DM |
294 | sub create_base { |
295 | my ($class, $storeid, $scfg, $volname) = @_; | |
296 | ||
297 | die "can't create base images in lvm storage\n"; | |
298 | } | |
299 | ||
300 | sub clone_image { | |
f236eaf8 | 301 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
5eab0272 DM |
302 | |
303 | die "can't clone images in lvm storage\n"; | |
304 | } | |
305 | ||
a44c0147 FE |
306 | sub find_free_diskname { |
307 | my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_; | |
308 | ||
309 | my $vg = $scfg->{vgname}; | |
310 | ||
311 | my $lvs = lvm_list_volumes($vg); | |
b1378461 | 312 | |
c4a29df4 | 313 | my $disk_list = [ keys %{$lvs->{$vg}} ]; |
b1378461 | 314 | |
c4a29df4 | 315 | return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg); |
b1378461 DM |
316 | } |
317 | ||
c86ba76d DC |
318 | sub lvcreate { |
319 | my ($vg, $name, $size, $tags) = @_; | |
320 | ||
321 | if ($size =~ m/\d$/) { # no unit is given | |
322 | $size .= "k"; # default to kilobytes | |
323 | } | |
324 | ||
325 | my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name]; | |
326 | for my $tag (@$tags) { | |
327 | push @$cmd, '--addtag', $tag; | |
328 | } | |
329 | push @$cmd, $vg; | |
330 | ||
331 | run_command($cmd, errmsg => "lvcreate '$vg/$name' error"); | |
332 | } | |
333 | ||
1dc01b9f DM |
334 | sub alloc_image { |
335 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
336 | ||
337 | die "unsupported format '$fmt'" if $fmt ne 'raw'; | |
338 | ||
5c687bd9 | 339 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" |
1dc01b9f DM |
340 | if $name && $name !~ m/^vm-$vmid-/; |
341 | ||
342 | my $vgs = lvm_vgs(); | |
343 | ||
344 | my $vg = $scfg->{vgname}; | |
345 | ||
e8acaa3c | 346 | die "no such volume group '$vg'\n" if !defined ($vgs->{$vg}); |
1dc01b9f DM |
347 | |
348 | my $free = int($vgs->{$vg}->{free}); | |
349 | ||
350 | die "not enough free space ($free < $size)\n" if $free < $size; | |
351 | ||
a44c0147 | 352 | $name = $class->find_free_diskname($storeid, $scfg, $vmid) |
1dc01b9f DM |
353 | if !$name; |
354 | ||
c86ba76d | 355 | lvcreate($vg, $name, $size, ["pve-vm-$vmid"]); |
1dc01b9f DM |
356 | |
357 | return $name; | |
358 | } | |
359 | ||
360 | sub free_image { | |
32437ed2 | 361 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
1dc01b9f DM |
362 | |
363 | my $vg = $scfg->{vgname}; | |
399ab2b6 | 364 | |
1dc01b9f DM |
365 | # we need to zero out LVM data for security reasons |
366 | # and to allow thin provisioning | |
367 | ||
368 | my $zero_out_worker = sub { | |
399ab2b6 PB |
369 | print "zero-out data on image $volname (/dev/$vg/del-$volname)\n"; |
370 | ||
371 | # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput | |
372 | my $throughput = '-10485760'; | |
373 | if ($scfg->{saferemove_throughput}) { | |
374 | $throughput = $scfg->{saferemove_throughput}; | |
375 | } | |
376 | ||
377 | my $cmd = [ | |
378 | '/usr/bin/cstream', | |
379 | '-i', '/dev/zero', | |
380 | '-o', "/dev/$vg/del-$volname", | |
381 | '-T', '10', | |
382 | '-v', '1', | |
383 | '-b', '1048576', | |
384 | '-t', "$throughput" | |
385 | ]; | |
386 | eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); }; | |
1dc01b9f DM |
387 | warn $@ if $@; |
388 | ||
389 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
390 | my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"]; | |
391 | run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error"); | |
392 | }); | |
399ab2b6 | 393 | print "successfully removed volume $volname ($vg/del-$volname)\n"; |
1dc01b9f DM |
394 | }; |
395 | ||
399ab2b6 PB |
396 | my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"]; |
397 | run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data"); | |
628a921a SI |
398 | $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"]; |
399 | run_command($cmd, errmsg => "can't refresh LV '$vg/$volname' to zero-out its data"); | |
399ab2b6 | 400 | |
1dc01b9f DM |
401 | if ($scfg->{saferemove}) { |
402 | # avoid long running task, so we only rename here | |
399ab2b6 | 403 | $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"]; |
1dc01b9f DM |
404 | run_command($cmd, errmsg => "lvrename '$vg/$volname' error"); |
405 | return $zero_out_worker; | |
406 | } else { | |
407 | my $tmpvg = $scfg->{vgname}; | |
399ab2b6 | 408 | $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"]; |
1dc01b9f DM |
409 | run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error"); |
410 | } | |
411 | ||
412 | return undef; | |
413 | } | |
414 | ||
7a9dd119 FG |
415 | my $check_tags = sub { |
416 | my ($tags) = @_; | |
417 | ||
418 | return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/; | |
419 | }; | |
420 | ||
1dc01b9f DM |
421 | sub list_images { |
422 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
423 | ||
424 | my $vgname = $scfg->{vgname}; | |
425 | ||
3e44cd84 | 426 | $cache->{lvs} = lvm_list_volumes() if !$cache->{lvs}; |
1dc01b9f DM |
427 | |
428 | my $res = []; | |
5c687bd9 | 429 | |
1dc01b9f DM |
430 | if (my $dat = $cache->{lvs}->{$vgname}) { |
431 | ||
432 | foreach my $volname (keys %$dat) { | |
433 | ||
3e44cd84 DM |
434 | next if $volname !~ m/^vm-(\d+)-/; |
435 | my $owner = $1; | |
436 | ||
437 | my $info = $dat->{$volname}; | |
438 | ||
7a9dd119 FG |
439 | next if $scfg->{tagged_only} && !&$check_tags($info->{tags}); |
440 | ||
3e44cd84 | 441 | next if $info->{lv_type} ne '-'; |
1dc01b9f DM |
442 | |
443 | my $volid = "$storeid:$volname"; | |
444 | ||
445 | if ($vollist) { | |
446 | my $found = grep { $_ eq $volid } @$vollist; | |
447 | next if !$found; | |
448 | } else { | |
3e44cd84 | 449 | next if defined($vmid) && ($owner ne $vmid); |
1dc01b9f DM |
450 | } |
451 | ||
3e44cd84 DM |
452 | push @$res, { |
453 | volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner, | |
454 | }; | |
1dc01b9f DM |
455 | } |
456 | } | |
457 | ||
458 | return $res; | |
459 | } | |
460 | ||
461 | sub status { | |
462 | my ($class, $storeid, $scfg, $cache) = @_; | |
463 | ||
464 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
465 | ||
466 | my $vgname = $scfg->{vgname}; | |
467 | ||
097a2b2f DM |
468 | if (my $info = $cache->{vgs}->{$vgname}) { |
469 | return ($info->{size}, $info->{free}, $info->{size} - $info->{free}, 1); | |
1dc01b9f DM |
470 | } |
471 | ||
472 | return undef; | |
473 | } | |
474 | ||
475 | sub activate_storage { | |
476 | my ($class, $storeid, $scfg, $cache) = @_; | |
477 | ||
478 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
479 | ||
480 | # In LVM2, vgscans take place automatically; | |
481 | # this is just to be sure | |
5c687bd9 | 482 | if ($cache->{vgs} && !$cache->{vgscaned} && |
1dc01b9f DM |
483 | !$cache->{vgs}->{$scfg->{vgname}}) { |
484 | $cache->{vgscaned} = 1; | |
485 | my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes']; | |
486 | eval { run_command($cmd, outfunc => sub {}); }; | |
487 | warn $@ if $@; | |
488 | } | |
489 | ||
490 | # we do not acticate any volumes here ('vgchange -aly') | |
491 | # instead, volumes are activate individually later | |
492 | } | |
493 | ||
494 | sub deactivate_storage { | |
495 | my ($class, $storeid, $scfg, $cache) = @_; | |
496 | ||
497 | my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}]; | |
498 | run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'"); | |
499 | } | |
500 | ||
501 | sub activate_volume { | |
02e797b8 | 502 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
c8943a85 | 503 | #fix me lvmchange is not provided on |
02e797b8 | 504 | my $path = $class->path($scfg, $volname, $snapname); |
1dc01b9f | 505 | |
c8943a85 | 506 | my $lvm_activate_mode = 'ey'; |
1dc01b9f DM |
507 | |
508 | my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path]; | |
509 | run_command($cmd, errmsg => "can't activate LV '$path'"); | |
628a921a SI |
510 | $cmd = ['/sbin/lvchange', '--refresh', $path]; |
511 | run_command($cmd, errmsg => "can't refresh LV '$path' for activation"); | |
1dc01b9f DM |
512 | } |
513 | ||
514 | sub deactivate_volume { | |
02e797b8 | 515 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f | 516 | |
02e797b8 | 517 | my $path = $class->path($scfg, $volname, $snapname); |
1dc01b9f DM |
518 | return if ! -b $path; |
519 | ||
520 | my $cmd = ['/sbin/lvchange', '-aln', $path]; | |
521 | run_command($cmd, errmsg => "can't deactivate LV '$path'"); | |
522 | } | |
523 | ||
530defb6 AD |
524 | sub volume_resize { |
525 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
526 | ||
527 | $size = ($size/1024/1024) . "M"; | |
528 | ||
529 | my $path = $class->path($scfg, $volname); | |
530 | my $cmd = ['/sbin/lvextend', '-L', $size, $path]; | |
49cc7802 TL |
531 | |
532 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
533 | run_command($cmd, errmsg => "error resizing volume '$path'"); | |
534 | }); | |
530defb6 AD |
535 | |
536 | return 1; | |
537 | } | |
538 | ||
955c1f2c SI |
539 | sub volume_size_info { |
540 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
541 | my $path = $class->filesystem_path($scfg, $volname); | |
542 | ||
543 | my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b', | |
544 | '--unbuffered', '--nosuffix', '--options', 'lv_size', $path]; | |
545 | ||
546 | my $size; | |
547 | run_command($cmd, timeout => $timeout, errmsg => "can't get size of '$path'", | |
548 | outfunc => sub { | |
549 | $size = int(shift); | |
550 | }); | |
551 | return wantarray ? ($size, 'raw', 0, undef) : $size; | |
552 | } | |
553 | ||
33818d16 | 554 | sub volume_snapshot { |
f5640e7d | 555 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
33818d16 AD |
556 | |
557 | die "lvm snapshot is not implemented"; | |
558 | } | |
559 | ||
051e85b8 AD |
560 | sub volume_snapshot_rollback { |
561 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
562 | ||
563 | die "lvm snapshot rollback is not implemented"; | |
564 | } | |
565 | ||
f57e796b AD |
566 | sub volume_snapshot_delete { |
567 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
568 | ||
569 | die "lvm snapshot delete is not implemented"; | |
570 | } | |
571 | ||
f7d4064f AD |
572 | sub volume_has_feature { |
573 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
574 | ||
9bb4abf6 AD |
575 | my $features = { |
576 | copy => { base => 1, current => 1}, | |
577 | }; | |
578 | ||
579 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
580 | $class->parse_volname($volname); | |
581 | ||
582 | my $key = undef; | |
583 | if($snapname){ | |
2c5a7097 | 584 | $key = 'snap'; |
9bb4abf6 AD |
585 | }else{ |
586 | $key = $isBase ? 'base' : 'current'; | |
587 | } | |
588 | return 1 if $features->{$feature}->{$key}; | |
589 | ||
f7d4064f AD |
590 | return undef; |
591 | } | |
592 | ||
5cbbc78f WB |
593 | sub volume_export_formats { |
594 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
595 | return () if defined($snapshot); # lvm-thin only | |
596 | return volume_import_formats($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots); | |
597 | } | |
598 | ||
599 | sub volume_export { | |
600 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
601 | die "volume export format $format not available for $class\n" | |
602 | if $format ne 'raw+size'; | |
603 | die "cannot export volumes together with their snapshots in $class\n" | |
604 | if $with_snapshots; | |
605 | die "cannot export a snapshot in $class\n" if defined($snapshot); | |
606 | die "cannot export an incremental stream in $class\n" if defined($base_snapshot); | |
607 | my $file = $class->path($scfg, $volname, $storeid); | |
608 | my $size; | |
609 | # should be faster than querying LVM, also checks for the device file's availability | |
610 | run_command(['/sbin/blockdev', '--getsize64', $file], outfunc => sub { | |
611 | my ($line) = @_; | |
612 | die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/; | |
613 | $size = int($1); | |
614 | }); | |
615 | PVE::Storage::Plugin::write_common_header($fh, $size); | |
616 | run_command(['dd', "if=$file", "bs=64k"], output => '>&'.fileno($fh)); | |
617 | } | |
618 | ||
619 | sub volume_import_formats { | |
620 | my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_; | |
621 | return () if $with_snapshots; # not supported | |
622 | return () if defined($base_snapshot); # not supported | |
623 | return ('raw+size'); | |
624 | } | |
625 | ||
626 | sub volume_import { | |
627 | my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_; | |
628 | die "volume import format $format not available for $class\n" | |
629 | if $format ne 'raw+size'; | |
630 | die "cannot import volumes together with their snapshots in $class\n" | |
631 | if $with_snapshots; | |
632 | die "cannot import an incremental stream in $class\n" if defined($base_snapshot); | |
633 | ||
634 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) = | |
635 | $class->parse_volname($volname); | |
636 | die "cannot import format $format into a file of format $file_format\n" | |
637 | if $file_format ne 'raw'; | |
638 | ||
639 | my $vg = $scfg->{vgname}; | |
640 | my $lvs = lvm_list_volumes($vg); | |
641 | die "volume $vg/$volname already exists\n" | |
642 | if $lvs->{$vg}->{$volname}; | |
643 | ||
644 | my ($size) = PVE::Storage::Plugin::read_common_header($fh); | |
645 | $size = int($size/1024); | |
646 | ||
647 | eval { | |
648 | my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size); | |
649 | if ($allocname ne $volname) { | |
650 | my $oldname = $volname; | |
651 | $volname = $allocname; # Let the cleanup code know what to free | |
652 | die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n"; | |
653 | } | |
654 | my $file = $class->path($scfg, $volname, $storeid) | |
655 | or die "internal error: failed to get path to newly allocated volume $volname\n"; | |
4e8de9ad SI |
656 | |
657 | $class->volume_import_write($fh, $file); | |
5cbbc78f WB |
658 | }; |
659 | if (my $err = $@) { | |
660 | eval { $class->free_image($storeid, $scfg, $volname, 0) }; | |
661 | warn $@ if $@; | |
662 | die $err; | |
663 | } | |
664 | } | |
665 | ||
4e8de9ad SI |
666 | sub volume_import_write { |
667 | my ($class, $input_fh, $output_file) = @_; | |
668 | run_command(['dd', "of=$output_file", 'bs=64k'], | |
669 | input => '<&'.fileno($input_fh)); | |
670 | } | |
671 | ||
1dc01b9f | 672 | 1; |