103
103
returnValue = SystemUtil.run(new String[]{"sudo", "tgtadm", "--help"});
104
104
if(returnValue.length() == 0) {
105
105
throw new EucalyptusCloudException("tgtadm not found: Is tgt installed?");
107
LOG.info(returnValue);
109
107
if(SystemUtil.runAndGetCode(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--mode", "target", "--op", "show"}) != 0) {
110
throw new EucalyptusCloudException("Unable to connect to tgt daemon. Is tgtd loaded?");
108
LOG.warn("Unable to connect to tgt daemon. Is tgtd loaded?");
109
LOG.info("Attempting to start tgtd ISCSI daemon");
110
if(SystemUtil.runAndGetCode(new String[]{"sudo", "tgtd"}) != 0) {
111
throw new EucalyptusCloudException("Unable to start tgt daemon. Cannot proceed.");
174
176
public void unexportTarget(int tid, int lun) {
177
Runtime rt = Runtime.getRuntime();
178
Process proc = rt.exec(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "unbind", "--mode", "target", "--tid", String.valueOf(tid), "-I", "ALL"});
179
StreamConsumer error = new StreamConsumer(proc.getErrorStream());
180
StreamConsumer output = new StreamConsumer(proc.getInputStream());
185
proc = rt.exec(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "delete", "--mode", "logicalunit", "--tid" , String.valueOf(tid), "--lun", String.valueOf(lun)});
186
error = new StreamConsumer(proc.getErrorStream());
187
output = new StreamConsumer(proc.getInputStream());
192
String errorValue = error.getReturnValue();
193
if(errorValue.length() > 0)
194
throw new EucalyptusCloudException(errorValue);
196
proc = rt.exec(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "delete", "--mode", "target", "--tid ", String.valueOf(tid)});
197
error = new StreamConsumer(proc.getErrorStream());
198
output = new StreamConsumer(proc.getInputStream());
179
if(SystemUtil.runAndGetCode(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "unbind", "--mode", "target", "--tid", String.valueOf(tid), "-I", "ALL"}) != 0) {
180
LOG.error("Unable to unbind tid: " + tid);
184
if(SystemUtil.runAndGetCode(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "delete", "--mode", "logicalunit", "--tid" , String.valueOf(tid), "--lun", String.valueOf(lun)}) != 0) {
185
LOG.error("Unable to delete lun for tid: " + tid);
189
if(SystemUtil.runAndGetCode(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "delete", "--mode", "target", "--tid", String.valueOf(tid)}) != 0) {
190
LOG.error("Unable to delete target: " + tid);
202
193
} catch (Throwable t) {
267
258
public synchronized void allocateTarget(LVMVolumeInfo volumeInfo) {
268
259
if(volumeInfo instanceof ISCSIVolumeInfo) {
269
260
ISCSIVolumeInfo iscsiVolumeInfo = (ISCSIVolumeInfo) volumeInfo;
270
ISCSIMetaInfo metaInfo = new ISCSIMetaInfo();
271
261
EntityWrapper<ISCSIMetaInfo> db = StorageProperties.getEntityWrapper();
272
List<ISCSIMetaInfo> metaInfoList = db.query(metaInfo);
262
List<ISCSIMetaInfo> metaInfoList = db.query(new ISCSIMetaInfo(StorageProperties.NAME));
263
int tid = -1, storeNumber = -1;
273
264
if(metaInfoList.size() > 0) {
274
265
ISCSIMetaInfo foundMetaInfo = metaInfoList.get(0);
275
int storeNumber = foundMetaInfo.getStoreNumber();
276
int tid = foundMetaInfo.getTid();
277
iscsiVolumeInfo.setStoreName(foundMetaInfo.getStorePrefix() + StorageProperties.NAME + ":store" + storeNumber);
278
iscsiVolumeInfo.setStoreUser(foundMetaInfo.getStoreUser());
279
iscsiVolumeInfo.setTid(tid);
280
iscsiVolumeInfo.setLun(1);
281
foundMetaInfo.setStoreNumber(++storeNumber);
282
foundMetaInfo.setTid(++tid);
266
storeNumber = foundMetaInfo.getStoreNumber();
267
tid = foundMetaInfo.getTid();
270
//check if tid is in use
274
String returnValue = SystemUtil.run(new String[]{"sudo", "tgtadm", "--lld", "iscsi", "--op", "show", "--mode", "target", "--tid" , String.valueOf(i)});
275
if(returnValue.length() == 0) {
279
} catch (ExecutionException e) {
281
iscsiVolumeInfo.setTid(-1);
284
i = (i + 1) % Integer.MAX_VALUE;
287
db = StorageProperties.getEntityWrapper();
288
metaInfoList = db.query(new ISCSIMetaInfo(StorageProperties.NAME));
289
if(metaInfoList.size() > 0) {
290
ISCSIMetaInfo foundMetaInfo = metaInfoList.get(0);
291
foundMetaInfo.setStoreNumber(++storeNumber);
292
foundMetaInfo.setTid(tid + 1);
293
iscsiVolumeInfo.setStoreName(foundMetaInfo.getStorePrefix() + StorageProperties.NAME + ":store" + storeNumber);
294
iscsiVolumeInfo.setStoreUser(foundMetaInfo.getStoreUser());
295
iscsiVolumeInfo.setTid(tid);
296
//LUN cannot be 0 (some clients don't like that).
297
iscsiVolumeInfo.setLun(1);
301
iscsiVolumeInfo.setTid(-1);
302
LOG.fatal("Unable to allocate ISCSI target id.");