]> git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ZFSPlugin.pm
zfspoolplugin: fix volume_resize
[pve-storage.git] / PVE / Storage / ZFSPlugin.pm
1 package PVE::Storage::ZFSPlugin;
2
3 use strict;
4 use warnings;
5 use IO::File;
6 use POSIX;
7 use PVE::Tools qw(run_command);
8 use PVE::Storage::ZFSPoolPlugin;
9
10 use base qw(PVE::Storage::ZFSPoolPlugin);
11 use PVE::Storage::LunCmd::Comstar;
12 use PVE::Storage::LunCmd::Istgt;
13 use PVE::Storage::LunCmd::Iet;
14
15 my @ssh_opts = ('-o', 'BatchMode=yes');
16 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
17 my $id_rsa_path = '/etc/pve/priv/zfs';
18
19 my $lun_cmds = {
20 create_lu => 1,
21 delete_lu => 1,
22 import_lu => 1,
23 modify_lu => 1,
24 add_view => 1,
25 list_view => 1,
26 list_lu => 1,
27 };
28
29 my $zfs_unknown_scsi_provider = sub {
30 my ($provider) = @_;
31
32 die "$provider: unknown iscsi provider. Available [comstar, istgt, iet]";
33 };
34
35 my $zfs_get_base = sub {
36 my ($scfg) = @_;
37
38 if ($scfg->{iscsiprovider} eq 'comstar') {
39 return PVE::Storage::LunCmd::Comstar::get_base;
40 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
41 return PVE::Storage::LunCmd::Istgt::get_base;
42 } elsif ($scfg->{iscsiprovider} eq 'iet') {
43 return PVE::Storage::LunCmd::Iet::get_base;
44 } else {
45 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
46 }
47 };
48
49 sub zfs_request {
50 my ($class, $scfg, $timeout, $method, @params) = @_;
51
52 $timeout = 5 if !$timeout;
53
54 my $msg = '';
55
56 if ($lun_cmds->{$method}) {
57 if ($scfg->{iscsiprovider} eq 'comstar') {
58 $msg = PVE::Storage::LunCmd::Comstar::run_lun_command($scfg, $timeout, $method, @params);
59 } elsif ($scfg->{iscsiprovider} eq 'istgt') {
60 $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params);
61 } elsif ($scfg->{iscsiprovider} eq 'iet') {
62 $msg = PVE::Storage::LunCmd::Iet::run_lun_command($scfg, $timeout, $method, @params);
63 } else {
64 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider});
65 }
66 } else {
67
68 my $target = 'root@' . $scfg->{portal};
69
70 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target];
71
72 if ($method eq 'zpool_list') {
73 push @$cmd, 'zpool', 'list';
74 } else {
75 push @$cmd, 'zfs', $method;
76 }
77
78 push @$cmd, @params;
79
80 my $output = sub {
81 my $line = shift;
82 $msg .= "$line\n";
83 };
84
85 run_command($cmd, outfunc => $output, timeout => $timeout);
86 }
87
88 return $msg;
89 }
90
91 sub zfs_get_lu_name {
92 my ($class, $scfg, $zvol) = @_;
93
94 my $base = $zfs_get_base->($scfg);
95
96 my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol" : "$base/$scfg->{pool}/$zvol";
97
98 my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
99
100 return $lu_name if $lu_name;
101
102 die "Could not find lu_name for zvol $zvol";
103 }
104
105 sub zfs_add_lun_mapping_entry {
106 my ($class, $scfg, $zvol, $guid) = @_;
107
108 if (!defined($guid)) {
109 $guid = $class->zfs_get_lu_name($scfg, $zvol);
110 }
111
112 $class->zfs_request($scfg, undef, 'add_view', $guid);
113 }
114
115 sub zfs_delete_lu {
116 my ($class, $scfg, $zvol) = @_;
117
118 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
119
120 $class->zfs_request($scfg, undef, 'delete_lu', $guid);
121 }
122
123 sub zfs_create_lu {
124 my ($class, $scfg, $zvol) = @_;
125
126 my $base = $zfs_get_base->($scfg);
127 my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol");
128
129 return $guid;
130 }
131
132 sub zfs_import_lu {
133 my ($class, $scfg, $zvol) = @_;
134
135 my $base = $zfs_get_base->($scfg);
136 $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol");
137 }
138
139 sub zfs_resize_lu {
140 my ($class, $scfg, $zvol, $size) = @_;
141
142 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
143
144 $class->zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid);
145 }
146
147 sub zfs_get_lun_number {
148 my ($class, $scfg, $guid) = @_;
149
150 die "could not find lun_number for guid $guid" if !$guid;
151
152 return $class->zfs_request($scfg, undef, 'list_view', $guid);
153 }
154
155 # Configuration
156
157 sub type {
158 return 'zfs';
159 }
160
161 sub plugindata {
162 return {
163 content => [ {images => 1}, { images => 1 }],
164 };
165 }
166
167 sub properties {
168 return {
169 iscsiprovider => {
170 description => "iscsi provider",
171 type => 'string',
172 },
173 # this will disable write caching on comstar and istgt.
174 # it is not implemented for iet. iet blockio always operates with
175 # writethrough caching when not in readonly mode
176 nowritecache => {
177 description => "disable write caching on the target",
178 type => 'boolean',
179 },
180 comstar_tg => {
181 description => "target group for comstar views",
182 type => 'string',
183 },
184 comstar_hg => {
185 description => "host group for comstar views",
186 type => 'string',
187 },
188 };
189 }
190
191 sub options {
192 return {
193 nodes => { optional => 1 },
194 disable => { optional => 1 },
195 portal => { fixed => 1 },
196 target => { fixed => 1 },
197 pool => { fixed => 1 },
198 blocksize => { fixed => 1 },
199 iscsiprovider => { fixed => 1 },
200 nowritecache => { optional => 1 },
201 sparse => { optional => 1 },
202 comstar_hg => { optional => 1 },
203 comstar_tg => { optional => 1 },
204 content => { optional => 1 },
205 };
206 }
207
208 # Storage implementation
209
210 sub path {
211 my ($class, $scfg, $volname) = @_;
212
213 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
214
215 my $target = $scfg->{target};
216 my $portal = $scfg->{portal};
217
218 my $guid = $class->zfs_get_lu_name($scfg, $name);
219 my $lun = $class->zfs_get_lun_number($scfg, $guid);
220
221 my $path = "iscsi://$portal/$target/$lun";
222
223 return ($path, $vmid, $vtype);
224 }
225
226 sub create_base {
227 my ($class, $storeid, $scfg, $volname) = @_;
228
229 my $snap = '__base__';
230
231 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
232 $class->parse_volname($volname);
233
234 die "create_base not possible with base image\n" if $isBase;
235
236 my $newname = $name;
237 $newname =~ s/^vm-/base-/;
238
239 my $newvolname = $basename ? "$basename/$newname" : "$newname";
240
241 $class->zfs_delete_lu($scfg, $name);
242 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname");
243
244 my $guid = $class->zfs_create_lu($scfg, $newname);
245 $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
246
247 my $running = undef; #fixme : is create_base always offline ?
248
249 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
250
251 return $newvolname;
252 }
253
254 sub clone_image {
255 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
256
257 my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
258
259 my $guid = $class->zfs_create_lu($scfg, $name);
260 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
261
262 return $name;
263 }
264
265 sub alloc_image {
266 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
267
268 die "unsupported format '$fmt'" if $fmt ne 'raw';
269
270 die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
271 if $name && $name !~ m/^vm-$vmid-/;
272
273 my $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid) if !$name;
274
275 $class->zfs_create_zvol($scfg, $name, $size);
276
277 my $guid = $class->zfs_create_lu($scfg, $volname);
278 $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
279
280 return $volname;
281 }
282
283 sub free_image {
284 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
285
286 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
287
288 $class->zfs_delete_lu($scfg, $name);
289
290 eval { $class->zfs_delete_zvol($scfg, $name); };
291 if (my $err = $@) {
292 my $guid = $class->zfs_create_lu($scfg, $name);
293 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
294 die $err;
295 }
296
297 return undef;
298 }
299
300 sub volume_resize {
301 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
302
303 my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
304
305 $class->zfs_resize_lu($scfg, $volname, $new_size);
306
307 return $new_size;
308 }
309
310 sub volume_snapshot_rollback {
311 my ($class, $scfg, $storeid, $volname, $snap) = @_;
312
313 # abort rollback if snapshot is not the latest
314 my $recentsnap = $class->zfs_get_latest_snapshot($scfg, $volname);
315 if ($snap ne $recentsnap) {
316 die "cannot rollback, more recent snapshots exist\n";
317 }
318
319 $class->zfs_delete_lu($scfg, $volname);
320
321 $class->zfs_request($class, $scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap");
322
323 $class->zfs_import_lu($scfg, $volname);
324
325 $class->zfs_add_lun_mapping_entry($scfg, $volname);
326 }
327
328 sub volume_has_feature {
329 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
330
331 my $features = {
332 snapshot => { current => 1, snap => 1},
333 clone => { base => 1},
334 template => { current => 1},
335 copy => { base => 1, current => 1},
336 };
337
338 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
339 $class->parse_volname($volname);
340
341 my $key = undef;
342
343 if ($snapname) {
344 $key = 'snap';
345 } else {
346 $key = $isBase ? 'base' : 'current';
347 }
348
349 return 1 if $features->{$feature}->{$key};
350
351 return undef;
352 }
353
354 sub activate_storage {
355 my ($class, $storeid, $scfg, $cache) = @_;
356 return 1;
357 }
358
359 1;