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