use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected Answer execute(StopCommand cmd) {
final String vmName = cmd.getVmName();
Long bytesReceived = new Long(0);
Long bytesSent = new Long(0);
State state = null;
synchronized (_vms) {
state = _vms.get(vmName);
_vms.put(vmName, State.Stopping);
}
try {
Connect conn = LibvirtConnection.getConnection();
List<DiskDef> disks = getDisks(conn, vmName);
destroy_network_rules_for_vm(conn, vmName);
String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
if (result == null) {
for (DiskDef disk : disks) {
if (disk.getDeviceType() == DiskDef.deviceType.CDROM && disk.getDiskPath() != null)
cleanupDisk(conn, disk);
}
}
final String result2 = cleanupVnet(conn, cmd.getVnet());
if (result != null && result2 != null) {
result = result2 + result;
}
state = State.Stopped;
return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
} catch (LibvirtException e) {
return new StopAnswer(cmd, e.getMessage());
} finally {
synchronized (_vms) {
if (state != null) {
_vms.put(vmName, state);
} else {
_vms.remove(vmName);
}
}
}
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
private synchronized Answer execute(PrepareForMigrationCommand cmd) {
VirtualMachineTO vm = cmd.getVirtualMachine();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Preparing host for migrating " + vm);
}
NicTO[] nics = vm.getNics();
try {
Connect conn = LibvirtConnection.getConnection();
for (NicTO nic : nics) {
String vlanId = null;
if (nic.getBroadcastType() == BroadcastDomainType.Vlan) {
URI broadcastUri = nic.getBroadcastUri();
vlanId = broadcastUri.getHost();
}
if (nic.getType() == TrafficType.Guest) {
if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
createVlanBr(vlanId, _pifs.first());
}
} else if (nic.getType() == TrafficType.Control) {
/* Make sure the network is still there */
createControlNetwork(conn);
} else if (nic.getType() == TrafficType.Public) {
if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
createVlanBr(vlanId, _pifs.second());
}
}
}
/* setup disks, e.g for iso */
VolumeTO[] volumes = vm.getDisks();
for (VolumeTO volume : volumes) {
if (volume.getType() == Volume.Type.ISO) {
getVolumePath(conn, volume);
}
}
synchronized (_vms) {
_vms.put(vm.getName(), State.Migrating);
}
return new PrepareForMigrationAnswer(cmd);
} catch (LibvirtException e) {
return new PrepareForMigrationAnswer(cmd, e.toString());
} catch (InternalErrorException e) {
return new PrepareForMigrationAnswer(cmd, e.toString());
} catch (URISyntaxException e) {
return new PrepareForMigrationAnswer(cmd, e.toString());
}
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
private Answer execute(CheckVirtualMachineCommand cmd) {
try {
Connect conn = LibvirtConnection.getConnection();
final State state = getVmState(conn, cmd.getVmName());
Integer vncPort = null;
if (state == State.Running) {
vncPort = getVncPort(conn, cmd.getVmName());
synchronized (_vms) {
_vms.put(cmd.getVmName(), State.Running);
}
}
return new CheckVirtualMachineAnswer(cmd, state, vncPort);
} catch (LibvirtException e) {
return new CheckVirtualMachineAnswer(cmd, e.getMessage());
}
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getHostInfo.
protected List<Object> getHostInfo() {
final ArrayList<Object> info = new ArrayList<Object>();
long speed = 0;
long cpus = 0;
long ram = 0;
String cap = null;
try {
Connect conn = LibvirtConnection.getConnection();
final NodeInfo hosts = conn.nodeInfo();
boolean result = false;
try {
BufferedReader in = new BufferedReader(new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"));
speed = Long.parseLong(in.readLine()) / 1000;
result = true;
} catch (FileNotFoundException e) {
} catch (IOException e) {
} catch (NumberFormatException e) {
}
if (!result) {
speed = hosts.mhz;
}
cpus = hosts.cpus;
ram = hosts.memory * 1024L;
LibvirtCapXMLParser parser = new LibvirtCapXMLParser();
parser.parseCapabilitiesXML(conn.getCapabilities());
ArrayList<String> oss = parser.getGuestOsType();
for (String s : oss) {
/*
* Even host supports guest os type more than hvm, we only
* report hvm to management server
*/
if (s.equalsIgnoreCase("hvm")) {
cap = "hvm";
}
}
} catch (LibvirtException e) {
}
if (isSnapshotSupported()) {
cap = cap + ",snapshot";
}
info.add((int) cpus);
info.add(speed);
info.add(ram);
info.add(cap);
// save a maximum
long dom0ram = Math.min(ram / 10, 768 * 1024 * 1024L);
// of 10% of
// system ram or
// 768M
dom0ram = Math.max(dom0ram, _dom0MinMem);
info.add(dom0ram);
s_logger.debug("cpus=" + cpus + ", speed=" + speed + ", ram=" + ram + ", dom0ram=" + dom0ram);
return info;
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
private Answer execute(AttachIsoCommand cmd) {
try {
Connect conn = LibvirtConnection.getConnection();
attachOrDetachISO(conn, cmd.getVmName(), cmd.getIsoPath(), cmd.isAttach());
} catch (LibvirtException e) {
return new Answer(cmd, false, e.toString());
} catch (URISyntaxException e) {
return new Answer(cmd, false, e.toString());
} catch (InternalErrorException e) {
return new Answer(cmd, false, e.toString());
}
return new Answer(cmd);
}
Aggregations