static String getFreeDatastoreName(VirtualMachine vm, int size) throws Exception { String dsName = null; Datastore[] datastores = vm.getDatastores(); for(int i=0; i<datastores.length; i++) { DatastoreSummary ds = datastores[i].getSummary(); if(ds.getFreeSpace() > size) { dsName = ds.getName(); break; } } return dsName; }
static VirtualDeviceConfigSpec createAddCdConfigSpec(VirtualMachine vm, String dsName, String isoName) throws Exception { VirtualDeviceConfigSpec cdSpec = new VirtualDeviceConfigSpec(); cdSpec.setOperation(VirtualDeviceConfigSpecOperation.add); VirtualCdrom cdrom = new VirtualCdrom(); VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo(); DatastoreSummary ds = findDatastoreSummary(vm, dsName); cdDeviceBacking.setDatastore(ds.getDatastore()); cdDeviceBacking.setFileName("[" + dsName +"] "+ vm.getName() + "/" + isoName + ".iso"); VirtualDevice vd = getIDEController(vm); cdrom.setBacking(cdDeviceBacking); cdrom.setControllerKey(vd.getKey()); cdrom.setUnitNumber(vd.getUnitNumber()); cdrom.setKey(-1); cdSpec.setDevice(cdrom); return cdSpec; }
static DatastoreSummary findDatastoreSummary(VirtualMachine vm, String dsName) throws Exception { DatastoreSummary dsSum = null; VirtualMachineRuntimeInfo vmRuntimeInfo = vm.getRuntime(); EnvironmentBrowser envBrowser = vm.getEnvironmentBrowser(); ManagedObjectReference hmor = vmRuntimeInfo.getHost(); if(hmor == null) { System.out.println("No Datastore found"); return null; } ConfigTarget configTarget = envBrowser.queryConfigTarget(new HostSystem(vm.getServerConnection(), hmor)); VirtualMachineDatastoreInfo[] dis = configTarget.getDatastore(); for (int i=0; dis!=null && i<dis.length; i++) { dsSum = dis[i].getDatastore(); if (dsSum.isAccessible() && dsName.equals(dsSum.getName())) { break; } } return dsSum; }
public List<Pair<ManagedObjectReference, String>> getLocalDatastoreOnHost() throws Exception { List<Pair<ManagedObjectReference, String>> dsList = new ArrayList<Pair<ManagedObjectReference, String>>(); ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name", "summary"}); if (ocs != null) { for (ObjectContent oc : ocs) { DatastoreSummary dsSummary = (DatastoreSummary)VmwareHelper.getPropValue(oc, "summary"); if (dsSummary.isMultipleHostAccess() == false && dsSummary.isAccessible() && dsSummary.getType().equalsIgnoreCase("vmfs")) { ManagedObjectReference morDs = oc.getObj(); String name = (String)VmwareHelper.getPropValue(oc, "name"); if (!name.startsWith("-iqn.") && !name.startsWith("_iqn.")) { dsList.add(new Pair<ManagedObjectReference, String>(morDs, name)); } } } } return dsList; }
protected String selectDatastore(VmwareProcessClient vmwareProcessClient, VmwareInstance vmwareInstance) { // データストアフォルダ内のデータストアのうち、アクセス可能で空き容量が最も大きいものを用いる Datastore datastore = null; long freeSpace = 0L; // ComputeResourceごとのフォルダがあれば、その中のデータストアを用いる String datastoreFolderName = vmwareInstance.getComputeResource() + "-storage"; Folder datastoreFolder = vmwareProcessClient.getVmwareClient().search(Folder.class, datastoreFolderName); if (datastoreFolder == null) { // ComputeResourceごとのフォルダがなければ、"storage"フォルダの中のデータストアを用いる datastoreFolder = vmwareProcessClient.getVmwareClient().search(Folder.class, "storage"); } if (datastoreFolder != null) { ManagedEntity[] entities = vmwareProcessClient.getVmwareClient().searchByType(datastoreFolder, Datastore.class); for (ManagedEntity entity : entities) { Datastore datastore2 = Datastore.class.cast(entity); DatastoreSummary summary2 = datastore2.getSummary(); if (summary2.isAccessible() && freeSpace < summary2.getFreeSpace()) { datastore = datastore2; freeSpace = summary2.getFreeSpace(); } } } if (datastore == null) { // 利用可能なデータストアがない場合 throw new AutoException("EPROCESS-000528", vmwareInstance.getComputeResource()); } return datastore.getName(); }
ManagedObjectReference getDataStoreRef(String dataStoreName, List<VirtualMachineDatastoreInfo> dataStoresList) { for (VirtualMachineDatastoreInfo dataStore : dataStoresList) { DatastoreSummary dsSummary = dataStore.getDatastore(); if (dataStoreName.equals(dsSummary.getName())) { if (!dsSummary.isAccessible()) { throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_ACCESSIBLE); } return dsSummary.getDatastore(); } } throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_FOUND); }
private ManagedObjectReference getDataStore(String dataStoreName, ConnectionResources connectionResources, ManagedObjectReference vmMor) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { ArrayOfManagedObjectReference dataStoresArray = (ArrayOfManagedObjectReference) new MorObjectHandler() .getObjectProperties(connectionResources, vmMor, ManagedObjectType.DATA_STORE.getValue()); List<ManagedObjectReference> dataStores = dataStoresArray.getManagedObjectReference(); for (ManagedObjectReference dataStore : dataStores) { DatastoreSummary datastoreSummary = (DatastoreSummary) new MorObjectHandler() .getObjectProperties(connectionResources, dataStore, ManagedObjectType.SUMMARY.getValue()); if (dataStoreName.equalsIgnoreCase(datastoreSummary.getName())) { return dataStore; } } return null; }
public ManagedObjectReference[] getHostLocalDatastore() throws Exception { List<ManagedObjectReference> datastores = _context.getVimClient().getDynamicProperty(_mor, "datastore"); List<ManagedObjectReference> l = new ArrayList<ManagedObjectReference>(); if (datastores != null) { for (ManagedObjectReference mor : datastores) { DatastoreSummary summary = (DatastoreSummary)_context.getVimClient().getDynamicProperty(mor, "summary"); if (summary.getType().equalsIgnoreCase("VMFS") && !summary.isMultipleHostAccess()) l.add(mor); } } return l.toArray(new ManagedObjectReference[1]); }
public DatastoreSummary getSummary() throws Exception { return (DatastoreSummary)_context.getVimClient().getDynamicProperty(_mor, "summary"); }
protected Answer execute(ModifyStoragePoolCommand cmd) { if (s_logger.isInfoEnabled()) { s_logger.info("Executing resource ModifyStoragePoolCommand: " + _gson.toJson(cmd)); } try { VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext()); StorageFilerTO pool = cmd.getPool(); if (pool.getType() != StoragePoolType.NetworkFilesystem && pool.getType() != StoragePoolType.VMFS) { throw new Exception("Unsupported storage pool type " + pool.getType()); } ManagedObjectReference morDatastore = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, pool.getUuid()); if (morDatastore == null) { morDatastore = hyperHost.mountDatastore(pool.getType() == StoragePoolType.VMFS, pool.getHost(), pool.getPort(), pool.getPath(), pool.getUuid().replace("-", "")); } assert (morDatastore != null); DatastoreSummary summary = new DatastoreMO(getServiceContext(), morDatastore).getSummary(); long capacity = summary.getCapacity(); long available = summary.getFreeSpace(); Map<String, TemplateProp> tInfo = new HashMap<>(); ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(cmd, capacity, available, tInfo); if (cmd.getAdd() && pool.getType() == StoragePoolType.VMFS) { answer.setLocalDatastoreName(morDatastore.getValue()); } return answer; } catch (Throwable e) { if (e instanceof RemoteException) { s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context"); invalidateServiceContext(); } String msg = "ModifyStoragePoolCommand failed due to " + VmwareHelper.getExceptionMessage(e); s_logger.error(msg, e); return new Answer(cmd, false, msg); } }
private List<StartupStorageCommand> initializeLocalStorage() { List<StartupStorageCommand> storageCmds = new ArrayList<StartupStorageCommand>(); VmwareContext context = getServiceContext(); try { VmwareHypervisorHost hyperHost = getHyperHost(context); if (hyperHost instanceof HostMO) { HostMO hostMo = (HostMO)hyperHost; List<Pair<ManagedObjectReference, String>> dsList = hostMo.getLocalDatastoreOnHost(); for (Pair<ManagedObjectReference, String> dsPair : dsList) { DatastoreMO dsMo = new DatastoreMO(context, dsPair.first()); String poolUuid = dsMo.getCustomFieldValue(CustomFieldConstants.CLOUD_UUID); if (poolUuid == null || poolUuid.isEmpty()) { poolUuid = UUID.randomUUID().toString(); dsMo.setCustomFieldValue(CustomFieldConstants.CLOUD_UUID, poolUuid); } DatastoreSummary dsSummary = dsMo.getSummary(); String address = hostMo.getHostName(); StoragePoolInfo pInfo = new StoragePoolInfo(poolUuid, address, dsMo.getMor().getValue(), "", StoragePoolType.VMFS, dsSummary.getCapacity(), dsSummary.getFreeSpace()); StartupStorageCommand cmd = new StartupStorageCommand(); cmd.setName(poolUuid); cmd.setPoolInfo(pInfo); cmd.setGuid(poolUuid); // give storage host the same UUID as the local storage pool itself cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL); cmd.setDataCenter(_dcId); cmd.setPod(_pod); cmd.setCluster(_cluster); s_logger.info("Add local storage startup command: " + _gson.toJson(cmd)); storageCmds.add(cmd); } } else { s_logger.info("Cluster host does not support local storage, skip it"); } } catch (Exception e) { String msg = "initializing local storage failed due to : " + VmwareHelper.getExceptionMessage(e); s_logger.error(msg); invalidateServiceContext(); throw new CloudRuntimeException(msg); } return storageCmds; }