]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/LVMPlugin.pm
1 package PVE
::Storage
::LVMPlugin
;
6 use PVE
::Tools
qw(run_command trim);
7 use PVE
::Storage
::Plugin
;
8 use PVE
::JSONSchema
qw(get_standard_option);
10 use base
qw(PVE::Storage::Plugin);
12 # lvm helper functions
17 die "no device specified" if !$device;
21 my $cmd = ['/usr/bin/file', '-L', '-s', $device];
22 run_command
($cmd, outfunc
=> sub {
24 $has_label = 1 if $line =~ m/LVM2/;
27 return undef if !$has_label;
29 $cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
30 '--unbuffered', '--nosuffix', '--options',
31 'pv_name,pv_size,vg_name,pv_uuid', $device];
34 run_command
($cmd, outfunc
=> sub {
39 my ($pvname, $size, $vgname, $uuid) = split(':', $line);
41 die "found multiple pvs entries for device '$device'\n"
55 sub clear_first_sector
{
58 if (my $fh = IO
::File-
>new($dev, "w")) {
65 sub lvm_create_volume_group
{
66 my ($device, $vgname, $shared) = @_;
68 my $res = lvm_pv_info
($device);
71 return if $res->{vgname
} eq $vgname; # already created
72 die "device '$device' is already used by volume group '$res->{vgname}'\n";
75 clear_first_sector
($device); # else pvcreate fails
77 # we use --metadatasize 250k, which reseults in "pe_start = 512"
78 # so pe_start is aligned on a 128k boundary (advantage for SSDs)
79 my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
81 run_command
($cmd, errmsg
=> "pvcreate '$device' error");
83 $cmd = ['/sbin/vgcreate', $vgname, $device];
84 # push @$cmd, '-c', 'y' if $shared; # we do not use this yet
86 run_command
($cmd, errmsg
=> "vgcreate $vgname $device error");
91 my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
92 '--unbuffered', '--nosuffix', '--options',
93 'vg_name,vg_size,vg_free'];
97 run_command
($cmd, outfunc
=> sub {
102 my ($name, $size, $free) = split (':', $line);
104 $vgs->{$name} = { size
=> int ($size), free
=> int ($free) };
109 # just warn (vgs return error code 5 if clvmd does not run)
110 # but output is still OK (list without clustered VGs)
119 my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
120 '--unbuffered', '--nosuffix', '--options',
121 'vg_name,lv_name,lv_size,uuid,tags'];
123 push @$cmd, $vgname if $vgname;
126 run_command
($cmd, outfunc
=> sub {
131 my ($vg, $name, $size, $uuid, $tags) = split(':', $line);
133 return if $name !~ m/^vm-(\d+)-/;
137 foreach my $tag (split (/,/, $tags)) {
138 if ($tag =~ m/^pve-vm-(\d+)$/) {
145 if ($owner ne $nid) {
146 warn "owner mismatch name = $name, owner = $owner\n";
149 $lvs->{$vg}->{$name} = { format
=> 'raw', size
=> $size,
150 uuid
=> $uuid, tags
=> $tags,
160 PVE
::JSONSchema
::register_format
('pve-storage-vgname', \
&parse_lvm_name
);
162 my ($name, $noerr) = @_;
164 if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
165 return undef if $noerr;
166 die "lvm name '$name' contains illegal characters\n";
178 content
=> [ {images
=> 1}, { images
=> 1 }],
185 description
=> "Volume group name.",
186 type
=> 'string', format
=> 'pve-storage-vgname',
189 description
=> "Base volume. This volume is automatically activated.",
190 type
=> 'string', format
=> 'pve-volume-id',
193 description
=> "Zero-out data when removing LVs.",
196 saferemove_throughput
=> {
197 description
=> "Wipe throughput (cstream -t parameter value).",
205 vgname
=> { fixed
=> 1 },
206 nodes
=> { optional
=> 1 },
207 shared
=> { optional
=> 1 },
208 disable
=> { optional
=> 1 },
209 saferemove
=> { optional
=> 1 },
210 saferemove_throughput
=> { optional
=> 1 },
211 content
=> { optional
=> 1 },
212 base
=> { fixed
=> 1, optional
=> 1 },
216 # Storage implementation
219 my ($class, $volname) = @_;
221 parse_lvm_name
($volname);
223 if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
224 return ('images', $1, $2, undef, undef, undef, 'raw');
227 die "unable to parse lvm volume name '$volname'\n";
230 sub filesystem_path
{
231 my ($class, $scfg, $volname, $snapname) = @_;
233 die "lvm snapshot is not implemented"if defined($snapname);
235 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
237 my $vg = $scfg->{vgname
};
239 my $path = "/dev/$vg/$name";
241 return wantarray ?
($path, $vmid, $vtype) : $path;
245 my ($class, $storeid, $scfg, $volname) = @_;
247 die "can't create base images in lvm storage\n";
251 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
253 die "can't clone images in lvm storage\n";
257 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
259 die "unsupported format '$fmt'" if $fmt ne 'raw';
261 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
262 if $name && $name !~ m/^vm-$vmid-/;
266 my $vg = $scfg->{vgname
};
268 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
270 my $free = int($vgs->{$vg}->{free
});
272 die "not enough free space ($free < $size)\n" if $free < $size;
275 my $lvs = lvm_lvs
($vg);
277 for (my $i = 1; $i < 100; $i++) {
278 my $tn = "vm-$vmid-disk-$i";
279 if (!defined ($lvs->{$vg}->{$tn})) {
286 die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
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");
350 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
352 my $vgname = $scfg->{vgname
};
354 $cache->{lvs
} = lvm_lvs
() if !$cache->{lvs
};
358 if (my $dat = $cache->{lvs
}->{$vgname}) {
360 foreach my $volname (keys %$dat) {
362 my $owner = $dat->{$volname}->{vmid
};
364 my $volid = "$storeid:$volname";
367 my $found = grep { $_ eq $volid } @$vollist;
370 next if defined ($vmid) && ($owner ne $vmid);
373 my $info = $dat->{$volname};
374 $info->{volid
} = $volid;
384 my ($class, $storeid, $scfg, $cache) = @_;
386 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
388 my $vgname = $scfg->{vgname
};
394 if (my $info = $cache->{vgs
}->{$vgname}) {
395 return ($info->{size
}, $info->{free
}, $total - $free, 1);
401 sub activate_storage
{
402 my ($class, $storeid, $scfg, $cache) = @_;
404 $cache->{vgs
} = lvm_vgs
() if !$cache->{vgs
};
406 # In LVM2, vgscans take place automatically;
407 # this is just to be sure
408 if ($cache->{vgs
} && !$cache->{vgscaned
} &&
409 !$cache->{vgs
}->{$scfg->{vgname
}}) {
410 $cache->{vgscaned
} = 1;
411 my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
412 eval { run_command
($cmd, outfunc
=> sub {}); };
416 # we do not acticate any volumes here ('vgchange -aly')
417 # instead, volumes are activate individually later
420 sub deactivate_storage
{
421 my ($class, $storeid, $scfg, $cache) = @_;
423 my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname
}];
424 run_command
($cmd, errmsg
=> "can't deactivate VG '$scfg->{vgname}'");
427 sub activate_volume
{
428 my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
430 my $path = $class->path($scfg, $volname);
432 my $lvm_activate_mode = $exclusive ?
'ey' : 'ly';
434 my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
435 run_command
($cmd, errmsg
=> "can't activate LV '$path'");
438 sub deactivate_volume
{
439 my ($class, $storeid, $scfg, $volname, $cache) = @_;
441 my $path = $class->path($scfg, $volname);
442 return if ! -b
$path;
444 my $cmd = ['/sbin/lvchange', '-aln', $path];
445 run_command
($cmd, errmsg
=> "can't deactivate LV '$path'");
449 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
451 $size = ($size/1024/1024) . "M";
453 my $path = $class->path($scfg, $volname);
454 my $cmd = ['/sbin/lvextend', '-L', $size, $path];
455 run_command
($cmd, errmsg
=> "error resizing volume '$path'");
460 sub volume_snapshot
{
461 my ($class, $scfg, $storeid, $volname, $snap) = @_;
463 die "lvm snapshot is not implemented";
466 sub volume_snapshot_rollback
{
467 my ($class, $scfg, $storeid, $volname, $snap) = @_;
469 die "lvm snapshot rollback is not implemented";
472 sub volume_snapshot_delete
{
473 my ($class, $scfg, $storeid, $volname, $snap) = @_;
475 die "lvm snapshot delete is not implemented";
478 sub volume_has_feature
{
479 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
482 copy
=> { base
=> 1, current
=> 1},
485 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
486 $class->parse_volname($volname);
492 $key = $isBase ?
'base' : 'current';
494 return 1 if $features->{$feature}->{$key};