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