]>
git.proxmox.com Git - pve-storage.git/blob - PVE/Storage/ISCSIPlugin.pm
1 package PVE
::Storage
::ISCSIPlugin
;
8 use PVE
::Tools
qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach $IPV4RE $IPV6RE);
9 use PVE
::Storage
::Plugin
;
10 use PVE
::JSONSchema
qw(get_standard_option);
12 use base
qw(PVE::Storage::Plugin);
14 # iscsi helper function
16 my $ISCSIADM = '/usr/bin/iscsiadm';
17 $ISCSIADM = undef if ! -X
$ISCSIADM;
19 sub check_iscsi_support
{
23 my $msg = "no iscsi support - please install open-iscsi";
25 warn "warning: $msg\n";
35 sub iscsi_session_list
{
37 check_iscsi_support
();
39 my $cmd = [$ISCSIADM, '--mode', 'session'];
44 run_command
($cmd, errmsg
=> 'iscsi session scan failed', outfunc
=> sub {
47 if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)(\s+\S+)?\s*$/) {
48 my ($session, $target) = ($1, $2);
49 # there can be several sessions per target (multipath)
50 push @{$res->{$target}}, $session;
55 die $err if $err !~ m/: No active sessions.$/i;
61 sub iscsi_test_portal
{
64 my ($server, $port) = PVE
::Tools
::parse_host_and_port
($portal);
66 return PVE
::Network
::tcp_ping
($server, $port || 3260, 2);
72 check_iscsi_support
();
74 my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
79 return $res if !iscsi_test_portal
($portal); # fixme: raise exception here?
81 run_command
($cmd, outfunc
=> sub {
84 if ($line =~ m/^((?:$IPV4RE|\[$IPV6RE\]):\d+)\,\S+\s+(\S+)\s*$/) {
87 # one target can have more than one portal (multipath).
88 push @{$res->{$target}}, $portal;
96 my ($target, $portal_in) = @_;
98 check_iscsi_support
();
100 eval { iscsi_discovery
($portal_in); };
103 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
108 my ($target, $portal) = @_;
110 check_iscsi_support
();
112 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
116 my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
118 sub iscsi_session_rescan
{
119 my $session_list = shift;
121 check_iscsi_support
();
123 my $rstat = stat($rescan_filename);
126 if (my $fh = IO
::File-
>new($rescan_filename, "a")) {
127 utime undef, undef, $fh;
131 my $atime = $rstat->atime;
132 my $tdiff = time() - $atime;
133 # avoid frequent rescans
134 return if !($tdiff < 0 || $tdiff > 10);
135 utime undef, undef, $rescan_filename;
138 foreach my $session (@$session_list) {
139 my $cmd = [$ISCSIADM, '--mode', 'session', '--sid', $session, '--rescan'];
140 eval { run_command
($cmd, outfunc
=> sub {}); };
145 sub load_stable_scsi_paths
{
147 my $stable_paths = {};
149 my $stabledir = "/dev/disk/by-id";
151 if (my $dh = IO
::Dir-
>new($stabledir)) {
152 while (defined(my $tmp = $dh->read)) {
153 # exclude filenames with part in name (same disk but partitions)
154 # use only filenames with scsi(with multipath i have the same device
155 # with dm-uuid-mpath , dm-name and scsi in name)
156 if($tmp !~ m/-part\d+$/ && $tmp =~ m/^scsi-/) {
157 my $path = "$stabledir/$tmp";
158 my $bdevdest = readlink($path);
159 if ($bdevdest && $bdevdest =~ m
|^../../([^/]+)|) {
160 $stable_paths->{$1}=$tmp;
166 return $stable_paths;
169 sub iscsi_device_list
{
173 my $dirname = '/sys/class/iscsi_session';
175 my $stable_paths = load_stable_scsi_paths
();
177 dir_glob_foreach
($dirname, 'session(\d+)', sub {
178 my ($ent, $session) = @_;
180 my $target = file_read_firstline
("$dirname/$ent/targetname");
183 my (undef, $host) = dir_glob_regex
("$dirname/$ent/device", 'target(\d+):.*');
184 return if !defined($host);
186 dir_glob_foreach
("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
187 my ($tmp, $channel, $id, $lun) = @_;
189 my $type = file_read_firstline
("/sys/bus/scsi/devices/$tmp/type");
190 return if !defined($type) || $type ne '0'; # list disks only
193 if (-d
"/sys/bus/scsi/devices/$tmp/block") { # newer kernels
194 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
196 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
201 if (-d
"/sys/block/$bdev/holders") {
202 my $multipathdev = dir_glob_regex
("/sys/block/$bdev/holders", '[A-Za-z]\S*');
203 $bdev = $multipathdev if $multipathdev;
206 my $blockdev = $stable_paths->{$bdev};
207 return if !$blockdev;
209 my $size = file_read_firstline
("/sys/block/$bdev/size");
212 my $volid = "$channel.$id.$lun.$blockdev";
214 $res->{$target}->{$volid} = {
216 'size' => int($size * 512),
217 'vmid' => 0, # not assigned to any vm
218 'channel' => int($channel),
223 #print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
239 content
=> [ {images
=> 1, none
=> 1}, { images
=> 1 }],
240 select_existing
=> 1,
247 description
=> "iSCSI target.",
251 description
=> "iSCSI portal (IP or DNS name with optional port).",
252 type
=> 'string', format
=> 'pve-storage-portal-dns',
259 portal
=> { fixed
=> 1 },
260 target
=> { fixed
=> 1 },
261 nodes
=> { optional
=> 1},
262 disable
=> { optional
=> 1},
263 content
=> { optional
=> 1},
264 bwlimit
=> { optional
=> 1 },
268 # Storage implementation
271 my ($class, $volname) = @_;
273 if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
274 return ('images', $1, undef, undef, undef, undef, 'raw');
277 die "unable to parse iscsi volume name '$volname'\n";
280 sub filesystem_path
{
281 my ($class, $scfg, $volname, $snapname) = @_;
283 die "snapshot is not possible on iscsi storage\n" if defined($snapname);
285 my ($vtype, $name, $vmid) = $class->parse_volname($volname);
287 my $path = "/dev/disk/by-id/$name";
289 return wantarray ?
($path, $vmid, $vtype) : $path;
293 my ($class, $storeid, $scfg, $volname) = @_;
295 die "can't create base images in iscsi storage\n";
299 my ($class, $scfg, $storeid, $volname, $vmid, $snap) = @_;
301 die "can't clone images in iscsi storage\n";
305 my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
307 die "can't allocate space in iscsi storage\n";
311 my ($class, $storeid, $scfg, $volname, $isBase) = @_;
313 die "can't free space in iscsi storage\n";
317 my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
321 $cache->{iscsi_devices
} = iscsi_device_list
() if !$cache->{iscsi_devices
};
323 # we have no owner for iscsi devices
325 my $target = $scfg->{target
};
327 if (my $dat = $cache->{iscsi_devices
}->{$target}) {
329 foreach my $volname (keys %$dat) {
331 my $volid = "$storeid:$volname";
334 my $found = grep { $_ eq $volid } @$vollist;
337 # we have no owner for iscsi devices
338 next if defined($vmid);
341 my $info = $dat->{$volname};
342 $info->{volid
} = $volid;
352 my ($class, $storeid, $scfg, $cache) = @_;
354 $cache->{iscsi_sessions
} = iscsi_session_list
() if !$cache->{iscsi_sessions
};
356 my $active = defined($cache->{iscsi_sessions
}->{$scfg->{target
}});
358 return (0, 0, 0, $active);
361 sub activate_storage
{
362 my ($class, $storeid, $scfg, $cache) = @_;
364 return if !check_iscsi_support
(1);
366 $cache->{iscsi_sessions
} = iscsi_session_list
() if !$cache->{iscsi_sessions
};
368 my $iscsi_sess = $cache->{iscsi_sessions
}->{$scfg->{target
}};
369 if (!defined ($iscsi_sess)) {
370 eval { iscsi_login
($scfg->{target
}, $scfg->{portal
}); };
373 # make sure we get all devices
374 iscsi_session_rescan
($iscsi_sess);
378 sub deactivate_storage
{
379 my ($class, $storeid, $scfg, $cache) = @_;
381 return if !check_iscsi_support
(1);
383 $cache->{iscsi_sessions
} = iscsi_session_list
() if !$cache->{iscsi_sessions
};
385 my $iscsi_sess = $cache->{iscsi_sessions
}->{$scfg->{target
}};
387 if (defined ($iscsi_sess)) {
388 iscsi_logout
($scfg->{target
}, $scfg->{portal
});
392 sub check_connection
{
393 my ($class, $storeid, $scfg) = @_;
395 my $portal = $scfg->{portal
};
396 return iscsi_test_portal
($portal);
400 my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
401 die "volume resize is not possible on iscsi device";
404 sub volume_has_feature
{
405 my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
408 copy
=> { current
=> 1},
411 my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
412 $class->parse_volname($volname);
418 $key = $isBase ?
'base' : 'current';
420 return 1 if $features->{$feature}->{$key};