~ubuntu-branches/ubuntu/raring/eucalyptus/raring

« back to all changes in this revision

Viewing changes to tools/connect_iscsitarget.pl

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-11-29 13:17:52 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 185.
  • Revision ID: package-import@ubuntu.com-20111129131752-rq31al3ntutv2vvl
Tags: upstream-3.0.999beta1
ImportĀ upstreamĀ versionĀ 3.0.999beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
# check input params
23
23
$dev_string = untaint(shift @ARGV);
24
24
 
25
 
($euca_home, $ip, $store, $encrypted_password) = parse_devstring($dev_string);
 
25
($euca_home, $ip, $store, $encrypted_password, $lun, $auth_mode) = parse_devstring($dev_string);
 
26
$store =~ s/\.$//g;
26
27
 
27
 
if(length($euca_home) <= 0) {
 
28
if (length($euca_home) <= 0) {
28
29
    print STDERR "EUCALYPTUS path is not defined.\n";
29
30
    do_exit(1);
30
31
}
31
32
 
32
33
$KEY_PATH = $euca_home."/var/lib/eucalyptus/keys/node-pk.pem";
33
34
 
34
 
$password = decrypt_password($encrypted_password);
35
 
 
36
 
login_target($ip, $store, $password);
37
 
 
38
 
#wait for device to be ready
39
 
 
40
 
sleep 1;
41
 
 
42
 
print get_device_name($store);
 
35
if ((length($lun) > 0) && ($lun > -1)) {
 
36
  # check if a session corresponding to the store exists
 
37
  if (get_session($ARGV[0]) == 1) {
 
38
    # rescan session
 
39
    rescan_target();  
 
40
  } else {
 
41
    # else login to session
 
42
    if(length($auth_mode) > 0) {
 
43
      $password = "not_required";
 
44
    } else {
 
45
      $password = decrypt_password($encrypted_password);
 
46
    }
 
47
    if(length($password) <= 0) {
 
48
      print STDERR "Unable to decrypt target password. Aborting.\n";
 
49
    }
 
50
    login_target($ip, $store, $password, $auth_mode);
 
51
  }
 
52
  # get dev from lun
 
53
  sleep(1);
 
54
  $localdevname = get_device_name_from_lun($store, $lun);
 
55
  print "$localdevname";
 
56
 
 
57
  # make sure device exists on the filesystem
 
58
  for ($trycount=0; $trycount < 12; $trycount++) { 
 
59
    if ( -e "$localdevname" ) {
 
60
      $trycount=12;
 
61
    } else {
 
62
      sleep(1);
 
63
    }
 
64
  }
 
65
} else {
 
66
  $password = decrypt_password($encrypted_password);
 
67
 
 
68
  if(length($password) <= 0) {
 
69
    print STDERR "Unable to decrypt target password. Aborting.\n";
 
70
  }
 
71
  login_target($ip, $store, $password);
 
72
  #wait for device to be ready
 
73
  sleep(1);
 
74
  $localdevname = get_device_name($store);
 
75
  print "$localdevname";
 
76
 
 
77
  # make sure device exists on the filesystem
 
78
  for ($trycount=0; $trycount < 12; $trycount++) { 
 
79
    if ( -e "$localdevname" ) {
 
80
      $trycount=12;
 
81
    } else {
 
82
      sleep(1);
 
83
    }
 
84
  }
 
85
}
43
86
 
44
87
sub parse_devstring {
45
88
    my ($dev_string) = @_;
55
98
 
56
99
    while(<DISCOVERY>) {};
57
100
 
58
 
    if(!open USERNAME, "iscsiadm -m node -T $store --op=update --name node.session.auth.username --value=$ISCSI_USER |") {
 
101
    if($password ne "not_required") {
 
102
      if(!open USERNAME, "iscsiadm -m node -T $store --op=update --name node.session.auth.username --value=$ISCSI_USER |") {
59
103
        print "Could not update target username";
60
104
        do_exit(1)
61
 
    }
62
 
 
63
 
    while(<USERNAME>) {};
64
 
 
65
 
    if(!open PASSWD, "iscsiadm -m node -T $store --op=update --name node.session.auth.password --value=$passwd |") {
 
105
      }
 
106
 
 
107
      while(<USERNAME>) {};
 
108
 
 
109
      if(!open PASSWD, "iscsiadm -m node -T $store --op=update --name node.session.auth.password --value=$passwd |") {
66
110
        print "Could not update target password";
67
111
        do_exit(1)
 
112
      }
 
113
 
 
114
      while(<PASSWD>) {};
 
115
 
68
116
    }
69
117
 
70
 
    while(<PASSWD>) {};
71
 
 
72
118
    if(!open LOGIN, "iscsiadm -m node -T $store -l |") {
73
119
        print "Could not login to target";
74
120
        do_exit(1)
75
121
    }
76
122
 
77
 
    while(<LOGIN>) {};
 
123
    my $login = "";
 
124
    while(<LOGIN>) {$login = $login.$_;};
 
125
    if(length($login) <= 0) {
 
126
        print STDERR "Unable to login to target. Aborting.\n";
 
127
        do_exit(1);
 
128
    }
78
129
}
79
130
 
80
131
sub decrypt_password {
98
149
    return $passwd;
99
150
}
100
151
 
 
152
sub get_session {
 
153
    my ($store) = @_;
 
154
    $num_retries = 5;
 
155
 
 
156
    for ($i = 0; $i < $num_retries; ++$i) {
 
157
      if(!open GETSESSION, "iscsiadm -m session |") {
 
158
          print STDERR "Could not get iscsi session information";
 
159
          do_exit(1)
 
160
      }
 
161
 
 
162
      while (<GETSESSION>) {
 
163
        if ($_ =~ /.*$store\n/) {
 
164
          close GETSESSION;
 
165
          return 1;
 
166
        }
 
167
      }
 
168
      close GETSESSION;
 
169
    }
 
170
    return 0;
 
171
}
 
172
 
101
173
sub get_device_name {
102
174
    my ($store) = @_;
103
175
    $num_retries = 5;
117
189
              $attach_seen = 0;
118
190
          } elsif($_ =~ /.*Attached scsi disk ([a-zA-Z0-9]+).*\n/) {
119
191
              if($found_target == 1) {
120
 
                return "/dev/", $1;
 
192
                return "/dev/" . $1;
121
193
              }
122
194
              $attach_seen = 1;
123
195
          }
126
198
    }
127
199
}
128
200
 
 
201
sub get_device_name_from_lun {
 
202
    my ($store, $lun) = @_;
 
203
    $num_retries = 5;
 
204
 
 
205
    for ($i = 0; $i < $num_retries; ++$i) {
 
206
      if(!open GETSESSION, "iscsiadm -m session -P 3 |") {
 
207
          print STDERR "Could not get iscsi session information";
 
208
          do_exit(1)
 
209
      }
 
210
 
 
211
      $found_target = 0;
 
212
      $found_lun = 0;
 
213
      $attach_seen = 1;
 
214
      while (<GETSESSION>) {
 
215
          if ($_ =~ /Target: (.*)\n/) {
 
216
              last if $attach_seen == 0;
 
217
              $found_target = 1 if $1 eq $store;
 
218
              $attach_seen = 0;
 
219
              $found_lun = 0;
 
220
          } elsif ($_ =~ /.*Attached scsi disk ([a-zA-Z0-9]+).*\n/) {
 
221
              if ($found_target == 1 && $found_lun == 1) {
 
222
                return "/dev/" . $1;
 
223
              }
 
224
              $attach_seen = 1;
 
225
          } elsif ($_ =~ /.*Lun: (.*)\n/) {
 
226
              $found_lun = 1 if $1 eq $lun;
 
227
          }
 
228
      }
 
229
      close GETSESSION;
 
230
    }
 
231
}
 
232
 
 
233
sub rescan_target {
 
234
 
 
235
  if(!open GETSESSION, "iscsiadm -m session -R |") {
 
236
    print STDERR "Could not get iscsi session information";
 
237
    do_exit(1)
 
238
  }
 
239
  close GETSESSION;
 
240
}
 
241
 
 
242
 
129
243
sub do_exit() {
130
244
    $e = shift;
131
245