2727import com .cloud .agent .api .ListHostLunDeviceAnswer ;
2828import com .cloud .agent .api .ListHostUsbDeviceAnswer ;
2929import com .cloud .agent .api .StartupCommand ;
30+ import com .cloud .agent .api .UpdateHostHbaDeviceAnswer ;
3031import com .cloud .agent .api .UpdateHostLunDeviceAnswer ;
3132import com .cloud .agent .api .UpdateHostUsbDeviceAnswer ;
3233import com .cloud .utils .net .NetUtils ;
5758import org .apache .logging .log4j .Logger ;
5859import org .json .JSONArray ;
5960import org .json .JSONObject ;
61+ import javax .naming .ConfigurationException ;
6062
6163
6264// import org.apache.cloudstack.storage.command.browser.ListRbdObjectsAnswer;
@@ -298,7 +300,6 @@ private boolean hasPartitionRecursive(JSONObject device) {
298300 protected Answer listHostHbaDevices (Command command ) {
299301 List <String > hostDevicesText = new ArrayList <>();
300302 List <String > hostDevicesNames = new ArrayList <>();
301- // lsscsi 대신 lspci 명령어로 HBA 조회
302303 Script listCommand = new Script ("/bin/bash" );
303304 listCommand .add ("-c" );
304305 listCommand .add ("lspci | grep -i 'scsi\\ |sas\\ |fibre\\ |raid\\ |hba'" );
@@ -729,4 +730,45 @@ protected Answer updateHostLunDevices(Command command, String vmName, String xml
729730 return new UpdateHostLunDeviceAnswer (false , vmName , xmlConfig , isAttach );
730731 }
731732 }
733+
734+ protected Answer updateHostHbaDevices (Command command , String vmName , String xmlConfig , boolean isAttach ) {
735+ String hbaXmlPath = String .format ("/tmp/hba_device_%s.xml" , vmName );
736+ try {
737+ // XML 파일이 없을 경우에만 생성
738+ File xmlFile = new File (hbaXmlPath );
739+ if (!xmlFile .exists ()) {
740+ try (PrintWriter writer = new PrintWriter (hbaXmlPath )) {
741+ writer .write (xmlConfig );
742+ }
743+ logger .info ("Generated XML file: {} for VM: {}" , hbaXmlPath , vmName );
744+ }
745+
746+ Script virshCmd = new Script ("virsh" );
747+ if (isAttach ) {
748+ virshCmd .add ("attach-device" , vmName , hbaXmlPath );
749+ } else {
750+ virshCmd .add ("detach-device" , vmName , hbaXmlPath );
751+ logger .info ("Executing detach command for VM: {} with XML: {}" , vmName , xmlConfig );
752+ }
753+
754+ logger .info ("isAttach value: {}" , isAttach );
755+
756+ String result = virshCmd .execute ();
757+
758+ if (result != null ) {
759+ String action = isAttach ? "attach" : "detach" ;
760+ logger .error ("Failed to {} HBA device: {}" , action , result );
761+ return new UpdateHostHbaDeviceAnswer (false , vmName , xmlConfig , isAttach );
762+ }
763+
764+ String action = isAttach ? "attached to" : "detached from" ;
765+ logger .info ("Successfully {} HBA device for VM {}" , action , vmName );
766+ return new UpdateHostHbaDeviceAnswer (true , vmName , xmlConfig , isAttach );
767+
768+ } catch (Exception e ) {
769+ String action = isAttach ? "attaching" : "detaching" ;
770+ logger .error ("Error {} HBA device: {}" , action , e .getMessage (), e );
771+ return new UpdateHostHbaDeviceAnswer (false , vmName , xmlConfig , isAttach );
772+ }
773+ }
732774}
0 commit comments