]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/LvmThinPlugin.pm
lvmthin: fix used space (only use data%)
[pve-storage.git] / PVE / Storage / LvmThinPlugin.pm
CommitLineData
610798bc
DM
1package PVE::Storage::LvmThinPlugin;
2
3use strict;
4use warnings;
15334c83 5use Data::Dumper;
610798bc
DM
6use IO::File;
7use PVE::Tools qw(run_command trim);
8use PVE::Storage::Plugin;
15334c83 9use PVE::Storage::LVMPlugin;
610798bc
DM
10use PVE::JSONSchema qw(get_standard_option);
11
12# see: man lvmthin
15334c83
DM
13# lvcreate -n ThinDataLV -L LargeSize VG
14# lvconvert --type thin-pool VG/ThinDataLV
15# lvcreate -n pvepool -L 20G pve
16# lvconvert --type thin-pool pve/pvepool
610798bc
DM
17
18use base qw(PVE::Storage::LVMPlugin);
19
20sub type {
21 return 'lvmthin';
22}
23
24sub plugindata {
25 return {
26 content => [ {images => 1, rootdir => 1}, { images => 1, rootdir => 1}],
27 };
28}
29
30sub properties {
31 return {
32 thinpool => {
33 description => "LVM thin pool LV name.",
34 type => 'string', format => 'pve-storage-vgname',
35 },
36 };
37}
38
39sub options {
40 return {
41 thinpool => { fixed => 1 },
42 vgname => { fixed => 1 },
43 nodes => { optional => 1 },
44 disable => { optional => 1 },
45 content => { optional => 1 },
46 };
47}
48
f1b59efc
DM
49sub parse_volname {
50 my ($class, $volname) = @_;
51
52 PVE::Storage::Plugin::parse_lvm_name($volname);
53
54 if ($volname =~ m/^((vm|base)-(\d+)-\S+)$/) {
55 return ('images', $1, $3, undef, undef, $2 eq 'base', 'raw');
56 }
57
58 die "unable to parse lvm volume name '$volname'\n";
59}
60
610798bc
DM
61sub alloc_image {
62 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
63
64 die "unsupported format '$fmt'" if $fmt ne 'raw';
65
15334c83 66 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
610798bc
DM
67 if $name && $name !~ m/^vm-$vmid-/;
68
f1b59efc 69 my $vgs = PVE::Storage::LVMPlugin::lvm_vgs();
5b41084a 70
15334c83
DM
71 my $vg = $scfg->{vgname};
72
5b41084a
DM
73 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
74
f1b59efc 75 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
15334c83 76
f1b59efc
DM
77 $name = PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid)
78 if !$name;
15334c83
DM
79
80 my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name,
81 '--thinpool', "$vg/$scfg->{thinpool}" ];
82
83 run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
84
85 return $name;
610798bc
DM
86}
87
88sub free_image {
89 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
90
91 my $vg = $scfg->{vgname};
663372bc
DM
92
93 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
94
95 if (my $dat = $lvs->{$scfg->{vgname}}) {
96
97 # remove all volume snapshots first
98 foreach my $lv (keys %$dat) {
99 next if $lv !~ m/^snap_${volname}_(\w+)$/;
100 my $cmd = ['/sbin/lvremove', '-f', "$vg/$lv"];
101 run_command($cmd, errmsg => "lvremove snapshot '$vg/$lv' error");
102 }
103
104 # finally remove original (if exists)
105 if ($dat->{$volname}) {
106 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
107 run_command($cmd, errmsg => "lvremove '$vg/$volname' error");
108 }
109 }
610798bc
DM
110
111 return undef;
112}
113
114sub list_images {
115 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
116
15334c83
DM
117 my $vgname = $scfg->{vgname};
118
119 $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs};
120
121 my $res = [];
122
123 if (my $dat = $cache->{lvs}->{$vgname}) {
124
125 foreach my $volname (keys %$dat) {
126
f1b59efc
DM
127 next if $volname !~ m/^(vm|base)-(\d+)-/;
128 my $owner = $2;
15334c83
DM
129
130 my $info = $dat->{$volname};
131
132 next if $info->{lv_type} ne 'V';
133
134 next if $info->{pool_lv} ne $scfg->{thinpool};
135
136 my $volid = "$storeid:$volname";
137
138 if ($vollist) {
139 my $found = grep { $_ eq $volid } @$vollist;
140 next if !$found;
141 } else {
142 next if defined($vmid) && ($owner ne $vmid);
143 }
144
145 push @$res, {
146 volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
147 };
148 }
149 }
150
151 return $res;
610798bc
DM
152}
153
154sub status {
155 my ($class, $storeid, $scfg, $cache) = @_;
156
157 my $lvname = "$scfg->{vgname}/$scfg->{thinpool}";
15334c83
DM
158
159 $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs};
160
161 my $lvs = $cache->{lvs};
162
163 return undef if !$lvs->{$scfg->{vgname}};
164
165 my $info = $lvs->{$scfg->{vgname}}->{$scfg->{thinpool}};
166
167 return undef if !$info;
168
169 return undef if $info->{lv_type} ne 't';
170
171 return ($info->{lv_size}, $info->{lv_size} - $info->{used}, $info->{used}, 1) if $info->{lv_size};
610798bc
DM
172
173 return undef;
174}
175
176sub activate_volume {
177 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
15334c83 178
663372bc
DM
179 my $vg = $scfg->{vgname};
180
181 # only snapshot volumes needs activation
182 if ($snapname) {
183 my $snapvol = "snap_${volname}_$snapname";
184 my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$snapvol"];
185 run_command($cmd, errmsg => "activate_volume '$vg/$snapvol' error");
186 } else {
187 # other volumes are active by default
188 }
610798bc
DM
189}
190
191sub deactivate_volume {
192 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
15334c83 193
663372bc
DM
194 my $vg = $scfg->{vgname};
195
196 # we only deactivate snapshot volumes
197 if ($snapname) {
198 my $snapvol = "snap_${volname}_$snapname";
199 my $cmd = ['/sbin/lvchange', '-an', "$vg/$snapvol"];
200 run_command($cmd, errmsg => "deactivate_volume '$vg/$snapvol' error");
201 } else {
202 # other volumes are kept active
203 }
610798bc
DM
204}
205
f1b59efc
DM
206sub clone_image {
207 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
208
209 die "clone_image from snapshots not implemented" if $snap;
210
211 my ($vtype, undef, undef, undef, undef, $isBase, $format) =
212 $class->parse_volname($volname);
213
214 die "clone_image only works on base images\n" if !$isBase;
215
216 my $vg = $scfg->{vgname};
217 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
218
219 my $name = PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid);
220
221 my $cmd = ['/sbin/lvcreate', '-n', $name, '-prw', '-kn', '-s', "$vg/$volname"];
222 run_command($cmd, errmsg => "clone image '$vg/$volname' error");
223
224 return $name;
225}
226
227sub create_base {
228 my ($class, $storeid, $scfg, $volname) = @_;
229
230 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
231 $class->parse_volname($volname);
232
233 die "create_base not possible with base image\n" if $isBase;
234
235 my $vg = $scfg->{vgname};
236 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
237
238 if (my $dat = $lvs->{$vg}) {
239 # to avoid confusion, reject if we find volume snapshots
240 foreach my $lv (keys %$dat) {
241 die "unable to create base volume - found snaphost '$lv'\n"
242 if $lv =~ m/^snap_${volname}_(\w+)$/;
243 }
244 }
245
246 my $newname = $name;
247 $newname =~ s/^vm-/base-/;
248
249 my $cmd = ['/sbin/lvrename', $vg, $volname, $newname];
250 run_command($cmd, errmsg => "lvrename '$vg/$volname' => '$vg/$newname' error");
251
252 # set inactive, read-only and activationskip flags
253 $cmd = ['/sbin/lvchange', '-an', '-pr', '-ky', "$vg/$newname"];
254 eval { run_command($cmd); };
255 warn $@ if $@;
256
257 my $newvolname = $newname;
258
259 return $newvolname;
260}
261
a9f0c6c4 262# sub volume_resize {} reuse code from parent class
610798bc 263
663372bc
DM
264sub volume_snapshot {
265 my ($class, $scfg, $storeid, $volname, $snap) = @_;
266
267 my $vg = $scfg->{vgname};
268 my $snapvol = "snap_${volname}_$snap";
269
270 my $cmd = ['/sbin/lvcreate', '-n', $snapvol, '-pr', '-s', "$vg/$volname"];
271 run_command($cmd, errmsg => "lvcreate snapshot '$vg/$snapvol' error");
272
273}
274
275sub volume_snapshot_rollback {
276 my ($class, $scfg, $storeid, $volname, $snap) = @_;
277
278 my $vg = $scfg->{vgname};
279 my $snapvol = "snap_${volname}_$snap";
280
281 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
282 run_command($cmd, errmsg => "lvremove '$vg/$volname' error");
283
284 $cmd = ['/sbin/lvcreate', '-kn', '-n', $volname, '-s', "$vg/$snapvol"];
285 run_command($cmd, errmsg => "lvm rollback '$vg/$snapvol' error");
286}
287
288sub volume_snapshot_delete {
289 my ($class, $scfg, $storeid, $volname, $snap) = @_;
290
291 my $vg = $scfg->{vgname};
292 my $snapvol = "snap_${volname}_$snap";
293
294 my $cmd = ['/sbin/lvremove', '-f', "$vg/$snapvol"];
295 run_command($cmd, errmsg => "lvremove snapshot '$vg/$snapvol' error");
296}
297
610798bc
DM
298sub volume_has_feature {
299 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
300
301 my $features = {
663372bc 302 snapshot => { current => 1 },
f1b59efc
DM
303 clone => { base => 1},
304 template => { current => 1},
610798bc
DM
305 copy => { base => 1, current => 1},
306 };
307
308 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
309 $class->parse_volname($volname);
310
311 my $key = undef;
312 if($snapname){
313 $key = 'snap';
314 }else{
315 $key = $isBase ? 'base' : 'current';
316 }
317 return 1 if $features->{$feature}->{$key};
318
319 return undef;
320}
321
3221;