use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtStopCommandWrapper method execute.
@Override
public Answer execute(final StopCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String vmName = command.getVmName();
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
if (command.checkBeforeCleanup()) {
try {
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
final Domain vm = conn.domainLookupByName(command.getVmName());
if (vm != null && vm.getInfo().state == DomainState.VIR_DOMAIN_RUNNING) {
return new StopAnswer(command, "vm is still running on host", false);
}
} catch (final Exception e) {
s_logger.debug("Failed to get vm status in case of checkboforecleanup is true", e);
}
}
File pemFile = new File(LibvirtComputingResource.SSHPRVKEYPATH);
try {
if (vmName.startsWith("s-") || vmName.startsWith("v-")) {
//move the command line file to backup.
s_logger.debug("backing up the cmdline");
try {
Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "mv -f " + CMDLINE_PATH + " " + CMDLINE_BACKUP_PATH);
if (!ret.first()) {
s_logger.debug("Failed to backup cmdline file due to " + ret.second());
}
} catch (Exception e) {
s_logger.debug("Failed to backup cmdline file due to " + e.getMessage());
}
}
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
final List<DiskDef> disks = libvirtComputingResource.getDisks(conn, vmName);
final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, vmName);
libvirtComputingResource.destroyNetworkRulesForVM(conn, vmName);
final String result = libvirtComputingResource.stopVM(conn, vmName);
if (result == null) {
for (final DiskDef disk : disks) {
libvirtComputingResource.cleanupDisk(disk);
}
for (final InterfaceDef iface : ifaces) {
// each interface at this point, so inform all vif drivers
for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
vifDriver.unplug(iface);
}
}
}
return new StopAnswer(command, result, true);
} catch (final LibvirtException e) {
s_logger.debug("unable to stop VM:" + vmName + " due to" + e.getMessage());
try {
if (vmName.startsWith("s-") || vmName.startsWith("v-"))
s_logger.debug("restoring cmdline file from backup");
Pair<Boolean, String> ret = SshHelper.sshExecute(command.getControlIp(), 3922, "root", pemFile, null, "mv " + CMDLINE_BACKUP_PATH + " " + CMDLINE_PATH);
if (!ret.first()) {
s_logger.debug("unable to restore cmdline due to " + ret.second());
}
} catch (final Exception ex) {
s_logger.debug("unable to restore cmdline due to:" + ex.getMessage());
}
return new StopAnswer(command, e.getMessage(), false);
}
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtUnPlugNicCommandWrapper method execute.
@Override
public Answer execute(final UnPlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
final NicTO nic = command.getNic();
final String vmName = command.getVmName();
Domain vm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
vm = libvirtComputingResource.getDomain(conn, vmName);
final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
for (final InterfaceDef pluggedNic : pluggedNics) {
if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
vm.detachDevice(pluggedNic.toString());
// each interface at this point, so inform all vif drivers
for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
vifDriver.unplug(pluggedNic);
}
return new UnPlugNicAnswer(command, true, "success");
}
}
return new UnPlugNicAnswer(command, true, "success");
} catch (final LibvirtException e) {
final String msg = " Unplug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new UnPlugNicAnswer(command, false, msg);
} finally {
if (vm != null) {
try {
vm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtPlugNicCommandWrapper method execute.
@Override
public Answer execute(final PlugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
final NicTO nic = command.getNic();
final String vmName = command.getVmName();
Domain vm = null;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
vm = libvirtComputingResource.getDomain(conn, vmName);
final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
Integer nicnum = 0;
for (final InterfaceDef pluggedNic : pluggedNics) {
if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
s_logger.debug("found existing nic for mac " + pluggedNic.getMacAddress() + " at index " + nicnum);
return new PlugNicAnswer(command, true, "success");
}
nicnum++;
}
final VifDriver vifDriver = libvirtComputingResource.getVifDriver(nic.getType());
final InterfaceDef interfaceDef = vifDriver.plug(nic, "Other PV", "");
vm.attachDevice(interfaceDef.toString());
return new PlugNicAnswer(command, true, "success");
} catch (final LibvirtException e) {
final String msg = " Plug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new PlugNicAnswer(command, false, msg);
} catch (final InternalErrorException e) {
final String msg = " Plug Nic failed due to " + e.toString();
s_logger.warn(msg, e);
return new PlugNicAnswer(command, false, msg);
} finally {
if (vm != null) {
try {
vm.free();
} catch (final LibvirtException l) {
s_logger.trace("Ignoring libvirt error.", l);
}
}
}
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtPvlanSetupCommandWrapper method execute.
@Override
public Answer execute(final PvlanSetupCommand command, final LibvirtComputingResource libvirtComputingResource) {
final String primaryPvlan = command.getPrimary();
final String isolatedPvlan = command.getIsolated();
final String op = command.getOp();
final String dhcpName = command.getDhcpName();
final String dhcpMac = command.getDhcpMac();
final String vmMac = command.getVmMac();
final String dhcpIp = command.getDhcpIp();
boolean add = true;
String opr = "-A";
if (op.equals("delete")) {
opr = "-D";
add = false;
}
String result = null;
try {
final String guestBridgeName = libvirtComputingResource.getGuestBridgeName();
final Duration timeout = libvirtComputingResource.getTimeout();
if (command.getType() == PvlanSetupCommand.Type.DHCP) {
final String ovsPvlanDhcpHostPath = libvirtComputingResource.getOvsPvlanDhcpHostPath();
final Script script = new Script(ovsPvlanDhcpHostPath, timeout, s_logger);
if (add) {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(dhcpName);
final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, dhcpName);
final InterfaceDef guestNic = ifaces.get(0);
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac, "-I", guestNic.getDevName());
} else {
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac);
}
result = script.execute();
if (result != null) {
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
return new Answer(command, false, result);
} else {
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
}
} else if (command.getType() == PvlanSetupCommand.Type.VM) {
final String ovsPvlanVmPath = libvirtComputingResource.getOvsPvlanVmPath();
final Script script = new Script(ovsPvlanVmPath, timeout, s_logger);
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-v", vmMac);
result = script.execute();
if (result != null) {
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
return new Answer(command, false, result);
} else {
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
}
}
} catch (final LibvirtException e) {
s_logger.error("Error whislt executing OVS Setup command! ==> " + e.getMessage());
return new Answer(command, false, e.getMessage());
}
return new Answer(command, true, result);
}
use of org.libvirt.Connect in project cloudstack by apache.
the class LibvirtNetworkRulesVmSecondaryIpCommandWrapper method execute.
@Override
public Answer execute(final NetworkRulesVmSecondaryIpCommand command, final LibvirtComputingResource libvirtComputingResource) {
boolean result = false;
try {
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
result = libvirtComputingResource.configureNetworkRulesVMSecondaryIP(conn, command.getVmName(), command.getVmSecIp(), command.getAction());
} catch (final LibvirtException e) {
s_logger.debug("Could not configure VM secondary IP! => " + e.getLocalizedMessage());
}
return new Answer(command, result, "");
}
Aggregations