]>
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);
9 use PVE
::Storage
::Plugin
;
10 use PVE
::JSONSchema
qw(get_standard_option);
13 use base
qw(PVE::Storage::Plugin);
15 # iscsi helper function
17 my $ISCSIADM = '/usr/bin/iscsiadm';
18 $ISCSIADM = undef if ! -X
$ISCSIADM;
20 sub check_iscsi_support
{
24 my $msg = "no iscsi support - please install open-iscsi";
26 warn "warning: $msg\n";
36 sub iscsi_session_list
{
38 check_iscsi_support
();
40 my $cmd = [$ISCSIADM, '--mode', 'session'];
45 run_command
($cmd, errmsg
=> 'iscsi session scan failed', outfunc
=> sub {
48 if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)\s*$/) {
49 my ($session, $target) = ($1, $2);
50 # there can be several sessions per target (multipath)
51 push @{$res->{$target}}, $session;
56 die $err if $err !~ m/: No active sessions.$/i;
62 sub iscsi_test_portal
{
65 my ($server, $port) = split(':', $portal);
66 my $p = Net
::Ping-
>new("tcp", 2);
67 $p->port_number($port || 3260);
68 return $p->ping($server);
74 check_iscsi_support
();
76 my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
81 return $res if !iscsi_test_portal
($portal); # fixme: raise exception here?
83 run_command
($cmd, outfunc
=> sub {
86 if ($line =~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\,\S+\s+(\S+)\s*$/) {
89 # one target can have more than one portal (multipath).
90 push @{$res->{$target}}, $portal;
98 my ($target, $portal_in) = @_;
100 check_iscsi_support
();
102 eval { iscsi_discovery
($portal_in); };
105 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
110 my ($target, $portal) = @_;
112 check_iscsi_support
();
114 my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
118 my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
120 sub iscsi_session_rescan
{
121 my $session_list = shift;
123 check_iscsi_support
();
125 my $rstat = stat($rescan_filename);
128 if (my $fh = IO
::File-
>new($rescan_filename, "a")) {
129 utime undef, undef, $fh;
133 my $atime = $rstat->atime;
134 my $tdiff = time() - $atime;
135 # avoid frequent rescans
136 return if !($tdiff < 0 || $tdiff > 10);
137 utime undef, undef, $rescan_filename;
140 foreach my $session (@$session_list) {
141 my $cmd = [$ISCSIADM, '--mode', 'session', '-r', $session, '-R'];
142 eval { run_command
($cmd, outfunc
=> sub {}); };
147 sub load_stable_scsi_paths
{
149 my $stable_paths = {};
151 my $stabledir = "/dev/disk/by-id";
153 if (my $dh = IO
::Dir-
>new($stabledir)) {
154 while (defined(my $tmp = $dh->read)) {
155 # exclude filenames with part in name (same disk but partitions)
156 # use only filenames with scsi(with multipath i have the same device
157 # with dm-uuid-mpath , dm-name and scsi in name)
158 if($tmp !~ m/-part\d+$/ && $tmp =~ m/^scsi-/) {
159 my $path = "$stabledir/$tmp";
160 my $bdevdest = readlink($path);
161 if ($bdevdest && $bdevdest =~ m
|^../../([^/]+)|) {
162 $stable_paths->{$1}=$tmp;
168 return $stable_paths;
171 sub iscsi_device_list
{
175 my $dirname = '/sys/class/iscsi_session';
177 my $stable_paths = load_stable_scsi_paths
();
179 dir_glob_foreach
($dirname, 'session(\d+)', sub {
180 my ($ent, $session) = @_;
182 my $target = file_read_firstline
("$dirname/$ent/targetname");
185 my (undef, $host) = dir_glob_regex
("$dirname/$ent/device", 'target(\d+):.*');
186 return if !defined($host);
188 dir_glob_foreach
("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
189 my ($tmp, $channel, $id, $lun) = @_;
191 my $type = file_read_firstline
("/sys/bus/scsi/devices/$tmp/type");
192 return if !defined($type) || $type ne '0'; # list disks only
195 if (-d
"/sys/bus/scsi/devices/$tmp/block") { # newer kernels
196 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
198 (undef, $bdev) = dir_glob_regex
("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
203 if (-d
"/sys/block/$bdev/holders") {
204 my $multipathdev = dir_glob_regex
("/sys/block/$bdev/holders", '[A-Za-z]\S*');
205 $bdev = $multipathdev if $multipathdev;
208 my $blockdev = $stable_paths->{$bdev};
209 return if !$blockdev;
211 my $size = file_read_firstline
("/sys/block/$bdev/size");
214 my $volid = "$channel.$id.$lun.$blockdev";
216 $res->{$target}->{$volid} = {
218 'size' => int($size * 512),
219 'vmid' => 0, # not assigned to any vm
220 'channel' => int($channel),
225 #print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
241 content
=> [ {images
=> 1, none
=> 1}, { images
=> 1 }],
248 description
=> "iSCSI target.",
252 description
=> "iSCSI portal (IP or DNS name with optional port).",
253 type
=> 'string', format
=> 'pve-storage-portal-dns',
260 portal
=> { fixed
=> 1 },
261 target
=> { fixed
=> 1 },
262 nodes
=> { optional
=> 1},
263 disable
=> { optional
=> 1},
264 content
=> { 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};