Skip to content

Commit ae23144

Browse files
committed
CLOUDSTACK-4585: make run-time datastore folder migration, VM snapshot, bug in root disk controller type carried from previous version work under upgrade situation
1 parent e81e75c commit ae23144

5 files changed

Lines changed: 136 additions & 86 deletions

File tree

‎engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private void updateSystemVmTemplates(Connection conn) {
727727
pstmt.close();
728728
} else {
729729
if (hypervisorsListInUse.contains(hypervisorAndTemplateName.getKey())){
730-
throw new CloudRuntimeException("4.2.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms");
730+
// throw new CloudRuntimeException("4.2.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. Cannot upgrade system Vms");
731731
} else {
732732
s_logger.warn("4.2.0 " + hypervisorAndTemplateName.getKey() + " SystemVm template not found. " + hypervisorAndTemplateName.getKey() + " hypervisor is not used, so not failing upgrade");
733733
// Update the latest template URLs for corresponding hypervisor

‎plugins/hypervisors/vmware/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 87 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,7 +2807,8 @@ protected StartAnswer execute(StartCommand cmd) {
28072807
if (vol.getType() == Volume.Type.ISO)
28082808
continue;
28092809

2810-
controllerKey = getDiskController(vol, vmSpec, ideControllerKey, scsiControllerKey);
2810+
VirtualMachineDiskInfo matchingExistingDisk = getMatchingExistingDisk(diskInfoBuilder, vol);
2811+
controllerKey = getDiskController(matchingExistingDisk, vol, vmSpec, ideControllerKey, scsiControllerKey);
28112812

28122813
VolumeObjectTO volumeTO = (VolumeObjectTO)vol.getData();
28132814
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volumeTO.getDataStore();
@@ -2816,19 +2817,16 @@ protected StartAnswer execute(StartCommand cmd) {
28162817
VirtualDevice device;
28172818

28182819
String[] diskChain = syncDiskChain(dcMo, vmMo, vmSpec,
2819-
vol, diskInfoBuilder,
2820-
dataStoresDetails,
2821-
(controllerKey == ideControllerKey) ? true : false,
2822-
0, // currently only support bus 0
2823-
(controllerKey == ideControllerKey) ? ideUnitNumber : scsiUnitNumber);
2820+
vol, matchingExistingDisk,
2821+
dataStoresDetails);
28242822

2825-
device = VmwareHelper.prepareDiskDevice(vmMo, controllerKey,
2823+
device = VmwareHelper.prepareDiskDevice(vmMo, null, controllerKey,
28262824
diskChain,
28272825
volumeDsDetails.first(),
28282826
(controllerKey == ideControllerKey) ? ideUnitNumber++ : scsiUnitNumber++, i + 1);
28292827

28302828
deviceConfigSpecArray[i].setDevice(device);
2831-
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.ADD);
2829+
deviceConfigSpecArray[i].setOperation(VirtualDeviceConfigSpecOperation.ADD);
28322830

28332831
if(s_logger.isDebugEnabled())
28342832
s_logger.debug("Prepare volume at new device " + _gson.toJson(device));
@@ -2951,55 +2949,37 @@ protected StartAnswer execute(StartCommand cmd) {
29512949

29522950
// return the finalized disk chain for startup, from top to bottom
29532951
private String[] syncDiskChain(DatacenterMO dcMo, VirtualMachineMO vmMo, VirtualMachineTO vmSpec,
2954-
DiskTO vol, VirtualMachineDiskInfoBuilder diskInfoBuilder,
2955-
HashMap<String ,Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails,
2956-
boolean ideController, int deviceBusNumber, int deviceUnitNumber) throws Exception {
2952+
DiskTO vol, VirtualMachineDiskInfo diskInfo,
2953+
HashMap<String ,Pair<ManagedObjectReference, DatastoreMO>> dataStoresDetails
2954+
) throws Exception {
29572955

29582956
VolumeObjectTO volumeTO = (VolumeObjectTO)vol.getData();
29592957
PrimaryDataStoreTO primaryStore = (PrimaryDataStoreTO)volumeTO.getDataStore();
29602958

2961-
String deviceBusName;
2962-
if(ideController)
2963-
deviceBusName = String.format("ide%d:%d", deviceBusNumber, deviceUnitNumber);
2964-
else
2965-
deviceBusName = String.format("scsi%d:%d", deviceBusNumber, deviceUnitNumber);
2966-
29672959
Pair<ManagedObjectReference, DatastoreMO> volumeDsDetails = dataStoresDetails.get(primaryStore.getUuid());
29682960
if(volumeDsDetails == null)
29692961
throw new Exception("Primary datastore " + primaryStore.getUuid() + " is not mounted on host.");
29702962
DatastoreMO dsMo = volumeDsDetails.second();
29712963

29722964
// we will honor vCenter's meta if it exists
2973-
if(diskInfoBuilder != null && diskInfoBuilder.getDiskCount() > 0) {
2974-
// we will always on-disk info from vCenter in this case
2975-
VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByDeviceBusName(deviceBusName);
2976-
if(diskInfo != null) {
2977-
if(s_logger.isInfoEnabled())
2978-
s_logger.info("Volume " + volumeTO.getId() + " does not seem to exist on datastore. use on-disk chain: " +
2979-
_gson.toJson(diskInfo));
2980-
2981-
return diskInfo.getDiskChain();
2982-
} else {
2983-
s_logger.warn("Volume " + volumeTO.getId() + " does not seem to exist on datastore. on-disk may be out of sync as well. disk device info: " + deviceBusName);
2984-
}
2985-
}
2965+
if(diskInfo != null) {
2966+
// to deal with run-time upgrade to maintain the new datastore folder structure
2967+
String disks[] = diskInfo.getDiskChain();
2968+
for(int i = 0; i < disks.length; i++) {
2969+
DatastoreFile file = new DatastoreFile(disks[i]);
2970+
if(file.getDir() != null && file.getDir().isEmpty()) {
2971+
s_logger.info("Perform run-time datastore folder upgrade. sync " + disks[i] + " to VM folder");
2972+
disks[i] = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(
2973+
dcMo, vmMo.getName(), dsMo, file.getFileBaseName());
2974+
}
2975+
}
2976+
return disks;
2977+
}
29862978

29872979
String datastoreDiskPath = VmwareStorageLayoutHelper.syncVolumeToVmDefaultFolder(
29882980
dcMo, vmMo.getName(), dsMo, volumeTO.getPath());
29892981
if(!dsMo.fileExists(datastoreDiskPath)) {
2990-
if(s_logger.isInfoEnabled())
2991-
s_logger.info("Volume " + volumeTO.getId() + " does not seem to exist on datastore, out of sync? path: " + datastoreDiskPath);
2992-
2993-
// last resort, try chain info stored in DB
2994-
if(volumeTO.getChainInfo() != null) {
2995-
VirtualMachineDiskInfo diskInfo = _gson.fromJson(volumeTO.getChainInfo(), VirtualMachineDiskInfo.class);
2996-
if(diskInfo != null) {
2997-
s_logger.info("Use chain info from DB: " + volumeTO.getChainInfo());
2998-
return diskInfo.getDiskChain();
2999-
}
3000-
3001-
throw new Exception("Volume " + volumeTO.getId() + " does not seem to exist on datastore. Broken disk chain");
3002-
}
2982+
s_logger.warn("Volume " + volumeTO.getId() + " does not seem to exist on datastore, out of sync? path: " + datastoreDiskPath);
30032983
}
30042984

30052985
return new String[] { datastoreDiskPath };
@@ -3209,21 +3189,80 @@ private static void postNvpConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachi
32093189
}
32103190
}
32113191

3212-
private static int getDiskController(DiskTO vol, VirtualMachineTO vmSpec, int ideControllerKey, int scsiControllerKey) {
3192+
private VirtualMachineDiskInfo getMatchingExistingDisk(VirtualMachineDiskInfoBuilder diskInfoBuilder, DiskTO vol) {
3193+
if(diskInfoBuilder != null) {
3194+
VolumeObjectTO volume = (VolumeObjectTO)vol.getData();
3195+
3196+
VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(volume.getPath());
3197+
if(diskInfo != null) {
3198+
s_logger.info("Found existing disk info from volume path: " + volume.getPath());
3199+
return diskInfo;
3200+
} else {
3201+
String chainInfo = volume.getChainInfo();
3202+
if(chainInfo != null) {
3203+
VirtualMachineDiskInfo infoInChain = _gson.fromJson(chainInfo, VirtualMachineDiskInfo.class);
3204+
if(infoInChain != null) {
3205+
String[] disks = infoInChain.getDiskChain();
3206+
if(disks.length > 0) {
3207+
for(String diskPath : disks) {
3208+
DatastoreFile file = new DatastoreFile(diskPath);
3209+
diskInfo = diskInfoBuilder.getDiskInfoByBackingFileBaseName(file.getFileBaseName());
3210+
if(diskInfo != null) {
3211+
s_logger.info("Found existing disk from chain info: " + diskPath);
3212+
return diskInfo;
3213+
}
3214+
}
3215+
}
3216+
3217+
if(diskInfo == null) {
3218+
diskInfo = diskInfoBuilder.getDiskInfoByDeviceBusName(infoInChain.getDiskDeviceBusName());
3219+
if(diskInfo != null) {
3220+
s_logger.info("Found existing disk from from chain device bus information: " + infoInChain.getDiskDeviceBusName());
3221+
return diskInfo;
3222+
}
3223+
}
3224+
}
3225+
}
3226+
}
3227+
}
3228+
3229+
return null;
3230+
}
3231+
3232+
private int getDiskController(VirtualMachineDiskInfo matchingExistingDisk, DiskTO vol,
3233+
VirtualMachineTO vmSpec, int ideControllerKey, int scsiControllerKey) {
3234+
32133235
int controllerKey;
3236+
if(matchingExistingDisk != null) {
3237+
s_logger.info("Chose disk controller based on existing information: " + matchingExistingDisk.getDiskDeviceBusName());
3238+
if(matchingExistingDisk.getDiskDeviceBusName().startsWith("ide"))
3239+
return ideControllerKey;
3240+
else
3241+
return scsiControllerKey;
3242+
}
32143243

32153244
if(vol.getType() == Volume.Type.ROOT) {
3216-
if(vmSpec.getDetails() != null && vmSpec.getDetails().get(VmDetailConstants.ROOK_DISK_CONTROLLER) != null)
3245+
Map<String, String> vmDetails = vmSpec.getDetails();
3246+
if(vmDetails != null && vmDetails.get(VmDetailConstants.ROOK_DISK_CONTROLLER) != null)
32173247
{
3218-
if(vmSpec.getDetails().get(VmDetailConstants.ROOK_DISK_CONTROLLER).equalsIgnoreCase("scsi"))
3248+
if(vmDetails.get(VmDetailConstants.ROOK_DISK_CONTROLLER).equalsIgnoreCase("scsi")) {
3249+
s_logger.info("Chose disk controller for vol " + vol.getType() + " -> scsi, based on root disk controller settings: "
3250+
+ vmDetails.get(VmDetailConstants.ROOK_DISK_CONTROLLER));
32193251
controllerKey = scsiControllerKey;
3220-
else
3252+
}
3253+
else {
3254+
s_logger.info("Chose disk controller for vol " + vol.getType() + " -> ide, based on root disk controller settings: "
3255+
+ vmDetails.get(VmDetailConstants.ROOK_DISK_CONTROLLER));
32213256
controllerKey = ideControllerKey;
3257+
}
32223258
} else {
3259+
s_logger.info("Chose disk controller for vol " + vol.getType() + " -> scsi. due to null root disk controller setting");
32233260
controllerKey = scsiControllerKey;
32243261
}
3262+
32253263
} else {
32263264
// DATA volume always use SCSI device
3265+
s_logger.info("Chose disk controller for vol " + vol.getType() + " -> scsi");
32273266
controllerKey = scsiControllerKey;
32283267
}
32293268

@@ -3234,25 +3273,14 @@ private void postDiskConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO v
32343273
int ideControllerKey, int scsiControllerKey) throws Exception {
32353274

32363275
VirtualMachineDiskInfoBuilder diskInfoBuilder = vmMo.getDiskInfoBuilder();
3237-
int controllerKey;
3238-
int ideUnitNumber = 1; // we always count in IDE device first
3239-
int scsiUnitNumber = 0;
32403276

32413277
for(DiskTO vol: sortedDisks) {
32423278
if (vol.getType() == Volume.Type.ISO)
32433279
continue;
32443280

32453281
VolumeObjectTO volumeTO = (VolumeObjectTO)vol.getData();
32463282

3247-
controllerKey = getDiskController(vol, vmSpec, ideControllerKey, scsiControllerKey);
3248-
3249-
String deviceBusName;
3250-
if(controllerKey == ideControllerKey)
3251-
deviceBusName = String.format("ide%d:%d", 0, ideUnitNumber++);
3252-
else
3253-
deviceBusName = String.format("scsi%d:%d", 0, scsiUnitNumber++);
3254-
3255-
VirtualMachineDiskInfo diskInfo = diskInfoBuilder.getDiskInfoByDeviceBusName(deviceBusName);
3283+
VirtualMachineDiskInfo diskInfo = getMatchingExistingDisk(diskInfoBuilder, vol);
32563284
assert(diskInfo != null);
32573285

32583286
String[] diskChain = diskInfo.getDiskChain();

‎plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageLayoutHelper.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ public static String syncVolumeToVmDefaultFolder(DatacenterMO dcMo, String vmNam
109109
String[] vmdkFullCloneModePair = getVmdkFilePairDatastorePath(ds, vmName, vmdkName,
110110
VmwareStorageLayoutType.VMWARE, false);
111111

112-
if(!ds.fileExists(vmdkLinkedCloneModeLegacyPair[0])) {
112+
if(!ds.fileExists(vmdkLinkedCloneModeLegacyPair[0]) && !ds.fileExists(vmdkLinkedCloneModePair[0])) {
113113
// To protect against inconsistency caused by non-atomic datastore file management, detached disk may
114114
// be left over in its previous owner VM. We will do a fixup synchronization here by moving it to root
115-
// again
115+
// again.
116116
//
117117
syncVolumeToRootFolder(dcMo, ds, vmdkName);
118118
}

‎vmware-base/src/com/cloud/hypervisor/vmware/mo/VirtualMachineMO.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ public void attachDisk(String[] vmdkDatastorePathChain, ManagedObjectReference m
988988
s_logger.trace("vCenter API trace - attachDisk(). target MOR: " + _mor.getValue() + ", vmdkDatastorePath: "
989989
+ new Gson().toJson(vmdkDatastorePathChain) + ", datastore: " + morDs.getValue());
990990

991-
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, getScsiDeviceControllerKey(),
991+
VirtualDevice newDisk = VmwareHelper.prepareDiskDevice(this, null, getScsiDeviceControllerKey(),
992992
vmdkDatastorePathChain, morDs, -1, 1);
993993
VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
994994
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
@@ -1573,7 +1573,7 @@ public void cloneFromDiskChain(String clonedVmName, int cpuSpeedMHz, int memoryM
15731573
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
15741574
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
15751575

1576-
VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, -1, disks, morDs, -1, 1);
1576+
VirtualDevice device = VmwareHelper.prepareDiskDevice(clonedVmMo, null, -1, disks, morDs, -1, 1);
15771577

15781578
deviceConfigSpec.setDevice(device);
15791579
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
@@ -1841,6 +1841,22 @@ public String getDiskCurrentTopBackingFileInChain(String deviceBusName) throws E
18411841
return null;
18421842
}
18431843

1844+
public VirtualDisk getDiskDeviceByDeviceBusName(String deviceBusName) throws Exception {
1845+
List<VirtualDevice> devices = (List<VirtualDevice>)_context.getVimClient().getDynamicProperty(_mor, "config.hardware.device");
1846+
1847+
if(devices != null && devices.size() > 0) {
1848+
for(VirtualDevice device : devices) {
1849+
if(device instanceof VirtualDisk) {
1850+
String deviceNumbering = getDeviceBusName(devices, device);
1851+
if(deviceNumbering.equals(deviceBusName))
1852+
return (VirtualDisk)device;
1853+
}
1854+
}
1855+
}
1856+
1857+
return null;
1858+
}
1859+
18441860
public VirtualMachineDiskInfoBuilder getDiskInfoBuilder() throws Exception {
18451861
VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder();
18461862

‎vmware-base/src/com/cloud/hypervisor/vmware/util/VmwareHelper.java‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,40 @@ public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int control
264264
}
265265

266266
// vmdkDatastorePath: [datastore name] vmdkFilePath
267-
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int controllerKey, String vmdkDatastorePathChain[],
267+
public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, VirtualDisk device, int controllerKey, String vmdkDatastorePathChain[],
268268
ManagedObjectReference morDs, int deviceNumber, int contextNumber) throws Exception {
269269

270270
assert(vmdkDatastorePathChain != null);
271271
assert(vmdkDatastorePathChain.length >= 1);
272272

273-
VirtualDisk disk = new VirtualDisk();
274-
275-
VirtualDiskFlatVer2BackingInfo backingInfo = new VirtualDiskFlatVer2BackingInfo();
276-
backingInfo.setDatastore(morDs);
273+
VirtualDisk disk;
274+
VirtualDiskFlatVer2BackingInfo backingInfo;
275+
if(device != null) {
276+
disk = device;
277+
backingInfo = (VirtualDiskFlatVer2BackingInfo)disk.getBacking();
278+
} else {
279+
disk = new VirtualDisk();
280+
backingInfo = new VirtualDiskFlatVer2BackingInfo();
281+
backingInfo.setDatastore(morDs);
282+
backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value());
283+
disk.setBacking(backingInfo);
284+
285+
if(controllerKey < 0)
286+
controllerKey = vmMo.getIDEDeviceControllerKey();
287+
if(deviceNumber < 0)
288+
deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
289+
290+
disk.setControllerKey(controllerKey);
291+
disk.setKey(-contextNumber);
292+
disk.setUnitNumber(deviceNumber);
293+
294+
VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
295+
connectInfo.setConnected(true);
296+
connectInfo.setStartConnected(true);
297+
disk.setConnectable(connectInfo);
298+
}
299+
277300
backingInfo.setFileName(vmdkDatastorePathChain[0]);
278-
backingInfo.setDiskMode(VirtualDiskMode.PERSISTENT.value());
279301
if(vmdkDatastorePathChain.length > 1) {
280302
String[] parentDisks = new String[vmdkDatastorePathChain.length - 1];
281303
for(int i = 0; i < vmdkDatastorePathChain.length - 1; i++)
@@ -284,22 +306,6 @@ public static VirtualDevice prepareDiskDevice(VirtualMachineMO vmMo, int control
284306
setParentBackingInfo(backingInfo, morDs, parentDisks);
285307
}
286308

287-
disk.setBacking(backingInfo);
288-
289-
if(controllerKey < 0)
290-
controllerKey = vmMo.getIDEDeviceControllerKey();
291-
if(deviceNumber < 0)
292-
deviceNumber = vmMo.getNextDeviceNumber(controllerKey);
293-
294-
disk.setControllerKey(controllerKey);
295-
disk.setKey(-contextNumber);
296-
disk.setUnitNumber(deviceNumber);
297-
298-
VirtualDeviceConnectInfo connectInfo = new VirtualDeviceConnectInfo();
299-
connectInfo.setConnected(true);
300-
connectInfo.setStartConnected(true);
301-
disk.setConnectable(connectInfo);
302-
303309
return disk;
304310
}
305311

0 commit comments

Comments
 (0)