]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
1 package PVE
::Storage
::LVMPlugin
;
8 use PVE
::Tools
qw(run_command trim);
9 use PVE
::Storage
::Plugin
;
10 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::Storage::Plugin);
14 # lvm helper functions
19 die "no device specified" if !$device;
23 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
24 run_command
($cmd, outfunc
=> sub {
26 $has_label = 1 if $line =~ m/LVM2/;
29 return undef if !$has_label;
31 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
32 '--unbuffered', '--nosuffix', '--options',
33 'pv_name,pv_size,vg_name,pv_uuid', $device];
36 run_command
($cmd, outfunc
=> sub {
41 my ($pvname, $size, $vgname, $uuid) = split(':', $line);
43 die "found multiple pvs entries for device '$device'\n"
57 sub clear_first_sector
{
60 if (my $fh = IO
::File-
>new($dev, "w")) {
67 sub lvm_create_volume_group
{
68 my ($device, $vgname, $shared) = @_;
70 my $res = lvm_pv_info
($device);
73 return if $res->{vgname
} eq $vgname; # already created
74 die "device '$device' is already used by volume group '$res->{vgname}'\n";
77 clear_first_sector
($device); # else pvcreate fails
79 # we use --metadatasize 250k, which reseults in "pe_start = 512"
80 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
81 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
83 run_command
($cmd, errmsg
=> "pvcreate '$device' error");
85 $cmd = ['/sbin/vgcreate', $vgname, $device];
86 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
88 run_command
($cmd, errmsg
=> "vgcreate $vgname $device error");
92 my ($includepvs) = @_;
94 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
95 '--unbuffered', '--nosuffix', '--options'];
97 my $cols = [qw(vg_name vg_size vg_free lv_count)];
100 push @$cols, qw(pv_name pv_size pv_free);
103 push @$cmd, join(',', @$cols);
107 run_command
($cmd, outfunc
=> sub {
112 my ($name, $size, $free, $lvcount, $pvname, $pvsize, $pvfree) = split (':', $line);
114 $vgs->{$name} = { size
=> int ($size), free
=> int ($free), lvcount
=> int($lvcount) }
117 if (defined($pvname) && defined($pvsize) && defined($pvfree)) {
118 push @{$vgs->{$name}->{pvs
}}, {
120 size
=> int($pvsize),
121 free
=> int($pvfree),
128 # just warn (vgs return error code 5 if clvmd does not run)
129 # but output is still OK (list without clustered VGs)
135 sub lvm_list_volumes
{
138 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
139 '--unbuffered', '--nosuffix', '--options',
140 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags,metadata_size'];
142 push @$cmd, $vgname if $vgname;
145 run_command
($cmd, outfunc
=> sub {
150 my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags, $meta_size) = split(':', $line);
154 my $lv_type = substr($lv_attr, 0, 1);
157 lv_size
=> int($lv_size),
160 $d->{pool_lv
} = $pool_lv if $pool_lv;
161 $d->{tags
} = $tags if $tags;
163 if ($lv_type eq 't') {
167 $d->{metadata_size
} = int($meta_size);
168 $d->{metadata_used
} = int(($meta_percent * $meta_size)/100);
169 $d->{used
} = int(($data_percent * $lv_size)/100);
171 $lvs->{$vg_name}->{$lv_name} = $d;
185 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
192 description
=> "Volume group name.",
193 type
=> 'string', format
=> 'pve-storage-vgname',
196 description
=> "Base volume. This volume is automatically activated.",
197 type
=> 'string', format
=> 'pve-volume-id',
200 description
=> "Zero-out data when removing LVs.",
203 saferemove_throughput
=> {
204 description
=> "Wipe throughput (cstream -t parameter value).",
208 description
=> "Only use logical volumes tagged with 'pve-vm-ID'.",
216 vgname
=> { fixed
=> 1 },
217 nodes
=> { optional
=> 1 },
218 shared
=> { optional
=> 1 },
219 disable
=> { optional
=> 1 },
220 saferemove
=> { optional
=> 1 },
221 saferemove_throughput
=> { optional
=> 1 },
222 content
=> { optional
=> 1 },
223 base
=> { fixed
=> 1, optional
=> 1 },
224 tagged_only
=> { optional
=> 1 },
225 bwlimit
=> { optional
=> 1 },
229 # Storage implementation
232 my ($class, $storeid, $scfg, %param) = @_;
234 if (my $base = $scfg->{base
}) {
235 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($base);
237 my $cfg = PVE
::Storage
::config
();
238 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
239 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
241 # we only support iscsi for now
242 die "unsupported base type '$basecfg->{type}'"
243 if $basecfg->{type
} ne 'iscsi';
245 my $path = PVE
::Storage
::path
($cfg, $base);
247 PVE
::Storage
::activate_storage
($cfg, $baseid);
249 lvm_create_volume_group
($path, $scfg->{vgname
}, $scfg->{shared
});
254 my ($class, $volname) = @_;
256 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
258 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
259 return ('images', $1, $2, undef, undef, undef, 'raw');
262 die "unable to parse lvm volume name '$volname'\n";
265 sub filesystem_path
{
266 my ($class, $scfg, $volname, $snapname) = @_;
268 die "lvm snapshot is not implemented"if defined($snapname);
270 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
272 my $vg = $scfg->{vgname
};
274 my $path = "/dev/$vg/$name";
276 return wantarray ?
($path, $vmid, $vtype) : $path;
280 my ($class, $storeid, $scfg, $volname) = @_;
282 die "can't create base images in lvm storage\n";
286 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
288 die "can't clone images in lvm storage\n";
291 sub lvm_find_free_diskname
{
292 my ($lvs, $vg, $storeid, $vmid, $scfg) = @_;
294 my $disk_list = [ keys %{$lvs->{$vg}} ];
296 return PVE
::Storage
::Plugin
::get_next_vm_diskname
($disk_list, $storeid, $vmid, undef, $scfg);
300 my ($vg, $name, $size, $tags) = @_;
302 if ($size =~ m/\d$/) { # no unit is given
303 $size .= "k"; # default to kilobytes
306 my $cmd = ['/sbin/lvcreate', '-aly', '--size', $size, '--name', $name];
307 for my $tag (@$tags) {
308 push @$cmd, '--addtag', $tag;
312 run_command
($cmd, errmsg
=> "lvcreate '$vg/$name' error");
316 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
318 die "unsupported format '$fmt'" if $fmt ne 'raw';
320 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
321 if $name && $name !~ m/^vm-$vmid-/;
325 my $vg = $scfg->{vgname
};
327 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
329 my $free = int($vgs->{$vg}->{free
});
331 die "not enough free space ($free < $size)\n" if $free < $size;
333 $name = lvm_find_free_diskname
(lvm_list_volumes
($vg), $vg, $storeid, $vmid, $scfg)
336 lvcreate
($vg, $name, $size, ["pve-vm-$vmid"]);
342 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
344 my $vg = $scfg->{vgname
};
346 # we need to zero out LVM data for security reasons
347 # and to allow thin provisioning
349 my $zero_out_worker = sub {
350 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
352 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
353 my $throughput = '-10485760';
354 if ($scfg->{saferemove_throughput
}) {
355 $throughput = $scfg->{saferemove_throughput
};
361 '-o', "/dev/$vg/del-$volname",
367 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
370 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
371 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
372 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
374 print "successfully removed volume $volname ($vg/del-$volname)\n";
377 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
378 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
379 $cmd = ['/sbin/lvchange', '--refresh', "$vg/$volname"];
380 run_command
($cmd, errmsg
=> "can't refresh LV '$vg/$volname' to zero-out its data");
382 if ($scfg->{saferemove
}) {
383 # avoid long running task, so we only rename here
384 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
385 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
386 return $zero_out_worker;
388 my $tmpvg = $scfg->{vgname
};
389 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
390 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
396 my $check_tags = sub {
399 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
403 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
405 my $vgname = $scfg->{vgname
};
407 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
411 if (my $dat = $cache->{lvs
}->{$vgname}) {
413 foreach my $volname (keys %$dat) {
415 next if $volname !~ m/^vm-(\d+)-/;
418 my $info = $dat->{$volname};
420 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
422 next if $info->{lv_type
} ne '-';
424 my $volid = "$storeid:$volname";
427 my $found = grep { $_ eq $volid } @$vollist;
430 next if defined($vmid) && ($owner ne $vmid);
434 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
443 my ($class, $storeid, $scfg, $cache) = @_;
445 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
447 my $vgname = $scfg->{vgname
};
449 if (my $info = $cache->{vgs
}->{$vgname}) {
450 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
456 sub activate_storage
{
457 my ($class, $storeid, $scfg, $cache) = @_;
459 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
461 # In LVM2, vgscans take place automatically;
462 # this is just to be sure
463 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
464 !$cache->{vgs
}->{$scfg->{vgname
}}) {
465 $cache->{vgscaned
} = 1;
466 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
467 eval { run_command
($cmd, outfunc
=> sub {}); };
471 # we do not acticate any volumes here ('vgchange -aly')
472 # instead, volumes are activate individually later
475 sub deactivate_storage
{
476 my ($class, $storeid, $scfg, $cache) = @_;
478 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
479 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
482 sub activate_volume
{
483 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
484 #fix me lvmchange is not provided on
485 my $path = $class->path($scfg, $volname, $snapname);
487 my $lvm_activate_mode = 'ey';
489 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
490 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
491 $cmd = ['/sbin/lvchange', '--refresh', $path];
492 run_command
($cmd, errmsg
=> "can't refresh LV '$path' for activation");
495 sub deactivate_volume
{
496 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
498 my $path = $class->path($scfg, $volname, $snapname);
499 return if ! -b
$path;
501 my $cmd = ['/sbin/lvchange', '-aln', $path];
502 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
506 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
508 $size = ($size/1024/1024) . "M";
510 my $path = $class->path($scfg, $volname);
511 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
513 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
514 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
520 sub volume_size_info
{
521 my ($class, $scfg, $storeid, $volname, $timeout) = @_;
522 my $path = $class->filesystem_path($scfg, $volname);
524 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
525 '--unbuffered', '--nosuffix', '--options', 'lv_size', $path];
528 run_command
($cmd, timeout
=> $timeout, errmsg
=> "can't get size of '$path'",
532 return wantarray ?
($size, 'raw', 0, undef) : $size;
535 sub volume_snapshot
{
536 my ($class, $scfg, $storeid, $volname, $snap) = @_;
538 die "lvm snapshot is not implemented";
541 sub volume_snapshot_rollback
{
542 my ($class, $scfg, $storeid, $volname, $snap) = @_;
544 die "lvm snapshot rollback is not implemented";
547 sub volume_snapshot_delete
{
548 my ($class, $scfg, $storeid, $volname, $snap) = @_;
550 die "lvm snapshot delete is not implemented";
553 sub volume_has_feature
{
554 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
557 copy
=> { base
=> 1, current
=> 1},
560 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
561 $class->parse_volname($volname);
567 $key = $isBase ?
'base' : 'current';
569 return 1 if $features->{$feature}->{$key};
574 sub volume_export_formats
{
575 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
576 return () if defined($snapshot); # lvm-thin only
577 return volume_import_formats
($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
581 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
582 die "volume export format $format not available for $class\n"
583 if $format ne 'raw+size';
584 die "cannot export volumes together with their snapshots in $class\n"
586 die "cannot export a snapshot in $class\n" if defined($snapshot);
587 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
588 my $file = $class->path($scfg, $volname, $storeid);
590 # should be faster than querying LVM, also checks for the device file's availability
591 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
593 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
596 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
597 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
600 sub volume_import_formats
{
601 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
602 return () if $with_snapshots; # not supported
603 return () if defined($base_snapshot); # not supported
608 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
609 die "volume import format $format not available for $class\n"
610 if $format ne 'raw+size';
611 die "cannot import volumes together with their snapshots in $class\n"
613 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
615 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
616 $class->parse_volname($volname);
617 die "cannot import format $format into a file of format $file_format\n"
618 if $file_format ne 'raw';
620 my $vg = $scfg->{vgname
};
621 my $lvs = lvm_list_volumes
($vg);
622 die "volume $vg/$volname already exists\n"
623 if $lvs->{$vg}->{$volname};
625 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
626 $size = int($size/1024);
629 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
630 if ($allocname ne $volname) {
631 my $oldname = $volname;
632 $volname = $allocname; # Let the cleanup code know what to free
633 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
635 my $file = $class->path($scfg, $volname, $storeid)
636 or die "internal error: failed to get path to newly allocated volume $volname\n";
638 $class->volume_import_write($fh, $file);
641 eval { $class->free_image($storeid, $scfg, $volname, 0) };
647 sub volume_import_write
{
648 my ($class, $input_fh, $output_file) = @_;
649 run_command
(['dd', "of=$output_file", 'bs=64k'],
650 input
=> '<&'.fileno($input_fh));