use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method getAllVms.
private HashMap<String, State> getAllVms() {
final HashMap<String, State> vmStates = new HashMap<String, State>();
String[] vms = null;
int[] ids = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
s_logger.debug("Failed to get connection: " + e.getMessage());
return vmStates;
}
try {
ids = conn.listDomains();
} catch (final LibvirtException e) {
s_logger.warn("Unable to listDomains", e);
return null;
}
try {
vms = conn.listDefinedDomains();
} catch (final LibvirtException e) {
s_logger.warn("Unable to listDomains", e);
return null;
}
Domain dm = null;
for (int i = 0; i < ids.length; i++) {
try {
dm = conn.domainLookupByID(ids[i]);
DomainInfo.DomainState ps = dm.getInfo().state;
final State state = convertToState(ps);
s_logger.trace("VM " + dm.getName() + ": powerstate = " + ps + "; vm state=" + state.toString());
String vmName = dm.getName();
vmStates.put(vmName, state);
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
}
for (int i = 0; i < vms.length; i++) {
try {
dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vms[i].getBytes()));
DomainInfo.DomainState ps = dm.getInfo().state;
final State state = convertToState(ps);
String vmName = dm.getName();
s_logger.trace("VM " + vmName + ": powerstate = " + ps + "; vm state=" + state.toString());
vmStates.put(vmName, state);
} catch (final LibvirtException e) {
s_logger.warn("Unable to get vms", e);
} catch (Exception e) {
s_logger.warn("Unable to get vms", e);
} finally {
try {
if (dm != null) {
dm.free();
}
} catch (LibvirtException e) {
}
}
}
return vmStates;
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method execute.
protected BackupSnapshotAnswer execute(final BackupSnapshotCommand cmd) {
Long dcId = cmd.getDataCenterId();
Long accountId = cmd.getAccountId();
Long volumeId = cmd.getVolumeId();
String secondaryStoragePoolUrl = cmd.getSecondaryStorageUrl();
String snapshotName = cmd.getSnapshotName();
String snapshotPath = cmd.getVolumePath();
String snapshotDestPath = null;
String snapshotRelPath = null;
String vmName = cmd.getVmName();
KVMStoragePool secondaryStoragePool = null;
try {
Connect conn = LibvirtConnection.getConnection();
secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);
String ssPmountPath = secondaryStoragePool.getLocalPath();
snapshotRelPath = File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
snapshotDestPath = ssPmountPath + File.separator + "snapshots" + File.separator + dcId + File.separator + accountId + File.separator + volumeId;
KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd.getPrimaryStoragePoolNameLabel());
KVMPhysicalDisk snapshotDisk = primaryPool.getPhysicalDisk(cmd.getVolumePath());
Script command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
command.add("-b", snapshotDisk.getPath());
command.add("-n", snapshotName);
command.add("-p", snapshotDestPath);
command.add("-t", snapshotName);
String result = command.execute();
if (result != null) {
s_logger.debug("Failed to backup snaptshot: " + result);
return new BackupSnapshotAnswer(cmd, false, result, null, true);
}
/* Delete the snapshot on primary */
DomainInfo.DomainState state = null;
Domain vm = null;
if (vmName != null) {
try {
vm = getDomain(conn, cmd.getVmName());
state = vm.getInfo().state;
} catch (LibvirtException e) {
}
}
KVMStoragePool primaryStorage = _storagePoolMgr.getStoragePool(cmd.getPool().getUuid());
if (state == DomainInfo.DomainState.VIR_DOMAIN_RUNNING && !primaryStorage.isExternalSnapshot()) {
String vmUuid = vm.getUUIDString();
Object[] args = new Object[] { snapshotName, vmUuid };
String snapshot = SnapshotXML.format(args);
s_logger.debug(snapshot);
DomainSnapshot snap = vm.snapshotLookupByName(snapshotName);
snap.delete(0);
/*
* libvirt on RHEL6 doesn't handle resume event emitted from
* qemu
*/
vm = getDomain(conn, cmd.getVmName());
state = vm.getInfo().state;
if (state == DomainInfo.DomainState.VIR_DOMAIN_PAUSED) {
vm.resume();
}
} else {
command = new Script(_manageSnapshotPath, _cmdsTimeout, s_logger);
command.add("-d", snapshotDisk.getPath());
command.add("-n", snapshotName);
result = command.execute();
if (result != null) {
s_logger.debug("Failed to backup snapshot: " + result);
return new BackupSnapshotAnswer(cmd, false, "Failed to backup snapshot: " + result, null, true);
}
}
} catch (LibvirtException e) {
return new BackupSnapshotAnswer(cmd, false, e.toString(), null, true);
} catch (CloudRuntimeException e) {
return new BackupSnapshotAnswer(cmd, false, e.toString(), null, true);
} finally {
if (secondaryStoragePool != null) {
secondaryStoragePool.delete();
}
}
return new BackupSnapshotAnswer(cmd, true, null, snapshotRelPath + File.separator + snapshotName, true);
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtComputingResource method configure.
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
boolean success = super.configure(name, params);
if (!success) {
return false;
}
try {
Class<?> clazz = Class.forName("com.cloud.storage.JavaStorageLayer");
_storage = (StorageLayer) ComponentLocator.inject(clazz);
_storage.configure("StorageLayer", params);
} catch (ClassNotFoundException e) {
throw new ConfigurationException("Unable to find class " + "com.cloud.storage.JavaStorageLayer");
}
_virtRouterResource = new VirtualRoutingResource();
// Set the domr scripts directory
params.put("domr.scripts.dir", "scripts/network/domr/kvm");
success = _virtRouterResource.configure(name, params);
String kvmScriptsDir = (String) params.get("kvm.scripts.dir");
if (kvmScriptsDir == null) {
kvmScriptsDir = "scripts/vm/hypervisor/kvm";
}
String networkScriptsDir = (String) params.get("network.scripts.dir");
if (networkScriptsDir == null) {
networkScriptsDir = getDefaultNetworkScriptsDir();
}
String storageScriptsDir = (String) params.get("storage.scripts.dir");
if (storageScriptsDir == null) {
storageScriptsDir = getDefaultStorageScriptsDir();
}
if (!success) {
return false;
}
_host = (String) params.get("host");
if (_host == null) {
_host = "localhost";
}
_dcId = (String) params.get("zone");
if (_dcId == null) {
_dcId = "default";
}
_pod = (String) params.get("pod");
if (_pod == null) {
_pod = "default";
}
_clusterId = (String) params.get("cluster");
_modifyVlanPath = Script.findScript(networkScriptsDir, "modifyvlan.sh");
if (_modifyVlanPath == null) {
throw new ConfigurationException("Unable to find modifyvlan.sh");
}
_versionstringpath = Script.findScript(kvmScriptsDir, "versions.sh");
if (_versionstringpath == null) {
throw new ConfigurationException("Unable to find versions.sh");
}
_patchdomrPath = Script.findScript(kvmScriptsDir + "/patch/", "rundomrpre.sh");
if (_patchdomrPath == null) {
throw new ConfigurationException("Unable to find rundomrpre.sh");
}
_heartBeatPath = Script.findScript(kvmScriptsDir, "kvmheartbeat.sh");
if (_heartBeatPath == null) {
throw new ConfigurationException("Unable to find kvmheartbeat.sh");
}
_createvmPath = Script.findScript(storageScriptsDir, "createvm.sh");
if (_createvmPath == null) {
throw new ConfigurationException("Unable to find the createvm.sh");
}
_manageSnapshotPath = Script.findScript(storageScriptsDir, "managesnapshot.sh");
if (_manageSnapshotPath == null) {
throw new ConfigurationException("Unable to find the managesnapshot.sh");
}
_createTmplPath = Script.findScript(storageScriptsDir, "createtmplt.sh");
if (_createTmplPath == null) {
throw new ConfigurationException("Unable to find the createtmplt.sh");
}
_securityGroupPath = Script.findScript(networkScriptsDir, "security_group.py");
if (_securityGroupPath == null) {
throw new ConfigurationException("Unable to find the security_group.py");
}
_networkUsagePath = Script.findScript("scripts/network/domr/", "networkUsage.sh");
if (_networkUsagePath == null) {
throw new ConfigurationException("Unable to find the networkUsage.sh");
}
String value = (String) params.get("developer");
boolean isDeveloper = Boolean.parseBoolean(value);
if (isDeveloper) {
params.putAll(getDeveloperProperties());
}
_pool = (String) params.get("pool");
if (_pool == null) {
_pool = "/root";
}
String instance = (String) params.get("instance");
_hypervisorType = (String) params.get("hypervisor.type");
if (_hypervisorType == null) {
_hypervisorType = "kvm";
}
_hypervisorURI = (String) params.get("hypervisor.uri");
if (_hypervisorURI == null) {
_hypervisorURI = "qemu:///system";
}
String startMac = (String) params.get("private.macaddr.start");
if (startMac == null) {
startMac = "00:16:3e:77:e2:a0";
}
String startIp = (String) params.get("private.ipaddr.start");
if (startIp == null) {
startIp = "192.168.166.128";
}
_pingTestPath = Script.findScript(kvmScriptsDir, "pingtest.sh");
if (_pingTestPath == null) {
throw new ConfigurationException("Unable to find the pingtest.sh");
}
_linkLocalBridgeName = (String) params.get("private.bridge.name");
if (_linkLocalBridgeName == null) {
if (isDeveloper) {
_linkLocalBridgeName = "cloud-" + instance + "-0";
} else {
_linkLocalBridgeName = "cloud0";
}
}
_publicBridgeName = (String) params.get("public.network.device");
if (_publicBridgeName == null) {
_publicBridgeName = "cloudbr0";
}
_privBridgeName = (String) params.get("private.network.device");
if (_privBridgeName == null) {
_privBridgeName = "cloudbr1";
}
_guestBridgeName = (String) params.get("guest.network.device");
if (_guestBridgeName == null) {
_guestBridgeName = _privBridgeName;
}
_privNwName = (String) params.get("private.network.name");
if (_privNwName == null) {
if (isDeveloper) {
_privNwName = "cloud-" + instance + "-private";
} else {
_privNwName = "cloud-private";
}
}
_localStoragePath = (String) params.get("local.storage.path");
if (_localStoragePath == null) {
_localStoragePath = "/var/lib/libvirt/images/";
}
_localStorageUUID = (String) params.get("local.storage.uuid");
if (_localStorageUUID == null) {
_localStorageUUID = UUID.randomUUID().toString();
params.put("local.storage.uuid", _localStorageUUID);
}
value = (String) params.get("scripts.timeout");
_timeout = NumbersUtil.parseInt(value, 30 * 60) * 1000;
value = (String) params.get("stop.script.timeout");
_stopTimeout = NumbersUtil.parseInt(value, 120) * 1000;
value = (String) params.get("cmds.timeout");
_cmdsTimeout = NumbersUtil.parseInt(value, 7200) * 1000;
value = (String) params.get("host.reserved.mem.mb");
_dom0MinMem = NumbersUtil.parseInt(value, 0) * 1024 * 1024;
value = (String) params.get("debug.mode");
LibvirtConnection.initialize(_hypervisorURI);
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.getMessage());
}
/* Does node support HVM guest? If not, exit */
if (!IsHVMEnabled(conn)) {
throw new ConfigurationException("NO HVM support on this machine, pls make sure: " + "1. VT/SVM is supported by your CPU, or is enabled in BIOS. " + "2. kvm modules is installed");
}
_hypervisorPath = getHypervisorPath(conn);
try {
_hvVersion = conn.getVersion();
_hvVersion = (_hvVersion % 1000000) / 1000;
} catch (LibvirtException e) {
}
String[] info = NetUtils.getNetworkParams(_privateNic);
_monitor = new KVMHAMonitor(null, info[0], _heartBeatPath);
Thread ha = new Thread(_monitor);
ha.start();
_storagePoolMgr = new KVMStoragePoolManager(_storage, _monitor);
_sysvmISOPath = (String) params.get("systemvm.iso.path");
if (_sysvmISOPath == null) {
String[] isoPaths = { "/usr/lib64/cloud/agent/vms/systemvm.iso", "/usr/lib/cloud/agent/vms/systemvm.iso" };
for (String isoPath : isoPaths) {
if (_storage.exists(isoPath)) {
_sysvmISOPath = isoPath;
break;
}
}
if (_sysvmISOPath == null) {
s_logger.debug("Can't find system vm ISO");
}
}
try {
createControlNetwork(conn);
} catch (LibvirtException e) {
throw new ConfigurationException(e.getMessage());
}
_pifs = getPifs();
if (_pifs.first() == null) {
s_logger.debug("Failed to get private nic name");
throw new ConfigurationException("Failed to get private nic name");
}
if (_pifs.second() == null) {
s_logger.debug("Failed to get public nic name");
throw new ConfigurationException("Failed to get public nic name");
}
s_logger.debug("Found pif: " + _pifs.first() + " on " + _privBridgeName + ", pif: " + _pifs.second() + " on " + _publicBridgeName);
_can_bridge_firewall = can_bridge_firewall(_pifs.second());
_localGateway = Script.runSimpleBashScript("ip route |grep default|awk '{print $3}'");
if (_localGateway == null) {
s_logger.debug("Failed to found the local gateway");
}
_mountPoint = (String) params.get("mount.path");
if (_mountPoint == null) {
_mountPoint = "/mnt";
}
value = (String) params.get("vm.migrate.speed");
_migrateSpeed = NumbersUtil.parseInt(value, -1);
if (_migrateSpeed == -1) {
//get guest network device speed
_migrateSpeed = 0;
String speed = Script.runSimpleBashScript("ethtool " + _pifs.second() + " |grep Speed | cut -d \\ -f 2");
if (speed != null) {
String[] tokens = speed.split("M");
if (tokens.length == 2) {
try {
_migrateSpeed = Integer.parseInt(tokens[0]);
} catch (Exception e) {
}
s_logger.debug("device " + _pifs.second() + " has speed: " + String.valueOf(_migrateSpeed));
}
}
params.put("vm.migrate.speed", String.valueOf(_migrateSpeed));
}
saveProperties(params);
return true;
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtStorageAdaptor method getStoragePool.
@Override
public KVMStoragePool getStoragePool(String uuid) {
StoragePool storage = null;
try {
Connect conn = LibvirtConnection.getConnection();
storage = conn.storagePoolLookupByUUIDString(uuid);
if (storage.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
storage.create(0);
}
LibvirtStoragePoolDef spd = getStoragePoolDef(conn, storage);
StoragePoolType type = null;
if (spd.getPoolType() == LibvirtStoragePoolDef.poolType.NETFS || spd.getPoolType() == LibvirtStoragePoolDef.poolType.DIR) {
type = StoragePoolType.Filesystem;
}
LibvirtStoragePool pool = new LibvirtStoragePool(uuid, storage.getName(), type, this, storage);
pool.setLocalPath(spd.getTargetPath());
getStats(pool);
return pool;
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
}
use of org.libvirt.Connect in project CloudStack-archive by CloudStack-extras.
the class LibvirtStorageAdaptor method createStoragePool.
@Override
public KVMStoragePool createStoragePool(String name, String host, String path, StoragePoolType type) {
StoragePool sp = null;
Connect conn = null;
try {
conn = LibvirtConnection.getConnection();
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
try {
sp = conn.storagePoolLookupByUUIDString(name);
if (sp.getInfo().state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
sp.undefine();
sp = null;
}
} catch (LibvirtException e) {
}
if (sp == null) {
if (type == StoragePoolType.NetworkFilesystem) {
sp = createNfsStoragePool(conn, name, host, path);
} else if (type == StoragePoolType.SharedMountPoint || type == StoragePoolType.Filesystem) {
sp = CreateSharedStoragePool(conn, name, host, path);
}
}
try {
StoragePoolInfo spi = sp.getInfo();
if (spi.state != StoragePoolState.VIR_STORAGE_POOL_RUNNING) {
sp.create(0);
}
LibvirtStoragePoolDef spd = getStoragePoolDef(conn, sp);
LibvirtStoragePool pool = new LibvirtStoragePool(name, sp.getName(), type, this, sp);
pool.setLocalPath(spd.getTargetPath());
getStats(pool);
return pool;
} catch (LibvirtException e) {
throw new CloudRuntimeException(e.toString());
}
}
Aggregations