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
;
17 my @ssh_opts = ('-o', 'BatchMode=yes');
18 my @ssh_cmd = ('/usr/bin/ssh', @ssh_opts);
19 my $id_rsa_path = '/etc/pve/priv/zfs';
31 my $zfs_unknown_scsi_provider = sub {
34 die "$provider: unknown iscsi provider. Available [comstar, istgt, iet]";
37 my $zfs_get_base = sub {
40 if ($scfg->{iscsiprovider
} eq 'comstar') {
41 return PVE
::Storage
::LunCmd
::Comstar
::get_base
;
42 } elsif ($scfg->{iscsiprovider
} eq 'istgt') {
43 return PVE
::Storage
::LunCmd
::Istgt
::get_base
;
44 } elsif ($scfg->{iscsiprovider
} eq 'iet') {
45 return PVE
::Storage
::LunCmd
::Iet
::get_base
;
47 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
52 my ($class, $scfg, $timeout, $method, @params) = @_;
54 $timeout = PVE
::RPCEnvironment-
>is_worker() ?
60*60 : 10
59 if ($lun_cmds->{$method}) {
60 if ($scfg->{iscsiprovider
} eq 'comstar') {
61 $msg = PVE
::Storage
::LunCmd
::Comstar
::run_lun_command
($scfg, $timeout, $method, @params);
62 } elsif ($scfg->{iscsiprovider
} eq 'istgt') {
63 $msg = PVE
::Storage
::LunCmd
::Istgt
::run_lun_command
($scfg, $timeout, $method, @params);
64 } elsif ($scfg->{iscsiprovider
} eq 'iet') {
65 $msg = PVE
::Storage
::LunCmd
::Iet
::run_lun_command
($scfg, $timeout, $method, @params);
67 $zfs_unknown_scsi_provider->($scfg->{iscsiprovider
});
71 my $target = 'root@' . $scfg->{portal
};
73 my $cmd = [@ssh_cmd, '-i', "$id_rsa_path/$scfg->{portal}_id_rsa", $target];
75 if ($method eq 'zpool_list') {
76 push @$cmd, 'zpool', 'list';
78 push @$cmd, 'zfs', $method;
88 run_command
($cmd, outfunc
=> $output, timeout
=> $timeout);
95 my ($class, $scfg, $zvol) = @_;
97 my $base = $zfs_get_base->($scfg);
99 my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol
" : "$base/$scfg->{pool
}/$zvol";
101 my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
103 return $lu_name if $lu_name;
105 die "Could
not find lu_name
for zvol
$zvol";
108 sub zfs_add_lun_mapping_entry {
109 my ($class, $scfg, $zvol, $guid) = @_;
111 if (!defined($guid)) {
112 $guid = $class->zfs_get_lu_name($scfg, $zvol);
115 $class->zfs_request($scfg, undef, 'add_view', $guid);
119 my ($class, $scfg, $zvol) = @_;
121 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
123 $class->zfs_request($scfg, undef, 'delete_lu', $guid);
127 my ($class, $scfg, $zvol) = @_;
129 my $base = $zfs_get_base->($scfg);
130 my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool
}/$zvol");
136 my ($class, $scfg, $zvol) = @_;
138 my $base = $zfs_get_base->($scfg);
139 $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool
}/$zvol");
143 my ($class, $scfg, $zvol, $size) = @_;
145 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
147 $class->zfs_request($scfg, undef, 'modify_lu', "${size
}K
", $guid);
150 sub zfs_get_lun_number {
151 my ($class, $scfg, $guid) = @_;
153 die "could
not find lun_number
for guid
$guid" if !$guid;
155 return $class->zfs_request($scfg, undef, 'list_view', $guid);
166 content => [ {images => 1}, { images => 1 }],
173 description => "iscsi provider
",
176 # this will disable write caching on comstar and istgt.
177 # it is not implemented for iet. iet blockio always operates with
178 # writethrough caching when not in readonly mode
180 description => "disable
write caching on the target
",
184 description => "target group
for comstar views
",
188 description => "host group
for comstar views
",
196 nodes => { optional => 1 },
197 disable => { optional => 1 },
198 portal => { fixed => 1 },
199 target => { fixed => 1 },
200 pool => { fixed => 1 },
201 blocksize => { fixed => 1 },
202 iscsiprovider => { fixed => 1 },
203 nowritecache => { optional => 1 },
204 sparse => { optional => 1 },
205 comstar_hg => { optional => 1 },
206 comstar_tg => { optional => 1 },
207 content => { optional => 1 },
208 bwlimit => { optional => 1 },
212 # Storage implementation
215 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
217 die "direct access to snapshots
not implemented
"
218 if defined($snapname);
220 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
222 my $target = $scfg->{target};
223 my $portal = $scfg->{portal};
225 my $guid = $class->zfs_get_lu_name($scfg, $name);
226 my $lun = $class->zfs_get_lun_number($scfg, $guid);
228 my $path = "iscsi
://$portal/$target/$lun";
230 return ($path, $vmid, $vtype);
234 my ($class, $storeid, $scfg, $volname) = @_;
236 my $snap = '__base__';
238 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
239 $class->parse_volname($volname);
241 die "create_base
not possible with base image
\n" if $isBase;
244 $newname =~ s/^vm-/base-/;
246 my $newvolname = $basename ? "$basename/$newname" : "$newname";
248 $class->zfs_delete_lu($scfg, $name);
249 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool
}/$name", "$scfg->{pool}/$newname");
251 my $guid = $class->zfs_create_lu($scfg, $newname);
252 $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
254 my $running = undef; #fixme : is create_base always offline ?
256 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
262 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
264 my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
266 # get ZFS dataset name from PVE volname
267 my (undef, $clonedname) = $class->parse_volname($name);
269 my $guid = $class->zfs_create_lu($scfg, $clonedname);
270 $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid);
276 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
278 die "unsupported format
'$fmt'" if $fmt ne 'raw';
280 die "illegal name
'$name' - sould be
'vm-$vmid-*'\n"
281 if $name && $name !~ m/^vm-$vmid-/;
285 $volname = $class->zfs_find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname;
287 $class->zfs_create_zvol($scfg, $volname, $size);
289 my $guid = $class->zfs_create_lu($scfg, $volname);
290 $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
296 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
298 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
300 $class->zfs_delete_lu($scfg, $name);
302 eval { $class->zfs_delete_zvol($scfg, $name); };
304 my $guid = $class->zfs_create_lu($scfg, $name);
305 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
313 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
315 my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
317 $class->zfs_resize_lu($scfg, $volname, $new_size);
322 sub volume_snapshot_delete {
323 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
325 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool
}/$volname\@$snap");
328 sub volume_snapshot_rollback {
329 my ($class, $scfg, $storeid, $volname, $snap) = @_;
331 $class->zfs_delete_lu($scfg, $volname);
333 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool
}/$volname\@$snap");
335 $class->zfs_import_lu($scfg, $volname);
337 $class->zfs_add_lun_mapping_entry($scfg, $volname);
340 sub storage_can_replicate {
341 my ($class, $scfg, $storeid, $format) = @_;
346 sub volume_has_feature {
347 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
350 snapshot => { current => 1, snap => 1},
351 clone => { base => 1},
352 template => { current => 1},
353 copy => { base => 1, current => 1},
356 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
357 $class->parse_volname($volname);
364 $key = $isBase ? 'base' : 'current';
367 return 1 if $features->{$feature}->{$key};
372 sub volume_snapshot_list {
373 my ($class, $scfg, $storeid, $volname) = @_;
374 # return an empty array if dataset does not exist.
375 die "Volume_snapshot_list
is not implemented
for ZFS over iSCSI
.\n";
378 sub activate_storage {
379 my ($class, $storeid, $scfg, $cache) = @_;
384 sub deactivate_storage {
385 my ($class, $storeid, $scfg, $cache) = @_;
390 sub activate_volume {
391 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
393 die "unable to activate snapshot from remote zfs storage
" if $snapname;
398 sub deactivate_volume {
399 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
401 die "unable to deactivate snapshot from remote zfs storage
" if $snapname;