]>
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, $storeid, $scfg, %param) = @_;
214 if (my $base = $scfg->{base
}) {
215 my ($baseid, $volname) = PVE
::Storage
::parse_volume_id
($base);
217 my $cfg = PVE
::Storage
::config
();
218 my $basecfg = PVE
::Storage
::storage_config
($cfg, $baseid, 1);
219 die "base storage ID '$baseid' does not exist\n" if !$basecfg;
221 # we only support iscsi for now
222 die "unsupported base type '$basecfg->{type}'"
223 if $basecfg->{type
} ne 'iscsi';
225 my $path = PVE
::Storage
::path
($cfg, $base);
227 PVE
::Storage
::activate_storage
($cfg, $baseid);
229 lvm_create_volume_group
($path, $scfg->{vgname
}, $scfg->{shared
});
234 my ($class, $volname) = @_;
236 PVE
::Storage
::Plugin
::parse_lvm_name
($volname);
238 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
239 return ('images', $1, $2, undef, undef, undef, 'raw');
242 die "unable to parse lvm volume name '$volname'\n";
245 sub filesystem_path
{
246 my ($class, $scfg, $volname, $snapname) = @_;
248 die "lvm snapshot is not implemented"if defined($snapname);
250 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
252 my $vg = $scfg->{vgname
};
254 my $path = "/dev/$vg/$name";
256 return wantarray ?
($path, $vmid, $vtype) : $path;
260 my ($class, $storeid, $scfg, $volname) = @_;
262 die "can't create base images in lvm storage\n";
266 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
268 die "can't clone images in lvm storage\n";
271 sub lvm_find_free_diskname
{
272 my ($lvs, $vg, $storeid, $vmid) = @_;
276 for (my $i = 1; $i < 100; $i++) {
277 my $tn = "vm-$vmid-disk-$i";
278 if (!defined ($lvs->{$vg}->{$tn})) {
284 die "unable to allocate an image name for ID $vmid in storage '$storeid'\n"
291 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
293 die "unsupported format '$fmt'" if $fmt ne 'raw';
295 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
296 if $name && $name !~ m/^vm-$vmid-/;
300 my $vg = $scfg->{vgname
};
302 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
304 my $free = int($vgs->{$vg}->{free
});
306 die "not enough free space ($free < $size)\n" if $free < $size;
308 $name = lvm_find_free_diskname
(lvm_list_volumes
($vg), $vg, $storeid, $vmid)
311 my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
313 run_command
($cmd, errmsg
=> "lvcreate '$vg/pve-vm-$vmid' error");
319 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
321 my $vg = $scfg->{vgname
};
323 # we need to zero out LVM data for security reasons
324 # and to allow thin provisioning
326 my $zero_out_worker = sub {
327 print "zero-out data on image $volname (/dev/$vg/del-$volname)\n";
329 # wipe throughput up to 10MB/s by default; may be overwritten with saferemove_throughput
330 my $throughput = '-10485760';
331 if ($scfg->{saferemove_throughput
}) {
332 $throughput = $scfg->{saferemove_throughput
};
338 '-o', "/dev/$vg/del-$volname",
344 eval { run_command
($cmd, errmsg
=> "zero out finished (note: 'No space left on device' is ok here)"); };
347 $class->cluster_lock_storage($storeid, $scfg->{shared
}, undef, sub {
348 my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
349 run_command
($cmd, errmsg
=> "lvremove '$vg/del-$volname' error");
351 print "successfully removed volume $volname ($vg/del-$volname)\n";
354 my $cmd = ['/sbin/lvchange', '-aly', "$vg/$volname"];
355 run_command
($cmd, errmsg
=> "can't activate LV '$vg/$volname' to zero-out its data");
357 if ($scfg->{saferemove
}) {
358 # avoid long running task, so we only rename here
359 $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
360 run_command
($cmd, errmsg
=> "lvrename '$vg/$volname' error");
361 return $zero_out_worker;
363 my $tmpvg = $scfg->{vgname
};
364 $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
365 run_command
($cmd, errmsg
=> "lvremove '$tmpvg/$volname' error");
371 my $check_tags = sub {
374 return defined($tags) && $tags =~ /(^|,)pve-vm-\d+(,|$)/;
378 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
380 my $vgname = $scfg->{vgname
};
382 $cache->{lvs
} = lvm_list_volumes
() if !$cache->{lvs
};
386 if (my $dat = $cache->{lvs
}->{$vgname}) {
388 foreach my $volname (keys %$dat) {
390 next if $volname !~ m/^vm-(\d+)-/;
393 my $info = $dat->{$volname};
395 next if $scfg->{tagged_only
} && !&$check_tags($info->{tags
});
397 next if $info->{lv_type
} ne '-';
399 my $volid = "$storeid:$volname";
402 my $found = grep { $_ eq $volid } @$vollist;
405 next if defined($vmid) && ($owner ne $vmid);
409 volid
=> $volid, format
=> 'raw', size
=> $info->{lv_size
}, vmid
=> $owner,
418 my ($class, $storeid, $scfg, $cache) = @_;
420 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
422 my $vgname = $scfg->{vgname
};
424 if (my $info = $cache->{vgs
}->{$vgname}) {
425 return ($info->{size
}, $info->{free
}, $info->{size
} - $info->{free
}, 1);
431 sub activate_storage
{
432 my ($class, $storeid, $scfg, $cache) = @_;
434 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
436 # In LVM2, vgscans take place automatically;
437 # this is just to be sure
438 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
439 !$cache->{vgs
}->{$scfg->{vgname
}}) {
440 $cache->{vgscaned
} = 1;
441 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
442 eval { run_command
($cmd, outfunc
=> sub {}); };
446 # we do not acticate any volumes here ('vgchange -aly')
447 # instead, volumes are activate individually later
450 sub deactivate_storage
{
451 my ($class, $storeid, $scfg, $cache) = @_;
453 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
454 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
457 sub activate_volume
{
458 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
459 #fix me lvmchange is not provided on
460 my $path = $class->path($scfg, $volname, $snapname);
462 my $lvm_activate_mode = 'ey';
464 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
465 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
468 sub deactivate_volume
{
469 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
471 my $path = $class->path($scfg, $volname, $snapname);
472 return if ! -b
$path;
474 my $cmd = ['/sbin/lvchange', '-aln', $path];
475 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
479 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
481 $size = ($size/1024/1024) . "M";
483 my $path = $class->path($scfg, $volname);
484 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
485 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
490 sub volume_snapshot
{
491 my ($class, $scfg, $storeid, $volname, $snap) = @_;
493 die "lvm snapshot is not implemented";
496 sub volume_snapshot_rollback
{
497 my ($class, $scfg, $storeid, $volname, $snap) = @_;
499 die "lvm snapshot rollback is not implemented";
502 sub volume_snapshot_delete
{
503 my ($class, $scfg, $storeid, $volname, $snap) = @_;
505 die "lvm snapshot delete is not implemented";
508 sub volume_has_feature
{
509 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
512 copy
=> { base
=> 1, current
=> 1},
515 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
516 $class->parse_volname($volname);
522 $key = $isBase ?
'base' : 'current';
524 return 1 if $features->{$feature}->{$key};
529 sub volume_export_formats
{
530 my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
531 return () if defined($snapshot); # lvm-thin only
532 return volume_import_formats
($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots);
536 my ($class, $scfg, $storeid, $fh, $volname, $format, $snapshot, $base_snapshot, $with_snapshots) = @_;
537 die "volume export format $format not available for $class\n"
538 if $format ne 'raw+size';
539 die "cannot export volumes together with their snapshots in $class\n"
541 die "cannot export a snapshot in $class\n" if defined($snapshot);
542 die "cannot export an incremental stream in $class\n" if defined($base_snapshot);
543 my $file = $class->path($scfg, $volname, $storeid);
545 # should be faster than querying LVM, also checks for the device file's availability
546 run_command
(['/sbin/blockdev', '--getsize64', $file], outfunc
=> sub {
548 die "unexpected output from /sbin/blockdev: $line\n" if $line !~ /^(\d+)$/;
551 PVE
::Storage
::Plugin
::write_common_header
($fh, $size);
552 run_command
(['dd', "if=$file", "bs=64k"], output
=> '>&'.fileno($fh));
555 sub volume_import_formats
{
556 my ($class, $scfg, $storeid, $volname, $base_snapshot, $with_snapshots) = @_;
557 return () if $with_snapshots; # not supported
558 return () if defined($base_snapshot); # not supported
563 my ($class, $scfg, $storeid, $fh, $volname, $format, $base_snapshot, $with_snapshots) = @_;
564 die "volume import format $format not available for $class\n"
565 if $format ne 'raw+size';
566 die "cannot import volumes together with their snapshots in $class\n"
568 die "cannot import an incremental stream in $class\n" if defined($base_snapshot);
570 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $file_format) =
571 $class->parse_volname($volname);
572 die "cannot import format $format into a file of format $file_format\n"
573 if $file_format ne 'raw';
575 my $vg = $scfg->{vgname
};
576 my $lvs = lvm_list_volumes
($vg);
577 die "volume $vg/$volname already exists\n"
578 if $lvs->{$vg}->{$volname};
580 my ($size) = PVE
::Storage
::Plugin
::read_common_header
($fh);
581 $size = int($size/1024);
584 my $allocname = $class->alloc_image($storeid, $scfg, $vmid, 'raw', $name, $size);
585 if ($allocname ne $volname) {
586 my $oldname = $volname;
587 $volname = $allocname; # Let the cleanup code know what to free
588 die "internal error: unexpected allocated name: '$allocname' != '$oldname'\n";
590 my $file = $class->path($scfg, $volname, $storeid)
591 or die "internal error: failed to get path to newly allocated volume $volname\n";
592 run_command
(['dd', "of=$file", 'conv=sparse', 'bs=64k'],
593 input
=> '<&'.fileno($fh));
596 eval { $class->free_image($storeid, $scfg, $volname, 0) };