]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/LVMPlugin.pm
bump version to 6.1-2
[pve-storage.git] / PVE / Storage / LVMPlugin.pm
CommitLineData
1dc01b9f
DM
1package PVE::Storage::LVMPlugin;
2
3use strict;
4use warnings;
074b2cb4 5
1dc01b9f 6use IO::File;
074b2cb4 7
1dc01b9f
DM
8use PVE::Tools qw(run_command trim);
9use PVE::Storage::Plugin;
10use PVE::JSONSchema qw(get_standard_option);
11
12use base qw(PVE::Storage::Plugin);
13
14# lvm helper functions
15
14092a37
TL
16my $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
25sub 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
66sub 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
76sub 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
14092a37 97 run_command($cmd, errmsg => "vgcreate $vgname $device error", errfunc => $ignore_no_medium_warnings);
1dc01b9f
DM
98}
99
100sub 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 148sub 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
194sub type {
195 return 'lvm';
196}
197
198sub plugindata {
199 return {
68b2c18a 200 content => [ {images => 1, rootdir => 1}, { images => 1 }],
1dc01b9f
DM
201 };
202}
203
204sub 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
229sub 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
246sub 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
268sub 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 280sub 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
294sub create_base {
295 my ($class, $storeid, $scfg, $volname) = @_;
296
297 die "can't create base images in lvm storage\n";
298}
299
300sub 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
b1378461 306sub lvm_find_free_diskname {
c4a29df4 307 my ($lvs, $vg, $storeid, $vmid, $scfg) = @_;
b1378461 308
c4a29df4 309 my $disk_list = [ keys %{$lvs->{$vg}} ];
b1378461 310
c4a29df4 311 return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg);
b1378461
DM
312}
313
c86ba76d
DC
314sub lvcreate {
315 my ($vg, $name, $size, $tags) = @_;
316
317 if ($size =~ m/\d$/) { # no unit is given
318 $size .= "k"; # default to kilobytes
319 }
320
321 my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name];
322 for my $tag (@$tags) {
323 push @$cmd, '--addtag', $tag;
324 }
325 push @$cmd, $vg;
326
327 run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
328}
329
1dc01b9f
DM
330sub alloc_image {
331 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
332
333 die "unsupported format '$fmt'" if $fmt ne 'raw';
334
5c687bd9 335 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
1dc01b9f
DM
336 if $name && $name !~ m/^vm-$vmid-/;
337
338 my $vgs = lvm_vgs();
339
340 my $vg = $scfg->{vgname};
341
e8acaa3c 342 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
1dc01b9f
DM
343
344 my $free = int($vgs->{$vg}->{free});
345
346 die "not enough free space ($free < $size)\n" if $free < $size;
347
c4a29df4 348 $name = lvm_find_free_diskname(lvm_list_volumes($vg), $vg, $storeid, $vmid, $scfg)
1dc01b9f
DM
349 if !$name;
350
c86ba76d 351 lvcreate($vg, $name, $size, ["pve-vm-$vmid"]);
1dc01b9f
DM
352
353 return $name;
354}
355
356sub free_image {
32437ed2 357 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
1dc01b9f
DM
358
359 my $vg = $scfg->{vgname};
399ab2b6 360
1dc01b9f
DM
361 # we need to zero out LVM data for security reasons
362 # and to allow thin provisioning
363
364 my $zero_out_worker = sub {
399ab2b6
PB
365 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
366
367 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
368 my $throughput = '-10485760';
369 if ($scfg->{saferemove_throughput}) {
370 $throughput = $scfg->{saferemove_throughput};
371 }
372
373 my $cmd = [
374 '/usr/bin/cstream',
375 '-i', '/dev/zero',
376 '-o', "/dev/$vg/del-$volname",
377 '-T', '10',
378 '-v', '1',
379 '-b', '1048576',
380 '-t', "$throughput"
381 ];
382 eval { run_command($cmd, errmsg => "zero out finished (note: 'No space left on device' is ok here)"); };
1dc01b9f
DM
383 warn $@ if $@;
384
385 $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
386 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
387 run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error");
388 });
399ab2b6 389 print "successfully removed volume $volname ($vg/del-$volname)\n";
1dc01b9f
DM
390 };
391
399ab2b6
PB
392 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
393 run_command($cmd, errmsg => "can't activate LV '$vg/$volname' to zero-out its data");
628a921a
SI
394 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
395 run_command($cmd, errmsg => "can't refresh LV '$vg/$volname' to zero-out its data");
399ab2b6 396
1dc01b9f
DM
397 if ($scfg->{saferemove}) {
398 # avoid long running task, so we only rename here
399ab2b6 399 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
1dc01b9f
DM
400 run_command($cmd, errmsg => "lvrename '$vg/$volname' error");
401 return $zero_out_worker;
402 } else {
403 my $tmpvg = $scfg->{vgname};
399ab2b6 404 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
1dc01b9f
DM
405 run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error");
406 }
407
408 return undef;
409}
410
7a9dd119
FG
411my $check_tags = sub {
412 my ($tags) = @_;
413
414 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
415};
416
1dc01b9f
DM
417sub list_images {
418 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
419
420 my $vgname = $scfg->{vgname};
421
3e44cd84 422 $cache->{lvs} = lvm_list_volumes() if !$cache->{lvs};
1dc01b9f
DM
423
424 my $res = [];
5c687bd9 425
1dc01b9f
DM
426 if (my $dat = $cache->{lvs}->{$vgname}) {
427
428 foreach my $volname (keys %$dat) {
429
3e44cd84
DM
430 next if $volname !~ m/^vm-(\d+)-/;
431 my $owner = $1;
432
433 my $info = $dat->{$volname};
434
7a9dd119
FG
435 next if $scfg->{tagged_only} && !&$check_tags($info->{tags});
436
3e44cd84 437 next if $info->{lv_type} ne '-';
1dc01b9f
DM
438
439 my $volid = "$storeid:$volname";
440
441 if ($vollist) {
442 my $found = grep { $_ eq $volid } @$vollist;
443 next if !$found;
444 } else {
3e44cd84 445 next if defined($vmid) && ($owner ne $vmid);
1dc01b9f
DM
446 }
447
3e44cd84
DM
448 push @$res, {
449 volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
450 };
1dc01b9f
DM
451 }
452 }
453
454 return $res;
455}
456
457sub status {
458 my ($class, $storeid, $scfg, $cache) = @_;
459
460 $cache->{vgs} = lvm_vgs() if !$cache->{vgs};
461
462 my $vgname = $scfg->{vgname};
463
097a2b2f
DM
464 if (my $info = $cache->{vgs}->{$vgname}) {
465 return ($info->{size}, $info->{free}, $info->{size} - $info->{free}, 1);
1dc01b9f
DM
466 }
467
468 return undef;
469}
470
471sub activate_storage {
472 my ($class, $storeid, $scfg, $cache) = @_;
473
474 $cache->{vgs} = lvm_vgs() if !$cache->{vgs};
475
476 # In LVM2, vgscans take place automatically;
477 # this is just to be sure
5c687bd9 478 if ($cache->{vgs} && !$cache->{vgscaned} &&
1dc01b9f
DM
479 !$cache->{vgs}->{$scfg->{vgname}}) {
480 $cache->{vgscaned} = 1;
481 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
482 eval { run_command($cmd, outfunc => sub {}); };
483 warn $@ if $@;
484 }
485
486 # we do not acticate any volumes here ('vgchange -aly')
487 # instead, volumes are activate individually later
488}
489
490sub deactivate_storage {
491 my ($class, $storeid, $scfg, $cache) = @_;
492
493 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}];
494 run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'");
495}
496
497sub activate_volume {
02e797b8 498 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
c8943a85 499 #fix me lvmchange is not provided on
02e797b8 500 my $path = $class->path($scfg, $volname, $snapname);
1dc01b9f 501
c8943a85 502 my $lvm_activate_mode = 'ey';
1dc01b9f
DM
503
504 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
505 run_command($cmd, errmsg => "can't activate LV '$path'");
628a921a
SI
506 $cmd = ['/sbin/lvchange', '--refresh', $path];
507 run_command($cmd, errmsg => "can't refresh LV '$path' for activation");
1dc01b9f
DM
508}
509
510sub deactivate_volume {
02e797b8 511 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
1dc01b9f 512
02e797b8 513 my $path = $class->path($scfg, $volname, $snapname);
1dc01b9f
DM
514 return if ! -b $path;
515
516 my $cmd = ['/sbin/lvchange', '-aln', $path];
517 run_command($cmd, errmsg => "can't deactivate LV '$path'");
518}
519
530defb6
AD
520sub volume_resize {
521 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
522
523 $size = ($size/1024/1024) . "M";
524
525 my $path = $class->path($scfg, $volname);
526 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
49cc7802
TL
527
528 $class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
529 run_command($cmd, errmsg => "error resizing volume '$path'");
530 });
530defb6
AD
531
532 return 1;
533}
534
955c1f2c
SI
535sub volume_size_info {
536 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
537 my $path = $class->filesystem_path($scfg, $volname);
538
539 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
540 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
541
542 my $size;
543 run_command($cmd, timeout => $timeout, errmsg => "can't get size of '$path'",
544 outfunc => sub {
545 $size = int(shift);
546 });
547 return wantarray ? ($size, 'raw', 0, undef) : $size;
548}
549
33818d16 550sub volume_snapshot {
f5640e7d 551 my ($class, $scfg, $storeid, $volname, $snap) = @_;
33818d16
AD
552
553 die "lvm snapshot is not implemented";
554}
555
051e85b8
AD
556sub volume_snapshot_rollback {
557 my ($class, $scfg, $storeid, $volname, $snap) = @_;
558
559 die "lvm snapshot rollback is not implemented";
560}
561
f57e796b
AD
562sub volume_snapshot_delete {
563 my ($class, $scfg, $storeid, $volname, $snap) = @_;
564
565 die "lvm snapshot delete is not implemented";
566}
567
f7d4064f
AD
568sub volume_has_feature {
569 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
570
9bb4abf6
AD
571 my $features = {
572 copy => { base => 1, current => 1},
573 };
574
575 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
576 $class->parse_volname($volname);
577
578 my $key = undef;
579 if($snapname){
2c5a7097 580 $key = 'snap';
9bb4abf6
AD
581 }else{
582 $key = $isBase ? 'base' : 'current';
583 }
584 return 1 if $features->{$feature}->{$key};
585
f7d4064f
AD
586 return undef;
587}
588
5cbbc78f
WB
589sub volume_export_formats {
590 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
591 return () if defined($snapshot); # lvm-thin only
592 return volume_import_formats($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
593}
594
595sub volume_export {
596 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
597 die "volume export format $format not available for $class\n"
598 if $format ne 'raw+size';
599 die "cannot export volumes together with their snapshots in $class\n"
600 if $with_snapshots;
601 die "cannot export a snapshot in $class\n" if defined($snapshot);
602 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
603 my $file = $class->path($scfg, $volname, $storeid);
604 my $size;
605 # should be faster than querying LVM, also checks for the device file's availability
606 run_command(['/sbin/blockdev', '--getsize64', $file], outfunc => sub {
607 my ($line) = @_;
608 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
609 $size = int($1);
610 });
611 PVE::Storage::Plugin::write_common_header($fh, $size);
612 run_command(['dd', "if=$file", "bs=64k"], output => '>&'.fileno($fh));
613}
614
615sub volume_import_formats {
616 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
617 return () if $with_snapshots; # not supported
618 return () if defined($base_snapshot); # not supported
619 return ('raw+size');
620}
621
622sub volume_import {
623 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
624 die "volume import format $format not available for $class\n"
625 if $format ne 'raw+size';
626 die "cannot import volumes together with their snapshots in $class\n"
627 if $with_snapshots;
628 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
629
630 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
631 $class->parse_volname($volname);
632 die "cannot import format $format into a file of format $file_format\n"
633 if $file_format ne 'raw';
634
635 my $vg = $scfg->{vgname};
636 my $lvs = lvm_list_volumes($vg);
637 die "volume $vg/$volname already exists\n"
638 if $lvs->{$vg}->{$volname};
639
640 my ($size) = PVE::Storage::Plugin::read_common_header($fh);
641 $size = int($size/1024);
642
643 eval {
644 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
645 if ($allocname ne $volname) {
646 my $oldname = $volname;
647 $volname = $allocname; # Let the cleanup code know what to free
648 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
649 }
650 my $file = $class->path($scfg, $volname, $storeid)
651 or die "internal error: failed to get path to newly allocated volume $volname\n";
4e8de9ad
SI
652
653 $class->volume_import_write($fh, $file);
5cbbc78f
WB
654 };
655 if (my $err = $@) {
656 eval { $class->free_image($storeid, $scfg, $volname, 0) };
657 warn $@ if $@;
658 die $err;
659 }
660}
661
4e8de9ad
SI
662sub volume_import_write {
663 my ($class, $input_fh, $output_file) = @_;
664 run_command(['dd', "of=$output_file", 'bs=64k'],
665 input => '<&'.fileno($input_fh));
666}
667
1dc01b9f 6681;