From e8abfa4d1f99efb41fe2bae82ba8473020695112 Mon Sep 17 00:00:00 2001 From: Edison Su Date: Wed, 28 Mar 2012 16:42:34 -0700 Subject: [PATCH] bug 14535: fix multiple data disks attached to a vm, then stop/start failed. status 14535: resolved fixed; Reviewed-by: sheng --- .../computing/LibvirtComputingResource.java | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java b/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java index 8a730724284..8a5963beccb 100644 --- a/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java +++ b/agent/src/com/cloud/agent/resource/computing/LibvirtComputingResource.java @@ -39,6 +39,7 @@ import java.net.URLConnection; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Calendar; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -204,6 +205,9 @@ import com.cloud.vm.VirtualMachine; import com.cloud.vm.VirtualMachine.State; import com.cloud.vm.VirtualMachineName; +import edu.emory.mathcs.backport.java.util.Arrays; +import edu.emory.mathcs.backport.java.util.Collections; + /** * LibvirtComputingResource execute requests on the computing/routing host using the libvirt API @@ -997,11 +1001,11 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv if (copyToSecondary) { StorageVol volume = _storageResource.getVolume(conn, primaryPool, volumePath); String volumeDestPath = ssPmountPath + File.separator + "volumes/" + cmd.getVolumeId() + File.separator; - _storageResource.copyVolume(volumePath, volumeDestPath, volumeName + ".qcow2", cmd.getWait()); + _storageResource.copyVolume(volumePath, volumeDestPath, volumeName + ".qcow2", cmd.getWait() * 1000); return new CopyVolumeAnswer(cmd, true, null, null, volumeName); } else { volumePath = ssPmountPath + File.separator + "volumes/" + cmd.getVolumeId() + File.separator + volumePath + ".qcow2"; - _storageResource.copyVolume(volumePath, primaryMountPath, volumeName, cmd.getWait()); + _storageResource.copyVolume(volumePath, primaryMountPath, volumeName, cmd.getWait() * 1000); return new CopyVolumeAnswer(cmd, true, null, null, primaryMountPath + File.separator + volumeName); } @@ -1286,7 +1290,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } } else { /*VM is not running, create a snapshot by ourself*/ - final Script command = new Script(_manageSnapshotPath, cmd.getWait(), s_logger); + final Script command = new Script(_manageSnapshotPath, cmd.getWait() * 1000, s_logger); if (cmd.getCommandSwitch().equalsIgnoreCase(ManageSnapshotCommand.CREATE_SNAPSHOT)) { command.add("-c", VolPath); } else { @@ -1323,7 +1327,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv LibvirtStoragePoolDef spd = _storageResource.getStoragePoolDef(conn, secondaryStoragePool); String ssPmountPath = spd.getTargetPath(); snapshotDestPath = ssPmountPath + File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId; - Script command = new Script(_manageSnapshotPath, cmd.getWait(), s_logger); + Script command = new Script(_manageSnapshotPath, cmd.getWait() * 1000, s_logger); command.add("-b", snapshotPath); command.add("-n", snapshotName); command.add("-p", snapshotDestPath); @@ -1361,7 +1365,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv vm.resume(); } } else { - command = new Script(_manageSnapshotPath, cmd.getWait(), s_logger); + command = new Script(_manageSnapshotPath, cmd.getWait() * 1000, s_logger); command.add("-d", snapshotPath); command.add("-n", snapshotName); result = command.execute(); @@ -1473,7 +1477,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv _storage.mkdirs(templatePath); String tmplPath = templateInstallFolder + File.separator + tmplFileName; - Script command = new Script(_createTmplPath, cmd.getWait(), s_logger); + Script command = new Script(_createTmplPath, cmd.getWait() * 1000, s_logger); command.add("-t", templatePath); command.add("-n", tmplFileName); command.add("-f", snapshotPath); @@ -1551,7 +1555,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv String tmpltPath = spd.getTargetPath() + File.separator + templateInstallFolder; _storage.mkdirs(tmpltPath); - Script command = new Script(_createTmplPath, cmd.getWait(), s_logger); + Script command = new Script(_createTmplPath, cmd.getWait() * 1000, s_logger); command.add("-f", cmd.getVolumePath()); command.add("-t", tmpltPath); command.add("-n", cmd.getUniqueName() + ".qcow2"); @@ -1642,7 +1646,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv primaryPool = _storageResource.getStoragePool(conn, cmd.getPoolUuid()); LibvirtStorageVolumeDef vol = new LibvirtStorageVolumeDef(UUID.randomUUID().toString(), tmplVol.getInfo().capacity, volFormat.QCOW2, null, null); s_logger.debug(vol.toString()); - primaryVol = _storageResource.copyVolume(primaryPool, vol, tmplVol, cmd.getWait()); + primaryVol = _storageResource.copyVolume(primaryPool, vol, tmplVol, cmd.getWait() * 1000); StorageVolInfo priVolInfo = primaryVol.getInfo(); return new PrimaryStorageDownloadAnswer(primaryVol.getKey(), priVolInfo.allocation); @@ -2429,8 +2433,15 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } protected void createVbd(Connect conn, VirtualMachineTO vmSpec, String vmName, LibvirtVMDef vm) throws InternalErrorException, LibvirtException, URISyntaxException{ - List vols = new ArrayList(); - for (VolumeTO volume : vmSpec.getDisks()) { + List disks = Arrays.asList(vmSpec.getDisks()); + Collections.sort(disks, new Comparator() { + @Override + public int compare(VolumeTO arg0, VolumeTO arg1) { + return arg0.getDeviceId() > arg1.getDeviceId() ? 1 : -1; + } + }); + + for (VolumeTO volume : disks) { String volPath = getVolumePath(conn, volume); @@ -2451,16 +2462,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv } else { disk.defFileBasedDisk(volume.getPath(), devId, diskBusType, DiskDef.diskFmtType.QCOW2); } - vols.add(devId, disk); - continue; } vm.getDevices().addDevice(disk); } - - for (DiskDef disk : vols) { - vm.getDevices().addDevice(disk); - } if (vmSpec.getType() != VirtualMachine.Type.User) { if (_sysvmISOPath != null) {