@@ -2243,13 +2243,11 @@ protected synchronized String attachOrDetachISO(Connect conn, String vmName, Str
22432243 isoPath = isoVol .getPath ();
22442244
22452245 DiskDef iso = new DiskDef ();
2246- iso .defFileBasedDisk (isoPath , "hdc" , DiskDef .diskBus .IDE , DiskDef .diskFmtType .RAW );
2247- iso .setDeviceType (DiskDef .deviceType .CDROM );
2246+ iso .defISODisk (isoPath );
22482247 isoXml = iso .toString ();
22492248 } else {
22502249 DiskDef iso = new DiskDef ();
2251- iso .defFileBasedDisk (null , "hdc" , DiskDef .diskBus .IDE , DiskDef .diskFmtType .RAW );
2252- iso .setDeviceType (DiskDef .deviceType .CDROM );
2250+ iso .defISODisk (null );
22532251 isoXml = iso .toString ();
22542252 }
22552253
@@ -2259,43 +2257,44 @@ protected synchronized String attachOrDetachISO(Connect conn, String vmName, Str
22592257 protected synchronized String attachOrDetachDisk (Connect conn , boolean attach , String vmName , String sourceFile , int devId ) throws LibvirtException , InternalErrorException {
22602258 List <DiskDef > disks = null ;
22612259 Domain dm = null ;
2260+ int deviceId = devId ;
22622261 try {
2263- dm = conn .domainLookupByUUID (UUID .nameUUIDFromBytes (vmName .getBytes ()));
2264- LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser ();
2265- String xml = dm .getXMLDesc (0 );
2266- parser .parseDomainXML (xml );
2267- disks = parser .getDisks ();
2268- } catch (LibvirtException e ) {
2269- throw e ;
2262+ if (!attach ) {
2263+ dm = conn .domainLookupByUUID (UUID .nameUUIDFromBytes (vmName .getBytes ()));
2264+ LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser ();
2265+ String xml = dm .getXMLDesc (0 );
2266+ parser .parseDomainXML (xml );
2267+ disks = parser .getDisks ();
2268+
2269+ boolean diskAttached = false ;
2270+
2271+ for (DiskDef disk : disks ) {
2272+ String file = disk .getDiskPath ();
2273+ if (file != null && file .equalsIgnoreCase (sourceFile )) {
2274+ deviceId = disk .getDiskSeq ();
2275+ diskAttached = true ;
2276+ break ;
2277+ }
2278+ }
2279+ if (!diskAttached ) {
2280+ throw new InternalErrorException ("disk: " + sourceFile + " is not attached before" );
2281+ }
2282+ }
2283+
2284+ DiskDef disk = new DiskDef ();
2285+ String guestOSType = getGuestType (conn , vmName );
2286+ if (isGuestPVEnabled (guestOSType )) {
2287+ disk .defFileBasedDisk (sourceFile , deviceId , DiskDef .diskBus .VIRTIO , DiskDef .diskFmtType .QCOW2 );
2288+ } else {
2289+ disk .defFileBasedDisk (sourceFile , deviceId , DiskDef .diskBus .SCSI , DiskDef .diskFmtType .QCOW2 );
2290+ }
2291+ String xml = disk .toString ();
2292+ return attachOrDetachDevice (conn , attach , vmName , xml );
22702293 } finally {
22712294 if (dm != null ) {
22722295 dm .free ();
22732296 }
22742297 }
2275-
2276- if (!attach ) {
2277- boolean diskAttached = false ;
2278-
2279- for (DiskDef disk : disks ) {
2280- if (disk .getDiskPath ().equalsIgnoreCase (sourceFile )) {
2281- devId = disk .getDiskSeq ();
2282- diskAttached = true ;
2283- }
2284- }
2285- if (!diskAttached ) {
2286- throw new InternalErrorException ("disk: " + sourceFile + " is not attached before" );
2287- }
2288- }
2289-
2290- DiskDef disk = new DiskDef ();
2291- String guestOSType = getGuestType (conn , vmName );
2292- if (isGuestPVEnabled (guestOSType )) {
2293- disk .defFileBasedDisk (sourceFile , devId , DiskDef .diskBus .VIRTIO , DiskDef .diskFmtType .QCOW2 );
2294- } else {
2295- disk .defFileBasedDisk (sourceFile , devId , DiskDef .diskBus .SCSI , DiskDef .diskFmtType .QCOW2 );
2296- }
2297- String xml = disk .toString ();
2298- return attachOrDetachDevice (conn , attach , vmName , xml );
22992298 }
23002299
23012300 private synchronized String attachOrDetachDevice (Connect conn , boolean attach , String vmName , String xml ) throws LibvirtException , InternalErrorException {
@@ -3193,7 +3192,8 @@ private boolean add_network_rules(String vmName, String vmId, String guestIP, St
31933192 cmd .add ("--sig" , sig );
31943193 cmd .add ("--seq" , seq );
31953194 cmd .add ("--vmmac" , mac );
3196- cmd .add ("--rules" , newRules );
3195+ if (rules != null )
3196+ cmd .add ("--rules" , newRules );
31973197 String result = cmd .execute ();
31983198 if (result != null ) {
31993199 return false ;
0 commit comments