]>
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 | ||
d65590d1 DM |
151 | my $option_list = 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size,time'; |
152 | ||
153 | my $cmd = [ | |
154 | '/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b', | |
155 | '--unbuffered', '--nosuffix', | |
156 | '--config', 'report/time_format="%s"', | |
157 | '--options', $option_list, | |
158 | ]; | |
1dc01b9f DM |
159 | |
160 | push @$cmd, $vgname if $vgname; | |
161 | ||
162 | my $lvs = {}; | |
163 | run_command($cmd, outfunc => sub { | |
164 | my $line = shift; | |
165 | ||
166 | $line = trim($line); | |
167 | ||
d65590d1 | 168 | my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size, $ctime) = split(':', $line); |
3e44cd84 DM |
169 | return if !$vg_name; |
170 | return if !$lv_name; | |
1dc01b9f | 171 | |
3e44cd84 | 172 | my $lv_type = substr($lv_attr, 0, 1); |
1dc01b9f | 173 | |
3e44cd84 | 174 | my $d = { |
a3f38a64 | 175 | lv_size => int($lv_size), |
3e44cd84 DM |
176 | lv_type => $lv_type, |
177 | }; | |
178 | $d->{pool_lv} = $pool_lv if $pool_lv; | |
7a9dd119 | 179 | $d->{tags} = $tags if $tags; |
d65590d1 | 180 | $d->{ctime} = $ctime; |
3e44cd84 DM |
181 | |
182 | if ($lv_type eq 't') { | |
183 | $data_percent ||= 0; | |
184 | $meta_percent ||= 0; | |
185 | $snap_percent ||= 0; | |
2c2fd98b DC |
186 | $d->{metadata_size} = int($meta_size); |
187 | $d->{metadata_used} = int(($meta_percent * $meta_size)/100); | |
faabe9e2 | 188 | $d->{used} = int(($data_percent * $lv_size)/100); |
1dc01b9f | 189 | } |
3e44cd84 | 190 | $lvs->{$vg_name}->{$lv_name} = $d; |
14092a37 TL |
191 | }, |
192 | errfunc => $ignore_no_medium_warnings, | |
193 | ); | |
1dc01b9f DM |
194 | |
195 | return $lvs; | |
196 | } | |
197 | ||
5c687bd9 | 198 | # Configuration |
1dc01b9f | 199 | |
1dc01b9f DM |
200 | sub type { |
201 | return 'lvm'; | |
202 | } | |
203 | ||
204 | sub plugindata { | |
205 | return { | |
68b2c18a | 206 | content => [ {images => 1, rootdir => 1}, { images => 1 }], |
1dc01b9f DM |
207 | }; |
208 | } | |
209 | ||
210 | sub properties { | |
211 | return { | |
212 | vgname => { | |
213 | description => "Volume group name.", | |
214 | type => 'string', format => 'pve-storage-vgname', | |
215 | }, | |
216 | base => { | |
217 | description => "Base volume. This volume is automatically activated.", | |
218 | type => 'string', format => 'pve-volume-id', | |
219 | }, | |
220 | saferemove => { | |
221 | description => "Zero-out data when removing LVs.", | |
222 | type => 'boolean', | |
223 | }, | |
399ab2b6 PB |
224 | saferemove_throughput => { |
225 | description => "Wipe throughput (cstream -t parameter value).", | |
226 | type => 'string', | |
227 | }, | |
7a9dd119 FG |
228 | tagged_only => { |
229 | description => "Only use logical volumes tagged with 'pve-vm-ID'.", | |
230 | type => 'boolean', | |
231 | } | |
1dc01b9f DM |
232 | }; |
233 | } | |
234 | ||
235 | sub options { | |
236 | return { | |
237 | vgname => { fixed => 1 }, | |
0423e8c6 | 238 | nodes => { optional => 1 }, |
1dc01b9f DM |
239 | shared => { optional => 1 }, |
240 | disable => { optional => 1 }, | |
0423e8c6 FG |
241 | saferemove => { optional => 1 }, |
242 | saferemove_throughput => { optional => 1 }, | |
1dc01b9f | 243 | content => { optional => 1 }, |
0423e8c6 | 244 | base => { fixed => 1, optional => 1 }, |
7a9dd119 | 245 | tagged_only => { optional => 1 }, |
9edb99a5 | 246 | bwlimit => { optional => 1 }, |
1dc01b9f DM |
247 | }; |
248 | } | |
249 | ||
250 | # Storage implementation | |
251 | ||
f9602323 TL |
252 | sub on_add_hook { |
253 | my ($class, $storeid, $scfg, %param) = @_; | |
254 | ||
255 | if (my $base = $scfg->{base}) { | |
256 | my ($baseid, $volname) = PVE::Storage::parse_volume_id($base); | |
257 | ||
258 | my $cfg = PVE::Storage::config(); | |
259 | my $basecfg = PVE::Storage::storage_config ($cfg, $baseid, 1); | |
260 | die "base storage ID '$baseid' does not exist\n" if !$basecfg; | |
261 | ||
262 | # we only support iscsi for now | |
263 | die "unsupported base type '$basecfg->{type}'" | |
264 | if $basecfg->{type} ne 'iscsi'; | |
265 | ||
266 | my $path = PVE::Storage::path($cfg, $base); | |
267 | ||
268 | PVE::Storage::activate_storage($cfg, $baseid); | |
269 | ||
270 | lvm_create_volume_group($path, $scfg->{vgname}, $scfg->{shared}); | |
271 | } | |
f3ccd0ef FE |
272 | |
273 | return; | |
f9602323 TL |
274 | } |
275 | ||
1dc01b9f DM |
276 | sub parse_volname { |
277 | my ($class, $volname) = @_; | |
278 | ||
5dca5c7c | 279 | PVE::Storage::Plugin::parse_lvm_name($volname); |
1dc01b9f DM |
280 | |
281 | if ($volname =~ m/^(vm-(\d+)-\S+)$/) { | |
7800e84d | 282 | return ('images', $1, $2, undef, undef, undef, 'raw'); |
1dc01b9f DM |
283 | } |
284 | ||
285 | die "unable to parse lvm volume name '$volname'\n"; | |
286 | } | |
287 | ||
452e3ee7 | 288 | sub filesystem_path { |
e67069eb DM |
289 | my ($class, $scfg, $volname, $snapname) = @_; |
290 | ||
291 | die "lvm snapshot is not implemented"if defined($snapname); | |
1dc01b9f DM |
292 | |
293 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
294 | ||
295 | my $vg = $scfg->{vgname}; | |
5c687bd9 | 296 | |
1dc01b9f DM |
297 | my $path = "/dev/$vg/$name"; |
298 | ||
5521b580 | 299 | return wantarray ? ($path, $vmid, $vtype) : $path; |
1dc01b9f DM |
300 | } |
301 | ||
5eab0272 DM |
302 | sub create_base { |
303 | my ($class, $storeid, $scfg, $volname) = @_; | |
304 | ||
305 | die "can't create base images in lvm storage\n"; | |
306 | } | |
307 | ||
308 | sub clone_image { | |
f236eaf8 | 309 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
5eab0272 DM |
310 | |
311 | die "can't clone images in lvm storage\n"; | |
312 | } | |
313 | ||
a44c0147 FE |
314 | sub find_free_diskname { |
315 | my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_; | |
316 | ||
317 | my $vg = $scfg->{vgname}; | |
318 | ||
319 | my $lvs = lvm_list_volumes($vg); | |
b1378461 | 320 | |
c4a29df4 | 321 | my $disk_list = [ keys %{$lvs->{$vg}} ]; |
b1378461 | 322 | |
c4a29df4 | 323 | return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg); |
b1378461 DM |
324 | } |
325 | ||
c86ba76d DC |
326 | sub lvcreate { |
327 | my ($vg, $name, $size, $tags) = @_; | |
328 | ||
329 | if ($size =~ m/\d$/) { # no unit is given | |
330 | $size .= "k"; # default to kilobytes | |
331 | } | |
332 | ||
333 | my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name]; | |
334 | for my $tag (@$tags) { | |
335 | push @$cmd, '--addtag', $tag; | |
336 | } | |
337 | push @$cmd, $vg; | |
338 | ||
339 | run_command($cmd, errmsg => "lvcreate '$vg/$name' error"); | |
340 | } | |
341 | ||
1dc01b9f DM |
342 | sub alloc_image { |
343 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
344 | ||
345 | die "unsupported format '$fmt'" if $fmt ne 'raw'; | |
346 | ||
ffc31266 | 347 | die "illegal name '$name' - should be 'vm-$vmid-*'\n" |
1dc01b9f DM |
348 | if $name && $name !~ m/^vm-$vmid-/; |
349 | ||
350 | my $vgs = lvm_vgs(); | |
351 | ||
352 | my $vg = $scfg->{vgname}; | |
353 | ||
e8acaa3c | 354 | die "no such volume group '$vg'\n" if !defined ($vgs->{$vg}); |
1dc01b9f DM |
355 | |
356 | my $free = int($vgs->{$vg}->{free}); | |
357 | ||
358 | die "not enough free space ($free < $size)\n" if $free < $size; | |
359 | ||
a44c0147 | 360 | $name = $class->find_free_diskname($storeid, $scfg, $vmid) |
1dc01b9f DM |
361 | if !$name; |
362 | ||
c86ba76d | 363 | lvcreate($vg, $name, $size, ["pve-vm-$vmid"]); |
1dc01b9f DM |
364 | |
365 | return $name; | |
366 | } | |
367 | ||
368 | sub free_image { | |
32437ed2 | 369 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; |
1dc01b9f DM |
370 | |
371 | my $vg = $scfg->{vgname}; | |
399ab2b6 | 372 | |
1dc01b9f DM |
373 | # we need to zero out LVM data for security reasons |
374 | # and to allow thin provisioning | |
375 | ||
376 | my $zero_out_worker = sub { | |
399ab2b6 PB |
377 | print "zero-out data on image $volname (/dev/$vg/del-$volname)\n"; |
378 | ||
379 | # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput | |
380 | my $throughput = '-10485760'; | |
381 | if ($scfg->{saferemove_throughput}) { | |
382 | $throughput = $scfg->{saferemove_throughput}; | |
383 | } | |
384 | ||
385 | my $cmd = [ | |
386 | '/usr/bin/cstream', | |
387 | '-i', '/dev/zero', | |
388 | '-o', "/dev/$vg/del-$volname", | |
389 | '-T', '10', | |
390 | '-v', '1', | |
391 | '-b', '1048576', | |
392 | '-t', "$throughput" | |
393 | ]; | |
394 | eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); }; | |
1dc01b9f DM |
395 | warn $@ if $@; |
396 | ||
397 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
398 | my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"]; | |
399 | run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error"); | |
400 | }); | |
399ab2b6 | 401 | print "successfully removed volume $volname ($vg/del-$volname)\n"; |
1dc01b9f DM |
402 | }; |
403 | ||
399ab2b6 PB |
404 | my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"]; |
405 | run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data"); | |
628a921a SI |
406 | $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"]; |
407 | run_command($cmd, errmsg => "can't refresh LV '$vg/$volname' to zero-out its data"); | |
399ab2b6 | 408 | |
1dc01b9f DM |
409 | if ($scfg->{saferemove}) { |
410 | # avoid long running task, so we only rename here | |
399ab2b6 | 411 | $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"]; |
1dc01b9f DM |
412 | run_command($cmd, errmsg => "lvrename '$vg/$volname' error"); |
413 | return $zero_out_worker; | |
414 | } else { | |
415 | my $tmpvg = $scfg->{vgname}; | |
399ab2b6 | 416 | $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"]; |
1dc01b9f DM |
417 | run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error"); |
418 | } | |
419 | ||
420 | return undef; | |
421 | } | |
422 | ||
7a9dd119 FG |
423 | my $check_tags = sub { |
424 | my ($tags) = @_; | |
425 | ||
426 | return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/; | |
427 | }; | |
428 | ||
1dc01b9f DM |
429 | sub list_images { |
430 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
431 | ||
432 | my $vgname = $scfg->{vgname}; | |
433 | ||
3e44cd84 | 434 | $cache->{lvs} = lvm_list_volumes() if !$cache->{lvs}; |
1dc01b9f DM |
435 | |
436 | my $res = []; | |
5c687bd9 | 437 | |
1dc01b9f DM |
438 | if (my $dat = $cache->{lvs}->{$vgname}) { |
439 | ||
440 | foreach my $volname (keys %$dat) { | |
441 | ||
3e44cd84 DM |
442 | next if $volname !~ m/^vm-(\d+)-/; |
443 | my $owner = $1; | |
444 | ||
445 | my $info = $dat->{$volname}; | |
446 | ||
7a9dd119 FG |
447 | next if $scfg->{tagged_only} && !&$check_tags($info->{tags}); |
448 | ||
d854a718 DJ |
449 | # Allow mirrored and RAID LVs |
450 | next if $info->{lv_type} !~ m/^[-mMrR]$/; | |
1dc01b9f DM |
451 | |
452 | my $volid = "$storeid:$volname"; | |
453 | ||
454 | if ($vollist) { | |
455 | my $found = grep { $_ eq $volid } @$vollist; | |
456 | next if !$found; | |
457 | } else { | |
3e44cd84 | 458 | next if defined($vmid) && ($owner ne $vmid); |
1dc01b9f DM |
459 | } |
460 | ||
3e44cd84 DM |
461 | push @$res, { |
462 | volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner, | |
d65590d1 | 463 | ctime => $info->{ctime}, |
3e44cd84 | 464 | }; |
1dc01b9f DM |
465 | } |
466 | } | |
467 | ||
468 | return $res; | |
469 | } | |
470 | ||
471 | sub status { | |
472 | my ($class, $storeid, $scfg, $cache) = @_; | |
473 | ||
474 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
475 | ||
476 | my $vgname = $scfg->{vgname}; | |
477 | ||
097a2b2f DM |
478 | if (my $info = $cache->{vgs}->{$vgname}) { |
479 | return ($info->{size}, $info->{free}, $info->{size} - $info->{free}, 1); | |
1dc01b9f DM |
480 | } |
481 | ||
482 | return undef; | |
483 | } | |
484 | ||
485 | sub activate_storage { | |
486 | my ($class, $storeid, $scfg, $cache) = @_; | |
487 | ||
488 | $cache->{vgs} = lvm_vgs() if !$cache->{vgs}; | |
489 | ||
490 | # In LVM2, vgscans take place automatically; | |
491 | # this is just to be sure | |
5c687bd9 | 492 | if ($cache->{vgs} && !$cache->{vgscaned} && |
1dc01b9f DM |
493 | !$cache->{vgs}->{$scfg->{vgname}}) { |
494 | $cache->{vgscaned} = 1; | |
495 | my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes']; | |
496 | eval { run_command($cmd, outfunc => sub {}); }; | |
497 | warn $@ if $@; | |
498 | } | |
499 | ||
500 | # we do not acticate any volumes here ('vgchange -aly') | |
501 | # instead, volumes are activate individually later | |
502 | } | |
503 | ||
504 | sub deactivate_storage { | |
505 | my ($class, $storeid, $scfg, $cache) = @_; | |
506 | ||
507 | my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}]; | |
508 | run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'"); | |
509 | } | |
510 | ||
511 | sub activate_volume { | |
02e797b8 | 512 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
c8943a85 | 513 | #fix me lvmchange is not provided on |
02e797b8 | 514 | my $path = $class->path($scfg, $volname, $snapname); |
1dc01b9f | 515 | |
c8943a85 | 516 | my $lvm_activate_mode = 'ey'; |
1dc01b9f DM |
517 | |
518 | my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path]; | |
519 | run_command($cmd, errmsg => "can't activate LV '$path'"); | |
628a921a SI |
520 | $cmd = ['/sbin/lvchange', '--refresh', $path]; |
521 | run_command($cmd, errmsg => "can't refresh LV '$path' for activation"); | |
1dc01b9f DM |
522 | } |
523 | ||
524 | sub deactivate_volume { | |
02e797b8 | 525 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; |
1dc01b9f | 526 | |
02e797b8 | 527 | my $path = $class->path($scfg, $volname, $snapname); |
1dc01b9f DM |
528 | return if ! -b $path; |
529 | ||
530 | my $cmd = ['/sbin/lvchange', '-aln', $path]; | |
531 | run_command($cmd, errmsg => "can't deactivate LV '$path'"); | |
532 | } | |
533 | ||
530defb6 AD |
534 | sub volume_resize { |
535 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
536 | ||
537 | $size = ($size/1024/1024) . "M"; | |
538 | ||
539 | my $path = $class->path($scfg, $volname); | |
540 | my $cmd = ['/sbin/lvextend', '-L', $size, $path]; | |
49cc7802 TL |
541 | |
542 | $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub { | |
543 | run_command($cmd, errmsg => "error resizing volume '$path'"); | |
544 | }); | |
530defb6 AD |
545 | |
546 | return 1; | |
547 | } | |
548 | ||
955c1f2c SI |
549 | sub volume_size_info { |
550 | my ($class, $scfg, $storeid, $volname, $timeout) = @_; | |
551 | my $path = $class->filesystem_path($scfg, $volname); | |
552 | ||
553 | my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b', | |
554 | '--unbuffered', '--nosuffix', '--options', 'lv_size', $path]; | |
555 | ||
556 | my $size; | |
557 | run_command($cmd, timeout => $timeout, errmsg => "can't get size of '$path'", | |
558 | outfunc => sub { | |
559 | $size = int(shift); | |
560 | }); | |
561 | return wantarray ? ($size, 'raw', 0, undef) : $size; | |
562 | } | |
563 | ||
33818d16 | 564 | sub volume_snapshot { |
f5640e7d | 565 | my ($class, $scfg, $storeid, $volname, $snap) = @_; |
33818d16 AD |
566 | |
567 | die "lvm snapshot is not implemented"; | |
568 | } | |
569 | ||
051e85b8 AD |
570 | sub volume_snapshot_rollback { |
571 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
572 | ||
573 | die "lvm snapshot rollback is not implemented"; | |
574 | } | |
575 | ||
f57e796b AD |
576 | sub volume_snapshot_delete { |
577 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
578 | ||
579 | die "lvm snapshot delete is not implemented"; | |
580 | } | |
581 | ||
f7d4064f AD |
582 | sub volume_has_feature { |
583 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
584 | ||
9bb4abf6 AD |
585 | my $features = { |
586 | copy => { base => 1, current => 1}, | |
587 | }; | |
588 | ||
589 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
590 | $class->parse_volname($volname); | |
591 | ||
592 | my $key = undef; | |
593 | if($snapname){ | |
2c5a7097 | 594 | $key = 'snap'; |
9bb4abf6 AD |
595 | }else{ |
596 | $key = $isBase ? 'base' : 'current'; | |
597 | } | |
598 | return 1 if $features->{$feature}->{$key}; | |
599 | ||
f7d4064f AD |
600 | return undef; |
601 | } | |
602 | ||
5cbbc78f WB |
603 | sub volume_export_formats { |
604 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
605 | return () if defined($snapshot); # lvm-thin only | |
3cc29a04 | 606 | return volume_import_formats($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots); |
5cbbc78f WB |
607 | } |
608 | ||
609 | sub volume_export { | |
610 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_; | |
611 | die "volume export format $format not available for $class\n" | |
612 | if $format ne 'raw+size'; | |
613 | die "cannot export volumes together with their snapshots in $class\n" | |
614 | if $with_snapshots; | |
615 | die "cannot export a snapshot in $class\n" if defined($snapshot); | |
616 | die "cannot export an incremental stream in $class\n" if defined($base_snapshot); | |
617 | my $file = $class->path($scfg, $volname, $storeid); | |
618 | my $size; | |
619 | # should be faster than querying LVM, also checks for the device file's availability | |
620 | run_command(['/sbin/blockdev', '--getsize64', $file], outfunc => sub { | |
621 | my ($line) = @_; | |
622 | die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/; | |
623 | $size = int($1); | |
624 | }); | |
625 | PVE::Storage::Plugin::write_common_header($fh, $size); | |
626 | run_command(['dd', "if=$file", "bs=64k"], output => '>&'.fileno($fh)); | |
627 | } | |
628 | ||
629 | sub volume_import_formats { | |
3cc29a04 | 630 | my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_; |
5cbbc78f WB |
631 | return () if $with_snapshots; # not supported |
632 | return () if defined($base_snapshot); # not supported | |
633 | return ('raw+size'); | |
634 | } | |
635 | ||
636 | sub volume_import { | |
3cc29a04 | 637 | my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots, $allow_rename) = @_; |
5cbbc78f WB |
638 | die "volume import format $format not available for $class\n" |
639 | if $format ne 'raw+size'; | |
640 | die "cannot import volumes together with their snapshots in $class\n" | |
641 | if $with_snapshots; | |
642 | die "cannot import an incremental stream in $class\n" if defined($base_snapshot); | |
643 | ||
644 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) = | |
645 | $class->parse_volname($volname); | |
646 | die "cannot import format $format into a file of format $file_format\n" | |
647 | if $file_format ne 'raw'; | |
648 | ||
649 | my $vg = $scfg->{vgname}; | |
650 | my $lvs = lvm_list_volumes($vg); | |
a97d3ee4 FE |
651 | if ($lvs->{$vg}->{$volname}) { |
652 | die "volume $vg/$volname already exists\n" if !$allow_rename; | |
653 | warn "volume $vg/$volname already exists - importing with a different name\n"; | |
654 | $name = undef; | |
655 | } | |
5cbbc78f WB |
656 | |
657 | my ($size) = PVE::Storage::Plugin::read_common_header($fh); | |
658 | $size = int($size/1024); | |
659 | ||
660 | eval { | |
661 | my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size); | |
a97d3ee4 FE |
662 | my $oldname = $volname; |
663 | $volname = $allocname; | |
664 | if (defined($name) && $allocname ne $oldname) { | |
5cbbc78f WB |
665 | die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n"; |
666 | } | |
667 | my $file = $class->path($scfg, $volname, $storeid) | |
668 | or die "internal error: failed to get path to newly allocated volume $volname\n"; | |
4e8de9ad SI |
669 | |
670 | $class->volume_import_write($fh, $file); | |
5cbbc78f WB |
671 | }; |
672 | if (my $err = $@) { | |
6a454560 | 673 | my $cleanup_worker = eval { $class->free_image($storeid, $scfg, $volname, 0) }; |
5cbbc78f | 674 | warn $@ if $@; |
6a454560 FE |
675 | |
676 | if ($cleanup_worker) { | |
677 | my $rpcenv = PVE::RPCEnvironment::get(); | |
678 | my $authuser = $rpcenv->get_user(); | |
679 | ||
680 | $rpcenv->fork_worker('imgdel', undef, $authuser, $cleanup_worker); | |
681 | } | |
682 | ||
5cbbc78f WB |
683 | die $err; |
684 | } | |
a97d3ee4 FE |
685 | |
686 | return "$storeid:$volname"; | |
5cbbc78f WB |
687 | } |
688 | ||
4e8de9ad SI |
689 | sub volume_import_write { |
690 | my ($class, $input_fh, $output_file) = @_; | |
691 | run_command(['dd', "of=$output_file", 'bs=64k'], | |
692 | input => '<&'.fileno($input_fh)); | |
693 | } | |
694 | ||
1dc01b9f | 695 | 1; |