]>
Commit | Line | Data |
---|---|---|
4f914e6e MR |
1 | package PVE::Storage::ZFSPlugin; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use IO::File; | |
6 | use POSIX; | |
5332e6c9 | 7 | use PVE::Tools qw(run_command); |
85fda4dd | 8 | use PVE::Storage::ZFSPoolPlugin; |
21430e50 | 9 | use PVE::RPCEnvironment; |
4f914e6e | 10 | |
85fda4dd | 11 | use base qw(PVE::Storage::ZFSPoolPlugin); |
a7d56be6 MR |
12 | use PVE::Storage::LunCmd::Comstar; |
13 | use PVE::Storage::LunCmd::Istgt; | |
78a64432 | 14 | use PVE::Storage::LunCmd::Iet; |
46c6107e | 15 | use PVE::Storage::LunCmd::LIO; |
4f914e6e | 16 | |
21430e50 | 17 | |
4f914e6e MR |
18 | my @ssh_opts = ('-o', 'BatchMode=yes'); |
19 | my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts); | |
3b219e80 | 20 | my $id_rsa_path = '/etc/pve/priv/zfs'; |
4f914e6e | 21 | |
a7d56be6 | 22 | my $lun_cmds = { |
3b219e80 MR |
23 | create_lu => 1, |
24 | delete_lu => 1, | |
25 | import_lu => 1, | |
26 | modify_lu => 1, | |
27 | add_view => 1, | |
28 | list_view => 1, | |
29 | list_lu => 1, | |
a7d56be6 MR |
30 | }; |
31 | ||
32 | my $zfs_unknown_scsi_provider = sub { | |
3b219e80 | 33 | my ($provider) = @_; |
a7d56be6 | 34 | |
46c6107e | 35 | die "$provider: unknown iscsi provider. Available [comstar, istgt, iet, LIO]"; |
a7d56be6 MR |
36 | }; |
37 | ||
38 | my $zfs_get_base = sub { | |
3b219e80 MR |
39 | my ($scfg) = @_; |
40 | ||
41 | if ($scfg->{iscsiprovider} eq 'comstar') { | |
42 | return PVE::Storage::LunCmd::Comstar::get_base; | |
43 | } elsif ($scfg->{iscsiprovider} eq 'istgt') { | |
44 | return PVE::Storage::LunCmd::Istgt::get_base; | |
45 | } elsif ($scfg->{iscsiprovider} eq 'iet') { | |
46 | return PVE::Storage::LunCmd::Iet::get_base; | |
46c6107e UR |
47 | } elsif ($scfg->{iscsiprovider} eq 'LIO') { |
48 | return PVE::Storage::LunCmd::LIO::get_base; | |
3b219e80 MR |
49 | } else { |
50 | $zfs_unknown_scsi_provider->($scfg->{iscsiprovider}); | |
51 | } | |
a7d56be6 MR |
52 | }; |
53 | ||
4f914e6e | 54 | sub zfs_request { |
7730694e | 55 | my ($class, $scfg, $timeout, $method, @params) = @_; |
4f914e6e | 56 | |
ef881e10 | 57 | $timeout = PVE::RPCEnvironment->is_worker() ? 60*60 : 10 |
21430e50 | 58 | if !$timeout; |
4f914e6e | 59 | |
e8004c9c DM |
60 | my $msg = ''; |
61 | ||
3b219e80 MR |
62 | if ($lun_cmds->{$method}) { |
63 | if ($scfg->{iscsiprovider} eq 'comstar') { | |
64 | $msg = PVE::Storage::LunCmd::Comstar::run_lun_command($scfg, $timeout, $method, @params); | |
65 | } elsif ($scfg->{iscsiprovider} eq 'istgt') { | |
66 | $msg = PVE::Storage::LunCmd::Istgt::run_lun_command($scfg, $timeout, $method, @params); | |
67 | } elsif ($scfg->{iscsiprovider} eq 'iet') { | |
68 | $msg = PVE::Storage::LunCmd::Iet::run_lun_command($scfg, $timeout, $method, @params); | |
46c6107e UR |
69 | } elsif ($scfg->{iscsiprovider} eq 'LIO') { |
70 | $msg = PVE::Storage::LunCmd::LIO::run_lun_command($scfg, $timeout, $method, @params); | |
3b219e80 MR |
71 | } else { |
72 | $zfs_unknown_scsi_provider->($scfg->{iscsiprovider}); | |
73 | } | |
74 | } else { | |
3b219e80 | 75 | |
e8004c9c DM |
76 | my $target = 'root@' . $scfg->{portal}; |
77 | ||
78 | my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target]; | |
3b219e80 | 79 | |
e8004c9c DM |
80 | if ($method eq 'zpool_list') { |
81 | push @$cmd, 'zpool', 'list'; | |
82 | } else { | |
83 | push @$cmd, 'zfs', $method; | |
84 | } | |
3b219e80 | 85 | |
e8004c9c | 86 | push @$cmd, @params; |
3b219e80 | 87 | |
e8004c9c DM |
88 | my $output = sub { |
89 | my $line = shift; | |
90 | $msg .= "$line\n"; | |
3b219e80 MR |
91 | }; |
92 | ||
93 | run_command($cmd, outfunc => $output, timeout => $timeout); | |
94 | } | |
4f914e6e MR |
95 | |
96 | return $msg; | |
97 | } | |
98 | ||
4f914e6e | 99 | sub zfs_get_lu_name { |
7730694e | 100 | my ($class, $scfg, $zvol) = @_; |
4f914e6e | 101 | |
3b219e80 | 102 | my $base = $zfs_get_base->($scfg); |
34eaae3f | 103 | |
c243be83 DB |
104 | $zvol = ($class->parse_volname($zvol))[1]; |
105 | ||
34eaae3f | 106 | my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol" : "$base/$scfg->{pool}/$zvol"; |
4f914e6e | 107 | |
7730694e | 108 | my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object); |
a7d56be6 | 109 | |
3b219e80 MR |
110 | return $lu_name if $lu_name; |
111 | ||
4f914e6e MR |
112 | die "Could not find lu_name for zvol $zvol"; |
113 | } | |
114 | ||
4f914e6e | 115 | sub zfs_add_lun_mapping_entry { |
7730694e | 116 | my ($class, $scfg, $zvol, $guid) = @_; |
4f914e6e | 117 | |
6b5bca68 DM |
118 | if (!defined($guid)) { |
119 | $guid = $class->zfs_get_lu_name($scfg, $zvol); | |
4f914e6e | 120 | } |
3b219e80 | 121 | |
7730694e | 122 | $class->zfs_request($scfg, undef, 'add_view', $guid); |
4f914e6e MR |
123 | } |
124 | ||
125 | sub zfs_delete_lu { | |
7730694e | 126 | my ($class, $scfg, $zvol) = @_; |
4f914e6e | 127 | |
7730694e | 128 | my $guid = $class->zfs_get_lu_name($scfg, $zvol); |
4f914e6e | 129 | |
7730694e | 130 | $class->zfs_request($scfg, undef, 'delete_lu', $guid); |
4f914e6e MR |
131 | } |
132 | ||
133 | sub zfs_create_lu { | |
7730694e | 134 | my ($class, $scfg, $zvol) = @_; |
4f914e6e | 135 | |
3b219e80 | 136 | my $base = $zfs_get_base->($scfg); |
7730694e | 137 | my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool}/$zvol"); |
4f914e6e MR |
138 | |
139 | return $guid; | |
140 | } | |
141 | ||
142 | sub zfs_import_lu { | |
7730694e | 143 | my ($class, $scfg, $zvol) = @_; |
4f914e6e | 144 | |
3b219e80 | 145 | my $base = $zfs_get_base->($scfg); |
7730694e | 146 | $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool}/$zvol"); |
4f914e6e MR |
147 | } |
148 | ||
149 | sub zfs_resize_lu { | |
7730694e | 150 | my ($class, $scfg, $zvol, $size) = @_; |
4f914e6e | 151 | |
7730694e | 152 | my $guid = $class->zfs_get_lu_name($scfg, $zvol); |
4f914e6e | 153 | |
7730694e | 154 | $class->zfs_request($scfg, undef, 'modify_lu', "${size}K", $guid); |
4f914e6e MR |
155 | } |
156 | ||
157 | sub zfs_get_lun_number { | |
7730694e | 158 | my ($class, $scfg, $guid) = @_; |
4f914e6e MR |
159 | |
160 | die "could not find lun_number for guid $guid" if !$guid; | |
161 | ||
609f117f SI |
162 | if ($class->zfs_request($scfg, undef, 'list_view', $guid) =~ /^(\d+)$/) { |
163 | return $1; | |
164 | } | |
165 | ||
166 | die "lun_number for guid $guid is not a number"; | |
4f914e6e MR |
167 | } |
168 | ||
169 | # Configuration | |
170 | ||
171 | sub type { | |
172 | return 'zfs'; | |
173 | } | |
174 | ||
175 | sub plugindata { | |
176 | return { | |
6b5bca68 | 177 | content => [ {images => 1}, { images => 1 }], |
4f914e6e MR |
178 | }; |
179 | } | |
180 | ||
181 | sub properties { | |
182 | return { | |
6b5bca68 DM |
183 | iscsiprovider => { |
184 | description => "iscsi provider", | |
185 | type => 'string', | |
186 | }, | |
187 | # this will disable write caching on comstar and istgt. | |
188 | # it is not implemented for iet. iet blockio always operates with | |
189 | # writethrough caching when not in readonly mode | |
190 | nowritecache => { | |
191 | description => "disable write caching on the target", | |
192 | type => 'boolean', | |
193 | }, | |
194 | comstar_tg => { | |
195 | description => "target group for comstar views", | |
196 | type => 'string', | |
197 | }, | |
198 | comstar_hg => { | |
199 | description => "host group for comstar views", | |
200 | type => 'string', | |
201 | }, | |
46c6107e UR |
202 | lio_tpg => { |
203 | description => "target portal group for Linux LIO targets", | |
204 | type => 'string', | |
205 | }, | |
4f914e6e MR |
206 | }; |
207 | } | |
208 | ||
209 | sub options { | |
210 | return { | |
6b5bca68 DM |
211 | nodes => { optional => 1 }, |
212 | disable => { optional => 1 }, | |
213 | portal => { fixed => 1 }, | |
214 | target => { fixed => 1 }, | |
215 | pool => { fixed => 1 }, | |
216 | blocksize => { fixed => 1 }, | |
217 | iscsiprovider => { fixed => 1 }, | |
218 | nowritecache => { optional => 1 }, | |
219 | sparse => { optional => 1 }, | |
220 | comstar_hg => { optional => 1 }, | |
221 | comstar_tg => { optional => 1 }, | |
46c6107e | 222 | lio_tpg => { optional => 1 }, |
6b5bca68 | 223 | content => { optional => 1 }, |
9edb99a5 | 224 | bwlimit => { optional => 1 }, |
4f914e6e MR |
225 | }; |
226 | } | |
227 | ||
228 | # Storage implementation | |
229 | ||
4f914e6e | 230 | sub path { |
e67069eb DM |
231 | my ($class, $scfg, $volname, $storeid, $snapname) = @_; |
232 | ||
233 | die "direct access to snapshots not implemented" | |
234 | if defined($snapname); | |
4f914e6e MR |
235 | |
236 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
237 | ||
238 | my $target = $scfg->{target}; | |
239 | my $portal = $scfg->{portal}; | |
240 | ||
7730694e DM |
241 | my $guid = $class->zfs_get_lu_name($scfg, $name); |
242 | my $lun = $class->zfs_get_lun_number($scfg, $guid); | |
3b219e80 | 243 | |
4f914e6e | 244 | my $path = "iscsi://$portal/$target/$lun"; |
3b219e80 | 245 | |
4f914e6e MR |
246 | return ($path, $vmid, $vtype); |
247 | } | |
248 | ||
4f914e6e MR |
249 | sub create_base { |
250 | my ($class, $storeid, $scfg, $volname) = @_; | |
251 | ||
252 | my $snap = '__base__'; | |
253 | ||
254 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
255 | $class->parse_volname($volname); | |
256 | ||
257 | die "create_base not possible with base image\n" if $isBase; | |
258 | ||
259 | my $newname = $name; | |
260 | $newname =~ s/^vm-/base-/; | |
261 | ||
262 | my $newvolname = $basename ? "$basename/$newname" : "$newname"; | |
263 | ||
7730694e DM |
264 | $class->zfs_delete_lu($scfg, $name); |
265 | $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool}/$name", "$scfg->{pool}/$newname"); | |
4f914e6e | 266 | |
7730694e DM |
267 | my $guid = $class->zfs_create_lu($scfg, $newname); |
268 | $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid); | |
4f914e6e MR |
269 | |
270 | my $running = undef; #fixme : is create_base always offline ? | |
271 | ||
272 | $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); | |
273 | ||
274 | return $newvolname; | |
275 | } | |
276 | ||
277 | sub clone_image { | |
f236eaf8 | 278 | my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_; |
4f914e6e | 279 | |
1993540b | 280 | my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap); |
4f914e6e | 281 | |
1993540b FG |
282 | # get ZFS dataset name from PVE volname |
283 | my (undef, $clonedname) = $class->parse_volname($name); | |
284 | ||
285 | my $guid = $class->zfs_create_lu($scfg, $clonedname); | |
286 | $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid); | |
4f914e6e MR |
287 | |
288 | return $name; | |
289 | } | |
290 | ||
291 | sub alloc_image { | |
292 | my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; | |
b3ba95e4 | 293 | |
9b61bc9e WL |
294 | die "unsupported format '$fmt'" if $fmt ne 'raw'; |
295 | ||
ffc31266 | 296 | die "illegal name '$name' - should be 'vm-$vmid-*'\n" |
9b61bc9e WL |
297 | if $name && $name !~ m/^vm-$vmid-/; |
298 | ||
4eff810d WL |
299 | my $volname = $name; |
300 | ||
a44c0147 | 301 | $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname; |
9b61bc9e | 302 | |
4eff810d | 303 | $class->zfs_create_zvol($scfg, $volname, $size); |
b3ba95e4 WL |
304 | |
305 | my $guid = $class->zfs_create_lu($scfg, $volname); | |
306 | $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid); | |
4f914e6e | 307 | |
b3ba95e4 | 308 | return $volname; |
4f914e6e MR |
309 | } |
310 | ||
311 | sub free_image { | |
312 | my ($class, $storeid, $scfg, $volname, $isBase) = @_; | |
313 | ||
314 | my ($vtype, $name, $vmid) = $class->parse_volname($volname); | |
315 | ||
7730694e | 316 | $class->zfs_delete_lu($scfg, $name); |
6b5bca68 DM |
317 | |
318 | eval { $class->zfs_delete_zvol($scfg, $name); }; | |
319 | if (my $err = $@) { | |
7730694e DM |
320 | my $guid = $class->zfs_create_lu($scfg, $name); |
321 | $class->zfs_add_lun_mapping_entry($scfg, $name, $guid); | |
4f914e6e | 322 | die $err; |
6b5bca68 | 323 | } |
4f914e6e MR |
324 | |
325 | return undef; | |
4f914e6e MR |
326 | } |
327 | ||
4f914e6e MR |
328 | sub volume_resize { |
329 | my ($class, $scfg, $storeid, $volname, $size, $running) = @_; | |
c243be83 DB |
330 | |
331 | $volname = ($class->parse_volname($volname))[1]; | |
332 | ||
a4034b9f | 333 | my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running); |
4f914e6e | 334 | |
7730694e | 335 | $class->zfs_resize_lu($scfg, $volname, $new_size); |
a4034b9f WL |
336 | |
337 | return $new_size; | |
4f914e6e MR |
338 | } |
339 | ||
27a27646 WL |
340 | sub volume_snapshot_delete { |
341 | my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; | |
342 | ||
c243be83 DB |
343 | $volname = ($class->parse_volname($volname))[1]; |
344 | ||
27a27646 WL |
345 | $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool}/$volname\@$snap"); |
346 | } | |
347 | ||
4f914e6e MR |
348 | sub volume_snapshot_rollback { |
349 | my ($class, $scfg, $storeid, $volname, $snap) = @_; | |
350 | ||
c243be83 DB |
351 | $volname = ($class->parse_volname($volname))[1]; |
352 | ||
7730694e | 353 | $class->zfs_delete_lu($scfg, $volname); |
4f914e6e | 354 | |
281f9587 | 355 | $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool}/$volname\@$snap"); |
4f914e6e | 356 | |
7730694e | 357 | $class->zfs_import_lu($scfg, $volname); |
4f914e6e | 358 | |
7730694e | 359 | $class->zfs_add_lun_mapping_entry($scfg, $volname); |
4f914e6e MR |
360 | } |
361 | ||
7118dd91 DM |
362 | sub storage_can_replicate { |
363 | my ($class, $scfg, $storeid, $format) = @_; | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
4f914e6e MR |
368 | sub volume_has_feature { |
369 | my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; | |
370 | ||
371 | my $features = { | |
6b5bca68 DM |
372 | snapshot => { current => 1, snap => 1}, |
373 | clone => { base => 1}, | |
374 | template => { current => 1}, | |
375 | copy => { base => 1, current => 1}, | |
4f914e6e MR |
376 | }; |
377 | ||
378 | my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = | |
6b5bca68 | 379 | $class->parse_volname($volname); |
4f914e6e MR |
380 | |
381 | my $key = undef; | |
5332e6c9 DM |
382 | |
383 | if ($snapname) { | |
6b5bca68 | 384 | $key = 'snap'; |
4f914e6e | 385 | } else { |
6b5bca68 | 386 | $key = $isBase ? 'base' : 'current'; |
4f914e6e | 387 | } |
5332e6c9 | 388 | |
4f914e6e MR |
389 | return 1 if $features->{$feature}->{$key}; |
390 | ||
391 | return undef; | |
392 | } | |
393 | ||
86d47239 WL |
394 | sub activate_storage { |
395 | my ($class, $storeid, $scfg, $cache) = @_; | |
02e797b8 WL |
396 | |
397 | return 1; | |
398 | } | |
399 | ||
400 | sub deactivate_storage { | |
401 | my ($class, $storeid, $scfg, $cache) = @_; | |
402 | ||
403 | return 1; | |
404 | } | |
405 | ||
406 | sub activate_volume { | |
407 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
408 | ||
409 | die "unable to activate snapshot from remote zfs storage" if $snapname; | |
410 | ||
411 | return 1; | |
412 | } | |
413 | ||
414 | sub deactivate_volume { | |
415 | my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; | |
416 | ||
417 | die "unable to deactivate snapshot from remote zfs storage" if $snapname; | |
418 | ||
86d47239 WL |
419 | return 1; |
420 | } | |
421 | ||
4f914e6e | 422 | 1; |