]>
Commit | Line | Data |
---|---|---|
610798bc DM |
1 | package PVE::Storage::LvmThinPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
15334c83 | 5 | use Data::Dumper; |
610798bc DM |
6 | use IO::File; |
7 | use PVE::Tools qw(run_command trim); | |
8 | use PVE::Storage::Plugin; | |
15334c83 | 9 | use PVE::Storage::LVMPlugin; |
610798bc DM |
10 | use 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 | 17 | |
787624df FG |
18 | # NOTE: volumes which were created as linked clones of another base volume |
19 | # are currently not tracking this relationship in their volume IDs. this is | |
20 | # generally not a problem, as LVM thin allows deletion of such base volumes | |
21 | # without affecting the linked clones. this leads to increased disk usage | |
22 | # when migrating LVM-thin volumes, which is normally prevented for linked clones. | |
23 | ||
610798bc DM |
24 | use base qw(PVE::Storage::LVMPlugin); |
25 | ||
26 | sub type { | |
27 | return 'lvmthin'; | |
28 | } | |
29 | ||
30 | sub plugindata { | |
31 | return { | |
32 | content => [ {images => 1, rootdir => 1}, { images => 1, rootdir => 1}], | |
33 | }; | |
34 | } | |
35 | ||
36 | sub properties { | |
37 | return { | |
38 | thinpool => { | |
39 | description => "LVM thin pool LV name.", | |
40 | type => 'string', format => 'pve-storage-vgname', | |
41 | }, | |
42 | }; | |
43 | } | |
44 | ||
45 | sub options { | |
46 | return { | |
47 | thinpool => { fixed => 1 }, | |
48 | vgname => { fixed => 1 }, | |
49 | nodes => { optional => 1 }, | |
50 | disable => { optional => 1 }, | |
51 | content => { optional => 1 }, | |
9edb99a5 | 52 | bwlimit => { optional => 1 }, |
610798bc DM |
53 | }; |
54 | } | |
55 | ||
787624df FG |
56 | # NOTE: the fourth and fifth element of the returned array are always |
57 | # undef, even if the volume is a linked clone of another volume. see note | |
58 | # at beginning of file. | |
f1b59efc DM |
59 | sub parse_volname { |
60 | my ($class, $volname) = @_; | |
61 | ||
62 | PVE::Storage::Plugin::parse_lvm_name($volname); | |
63 | ||
64 | if ($volname =~ m/^((vm|base)-(\d+)-\S+)$/) { | |
65 | return ('images', $1, $3, undef, undef, $2 eq 'base', 'raw'); | |
66 | } | |
67 | ||
68 | die "unable to parse lvm volume name '$volname'\n"; | |
69 | } | |
70 | ||
33adbdc4 DM |
71 | sub filesystem_path { |
72 | my ($class, $scfg, $volname, $snapname) = @_; | |
73 | ||
74 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
75 | ||
76 | my $vg = $scfg->{vgname}; | |
77 | ||
78 | my $path = defined($snapname) ? "/dev/$vg/snap_${name}_$snapname": "/dev/$vg/$name"; | |
79 | ||
80 | return wantarray ? ($path, $vmid, $vtype) : $path; | |
81 | } | |
82 | ||
610798bc DM |
83 | sub alloc_image { |
84 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
85 | ||
86 | die "unsupported format '$fmt'" if $fmt ne 'raw'; | |
87 | ||
15334c83 | 88 | die "illegal name '$name' - sould be 'vm-$vmid-*'\n" |
610798bc DM |
89 | if $name && $name !~ m/^vm-$vmid-/; |
90 | ||
f1b59efc | 91 | my $vgs = PVE::Storage::LVMPlugin::lvm_vgs(); |
5b41084a | 92 | |
15334c83 DM |
93 | my $vg = $scfg->{vgname}; |
94 | ||
5b41084a DM |
95 | die "no such volume group '$vg'\n" if !defined ($vgs->{$vg}); |
96 | ||
f1b59efc | 97 | my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg); |
15334c83 | 98 | |
f1b59efc DM |
99 | $name = PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid) |
100 | if !$name; | |
15334c83 DM |
101 | |
102 | my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name, | |
103 | '--thinpool', "$vg/$scfg->{thinpool}" ]; | |
104 | ||
105 | run_command($cmd, errmsg => "lvcreate '$vg/$name' error"); | |
106 | ||
107 | return $name; | |
610798bc DM |
108 | } |
109 | ||
110 | sub free_image { | |
111 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; | |
112 | ||
113 | my $vg = $scfg->{vgname}; | |
663372bc DM |
114 | |
115 | my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg); | |
116 | ||
117 | if (my $dat = $lvs->{$scfg->{vgname}}) { | |
118 | ||
119 | # remove all volume snapshots first | |
120 | foreach my $lv (keys %$dat) { | |
121 | next if $lv !~ m/^snap_${volname}_(\w+)$/; | |
122 | my $cmd = ['/sbin/lvremove', '-f', "$vg/$lv"]; | |
123 | run_command($cmd, errmsg => "lvremove snapshot '$vg/$lv' error"); | |
124 | } | |
125 | ||
126 | # finally remove original (if exists) | |
127 | if ($dat->{$volname}) { | |
128 | my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"]; | |
129 | run_command($cmd, errmsg => "lvremove '$vg/$volname' error"); | |
130 | } | |
131 | } | |
610798bc DM |
132 | |
133 | return undef; | |
134 | } | |
135 | ||
136 | sub list_images { | |
137 | my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; | |
138 | ||
15334c83 DM |
139 | my $vgname = $scfg->{vgname}; |
140 | ||
141 | $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs}; | |
142 | ||
143 | my $res = []; | |
144 | ||
145 | if (my $dat = $cache->{lvs}->{$vgname}) { | |
146 | ||
147 | foreach my $volname (keys %$dat) { | |
148 | ||
f1b59efc DM |
149 | next if $volname !~ m/^(vm|base)-(\d+)-/; |
150 | my $owner = $2; | |
15334c83 DM |
151 | |
152 | my $info = $dat->{$volname}; | |
153 | ||
154 | next if $info->{lv_type} ne 'V'; | |
155 | ||
156 | next if $info->{pool_lv} ne $scfg->{thinpool}; | |
157 | ||
158 | my $volid = "$storeid:$volname"; | |
159 | ||
160 | if ($vollist) { | |
161 | my $found = grep { $_ eq $volid } @$vollist; | |
162 | next if !$found; | |
163 | } else { | |
164 | next if defined($vmid) && ($owner ne $vmid); | |
165 | } | |
166 | ||
167 | push @$res, { | |
168 | volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner, | |
169 | }; | |
170 | } | |
171 | } | |
172 | ||
173 | return $res; | |
610798bc DM |
174 | } |
175 | ||
d0ea89e5 DC |
176 | sub list_thinpools { |
177 | my ($vg) = @_; | |
178 | ||
179 | my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg); | |
180 | my $thinpools = []; | |
181 | ||
182 | foreach my $lvname (keys %{$lvs->{$vg}}) { | |
183 | next if $lvs->{$vg}->{$lvname}->{lv_type} ne 't'; | |
184 | push @$thinpools, { lv => $lvname }; | |
185 | } | |
186 | ||
187 | return $thinpools; | |
188 | } | |
189 | ||
610798bc DM |
190 | sub status { |
191 | my ($class, $storeid, $scfg, $cache) = @_; | |
192 | ||
193 | my $lvname = "$scfg->{vgname}/$scfg->{thinpool}"; | |
15334c83 DM |
194 | |
195 | $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs}; | |
196 | ||
197 | my $lvs = $cache->{lvs}; | |
198 | ||
199 | return undef if !$lvs->{$scfg->{vgname}}; | |
200 | ||
201 | my $info = $lvs->{$scfg->{vgname}}->{$scfg->{thinpool}}; | |
202 | ||
203 | return undef if !$info; | |
204 | ||
205 | return undef if $info->{lv_type} ne 't'; | |
206 | ||
207 | return ($info->{lv_size}, $info->{lv_size} - $info->{used}, $info->{used}, 1) if $info->{lv_size}; | |
610798bc DM |
208 | |
209 | return undef; | |
210 | } | |
211 | ||
212 | sub activate_volume { | |
213 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
15334c83 | 214 | |
663372bc DM |
215 | my $vg = $scfg->{vgname}; |
216 | ||
217 | # only snapshot volumes needs activation | |
218 | if ($snapname) { | |
219 | my $snapvol = "snap_${volname}_$snapname"; | |
220 | my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$snapvol"]; | |
221 | run_command($cmd, errmsg => "activate_volume '$vg/$snapvol' error"); | |
f44e50fe WB |
222 | } elsif ($volname =~ /^base-/) { |
223 | my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$volname"]; | |
224 | run_command($cmd, errmsg => "activate_volume '$vg/$volname' error"); | |
663372bc DM |
225 | } else { |
226 | # other volumes are active by default | |
227 | } | |
610798bc DM |
228 | } |
229 | ||
230 | sub deactivate_volume { | |
231 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
15334c83 | 232 | |
663372bc DM |
233 | my $vg = $scfg->{vgname}; |
234 | ||
235 | # we only deactivate snapshot volumes | |
236 | if ($snapname) { | |
237 | my $snapvol = "snap_${volname}_$snapname"; | |
238 | my $cmd = ['/sbin/lvchange', '-an', "$vg/$snapvol"]; | |
239 | run_command($cmd, errmsg => "deactivate_volume '$vg/$snapvol' error"); | |
f44e50fe WB |
240 | } elsif ($volname =~ /^base-/) { |
241 | my $cmd = ['/sbin/lvchange', '-an', "$vg/$volname"]; | |
242 | run_command($cmd, errmsg => "deactivate_volume '$vg/$volname' error"); | |
663372bc DM |
243 | } else { |
244 | # other volumes are kept active | |
245 | } | |
610798bc DM |
246 | } |
247 | ||
f1b59efc DM |
248 | sub clone_image { |
249 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; | |
250 | ||
030bc5c8 | 251 | my $vg = $scfg->{vgname}; |
f1b59efc | 252 | |
030bc5c8 | 253 | my $lv; |
f1b59efc | 254 | |
030bc5c8 DM |
255 | if ($snap) { |
256 | $lv = "$vg/snap_${volname}_$snap"; | |
257 | } else { | |
258 | my ($vtype, undef, undef, undef, undef, $isBase, $format) = | |
259 | $class->parse_volname($volname); | |
260 | ||
261 | die "clone_image only works on base images\n" if !$isBase; | |
262 | ||
263 | $lv = "$vg/$volname"; | |
264 | } | |
f1b59efc | 265 | |
f1b59efc DM |
266 | my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg); |
267 | ||
268 | my $name = PVE::Storage::LVMPlugin::lvm_find_free_diskname($lvs, $vg, $storeid, $vmid); | |
269 | ||
030bc5c8 DM |
270 | my $cmd = ['/sbin/lvcreate', '-n', $name, '-prw', '-kn', '-s', $lv]; |
271 | run_command($cmd, errmsg => "clone image '$lv' error"); | |
f1b59efc DM |
272 | |
273 | return $name; | |
274 | } | |
275 | ||
276 | sub create_base { | |
277 | my ($class, $storeid, $scfg, $volname) = @_; | |
278 | ||
279 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
280 | $class->parse_volname($volname); | |
281 | ||
282 | die "create_base not possible with base image\n" if $isBase; | |
283 | ||
284 | my $vg = $scfg->{vgname}; | |
285 | my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg); | |
286 | ||
287 | if (my $dat = $lvs->{$vg}) { | |
288 | # to avoid confusion, reject if we find volume snapshots | |
289 | foreach my $lv (keys %$dat) { | |
290 | die "unable to create base volume - found snaphost '$lv'\n" | |
291 | if $lv =~ m/^snap_${volname}_(\w+)$/; | |
292 | } | |
293 | } | |
294 | ||
295 | my $newname = $name; | |
296 | $newname =~ s/^vm-/base-/; | |
297 | ||
298 | my $cmd = ['/sbin/lvrename', $vg, $volname, $newname]; | |
299 | run_command($cmd, errmsg => "lvrename '$vg/$volname' => '$vg/$newname' error"); | |
300 | ||
301 | # set inactive, read-only and activationskip flags | |
302 | $cmd = ['/sbin/lvchange', '-an', '-pr', '-ky', "$vg/$newname"]; | |
303 | eval { run_command($cmd); }; | |
304 | warn $@ if $@; | |
305 | ||
306 | my $newvolname = $newname; | |
307 | ||
308 | return $newvolname; | |
309 | } | |
310 | ||
a9f0c6c4 | 311 | # sub volume_resize {} reuse code from parent class |
610798bc | 312 | |
663372bc DM |
313 | sub volume_snapshot { |
314 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
315 | ||
316 | my $vg = $scfg->{vgname}; | |
317 | my $snapvol = "snap_${volname}_$snap"; | |
318 | ||
319 | my $cmd = ['/sbin/lvcreate', '-n', $snapvol, '-pr', '-s', "$vg/$volname"]; | |
320 | run_command($cmd, errmsg => "lvcreate snapshot '$vg/$snapvol' error"); | |
321 | ||
322 | } | |
323 | ||
324 | sub volume_snapshot_rollback { | |
325 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
326 | ||
327 | my $vg = $scfg->{vgname}; | |
328 | my $snapvol = "snap_${volname}_$snap"; | |
329 | ||
330 | my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"]; | |
331 | run_command($cmd, errmsg => "lvremove '$vg/$volname' error"); | |
332 | ||
333 | $cmd = ['/sbin/lvcreate', '-kn', '-n', $volname, '-s', "$vg/$snapvol"]; | |
334 | run_command($cmd, errmsg => "lvm rollback '$vg/$snapvol' error"); | |
335 | } | |
336 | ||
337 | sub volume_snapshot_delete { | |
338 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
339 | ||
340 | my $vg = $scfg->{vgname}; | |
341 | my $snapvol = "snap_${volname}_$snap"; | |
342 | ||
343 | my $cmd = ['/sbin/lvremove', '-f', "$vg/$snapvol"]; | |
344 | run_command($cmd, errmsg => "lvremove snapshot '$vg/$snapvol' error"); | |
345 | } | |
346 | ||
610798bc DM |
347 | sub volume_has_feature { |
348 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
349 | ||
350 | my $features = { | |
663372bc | 351 | snapshot => { current => 1 }, |
030bc5c8 | 352 | clone => { base => 1, snap => 1}, |
f1b59efc | 353 | template => { current => 1}, |
30650d3c | 354 | copy => { base => 1, current => 1, snap => 1}, |
baafddbd | 355 | sparseinit => { base => 1, current => 1}, |
610798bc DM |
356 | }; |
357 | ||
358 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
359 | $class->parse_volname($volname); | |
360 | ||
361 | my $key = undef; | |
362 | if($snapname){ | |
363 | $key = 'snap'; | |
364 | }else{ | |
365 | $key = $isBase ? 'base' : 'current'; | |
366 | } | |
367 | return 1 if $features->{$feature}->{$key}; | |
368 | ||
369 | return undef; | |
370 | } | |
371 | ||
372 | 1; |