]> git.proxmox.com Git - pve-storage.git/blame - PVE/Storage/LvmThinPlugin.pm
new helper lvm_find_free_diskname()
[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
49sub alloc_image {
50 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
51
52 die "unsupported format '$fmt'" if $fmt ne 'raw';
53
15334c83 54 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
610798bc
DM
55 if $name && $name !~ m/^vm-$vmid-/;
56
5b41084a
DM
57 my $vgs = PVE::Storage::lvm_vgs();
58
15334c83
DM
59 my $vg = $scfg->{vgname};
60
5b41084a
DM
61 die "no such volume group '$vg'\n" if !defined ($vgs->{$vg});
62
15334c83
DM
63 if (!$name) {
64 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($scfg->{vgname});
65
66 for (my $i = 1; $i < 100; $i++) {
67 my $tn = "vm-$vmid-disk-$i";
68 if (!defined ($lvs->{$vg}->{$tn})) {
69 $name = $tn;
70 last;
71 }
72 }
73 }
74
75 my $cmd = ['/sbin/lvcreate', '-aly', '-V', "${size}k", '--name', $name,
76 '--thinpool', "$vg/$scfg->{thinpool}" ];
77
78 run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
79
80 return $name;
610798bc
DM
81}
82
83sub free_image {
84 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
85
86 my $vg = $scfg->{vgname};
663372bc
DM
87
88 my $lvs = PVE::Storage::LVMPlugin::lvm_list_volumes($vg);
89
90 if (my $dat = $lvs->{$scfg->{vgname}}) {
91
92 # remove all volume snapshots first
93 foreach my $lv (keys %$dat) {
94 next if $lv !~ m/^snap_${volname}_(\w+)$/;
95 my $cmd = ['/sbin/lvremove', '-f', "$vg/$lv"];
96 run_command($cmd, errmsg => "lvremove snapshot '$vg/$lv' error");
97 }
98
99 # finally remove original (if exists)
100 if ($dat->{$volname}) {
101 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
102 run_command($cmd, errmsg => "lvremove '$vg/$volname' error");
103 }
104 }
610798bc
DM
105
106 return undef;
107}
108
109sub list_images {
110 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
111
15334c83
DM
112 my $vgname = $scfg->{vgname};
113
114 $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs};
115
116 my $res = [];
117
118 if (my $dat = $cache->{lvs}->{$vgname}) {
119
120 foreach my $volname (keys %$dat) {
121
122 next if $volname !~ m/^vm-(\d+)-/;
123 my $owner = $1;
124
125 my $info = $dat->{$volname};
126
127 next if $info->{lv_type} ne 'V';
128
129 next if $info->{pool_lv} ne $scfg->{thinpool};
130
131 my $volid = "$storeid:$volname";
132
133 if ($vollist) {
134 my $found = grep { $_ eq $volid } @$vollist;
135 next if !$found;
136 } else {
137 next if defined($vmid) && ($owner ne $vmid);
138 }
139
140 push @$res, {
141 volid => $volid, format => 'raw', size => $info->{lv_size}, vmid => $owner,
142 };
143 }
144 }
145
146 return $res;
610798bc
DM
147}
148
149sub status {
150 my ($class, $storeid, $scfg, $cache) = @_;
151
152 my $lvname = "$scfg->{vgname}/$scfg->{thinpool}";
15334c83
DM
153
154 $cache->{lvs} = PVE::Storage::LVMPlugin::lvm_list_volumes() if !$cache->{lvs};
155
156 my $lvs = $cache->{lvs};
157
158 return undef if !$lvs->{$scfg->{vgname}};
159
160 my $info = $lvs->{$scfg->{vgname}}->{$scfg->{thinpool}};
161
162 return undef if !$info;
163
164 return undef if $info->{lv_type} ne 't';
165
166 return ($info->{lv_size}, $info->{lv_size} - $info->{used}, $info->{used}, 1) if $info->{lv_size};
610798bc
DM
167
168 return undef;
169}
170
171sub activate_volume {
172 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
15334c83 173
663372bc
DM
174 my $vg = $scfg->{vgname};
175
176 # only snapshot volumes needs activation
177 if ($snapname) {
178 my $snapvol = "snap_${volname}_$snapname";
179 my $cmd = ['/sbin/lvchange', '-ay', '-K', "$vg/$snapvol"];
180 run_command($cmd, errmsg => "activate_volume '$vg/$snapvol' error");
181 } else {
182 # other volumes are active by default
183 }
610798bc
DM
184}
185
186sub deactivate_volume {
187 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
15334c83 188
663372bc
DM
189 my $vg = $scfg->{vgname};
190
191 # we only deactivate snapshot volumes
192 if ($snapname) {
193 my $snapvol = "snap_${volname}_$snapname";
194 my $cmd = ['/sbin/lvchange', '-an', "$vg/$snapvol"];
195 run_command($cmd, errmsg => "deactivate_volume '$vg/$snapvol' error");
196 } else {
197 # other volumes are kept active
198 }
610798bc
DM
199}
200
a9f0c6c4 201# sub volume_resize {} reuse code from parent class
610798bc 202
663372bc
DM
203sub volume_snapshot {
204 my ($class, $scfg, $storeid, $volname, $snap) = @_;
205
206 my $vg = $scfg->{vgname};
207 my $snapvol = "snap_${volname}_$snap";
208
209 my $cmd = ['/sbin/lvcreate', '-n', $snapvol, '-pr', '-s', "$vg/$volname"];
210 run_command($cmd, errmsg => "lvcreate snapshot '$vg/$snapvol' error");
211
212}
213
214sub volume_snapshot_rollback {
215 my ($class, $scfg, $storeid, $volname, $snap) = @_;
216
217 my $vg = $scfg->{vgname};
218 my $snapvol = "snap_${volname}_$snap";
219
220 my $cmd = ['/sbin/lvremove', '-f', "$vg/$volname"];
221 run_command($cmd, errmsg => "lvremove '$vg/$volname' error");
222
223 $cmd = ['/sbin/lvcreate', '-kn', '-n', $volname, '-s', "$vg/$snapvol"];
224 run_command($cmd, errmsg => "lvm rollback '$vg/$snapvol' error");
225}
226
227sub volume_snapshot_delete {
228 my ($class, $scfg, $storeid, $volname, $snap) = @_;
229
230 my $vg = $scfg->{vgname};
231 my $snapvol = "snap_${volname}_$snap";
232
233 my $cmd = ['/sbin/lvremove', '-f', "$vg/$snapvol"];
234 run_command($cmd, errmsg => "lvremove snapshot '$vg/$snapvol' error");
235}
236
610798bc
DM
237sub volume_has_feature {
238 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
239
240 my $features = {
663372bc 241 snapshot => { current => 1 },
610798bc
DM
242 copy => { base => 1, current => 1},
243 };
244
245 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
246 $class->parse_volname($volname);
247
248 my $key = undef;
249 if($snapname){
250 $key = 'snap';
251 }else{
252 $key = $isBase ? 'base' : 'current';
253 }
254 return 1 if $features->{$feature}->{$key};
255
256 return undef;
257}
258
2591;