2525import java .io .FileNotFoundException ;
2626import java .io .FileOutputStream ;
2727import java .io .IOException ;
28+ import java .nio .file .Files ;
29+ import java .nio .file .Paths ;
2830import java .text .DateFormat ;
2931import java .text .SimpleDateFormat ;
3032import java .util .Arrays ;
3133import java .util .Date ;
3234import java .util .HashMap ;
35+ import java .util .HashSet ;
3336import java .util .List ;
3437import java .util .Map ;
38+ import java .util .Set ;
3539import java .util .UUID ;
40+ import java .util .stream .Collectors ;
3641
3742import javax .naming .ConfigurationException ;
3843
39- import org .apache .cloudstack .direct .download .DirectDownloadHelper ;
40- import org .apache .cloudstack .direct .download .DirectTemplateDownloader ;
41- import com .cloud .storage .ScopeType ;
42- import com .cloud .storage .Volume ;
4344import org .apache .cloudstack .agent .directdownload .DirectDownloadAnswer ;
4445import org .apache .cloudstack .agent .directdownload .DirectDownloadCommand ;
46+ import org .apache .cloudstack .direct .download .DirectDownloadHelper ;
47+ import org .apache .cloudstack .direct .download .DirectTemplateDownloader ;
4548import org .apache .cloudstack .engine .subsystem .api .storage .SnapshotInfo ;
4649import org .apache .cloudstack .storage .command .AttachAnswer ;
4750import org .apache .cloudstack .storage .command .AttachCommand ;
7174import org .apache .cloudstack .utils .qemu .QemuObject ;
7275import org .apache .commons .collections .MapUtils ;
7376import org .apache .commons .io .FileUtils ;
74-
77+ import org .apache .commons .lang3 .BooleanUtils ;
78+ import org .apache .commons .lang3 .StringUtils ;
79+ import org .apache .commons .lang3 .builder .ToStringBuilder ;
80+ import org .apache .commons .lang3 .builder .ToStringStyle ;
7581import org .apache .log4j .Logger ;
7682import org .libvirt .Connect ;
7783import org .libvirt .Domain ;
110116import com .cloud .hypervisor .kvm .resource .wrapper .LibvirtUtilitiesHelper ;
111117import com .cloud .storage .JavaStorageLayer ;
112118import com .cloud .storage .MigrationOptions ;
119+ import com .cloud .storage .ScopeType ;
113120import com .cloud .storage .Storage .ImageFormat ;
114121import com .cloud .storage .Storage .StoragePoolType ;
115122import com .cloud .storage .StorageLayer ;
123+ import com .cloud .storage .Volume ;
116124import com .cloud .storage .resource .StorageProcessor ;
117125import com .cloud .storage .template .Processor ;
118126import com .cloud .storage .template .Processor .FormatInfo ;
126134import com .cloud .utils .storage .S3 .S3Utils ;
127135import com .cloud .vm .VmDetailConstants ;
128136
129- import java .nio .file .Files ;
130- import java .nio .file .Paths ;
131- import java .util .HashSet ;
132- import java .util .Set ;
133- import java .util .stream .Collectors ;
134- import org .apache .commons .lang3 .BooleanUtils ;
135- import org .apache .commons .lang3 .StringUtils ;
136- import org .apache .commons .lang3 .builder .ToStringBuilder ;
137- import org .apache .commons .lang3 .builder .ToStringStyle ;
138-
139137public class KVMStorageProcessor implements StorageProcessor {
140138 private static final Logger s_logger = Logger .getLogger (KVMStorageProcessor .class );
141139 private final KVMStoragePoolManager storagePoolMgr ;
@@ -1074,16 +1072,23 @@ private void deleteSnapshotOnPrimary(final CopyCommand cmd, final SnapshotObject
10741072 s_logger .debug (String .format ("This backup is temporary, not deleting snapshot [%s] on primary storage [%s]" , snapshotPath , primaryPool .getUuid ()));
10751073 }
10761074 }
1077- protected synchronized void attachOrDetachISO (final Connect conn , final String vmName , String isoPath , final boolean isAttach , Map <String , String > params ) throws
1075+
1076+ protected synchronized void attachOrDetachISO (final Connect conn , final String vmName , String isoPath , final boolean isAttach , Map <String , String > params , DataStoreTO store ) throws
10781077 LibvirtException , InternalErrorException {
10791078 DiskDef iso = new DiskDef ();
10801079 boolean isUefiEnabled = MapUtils .isNotEmpty (params ) && params .containsKey ("UEFI" );
10811080 if (isoPath != null && isAttach ) {
10821081 final int index = isoPath .lastIndexOf ("/" );
10831082 final String path = isoPath .substring (0 , index );
10841083 final String name = isoPath .substring (index + 1 );
1085- final KVMStoragePool secondaryPool = storagePoolMgr .getStoragePoolByURI (path );
1086- final KVMPhysicalDisk isoVol = secondaryPool .getPhysicalDisk (name );
1084+ KVMStoragePool storagePool ;
1085+ if (store instanceof PrimaryDataStoreTO ) {
1086+ PrimaryDataStoreTO primaryDataStoreTO = (PrimaryDataStoreTO )store ;
1087+ storagePool = storagePoolMgr .getStoragePool (primaryDataStoreTO .getPoolType (), store .getUuid ());
1088+ } else {
1089+ storagePool = storagePoolMgr .getStoragePoolByURI (path );
1090+ }
1091+ final KVMPhysicalDisk isoVol = storagePool .getPhysicalDisk (name );
10871092 isoPath = isoVol .getPath ();
10881093
10891094 iso .defISODisk (isoPath , isUefiEnabled );
@@ -1112,7 +1117,7 @@ public Answer attachIso(final AttachCommand cmd) {
11121117 try {
11131118 String dataStoreUrl = getDataStoreUrlFromStore (store );
11141119 final Connect conn = LibvirtConnection .getConnectionByVmName (cmd .getVmName ());
1115- attachOrDetachISO (conn , cmd .getVmName (), dataStoreUrl + File .separator + isoTO .getPath (), true , cmd .getControllerInfo ());
1120+ attachOrDetachISO (conn , cmd .getVmName (), dataStoreUrl + File .separator + isoTO .getPath (), true , cmd .getControllerInfo (), store );
11161121 } catch (final LibvirtException e ) {
11171122 return new Answer (cmd , false , e .toString ());
11181123 } catch (final InternalErrorException e ) {
@@ -1133,7 +1138,7 @@ public Answer dettachIso(final DettachCommand cmd) {
11331138 try {
11341139 String dataStoreUrl = getDataStoreUrlFromStore (store );
11351140 final Connect conn = LibvirtConnection .getConnectionByVmName (cmd .getVmName ());
1136- attachOrDetachISO (conn , cmd .getVmName (), dataStoreUrl + File .separator + isoTO .getPath (), false , cmd .getParams ());
1141+ attachOrDetachISO (conn , cmd .getVmName (), dataStoreUrl + File .separator + isoTO .getPath (), false , cmd .getParams (), store );
11371142 } catch (final LibvirtException e ) {
11381143 return new Answer (cmd , false , e .toString ());
11391144 } catch (final InternalErrorException e ) {
@@ -1149,19 +1154,25 @@ public Answer dettachIso(final DettachCommand cmd) {
11491154 * Return data store URL from store
11501155 */
11511156 private String getDataStoreUrlFromStore (DataStoreTO store ) {
1152- if (!(store instanceof NfsTO ) && (!(store instanceof PrimaryDataStoreTO ) ||
1153- store instanceof PrimaryDataStoreTO && !((PrimaryDataStoreTO ) store ).getPoolType ().equals (StoragePoolType .NetworkFilesystem ))) {
1157+ List <StoragePoolType > supportedPoolType = List .of (StoragePoolType .NetworkFilesystem , StoragePoolType .Filesystem );
1158+ if (!(store instanceof NfsTO ) && (!(store instanceof PrimaryDataStoreTO ) || !supportedPoolType .contains (((PrimaryDataStoreTO ) store ).getPoolType ()))) {
1159+ s_logger .error (String .format ("Unsupported protocol, store: %s" , store .getUuid ()));
11541160 throw new InvalidParameterValueException ("unsupported protocol" );
11551161 }
11561162
11571163 if (store instanceof NfsTO ) {
11581164 NfsTO nfsStore = (NfsTO )store ;
11591165 return nfsStore .getUrl ();
1160- } else if (store instanceof PrimaryDataStoreTO && (( PrimaryDataStoreTO ) store ). getPoolType (). equals ( StoragePoolType . NetworkFilesystem ) ) {
1166+ } else if (store instanceof PrimaryDataStoreTO ) {
11611167 //In order to support directly downloaded ISOs
1168+ StoragePoolType poolType = ((PrimaryDataStoreTO )store ).getPoolType ();
11621169 String psHost = ((PrimaryDataStoreTO ) store ).getHost ();
11631170 String psPath = ((PrimaryDataStoreTO ) store ).getPath ();
1164- return "nfs://" + psHost + File .separator + psPath ;
1171+ if (StoragePoolType .NetworkFilesystem .equals (poolType )) {
1172+ return "nfs://" + psHost + File .separator + psPath ;
1173+ } else if (StoragePoolType .Filesystem .equals (poolType )) {
1174+ return StoragePoolType .Filesystem .toString ().toLowerCase () + "://" + psHost + File .separator + psPath ;
1175+ }
11651176 }
11661177 return store .getUrl ();
11671178 }
0 commit comments