1 package PVE
::Storage
::ZFSPlugin
;
7 use PVE
::Tools
qw(run_command);
8 use PVE
::Storage
::ZFSPoolPlugin
;
9 use PVE
::RPCEnvironment
;
11 use base
qw(PVE::Storage::ZFSPoolPlugin);
12 use PVE
::Storage
::LunCmd
::Comstar
;
13 use PVE
::Storage
::LunCmd
::Istgt
;
14 use PVE
::Storage
::LunCmd
::Iet
;
15 use PVE
::Storage
::LunCmd
::LIO
;
18 my @ssh_opts = ('-o', 'BatchMode=yes');
19 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
20 my $id_rsa_path = '/etc/pve/priv/zfs';
32 my $zfs_unknown_scsi_provider = sub {
35 die "$provider: unknown iscsi provider. Available [comstar, istgt, iet, LIO]";
38 my $zfs_get_base = sub {
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
;
47 } elsif ($scfg->{iscsiprovider
} eq 'LIO') {
48 return PVE
::Storage
::LunCmd
::LIO
::get_base
;
50 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
55 my ($class, $scfg, $timeout, $method, @params) = @_;
57 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 10
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);
69 } elsif ($scfg->{iscsiprovider
} eq 'LIO') {
70 $msg = PVE
::Storage
::LunCmd
::LIO
::run_lun_command
($scfg, $timeout, $method, @params);
72 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
76 my $target = 'root@' . $scfg->{portal
};
78 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target];
80 if ($method eq 'zpool_list') {
81 push @$cmd, 'zpool', 'list';
83 push @$cmd, 'zfs', $method;
93 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
100 my ($class, $scfg, $zvol) = @_;
102 my $base = $zfs_get_base->($scfg);
104 my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol
" : "$base/$scfg->{pool
}/$zvol";
106 my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
108 return $lu_name if $lu_name;
110 die "Could
not find lu_name
for zvol
$zvol";
113 sub zfs_add_lun_mapping_entry {
114 my ($class, $scfg, $zvol, $guid) = @_;
116 if (!defined($guid)) {
117 $guid = $class->zfs_get_lu_name($scfg, $zvol);
120 $class->zfs_request($scfg, undef, 'add_view', $guid);
124 my ($class, $scfg, $zvol) = @_;
126 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
128 $class->zfs_request($scfg, undef, 'delete_lu', $guid);
132 my ($class, $scfg, $zvol) = @_;
134 my $base = $zfs_get_base->($scfg);
135 my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool
}/$zvol");
141 my ($class, $scfg, $zvol) = @_;
143 my $base = $zfs_get_base->($scfg);
144 $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool
}/$zvol");
148 my ($class, $scfg, $zvol, $size) = @_;
150 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
152 $class->zfs_request($scfg, undef, 'modify_lu', "${size
}K
", $guid);
155 sub zfs_get_lun_number {
156 my ($class, $scfg, $guid) = @_;
158 die "could
not find lun_number
for guid
$guid" if !$guid;
160 return $class->zfs_request($scfg, undef, 'list_view', $guid);
171 content => [ {images => 1}, { images => 1 }],
178 description => "iscsi provider
",
181 # this will disable write caching on comstar and istgt.
182 # it is not implemented for iet. iet blockio always operates with
183 # writethrough caching when not in readonly mode
185 description => "disable
write caching on the target
",
189 description => "target group
for comstar views
",
193 description => "host group
for comstar views
",
197 description => "target portal group
for Linux LIO targets
",
205 nodes => { optional => 1 },
206 disable => { optional => 1 },
207 portal => { fixed => 1 },
208 target => { fixed => 1 },
209 pool => { fixed => 1 },
210 blocksize => { fixed => 1 },
211 iscsiprovider => { fixed => 1 },
212 nowritecache => { optional => 1 },
213 sparse => { optional => 1 },
214 comstar_hg => { optional => 1 },
215 comstar_tg => { optional => 1 },
216 lio_tpg => { optional => 1 },
217 content => { optional => 1 },
218 bwlimit => { optional => 1 },
222 # Storage implementation
225 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
227 die "direct access to snapshots
not implemented
"
228 if defined($snapname);
230 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
232 my $target = $scfg->{target};
233 my $portal = $scfg->{portal};
235 my $guid = $class->zfs_get_lu_name($scfg, $name);
236 my $lun = $class->zfs_get_lun_number($scfg, $guid);
238 my $path = "iscsi
://$portal/$target/$lun";
240 return ($path, $vmid, $vtype);
244 my ($class, $storeid, $scfg, $volname) = @_;
246 my $snap = '__base__';
248 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
249 $class->parse_volname($volname);
251 die "create_base
not possible with base image
\n" if $isBase;
254 $newname =~ s/^vm-/base-/;
256 my $newvolname = $basename ? "$basename/$newname" : "$newname";
258 $class->zfs_delete_lu($scfg, $name);
259 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool
}/$name", "$scfg->{pool}/$newname");
261 my $guid = $class->zfs_create_lu($scfg, $newname);
262 $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
264 my $running = undef; #fixme : is create_base always offline ?
266 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
272 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
274 my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
276 # get ZFS dataset name from PVE volname
277 my (undef, $clonedname) = $class->parse_volname($name);
279 my $guid = $class->zfs_create_lu($scfg, $clonedname);
280 $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid);
286 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
288 die "unsupported format
'$fmt'" if $fmt ne 'raw';
290 die "illegal name
'$name' - sould be
'vm-$vmid-*'\n"
291 if $name && $name !~ m/^vm-$vmid-/;
295 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname;
297 $class->zfs_create_zvol($scfg, $volname, $size);
299 my $guid = $class->zfs_create_lu($scfg, $volname);
300 $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
306 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
308 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
310 $class->zfs_delete_lu($scfg, $name);
312 eval { $class->zfs_delete_zvol($scfg, $name); };
314 my $guid = $class->zfs_create_lu($scfg, $name);
315 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
323 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
325 my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
327 $class->zfs_resize_lu($scfg, $volname, $new_size);
332 sub volume_snapshot_delete {
333 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
335 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool
}/$volname\@$snap");
338 sub volume_snapshot_rollback {
339 my ($class, $scfg, $storeid, $volname, $snap) = @_;
341 $class->zfs_delete_lu($scfg, $volname);
343 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool
}/$volname\@$snap");
345 $class->zfs_import_lu($scfg, $volname);
347 $class->zfs_add_lun_mapping_entry($scfg, $volname);
350 sub storage_can_replicate {
351 my ($class, $scfg, $storeid, $format) = @_;
356 sub volume_has_feature {
357 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
360 snapshot => { current => 1, snap => 1},
361 clone => { base => 1},
362 template => { current => 1},
363 copy => { base => 1, current => 1},
366 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
367 $class->parse_volname($volname);
374 $key = $isBase ? 'base' : 'current';
377 return 1 if $features->{$feature}->{$key};
382 sub volume_snapshot_list {
383 my ($class, $scfg, $storeid, $volname) = @_;
384 # return an empty array if dataset does not exist.
385 die "Volume_snapshot_list
is not implemented
for ZFS over iSCSI
.\n";
388 sub activate_storage {
389 my ($class, $storeid, $scfg, $cache) = @_;
394 sub deactivate_storage {
395 my ($class, $storeid, $scfg, $cache) = @_;
400 sub activate_volume {
401 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
403 die "unable to activate snapshot from remote zfs storage
" if $snapname;
408 sub deactivate_volume {
409 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
411 die "unable to deactivate snapshot from remote zfs storage
" if $snapname;