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