Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge SAN multipathing implementation
SAN-22, SAN-26

Conflicts:
	tools/connect_iscsitarget.pl
	tools/connect_iscsitarget_sc.pl
  • Loading branch information
Ye Wen committed Oct 3, 2012
1 parent 9b8a342 commit f09c269
Show file tree
Hide file tree
Showing 12 changed files with 898 additions and 983 deletions.
Expand Up @@ -154,6 +154,7 @@ public static CommandOutput runWithRawOutput(String[] command) throws Exception
output.start();
int returnValue = proc.waitFor();
output.join();
error.join();
return new CommandOutput(returnValue, output.getReturnValue(), error.getReturnValue());
}

Expand Down
Expand Up @@ -1057,7 +1057,7 @@ public String getVolumeProperty(String volumeId) {
LOG.error(e);
return null;
}
return StorageProperties.STORAGE_HOST + "," + storeName + "," + encryptedPassword;
return ",,," + encryptedPassword + ",," + StorageProperties.STORAGE_HOST + "," + storeName;
}
}
return null;
Expand Down
3 changes: 3 additions & 0 deletions tools/Makefile
Expand Up @@ -105,9 +105,12 @@ install: build
@$(INSTALL) -m 755 euca_sync_key $(DESTDIR)$(sbindir)
@$(INSTALL) -m 755 connect_iscsitarget.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 connect_iscsitarget_sc.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 connect_iscsitarget_main.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 disconnect_iscsitarget.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 disconnect_iscsitarget_sc.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 disconnect_iscsitarget_main.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 get_iscsitarget.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 iscsitarget_common.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 644 floppy $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 755 dynserv.pl $(DESTDIR)$(datarootdir)/eucalyptus
@$(INSTALL) -m 644 libvirt-kvm-windows-example.xml $(DESTDIR)$(datarootdir)/eucalyptus/doc/
Expand Down
277 changes: 18 additions & 259 deletions tools/connect_iscsitarget.pl
Expand Up @@ -60,268 +60,27 @@
# IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
# NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.

