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 $zvol = ($class->parse_volname($zvol))[1];
106 my $object = ($zvol =~ /^.+\/.+/) ? "$base/$zvol
" : "$base/$scfg->{pool
}/$zvol";
108 my $lu_name = $class->zfs_request($scfg, undef, 'list_lu', $object);
110 return $lu_name if $lu_name;
112 die "Could
not find lu_name
for zvol
$zvol";
115 sub zfs_add_lun_mapping_entry {
116 my ($class, $scfg, $zvol, $guid) = @_;
118 if (!defined($guid)) {
119 $guid = $class->zfs_get_lu_name($scfg, $zvol);
122 $class->zfs_request($scfg, undef, 'add_view', $guid);
126 my ($class, $scfg, $zvol) = @_;
128 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
130 $class->zfs_request($scfg, undef, 'delete_lu', $guid);
134 my ($class, $scfg, $zvol) = @_;
136 my $base = $zfs_get_base->($scfg);
137 my $guid = $class->zfs_request($scfg, undef, 'create_lu', "$base/$scfg->{pool
}/$zvol");
143 my ($class, $scfg, $zvol) = @_;
145 my $base = $zfs_get_base->($scfg);
146 $class->zfs_request($scfg, undef, 'import_lu', "$base/$scfg->{pool
}/$zvol");
150 my ($class, $scfg, $zvol, $size) = @_;
152 my $guid = $class->zfs_get_lu_name($scfg, $zvol);
154 $class->zfs_request($scfg, undef, 'modify_lu', "${size
}K
", $guid);
157 sub zfs_get_lun_number {
158 my ($class, $scfg, $guid) = @_;
160 die "could
not find lun_number
for guid
$guid" if !$guid;
162 return $class->zfs_request($scfg, undef, 'list_view', $guid);
173 content => [ {images => 1}, { images => 1 }],
180 description => "iscsi provider
",
183 # this will disable write caching on comstar and istgt.
184 # it is not implemented for iet. iet blockio always operates with
185 # writethrough caching when not in readonly mode
187 description => "disable
write caching on the target
",
191 description => "target group
for comstar views
",
195 description => "host group
for comstar views
",
199 description => "target portal group
for Linux LIO targets
",
207 nodes => { optional => 1 },
208 disable => { optional => 1 },
209 portal => { fixed => 1 },
210 target => { fixed => 1 },
211 pool => { fixed => 1 },
212 blocksize => { fixed => 1 },
213 iscsiprovider => { fixed => 1 },
214 nowritecache => { optional => 1 },
215 sparse => { optional => 1 },
216 comstar_hg => { optional => 1 },
217 comstar_tg => { optional => 1 },
218 lio_tpg => { optional => 1 },
219 content => { optional => 1 },
220 bwlimit => { optional => 1 },
224 # Storage implementation
227 my ($class, $scfg, $volname, $storeid, $snapname) = @_;
229 die "direct access to snapshots
not implemented
"
230 if defined($snapname);
232 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
234 my $target = $scfg->{target};
235 my $portal = $scfg->{portal};
237 my $guid = $class->zfs_get_lu_name($scfg, $name);
238 my $lun = $class->zfs_get_lun_number($scfg, $guid);
240 my $path = "iscsi
://$portal/$target/$lun";
242 return ($path, $vmid, $vtype);
246 my ($class, $storeid, $scfg, $volname) = @_;
248 my $snap = '__base__';
250 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
251 $class->parse_volname($volname);
253 die "create_base
not possible with base image
\n" if $isBase;
256 $newname =~ s/^vm-/base-/;
258 my $newvolname = $basename ? "$basename/$newname" : "$newname";
260 $class->zfs_delete_lu($scfg, $name);
261 $class->zfs_request($scfg, undef, 'rename', "$scfg->{pool
}/$name", "$scfg->{pool}/$newname");
263 my $guid = $class->zfs_create_lu($scfg, $newname);
264 $class->zfs_add_lun_mapping_entry($scfg, $newname, $guid);
266 my $running = undef; #fixme : is create_base always offline ?
268 $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
274 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
276 my $name = $class->SUPER::clone_image($scfg, $storeid, $volname, $vmid, $snap);
278 # get ZFS dataset name from PVE volname
279 my (undef, $clonedname) = $class->parse_volname($name);
281 my $guid = $class->zfs_create_lu($scfg, $clonedname);
282 $class->zfs_add_lun_mapping_entry($scfg, $clonedname, $guid);
288 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
290 die "unsupported format
'$fmt'" if $fmt ne 'raw';
292 die "illegal name
'$name' - sould be
'vm-$vmid-*'\n"
293 if $name && $name !~ m/^vm-$vmid-/;
297 $volname = $class->find_free_diskname($storeid, $scfg, $vmid, $fmt) if !$volname;
299 $class->zfs_create_zvol($scfg, $volname, $size);
301 my $guid = $class->zfs_create_lu($scfg, $volname);
302 $class->zfs_add_lun_mapping_entry($scfg, $volname, $guid);
308 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
310 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
312 $class->zfs_delete_lu($scfg, $name);
314 eval { $class->zfs_delete_zvol($scfg, $name); };
316 my $guid = $class->zfs_create_lu($scfg, $name);
317 $class->zfs_add_lun_mapping_entry($scfg, $name, $guid);
325 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
327 $volname = ($class->parse_volname($volname))[1];
329 my $new_size = $class->SUPER::volume_resize($scfg, $storeid, $volname, $size, $running);
331 $class->zfs_resize_lu($scfg, $volname, $new_size);
336 sub volume_snapshot_delete {
337 my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
339 $volname = ($class->parse_volname($volname))[1];
341 $class->zfs_request($scfg, undef, 'destroy', "$scfg->{pool
}/$volname\@$snap");
344 sub volume_snapshot_rollback {
345 my ($class, $scfg, $storeid, $volname, $snap) = @_;
347 $volname = ($class->parse_volname($volname))[1];
349 $class->zfs_delete_lu($scfg, $volname);
351 $class->zfs_request($scfg, undef, 'rollback', "$scfg->{pool
}/$volname\@$snap");
353 $class->zfs_import_lu($scfg, $volname);
355 $class->zfs_add_lun_mapping_entry($scfg, $volname);
358 sub storage_can_replicate {
359 my ($class, $scfg, $storeid, $format) = @_;
364 sub volume_has_feature {
365 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
368 snapshot => { current => 1, snap => 1},
369 clone => { base => 1},
370 template => { current => 1},
371 copy => { base => 1, current => 1},
374 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
375 $class->parse_volname($volname);
382 $key = $isBase ? 'base' : 'current';
385 return 1 if $features->{$feature}->{$key};
390 sub volume_snapshot_list {
391 my ($class, $scfg, $storeid, $volname) = @_;
392 # return an empty array if dataset does not exist.
393 die "Volume_snapshot_list
is not implemented
for ZFS over iSCSI
.\n";
396 sub activate_storage {
397 my ($class, $storeid, $scfg, $cache) = @_;
402 sub deactivate_storage {
403 my ($class, $storeid, $scfg, $cache) = @_;
408 sub activate_volume {
409 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
411 die "unable to activate snapshot from remote zfs storage
" if $snapname;
416 sub deactivate_volume {
417 my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_;
419 die "unable to deactivate snapshot from remote zfs storage
" if $snapname;