use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtStorageAdaptor method deleteStoragePool.
@Override
public boolean deleteStoragePool(String uuid) {
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
StoragePool sp = null;
try {
sp = conn.storagePoolLookupByUUIDString(uuid);
} catch (LibvirtException e) {
return true;
}
try {
sp.destroy();
sp.undefine();
sp.free();
return true;
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
private synchronized Answer execute(MigrateCommand cmd) {
String vmName = cmd.getVmName();
State state = null;
String result = null;
synchronized (_vms) {
state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
Domain dm = null;
Connect dconn = null;
Domain destDomain = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
dconn = new Connect("qemu+tcp://" + cmd.getDestinationIp() + "/system");
/*
* Hard code lm flags: VIR_MIGRATE_LIVE(1<<0) and
* VIR_MIGRATE_PERSIST_DEST(1<<3)
*/
destDomain = dm.migrate(dconn, (1 << 0) | (1 << 3), vmName, "tcp:" + cmd.getDestinationIp(), _migrateSpeed);
} catch (LibvirtException e) {
s_logger.debug("Can't migrate domain: " + e.getMessage());
result = e.getMessage();
} catch (Exception e) {
s_logger.debug("Can't migrate domain: " + e.getMessage());
result = e.getMessage();
} finally {
try {
if (dm != null) {
dm.free();
}
if (dconn != null) {
dconn.close();
}
if (destDomain != null) {
destDomain.free();
}
} catch (final LibvirtException e) {
}
}
if (result != null) {
synchronized (_vms) {
_vms.put(vmName, state);
}
} else {
destroy_network_rules_for_vm(conn, vmName);
cleanupVM(conn, vmName, getVnetId(VirtualMachineName.getVnet(vmName)));
}
return new MigrateAnswer(cmd, result == null, result, null);
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class CloudZonesComputingResource method configure.
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
boolean success = super.configure(name, params);
if (!success) {
return false;
}
_parent = (String) params.get("mount.path");
try {
_dhcpTimeout = Long.parseLong((String) params.get("dhcp.timeout"));
} catch (Exception e) {
_dhcpTimeout = 1200000;
}
_hostIp = (String) params.get("host.ip");
_hostMacAddress = (String) params.get("host.mac.address");
try {
Connect conn;
conn = LibvirtConnection.getConnection();
setupDhcpManager(conn, _guestBridgeName);
} catch (LibvirtException e) {
s_logger.debug("Failed to get libvirt connection:" + e.toString());
return false;
}
_dhcpSnooper.configure(name, params);
_vmDataServer.configure(name, params);
return true;
}
use of org.libvirt.Connect 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);
}
}
}
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getRealPowerState.
protected State getRealPowerState(String vm) {
int i = 0;
s_logger.trace("Checking on the HALTED State");
Domain dm = null;
for (; i < 5; i++) {
try {
Connect conn = LibvirtConnection.getConnection();
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vm.getBytes()));
DomainInfo.DomainState vps = dm.getInfo().state;
if (vps != null && vps != DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF && vps != DomainInfo.DomainState.VIR_DOMAIN_NOSTATE) {
return convertToState(vps);
}
} catch (final LibvirtException e) {
s_logger.trace(e.getMessage());
} catch (Exception e) {
s_logger.trace(e.getMessage());
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (final LibvirtException e) {
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
return State.Stopped;
}
Aggregations