From 3429c55e5580f82710ca49e9cd2e03ded16e0b18 Mon Sep 17 00:00:00 2001 From: Edison Su Date: Thu, 30 Aug 2012 10:28:03 -0700 Subject: [PATCH] Implement PlugNicCommand,UnPlugNicCommand for KVM This is part 1 in enabling VPC for KVM. The various commands needing implementation will be submitted individually unless I'm told to do otherwise, in case I don't complete all of the commands, such that someone else can take over or build on my work. RB: https://reviews.apache.org/r/6859 Send-by: shadowsor@gmail.com --- .../resource/LibvirtComputingResource.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 48dc3d0ad28..7e8a11cc587 100755 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -115,6 +115,8 @@ import com.cloud.agent.api.PingCommand; import com.cloud.agent.api.PingRoutingCommand; import com.cloud.agent.api.PingRoutingWithNwGroupsCommand; import com.cloud.agent.api.PingTestCommand; +import com.cloud.agent.api.PlugNicAnswer; +import com.cloud.agent.api.PlugNicCommand; import com.cloud.agent.api.PrepareForMigrationAnswer; import com.cloud.agent.api.PrepareForMigrationCommand; import com.cloud.agent.api.ReadyAnswer; @@ -131,6 +133,8 @@ import com.cloud.agent.api.StartupRoutingCommand; import com.cloud.agent.api.StartupStorageCommand; import com.cloud.agent.api.StopAnswer; import com.cloud.agent.api.StopCommand; +import com.cloud.agent.api.UnPlugNicAnswer; +import com.cloud.agent.api.UnPlugNicCommand; import com.cloud.agent.api.UpgradeSnapshotCommand; import com.cloud.agent.api.VmStatsEntry; import com.cloud.agent.api.check.CheckSshAnswer; @@ -1020,6 +1024,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements return execute((FenceCommand) cmd); } else if (cmd instanceof StartCommand) { return execute((StartCommand) cmd); + } else if (cmd instanceof PlugNicCommand) { + return execute((PlugNicCommand) cmd); + } else if (cmd instanceof UnPlugNicCommand) { + return execute((UnPlugNicCommand) cmd); } else if (cmd instanceof IpAssocCommand) { return execute((IpAssocCommand) cmd); } else if (cmd instanceof NetworkElementCommand) { @@ -1234,6 +1242,44 @@ public class LibvirtComputingResource extends ServerResourceBase implements vm.attachDevice(_vifDriver.plug(nicTO, "Other PV").toString()); } + private PlugNicAnswer execute(PlugNicCommand cmd) { + Connect conn; + NicTO nic = cmd.getNic(); + String vmName = cmd.getVmName(); + try { + conn = LibvirtConnection.getConnection(); + Domain vm = getDomain(conn, vmName); + vm.attachDevice(_vifDriver.plug(nic, "Other PV").toString()); + return new PlugNicAnswer(cmd, true, "success"); + } catch (Exception e) { + String msg = " Plug Nic failed due to " + e.toString(); + s_logger.warn(msg, e); + return new PlugNicAnswer(cmd, false, msg); + } + } + + private UnPlugNicAnswer execute(UnPlugNicCommand cmd) { + Connect conn; + NicTO nic = cmd.getNic(); + String vmName = cmd.getInstanceName(); + try { + conn = LibvirtConnection.getConnection(); + Domain vm = getDomain(conn, vmName); + List pluggedNics = getInterfaces(conn, vmName); + for (InterfaceDef pluggedNic : pluggedNics) { + if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) { + vm.detachDevice(pluggedNic.toString()); + return new UnPlugNicAnswer(cmd, true, "success"); + } + } + return new UnPlugNicAnswer(cmd, true, "success"); + } catch (Exception e) { + String msg = " Unplug Nic failed due to " + e.toString(); + s_logger.warn(msg, e); + return new UnPlugNicAnswer(cmd, false, msg); + } + } + public Answer execute(IpAssocCommand cmd) { String routerName = cmd .getAccessDetail(NetworkElementCommand.ROUTER_NAME);