1919
2020package com .cloud .resource ;
2121
22+ import com .cloud .agent .IAgentControl ;
23+ import com .cloud .agent .api .Answer ;
24+ import com .cloud .agent .api .Command ;
25+ import com .cloud .agent .api .ListHostDeviceAnswer ;
26+ import com .cloud .agent .api .ListHostLunDeviceAnswer ;
27+ import com .cloud .agent .api .ListHostUsbDeviceAnswer ;
28+ import com .cloud .agent .api .StartupCommand ;
29+ import com .cloud .agent .api .UpdateHostLunDeviceAnswer ;
30+ import com .cloud .agent .api .UpdateHostUsbDeviceAnswer ;
31+ import com .cloud .utils .net .NetUtils ;
32+ import com .cloud .utils .script .OutputInterpreter ;
33+ import com .cloud .utils .script .Script ;
2234import java .io .File ;
2335import java .io .FileWriter ;
2436import java .io .IOException ;
3648import java .util .List ;
3749import java .util .Locale ;
3850import java .util .Map ;
39-
4051import javax .naming .ConfigurationException ;
41-
42- import org .apache .cloudstack .storage .command .browser .ListRbdObjectsAnswer ;
4352import org .apache .cloudstack .storage .command .browser .ListDataStoreObjectsAnswer ;
53+ import org .apache .cloudstack .storage .command .browser .ListRbdObjectsAnswer ;
4454import org .apache .commons .collections .CollectionUtils ;
4555import org .apache .commons .lang3 .StringUtils ;
4656import org .apache .logging .log4j .LogManager ;
4757import org .apache .logging .log4j .Logger ;
4858import org .json .JSONArray ;
4959import org .json .JSONObject ;
50- import com .cloud .agent .IAgentControl ;
51- import com .cloud .agent .api .Answer ;
52- import com .cloud .agent .api .Command ;
53- import com .cloud .agent .api .ListHostDeviceAnswer ;
54- import com .cloud .agent .api .ListHostLunDeviceAnswer ;
55- import com .cloud .agent .api .ListHostUsbDeviceAnswer ;
56- import com .cloud .agent .api .StartupCommand ;
57- import com .cloud .agent .api .UpdateHostUsbDeviceAnswer ;
58- import com .cloud .utils .net .NetUtils ;
59- import com .cloud .utils .script .OutputInterpreter ;
60- import com .cloud .utils .script .Script ;
60+
61+
62+ // import org.apache.cloudstack.storage.command.browser.ListRbdObjectsAnswer;
63+
6164// import com.cloud.agent.api.ListHostLunDeviceCommand;
6265
6366public abstract class ServerResourceBase implements ServerResource {
@@ -235,18 +238,17 @@ public Answer listHostLunDevices(Command command) {
235238 String size = device .getString ("size" );
236239
237240 // 파티션 존재 여부 확인
238- JSONArray children = device .optJSONArray ("children" );
239- boolean hasPartition = (children != null && children .length () > 0 );
241+ boolean hasPartition = hasPartitionRecursive (device );
240242
241243 StringBuilder info = new StringBuilder ();
242244 if (isMultipathActive && name .startsWith ("/dev/disk/by-path/" )) {
243- info .append ("Multipath LUN Device: " ).append (name );
245+ // info.append("Multipath LUN Device: ").append(name);
244246 } else {
245- info .append ("LUN Device: " ).append (name );
247+ // info.append("LUN Device: ").append(name);
246248 }
247- info .append (" Size: " ). append ( size );
249+ info .append (size );
248250 if (hasPartition ) {
249- info .append (" (" ).append (children . length ()) .append (" partitions )" );
251+ info .append (" (" ).append (hasPartition ? "has partitions" : "no partitions" ) .append (")" );
250252 }
251253
252254 hostDevicesNames .add (name );
@@ -274,6 +276,25 @@ private boolean checkMultipathStatus() {
274276 return "active" .equals (result != null ? result .trim () : "" );
275277 }
276278
279+ private boolean hasPartitionRecursive (JSONObject device ) {
280+ // children이 없으면 false
281+ if (!device .has ("children" )) {
282+ return false ;
283+ }
284+ JSONArray children = device .getJSONArray ("children" );
285+ for (int i = 0 ; i < children .length (); i ++) {
286+ JSONObject child = children .getJSONObject (i );
287+ String type = child .optString ("type" , "" );
288+ if ("part" .equals (type )) {
289+ return true ;
290+ }
291+ if (hasPartitionRecursive (child )) {
292+ return true ;
293+ }
294+ }
295+ return false ;
296+ }
297+
277298 protected Answer createImageRbd (String poolUuid , String skey , String authUserName , String host , String names , long sizes , String poolPath ) {
278299 createRBDSecretKeyFileIfNoExist (poolUuid , DEFAULT_LOCAL_STORAGE_PATH , skey );
279300 String cmdout = Script .runSimpleBashScript ("rbd -p " + poolPath + " --id " + authUserName + " -m " + host + " -K " + DEFAULT_LOCAL_STORAGE_PATH + poolUuid + " create -s " + (sizes * 1024 ) + " " + names );
@@ -626,6 +647,8 @@ protected Answer updateHostUsbDevices(Command command, String vmName, String xml
626647 logger .info ("Executing detach command for VM: {} with XML: {}" , vmName , xmlConfig );
627648 }
628649
650+ logger .info ("isAttach value: {}" , isAttach );
651+
629652 String result = virshCmd .execute ();
630653
631654 if (result != null ) {
@@ -664,22 +687,24 @@ protected Answer updateHostLunDevices(Command command, String vmName, String xml
664687 logger .info ("Executing detach command for VM: {} with XML: {}" , vmName , xmlConfig );
665688 }
666689
690+ logger .info ("isAttach value: {}" , isAttach );
691+
667692 String result = virshCmd .execute ();
668693
669694 if (result != null ) {
670695 String action = isAttach ? "attach" : "detach" ;
671696 logger .error ("Failed to {} USB device: {}" , action , result );
672- return new UpdateHostUsbDeviceAnswer (false , vmName , xmlConfig , isAttach );
697+ return new UpdateHostLunDeviceAnswer (false , vmName , xmlConfig , isAttach );
673698 }
674699
675700 String action = isAttach ? "attached to" : "detached from" ;
676701 logger .info ("Successfully {} USB device for VM {}" , action , vmName );
677- return new UpdateHostUsbDeviceAnswer (true , vmName , xmlConfig , isAttach );
702+ return new UpdateHostLunDeviceAnswer (true , vmName , xmlConfig , isAttach );
678703
679704 } catch (Exception e ) {
680705 String action = isAttach ? "attaching" : "detaching" ;
681706 logger .error ("Error {} USB device: {}" , action , e .getMessage (), e );
682- return new UpdateHostUsbDeviceAnswer (false , vmName , xmlConfig , isAttach );
707+ return new UpdateHostLunDeviceAnswer (false , vmName , xmlConfig , isAttach );
683708 }
684709 }
685710}
0 commit comments