Search in sources :

Example 51 with Connect

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);
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) LibvirtException(org.libvirt.LibvirtException) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) DiskDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef) Domain(org.libvirt.Domain) StopAnswer(com.cloud.agent.api.StopAnswer) File(java.io.File) Pair(com.cloud.utils.Pair)

Example 52 with Connect

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);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Example 53 with Connect

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);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Example 54 with Connect

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);
}
Also used : Script(com.cloud.utils.script.Script) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) Answer(com.cloud.agent.api.Answer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) Duration(org.joda.time.Duration)

Example 55 with Connect

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, "");
}
Also used : Answer(com.cloud.agent.api.Answer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect)

Aggregations

Connect (org.libvirt.Connect)113 LibvirtException (org.libvirt.LibvirtException)112 Answer (com.cloud.agent.api.Answer)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)42 Test (org.junit.Test)40 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)39 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)39 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)39 InternalErrorException (com.cloud.exception.InternalErrorException)33 Domain (org.libvirt.Domain)30 URISyntaxException (java.net.URISyntaxException)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)24 NicTO (com.cloud.agent.api.to.NicTO)23 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)22 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)16 ConfigurationException (javax.naming.ConfigurationException)15 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)11