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