@@ -3792,6 +3792,81 @@ protected KVMStoragePoolManager getPoolManager() {
37923792 return storagePoolManager ;
37933793 }
37943794
3795+ public void detachAndAttachConfigDriveISO (final Connect conn , final String vmName , VirtualMachineTO to ) {
3796+ // detach and re-attach configdrive ISO
3797+ List <DiskDef > disks = getDisks (conn , vmName );
3798+ DiskDef configdrive = null ;
3799+ for (DiskDef disk : disks ) {
3800+ if (disk .getDeviceType () == DiskDef .DeviceType .CDROM && CONFIG_DRIVE_ISO_DISK_LABEL .equals (disk .getDiskLabel ())) {
3801+ configdrive = disk ;
3802+ }
3803+ }
3804+
3805+ if (configdrive != null ) {
3806+ try {
3807+ LOGGER .debug (String .format ("Detaching ConfigDrive ISO of the VM %s, at path %s" , vmName , configdrive .getDiskPath ()));
3808+ String result = attachOrDetachConfigDriveISO (conn , vmName , to , configdrive .getDiskPath (), false , CONFIG_DRIVE_ISO_DEVICE_ID );
3809+ if (result != null ) {
3810+ LOGGER .warn (String .format ("Detach ConfigDrive ISO of the VM %s, at path %s with %s: " , vmName , configdrive .getDiskPath (), result ));
3811+ }
3812+ LOGGER .debug (String .format ("Attaching ConfigDrive ISO of the VM %s, at path %s" , vmName , configdrive .getDiskPath ()));
3813+ result = attachOrDetachConfigDriveISO (conn , vmName , to , configdrive .getDiskPath (), true , CONFIG_DRIVE_ISO_DEVICE_ID );
3814+ if (result != null ) {
3815+ LOGGER .warn (String .format ("Attach ConfigDrive ISO of the VM %s, at path %s with %s: " , vmName , configdrive .getDiskPath (), result ));
3816+ }
3817+ } catch (final LibvirtException | InternalErrorException | URISyntaxException e ) {
3818+ final String msg = "Detach and attach ConfigDrive ISO failed due to " + e .toString ();
3819+ LOGGER .warn (msg , e );
3820+ }
3821+ }
3822+ }
3823+
3824+ public synchronized String attachOrDetachConfigDriveISO (final Connect conn , final String vmName , VirtualMachineTO to , String cdPath , final boolean isAttach , final Integer diskSeq ) throws LibvirtException , URISyntaxException ,
3825+ InternalErrorException {
3826+ String isoPath = "" ;
3827+ DiskTO configDriveDisk = null ;
3828+ for (DiskTO disk : to .getDisks ()) {
3829+ if (disk .getPath () != null && disk .getPath ().contains ("configdrive" )) {
3830+ configDriveDisk = disk ;
3831+ break ;
3832+ }
3833+ }
3834+ isoPath = getVolumePath (conn , configDriveDisk , to .isConfigDriveOnHostCache ());
3835+ DiskDef iso = new DiskDef ();
3836+ if (isAttach && StringUtils .isNotBlank (isoPath ) && configDriveDisk !=null && isoPath .lastIndexOf ("/" ) > 0 ) {
3837+ if (isoPath .startsWith (getConfigPath () + "/" + ConfigDrive .CONFIGDRIVEDIR ) && isoPath .contains (vmName )) {
3838+ iso .defISODisk (isoPath , diskSeq , DiskDef .DiskType .FILE );
3839+ } else {
3840+ final DataTO diskData = configDriveDisk .getData ();
3841+ final String dataName = configDriveDisk .getPath ();
3842+ final DataStoreTO store = diskData .getDataStore ();
3843+ isoPath = store .getUrl ().split ("\\ ?" )[0 ] + File .separator + dataName ;
3844+
3845+ final int index = isoPath .lastIndexOf ("/" );
3846+ final String path = isoPath .substring (0 , index );
3847+ final String name = isoPath .substring (index + 1 );
3848+ final KVMStoragePool storagePool = storagePoolManager .getStoragePoolByURI (path );
3849+ final KVMPhysicalDisk isoVol = storagePool .getPhysicalDisk (name );
3850+ final DiskDef .DiskType diskType = getDiskType (isoVol );
3851+ isoPath = isoVol .getPath ();
3852+ iso .defISODisk (isoPath , diskSeq , diskType );
3853+ }
3854+ } else {
3855+ iso .defISODisk (null , diskSeq , DiskDef .DiskType .FILE );
3856+ }
3857+ final String result = attachOrDetachDevice (conn , true , vmName , iso .toString ());
3858+ if (result == null && !isAttach ) {
3859+ final List <DiskDef > disks = getDisks (conn , vmName );
3860+ for (final DiskDef disk : disks ) {
3861+ if (disk .getDeviceType () == DiskDef .DeviceType .CDROM
3862+ && (diskSeq == null || disk .getDiskLabel ().equals (iso .getDiskLabel ()))) {
3863+ cleanupDisk (disk );
3864+ }
3865+ }
3866+ }
3867+ return result ;
3868+ }
3869+
37953870 public void detachAndAttachConfigDriveISO (final Connect conn , final String vmName ) {
37963871 // detach and re-attach configdrive ISO
37973872 List <DiskDef > disks = getDisks (conn , vmName );
0 commit comments