Search in sources :

Example 1 with LibvirtVMDef

use of com.cloud.agent.resource.computing.LibvirtVMDef in project CloudStack-archive by CloudStack-extras.

the class CloudZonesComputingResource method execute.

@Override
protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                } else {
                    nic.setIp(null);
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        // attached disk
        for (DiskDef disk : vm.getDevices().getDisks()) {
            if (disk.isAttachDeferred()) {
                attachOrDetachDevice(conn, true, vmName, disk.toString());
            }
        }
        if (vmSpec.getType() == VirtualMachine.Type.User) {
            for (NicTO nic : nics) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress ipAddr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    if (ipAddr == null) {
                        s_logger.debug("Failed to get guest DHCP ip, stop it");
                        StopCommand stpCmd = new StopCommand(vmName);
                        execute(stpCmd);
                        return new StartAnswer(cmd, "Failed to get guest DHCP ip, stop it");
                    }
                    s_logger.debug(ipAddr);
                    nic.setIp(ipAddr.getHostAddress());
                    post_default_network_rules(conn, vmName, nic, vmSpec.getId(), _dhcpSnooper.getDhcpServerIP(), _hostIp, _hostMacAddress);
                    _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : LibvirtVMDef(com.cloud.agent.resource.computing.LibvirtVMDef) DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) StopCommand(com.cloud.agent.api.StopCommand) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

StartAnswer (com.cloud.agent.api.StartAnswer)1 StopCommand (com.cloud.agent.api.StopCommand)1 NicTO (com.cloud.agent.api.to.NicTO)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 LibvirtVMDef (com.cloud.agent.resource.computing.LibvirtVMDef)1 DiskDef (com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef)1 State (com.cloud.vm.VirtualMachine.State)1 InetAddress (java.net.InetAddress)1 ConfigurationException (javax.naming.ConfigurationException)1 Connect (org.libvirt.Connect)1 LibvirtException (org.libvirt.LibvirtException)1