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