Skip to content

Commit 3c80f00

Browse files
bvbharatbvbharatk
authored andcommitted
CLOUDSTACK-9641 In KVM SSVM and CPVM may use the old cmdline data, if we fail to fetch the new cmdline in the first pass.
1 parent bb274a1 commit 3c80f00

5 files changed

Lines changed: 75 additions & 10 deletions

File tree

core/src/com/cloud/agent/api/StopCommand.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class StopCommand extends RebootCommand {
2828
private String publicConsoleProxyIpAddress = null;
2929
private GPUDeviceTO gpuDevice;
3030
boolean checkBeforeCleanup = false;
31+
String controlIp = null;
3132

3233
protected StopCommand() {
3334
}
@@ -82,4 +83,12 @@ public void setGpuDevice(GPUDeviceTO gpuDevice) {
8283
public boolean checkBeforeCleanup() {
8384
return this.checkBeforeCleanup;
8485
}
86+
87+
public String getControlIp(){
88+
return controlIp;
89+
}
90+
91+
public void setControlIp(String controlIp){
92+
this.controlIp =controlIp;
93+
}
8594
}

engine/orchestration/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,9 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil
10721072
if (s_logger.isDebugEnabled()) {
10731073
s_logger.info("The guru did not like the answers so stopping " + vm);
10741074
}
1075-
1076-
final StopCommand cmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
1075+
StopCommand stopCmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
1076+
stopCmd.setControlIp(getControlNicIpForVM(vm));
1077+
final StopCommand cmd = stopCmd;
10771078
final Answer answer = _agentMgr.easySend(destHostId, cmd);
10781079
if (answer != null && answer instanceof StopAnswer) {
10791080
final StopAnswer stopAns = (StopAnswer)answer;
@@ -1278,7 +1279,9 @@ public boolean getExecuteInSequence(final HypervisorType hypervisorType) {
12781279

12791280
protected boolean sendStop(final VirtualMachineGuru guru, final VirtualMachineProfile profile, final boolean force, final boolean checkBeforeCleanup) {
12801281
final VirtualMachine vm = profile.getVirtualMachine();
1281-
final StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), checkBeforeCleanup);
1282+
StopCommand stpCmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), checkBeforeCleanup);
1283+
stpCmd.setControlIp(getControlNicIpForVM(vm));
1284+
final StopCommand stop = stpCmd;
12821285
try {
12831286
final Answer answer = _agentMgr.send(vm.getHostId(), stop);
12841287
if (answer != null && answer instanceof StopAnswer) {
@@ -1541,8 +1544,9 @@ private void advanceStop(final VMInstanceVO vm, final boolean cleanUpEvenIfUnabl
15411544
}
15421545

15431546
vmGuru.prepareStop(profile);
1544-
1545-
final StopCommand stop = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
1547+
StopCommand stpCmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
1548+
stpCmd.setControlIp(getControlNicIpForVM(vm));
1549+
final StopCommand stop = stpCmd;
15461550

15471551
boolean stopped = false;
15481552
Answer answer = null;
@@ -2669,12 +2673,24 @@ private void orchestrateReboot(final String vmUuid, final Map<VirtualMachineProf
26692673
}
26702674

26712675
public Command cleanup(final VirtualMachine vm) {
2672-
return new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
2676+
StopCommand cmd = new StopCommand(vm, getExecuteInSequence(vm.getHypervisorType()), false);
2677+
cmd.setControlIp(getControlNicIpForVM(vm));
2678+
return cmd;
26732679
}
26742680

2681+
private String getControlNicIpForVM(VirtualMachine vm) {
2682+
if (vm.getType() == VirtualMachine.Type.ConsoleProxy || vm.getType() == VirtualMachine.Type.SecondaryStorageVm) {
2683+
NicVO nic = _nicsDao.getControlNicForVM(vm.getId());
2684+
return nic.getIPv4Address();
2685+
} else if (vm.getType() == VirtualMachine.Type.DomainRouter) return vm.getPrivateIpAddress();
2686+
else return null;
2687+
}
26752688
public Command cleanup(final String vmName) {
2676-
return new StopCommand(vmName, getExecuteInSequence(null), false);
2689+
VirtualMachine vm = _vmDao.findVMByInstanceName(vmName);
26772690

2691+
StopCommand cmd = new StopCommand(vmName, getExecuteInSequence(null), false);
2692+
cmd.setControlIp(getControlNicIpForVM(vm));
2693+
return cmd;
26782694
}
26792695

26802696

engine/schema/src/com/cloud/vm/dao/NicDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
7474
List<NicVO> listByNetworkIdTypeAndGatewayAndBroadcastUri(long networkId, VirtualMachine.Type vmType, String gateway, URI broadcastUri);
7575

7676
int countNicsForStartingVms(long networkId);
77+
78+
NicVO getControlNicForVM(long vmId);
7779
}

engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected void init() {
6666
AllFieldsSearch.and("secondaryip", AllFieldsSearch.entity().getSecondaryIp(), Op.EQ);
6767
AllFieldsSearch.and("nicid", AllFieldsSearch.entity().getId(), Op.EQ);
6868
AllFieldsSearch.and("strategy", AllFieldsSearch.entity().getReservationStrategy(), Op.EQ);
69+
AllFieldsSearch.and("reserverName",AllFieldsSearch.entity().getReserver(),Op.EQ);
6970
AllFieldsSearch.done();
7071

7172
IpSearch = createSearchBuilder(String.class);
@@ -198,6 +199,14 @@ public NicVO findDefaultNicForVM(long instanceId) {
198199
return findOneBy(sc);
199200
}
200201

202+
@Override
203+
public NicVO getControlNicForVM(long vmId){
204+
SearchCriteria<NicVO> sc = AllFieldsSearch.create();
205+
sc.setParameters("instance", vmId);
206+
sc.setParameters("reserverName", "ControlNetworkGuru");
207+
return findOneBy(sc);
208+
}
209+
201210
@Override
202211
public NicVO findNonReleasedByInstanceIdAndNetworkId(long networkId, long instanceId) {
203212
SearchCriteria<NicVO> sc = NonReleasedSearch.create();

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStopCommandWrapper.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
package com.cloud.hypervisor.kvm.resource.wrapper;
2121

2222
import java.util.List;
23+
import java.io.File;
2324

25+
import com.cloud.utils.Pair;
26+
import com.cloud.utils.ssh.SshHelper;
2427
import org.apache.log4j.Logger;
2528
import org.libvirt.Connect;
2629
import org.libvirt.Domain;
2730
import org.libvirt.DomainInfo.DomainState;
28-
import org.libvirt.LibvirtException;
2931

3032
import com.cloud.agent.api.Answer;
3133
import com.cloud.agent.api.StopAnswer;
@@ -36,11 +38,14 @@
3638
import com.cloud.hypervisor.kvm.resource.VifDriver;
3739
import com.cloud.resource.CommandWrapper;
3840
import com.cloud.resource.ResourceWrapper;
41+
import org.libvirt.LibvirtException;
3942

4043
@ResourceWrapper(handles = StopCommand.class)
4144
public final class LibvirtStopCommandWrapper extends CommandWrapper<StopCommand, Answer, LibvirtComputingResource> {
4245

4346
private static final Logger s_logger = Logger.getLogger(LibvirtStopCommandWrapper.class);
47+
private static final String CMDLINE_PATH = "/var/cache/cloud/cmdline";
48+
private static final String CMDLINE_BACKUP_PATH = "/var/cache/cloud/cmdline.backup";
4449

4550
@Override
4651
public Answer execute(final StopCommand command, final LibvirtComputingResource libvirtComputingResource) {
@@ -59,8 +64,21 @@ public Answer execute(final StopCommand command, final LibvirtComputingResource
5964
s_logger.debug("Failed to get vm status in case of checkboforecleanup is true", e);
6065
}
6166
}
62-
67+
File pemFile = new File(LibvirtComputingResource.SSHPRVKEYPATH);
6368
try {
69+
if(vmName.startsWith("s-") || vmName.startsWith("v-")){
70+
//move the command line file to backup.
71+
s_logger.debug("backing up the cmdline");
72+
try{
73+
Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null,"mv -f "+CMDLINE_PATH+" "+CMDLINE_BACKUP_PATH);
74+
if(!ret.first()){
75+
s_logger.debug("Failed to backup cmdline file due to "+ret.second());
76+
}
77+
} catch (Exception e){
78+
s_logger.debug("Failed to backup cmdline file due to "+e.getMessage());
79+
}
80+
}
81+
6482
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
6583

6684
final List<DiskDef> disks = libvirtComputingResource.getDisks(conn, vmName);
@@ -83,7 +101,18 @@ public Answer execute(final StopCommand command, final LibvirtComputingResource
83101

84102
return new StopAnswer(command, result, true);
85103
} catch (final LibvirtException e) {
104+
s_logger.debug("unable to stop VM:"+vmName+" due to"+e.getMessage());
105+
try{
106+
if(vmName.startsWith("s-") || vmName.startsWith("v-"))
107+
s_logger.debug("restoring cmdline file from backup");
108+
Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "mv "+CMDLINE_BACKUP_PATH+" "+CMDLINE_PATH);
109+
if(!ret.first()){
110+
s_logger.debug("unable to restore cmdline due to "+ret.second());
111+
}
112+
}catch (final Exception ex){
113+
s_logger.debug("unable to restore cmdline due to:"+ex.getMessage());
114+
}
86115
return new StopAnswer(command, e.getMessage(), false);
87116
}
88117
}
89-
}
118+
}

0 commit comments

Comments
 (0)