use Crypt::OpenSSL::Random ;
use Crypt::OpenSSL::RSA ;
use MIME::Base64;
BEGIN {
use File::Spec::Functions qw(rel2abs);
use File::Basename qw(dirname);

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{'PATH'}='/bin:/usr/bin:/sbin:/usr/sbin/';
$KEY_PATH="";

$DELIMITER = ",";
$ISCSIADM = untaint(`which iscsiadm`);
$ISCSI_USER = "eucalyptus";

# check binaries
if (!-x $ISCSIADM) {
print STDERR "Unable to find iscsiadm\n";
do_exit(1);
my $script_abs_path = rel2abs($0);
our $script_dir = dirname($script_abs_path);
}

# check input params
$dev_string = untaint(shift @ARGV);

($euca_home, $ip, $store, $encrypted_password, $lun, $auth_mode, $opt_user) = parse_devstring($dev_string);
$store =~ s/\.$//g;
use lib $script_dir;

if (length($euca_home) <= 0) {
print STDERR "EUCALYPTUS path is not defined.\n";
require "iscsitarget_common.pl";
require "connect_iscsitarget_main.pl";
##################################################################
# load the NC private key
sub get_private_key {
my $KEY_PATH = $euca_home."/var/lib/eucalyptus/keys/node-pk.pem";
if (!open KEYFILE, $KEY_PATH) {
print STDERR "Cound not load key file: $KEY_PATH\n";
do_exit(1);
}

$KEY_PATH = $euca_home."/var/lib/eucalyptus/keys/node-pk.pem";

if (length($opt_user) > 0) {
$ISCSI_USER = $opt_user;
}
if ((length($lun) > 0) && ($lun > -1)) {
# check if a session corresponding to the store exists
if (get_session($store) == 1) {
# rescan session
rescan_target();
} else {
# else login to session
if(length($auth_mode) > 0) {
$password = "not_required";
} else {
$password = decrypt_password($encrypted_password);
}
if(length($password) <= 0) {
print STDERR "Unable to decrypt target password. Aborting.\n";
}
login_target($ip, $store, $password, $auth_mode);
}
# get dev from lun
sleep(1);
$localdevname = get_device_name_from_lun($store, $lun);
print "$localdevname";

# make sure device exists on the filesystem
for ($trycount=0; $trycount < 12; $trycount++) {
if ( -e "$localdevname" ) {
$trycount=12;
} else {
sleep(1);
}
}
} else {
$password = decrypt_password($encrypted_password);

if(length($password) <= 0) {
print STDERR "Unable to decrypt target password. Aborting.\n";
}
login_target($ip, $store, $password);
#wait for device to be ready
sleep(1);
$localdevname = get_device_name($store);
print "$localdevname";

# make sure device exists on the filesystem
for ($trycount=0; $trycount < 12; $trycount++) {
if ( -e "$localdevname" ) {
$trycount=12;
} else {
sleep(1);
}
}
}

sub parse_devstring {
my ($dev_string) = @_;
return split($DELIMITER, $dev_string);
}

sub login_target {
my ($ip, $store, $passwd) = @_;
if(!open STATICTARGET, "iscsiadm -m node -T $store -p $ip -o new |") {
print "Could not create static target";
do_exit(1)
}
while(<STATICTARGET>) {};

if($password ne "not_required") {
if(!open USERNAME, "iscsiadm -m node -T $store -p $ip --op=update --name node.session.auth.username --value=$ISCSI_USER |") {
print "Could not update target username";
do_exit(1)
}

while(<USERNAME>) {};

if(!open PASSWD, "iscsiadm -m node -T $store -p $ip --op=update --name node.session.auth.password --value=$passwd |") {
print "Could not update target password";
do_exit(1)
}

while(<PASSWD>) {};

}

if(!open LOGIN, "iscsiadm -m node -T $store -p $ip -l |") {
print "Could not login to target";
do_exit(1)
}

my $login = "";
while(<LOGIN>) {$login = $login.$_;};
if(length($login) <= 0) {
print STDERR "Unable to login to target. Aborting.\n";
do_exit(1);
}
}

sub decrypt_password {
my ($encrypted_passwd) = @_;

$private_key = "" ;
open (KEYFILE, $KEY_PATH) ;
while (<KEYFILE>) {
$private_key .= $_ ;
}
close(KEYFILE);

$rsa_priv = Crypt::OpenSSL::RSA->new_private_key($private_key);

$msg = decode_base64($encrypted_passwd);
$rsa_priv->use_pkcs1_padding();
$rsa_priv->use_sha1_hash() ;

my $passwd = $rsa_priv->decrypt($msg);

return $passwd;
}

sub get_session {
my ($store) = @_;
$num_retries = 5;

for ($i = 0; $i < $num_retries; ++$i) {
if(!open GETSESSION, "iscsiadm -m session |") {
print STDERR "Could not get iscsi session information";
do_exit(1)
}

while (<GETSESSION>) {
if ($_ =~ /.*$store\n/) {
close GETSESSION;
return 1;
}
}
close GETSESSION;
}
return 0;
}

sub get_device_name {
my ($store) = @_;
$num_retries = 5;

for ($i = 0; $i < $num_retries; ++$i) {
if(!open GETSESSION, "iscsiadm -m session -P 3 |") {
print STDERR "Could not get iscsi session information";
do_exit(1)
}

$found_target = 0;
$attach_seen = 1;
while (<GETSESSION>) {
if($_ =~ /Target: (.*)\n/) {
last if $attach_seen == 0;
$found_target = 1 if $1 eq $store;
$attach_seen = 0;
} elsif($_ =~ /.*Attached scsi disk ([a-zA-Z0-9]+).*\n/) {
if($found_target == 1) {
return "/dev/" . $1;
}
$attach_seen = 1;
}
}
close GETSESSION;
}
}

sub get_device_name_from_lun {
my ($store, $lun) = @_;
$num_retries = 5;

for ($i = 0; $i < $num_retries; ++$i) {
if(!open GETSESSION, "iscsiadm -m session -P 3 |") {
print STDERR "Could not get iscsi session information";
do_exit(1)
}

$found_target = 0;
$found_lun = 0;
$attach_seen = 1;
while (<GETSESSION>) {
if ($_ =~ /Target: (.*)\n/) {
last if $attach_seen == 0;
$found_target = 1 if $1 eq $store;
$attach_seen = 0;
$found_lun = 0;
} elsif ($_ =~ /.*Attached scsi disk ([a-zA-Z0-9]+).*\n/) {
if ($found_target == 1 && $found_lun == 1) {
return "/dev/" . $1;
}
$attach_seen = 1;
} elsif ($_ =~ /.*Lun: (.*)\n/) {
$found_lun = 1 if $1 eq $lun;
}
}
close GETSESSION;
}
}

sub rescan_target {

if(!open GETSESSION, "iscsiadm -m session -R |") {
print STDERR "Could not get iscsi session information";
do_exit(1)
}
close GETSESSION;
}


sub do_exit() {
$e = shift;

if ($mounted && ($tmpfile ne "")) {
system("$mounter umount $tmpfile");
}
if ($attached && ($loopdev ne "")) {
system("$LOSETUP -d $loopdev");
}
if ($tmpfile ne "") {
system("$RMDIR $tmpfile");
}
exit($e);
}

sub untaint() {
$str = shift;
if ($str =~ /^([ &:#-\@\w.]+)$/) {
$str = $1; #data is now untainted
} else {
$str = "";
}
return($str);
};
@key = <KEYFILE>;
close KEYFILE;
return join("", @key);
}

0 comments on commit f09c269

Please sign in to comment.