]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
1 package PVE
::Storage
::LVMPlugin
;
7 use PVE
::Tools
qw(run_command trim);
8 use PVE
::Storage
::Plugin
;
9 use PVE
::JSONSchema
qw(get_standard_option);
11 use base
qw(PVE::Storage::Plugin);
13 # lvm helper functions
18 die "no device specified" if !$device;
22 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
23 run_command
($cmd, outfunc
=> sub {
25 $has_label = 1 if $line =~ m/LVM2/;
28 return undef if !$has_label;
30 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
31 '--unbuffered', '--nosuffix', '--options',
32 'pv_name,pv_size,vg_name,pv_uuid', $device];
35 run_command
($cmd, outfunc
=> sub {
40 my ($pvname, $size, $vgname, $uuid) = split(':', $line);
42 die "found multiple pvs entries for device '$device'\n"
56 sub clear_first_sector
{
59 if (my $fh = IO
::File-
>new($dev, "w")) {
66 sub lvm_create_volume_group
{
67 my ($device, $vgname, $shared) = @_;
69 my $res = lvm_pv_info
($device);
72 return if $res->{vgname
} eq $vgname; # already created
73 die "device '$device' is already used by volume group '$res->{vgname}'\n";
76 clear_first_sector
($device); # else pvcreate fails
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];
82 run_command
($cmd, errmsg
=> "pvcreate '$device' error");
84 $cmd = ['/sbin/vgcreate', $vgname, $device];
85 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
87 run_command
($cmd, errmsg
=> "vgcreate $vgname $device error");
92 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
93 '--unbuffered', '--nosuffix', '--options',
94 'vg_name,vg_size,vg_free'];
98 run_command
($cmd, outfunc
=> sub {
103 my ($name, $size, $free) = split (':', $line);
105 $vgs->{$name} = { size
=> int ($size), free
=> int ($free) };
110 # just warn (vgs return error code 5 if clvmd does not run)
111 # but output is still OK (list without clustered VGs)
117 sub lvm_list_volumes
{
120 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
121 '--unbuffered', '--nosuffix', '--options',
122 'vg_name,lv_name,lv_size,lv_attr,pool_lv,data_percent,metadata_percent,snap_percent,uuid,tags'];
124 push @$cmd, $vgname if $vgname;
127 run_command
($cmd, outfunc
=> sub {
132 my ($vg_name, $lv_name, $lv_size, $lv_attr, $pool_lv, $data_percent, $meta_percent, $snap_percent, $uuid, $tags) = split(':', $line);
136 my $lv_type = substr($lv_attr, 0, 1);
139 lv_size
=> int($lv_size),
142 $d->{pool_lv
} = $pool_lv if $pool_lv;
143 $d->{tags
} = $tags if $tags;
145 if ($lv_type eq 't') {
149 $d->{used
} = int(($data_percent * $lv_size)/100);
151 $lvs->{$vg_name}->{$lv_name} = $d;
165 content
=> [ {images
=> 1, rootdir
=> 1}, { images
=> 1 }],
172 description
=> "Volume group name.",
173 type
=> 'string', format
=> 'pve-storage-vgname',
176 description
=> "Base volume. This volume is automatically activated.",
177 type
=> 'string', format
=> 'pve-volume-id',
180 description
=> "Zero-out data when removing LVs.",
183 saferemove_throughput
=> {
184 description
=> "Wipe throughput (cstream -t parameter value).",
188 description
=> "Only use logical volumes tagged with 'pve-vm-ID'.",
196 vgname
=> { fixed
=> 1 },
197 nodes
=> { optional
=> 1 },
198 shared
=> { optional
=> 1 },
199 disable
=> { optional
=> 1 },
200 saferemove
=> { optional
=> 1 },
201 saferemove_throughput
=> { optional
=> 1 },
202 content
=> { optional
=> 1 },
203 base
=> { fixed
=> 1, optional
=> 1 },
204 tagged_only
=> { optional
=> 1 },
205 bwlimit
=> { optional
=> 1 },
209 # Storage implementation
212 my ($class, $volname) = @_;
214 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
216 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
217 return ('images', $1, $2, undef, undef, undef, 'raw');
220 die "unable to parse lvm volume name '$volname'\n";
223 sub filesystem_path
{
224 my ($class, $scfg, $volname, $snapname) = @_;
226 die "lvm snapshot is not implemented"if defined($snapname);
228 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
230 my $vg = $scfg->{vgname
};
232 my $path = "/dev/$vg/$name";
234 return wantarray ?
($path, $vmid, $vtype) : $path;
238 my ($class, $storeid, $scfg, $volname) = @_;
240 die "can't create base images in lvm storage\n";
244 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
246 die "can't clone images in lvm storage\n";
249 sub lvm_find_free_diskname
{
250 my ($lvs, $vg, $storeid, $vmid) = @_;
254 for (my $i = 1; $i < 100; $i++) {
255 my $tn = "vm-$vmid-disk-$i";
256 if (!defined ($lvs->{$vg}->{$tn})) {
262 die "unable to allocate an image name for ID $vmid in storage '$storeid'\n"
269 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
271 die "unsupported format '$fmt'" if $fmt ne 'raw';
273 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
274 if $name && $name !~ m/^vm-$vmid-/;
278 my $vg = $scfg->{vgname
};
280 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
282 my $free = int($vgs->{$vg}->{free
});
284 die "not enough free space ($free < $size)\n" if $free < $size;
286 $name = lvm_find_free_diskname
(lvm_list_volumes
($vg), $vg, $storeid, $vmid)
289 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
291 run_command
($cmd, errmsg
=> "lvcreate '$vg/pve-vm-$vmid' error");
297 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
299 my $vg = $scfg->{vgname
};
301 # we need to zero out LVM data for security reasons
302 # and to allow thin provisioning
304 my $zero_out_worker = sub {
305 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
307 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
308 my $throughput = '-10485760';
309 if ($scfg->{saferemove_throughput
}) {
310 $throughput = $scfg->{saferemove_throughput
};
316 '-o', "/dev/$vg/del-$volname",
322 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
325 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
326 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
327 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
329 print "successfully removed volume $volname ($vg/del-$volname)\n";
332 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
333 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
335 if ($scfg->{saferemove
}) {
336 # avoid long running task, so we only rename here
337 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
338 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
339 return $zero_out_worker;
341 my $tmpvg = $scfg->{vgname
};
342 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
343 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
349 my $check_tags = sub {
352 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
356 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
358 my $vgname = $scfg->{vgname
};
360 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
364 if (my $dat = $cache->{lvs
}->{$vgname}) {
366 foreach my $volname (keys %$dat) {
368 next if $volname !~ m/^vm-(\d+)-/;
371 my $info = $dat->{$volname};
373 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
375 next if $info->{lv_type
} ne '-';
377 my $volid = "$storeid:$volname";
380 my $found = grep { $_ eq $volid } @$vollist;
383 next if defined($vmid) && ($owner ne $vmid);
387 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
396 my ($class, $storeid, $scfg, $cache) = @_;
398 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
400 my $vgname = $scfg->{vgname
};
402 if (my $info = $cache->{vgs
}->{$vgname}) {
403 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
409 sub activate_storage
{
410 my ($class, $storeid, $scfg, $cache) = @_;
412 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
414 # In LVM2, vgscans take place automatically;
415 # this is just to be sure
416 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
417 !$cache->{vgs
}->{$scfg->{vgname
}}) {
418 $cache->{vgscaned
} = 1;
419 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
420 eval { run_command
($cmd, outfunc
=> sub {}); };
424 # we do not acticate any volumes here ('vgchange -aly')
425 # instead, volumes are activate individually later
428 sub deactivate_storage
{
429 my ($class, $storeid, $scfg, $cache) = @_;
431 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
432 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
435 sub activate_volume
{
436 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
437 #fix me lvmchange is not provided on
438 my $path = $class->path($scfg, $volname, $snapname);
440 my $lvm_activate_mode = 'ey';
442 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
443 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
446 sub deactivate_volume
{
447 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
449 my $path = $class->path($scfg, $volname, $snapname);
450 return if ! -b
$path;
452 my $cmd = ['/sbin/lvchange', '-aln', $path];
453 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
457 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
459 $size = ($size/1024/1024) . "M";
461 my $path = $class->path($scfg, $volname);
462 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
463 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
468 sub volume_snapshot
{
469 my ($class, $scfg, $storeid, $volname, $snap) = @_;
471 die "lvm snapshot is not implemented";
474 sub volume_snapshot_rollback
{
475 my ($class, $scfg, $storeid, $volname, $snap) = @_;
477 die "lvm snapshot rollback is not implemented";
480 sub volume_snapshot_delete
{
481 my ($class, $scfg, $storeid, $volname, $snap) = @_;
483 die "lvm snapshot delete is not implemented";
486 sub volume_has_feature
{
487 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
490 copy
=> { base
=> 1, current
=> 1},
493 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
494 $class->parse_volname($volname);
500 $key = $isBase ?
'base' : 'current';
502 return 1 if $features->{$feature}->{$key};
507 sub volume_export_formats
{
508 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
509 return () if defined($snapshot); # lvm-thin only
510 return volume_import_formats
($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
514 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
515 die "volume export format $format not available for $class\n"
516 if $format ne 'raw+size';
517 die "cannot export volumes together with their snapshots in $class\n"
519 die "cannot export a snapshot in $class\n" if defined($snapshot);
520 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
521 my $file = $class->path($scfg, $volname, $storeid);
523 # should be faster than querying LVM, also checks for the device file's availability
524 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
526 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
529 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
530 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
533 sub volume_import_formats
{
534 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
535 return () if $with_snapshots; # not supported
536 return () if defined($base_snapshot); # not supported
541 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
542 die "volume import format $format not available for $class\n"
543 if $format ne 'raw+size';
544 die "cannot import volumes together with their snapshots in $class\n"
546 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
548 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
549 $class->parse_volname($volname);
550 die "cannot import format $format into a file of format $file_format\n"
551 if $file_format ne 'raw';
553 my $vg = $scfg->{vgname
};
554 my $lvs = lvm_list_volumes
($vg);
555 die "volume $vg/$volname already exists\n"
556 if $lvs->{$vg}->{$volname};
558 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
559 $size = int($size/1024);
562 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
563 if ($allocname ne $volname) {
564 my $oldname = $volname;
565 $volname = $allocname; # Let the cleanup code know what to free
566 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
568 my $file = $class->path($scfg, $volname, $storeid)
569 or die "internal error: failed to get path to newly allocated volume $volname\n";
570 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
571 input
=> '<&'.fileno($fh));
574 eval { $class->free_image($storeid, $scfg, $volname, 0) };