You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by al...@apache.org on 2012/08/02 04:08:32 UTC
[2/44] Merge branch 'master' into vpc
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ce9f1afa/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
----------------------------------------------------------------------
diff --cc plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
index 643a4e8,73101a9..6862e14
--- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
+++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java
@@@ -231,1077 -231,1077 +231,1077 @@@ import com.cloud.vm.VirtualMachineName
**/
@Local(value = { ServerResource.class })
public class LibvirtComputingResource extends ServerResourceBase implements
- ServerResource {
- private static final Logger s_logger = Logger
- .getLogger(LibvirtComputingResource.class);
-
- private String _modifyVlanPath;
- private String _versionstringpath;
- private String _patchdomrPath;
- private String _createvmPath;
- private String _manageSnapshotPath;
- private String _createTmplPath;
- private String _heartBeatPath;
- private String _securityGroupPath;
- private String _networkUsagePath;
- private String _host;
- private String _dcId;
- private String _pod;
- private String _clusterId;
- private int _migrateSpeed;
-
- private long _hvVersion;
- private KVMHAMonitor _monitor;
- private final String _SSHKEYSPATH = "/root/.ssh";
- private final String _SSHPRVKEYPATH = _SSHKEYSPATH + File.separator
- + "id_rsa.cloud";
- private final String _SSHPUBKEYPATH = _SSHKEYSPATH + File.separator
- + "id_rsa.pub.cloud";
- private String _mountPoint = "/mnt";
- StorageLayer _storage;
- private KVMStoragePoolManager _storagePoolMgr;
-
- private static final class KeyValueInterpreter extends OutputInterpreter {
- private final Map<String, String> map = new HashMap<String, String>();
-
- @Override
- public String interpret(BufferedReader reader) throws IOException {
- String line = null;
- int numLines = 0;
- while ((line = reader.readLine()) != null) {
- String[] toks = line.trim().split("=");
- if (toks.length < 2) {
- s_logger.warn("Failed to parse Script output: " + line);
- } else {
- map.put(toks[0].trim(), toks[1].trim());
- }
- numLines++;
- }
- if (numLines == 0) {
- s_logger.warn("KeyValueInterpreter: no output lines?");
- }
- return null;
- }
-
- public Map<String, String> getKeyValues() {
- return map;
- }
- }
-
- @Override
- protected String getDefaultScriptsDir() {
- return null;
- }
-
- protected static MessageFormat SnapshotXML = new MessageFormat(
- " <domainsnapshot>" + " <name>{0}</name>" + " <domain>"
- + " <uuid>{1}</uuid>" + " </domain>"
- + " </domainsnapshot>");
-
- protected String _hypervisorType;
- protected String _hypervisorURI;
- protected String _hypervisorPath;
- protected String _sysvmISOPath;
- protected String _privNwName;
- protected String _privBridgeName;
- protected String _linkLocalBridgeName;
- protected String _publicBridgeName;
- protected String _guestBridgeName;
- protected String _privateIp;
- protected String _pool;
- protected String _localGateway;
- private boolean _can_bridge_firewall;
- protected String _localStoragePath;
- protected String _localStorageUUID;
- private Pair<String, String> _pifs;
- private final Map<String, vmStats> _vmStats = new ConcurrentHashMap<String, vmStats>();
-
- protected boolean _disconnected = true;
- protected int _timeout;
- protected int _cmdsTimeout;
- protected int _stopTimeout;
- protected static HashMap<DomainInfo.DomainState, State> s_statesTable;
- static {
- s_statesTable = new HashMap<DomainInfo.DomainState, State>();
- s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF,
- State.Stopped);
- s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_PAUSED,
- State.Running);
- s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_RUNNING,
- State.Running);
- s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_BLOCKED,
- State.Running);
- s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_NOSTATE,
- State.Unknown);
- s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_SHUTDOWN,
- State.Stopping);
- }
-
- protected HashMap<String, State> _vms = new HashMap<String, State>(20);
- protected List<String> _vmsKilled = new ArrayList<String>();
-
- private VirtualRoutingResource _virtRouterResource;
-
- private String _pingTestPath;
-
- private int _dom0MinMem;
-
- protected enum defineOps {
- UNDEFINE_VM, DEFINE_VM
- }
-
- private String getEndIpFromStartIp(String startIp, int numIps) {
- String[] tokens = startIp.split("[.]");
- assert (tokens.length == 4);
- int lastbyte = Integer.parseInt(tokens[3]);
- lastbyte = lastbyte + numIps;
- tokens[3] = Integer.toString(lastbyte);
- StringBuilder end = new StringBuilder(15);
- end.append(tokens[0]).append(".").append(tokens[1]).append(".")
- .append(tokens[2]).append(".").append(tokens[3]);
- return end.toString();
- }
-
- private Map<String, Object> getDeveloperProperties()
- throws ConfigurationException {
- final File file = PropertiesUtil.findConfigFile("developer.properties");
- if (file == null) {
- throw new ConfigurationException(
- "Unable to find developer.properties.");
- }
-
- s_logger.info("developer.properties found at " + file.getAbsolutePath());
- Properties properties = new Properties();
- try {
- properties.load(new FileInputStream(file));
-
- String startMac = (String) properties.get("private.macaddr.start");
- if (startMac == null) {
- throw new ConfigurationException(
- "Developers must specify start mac for private ip range");
- }
-
- String startIp = (String) properties.get("private.ipaddr.start");
- if (startIp == null) {
- throw new ConfigurationException(
- "Developers must specify start ip for private ip range");
- }
- final Map<String, Object> params = PropertiesUtil.toMap(properties);
-
- String endIp = (String) properties.get("private.ipaddr.end");
- if (endIp == null) {
- endIp = getEndIpFromStartIp(startIp, 16);
- params.put("private.ipaddr.end", endIp);
- }
- return params;
- } catch (final FileNotFoundException ex) {
- throw new CloudRuntimeException("Cannot find the file: "
- + file.getAbsolutePath(), ex);
- } catch (final IOException ex) {
- throw new CloudRuntimeException("IOException in reading "
- + file.getAbsolutePath(), ex);
- }
- }
-
- protected String getDefaultNetworkScriptsDir() {
- return "scripts/vm/network/vnet";
- }
-
- protected String getDefaultStorageScriptsDir() {
- return "scripts/storage/qcow2";
- }
-
- private void saveProperties(Map<String, Object> params)
- throws ConfigurationException {
- final File file = PropertiesUtil.findConfigFile("agent.properties");
- if (file == null) {
- throw new ConfigurationException("Unable to find agent.properties.");
- }
-
- s_logger.info("agent.properties found at " + file.getAbsolutePath());
-
- try {
- Properties _properties = new Properties();
- _properties.load(new FileInputStream(file));
- Set<String> names = _properties.stringPropertyNames();
- for (String key : params.keySet()) {
- if (!names.contains(key)) {
- _properties.setProperty(key, (String) params.get(key));
- }
- }
- _properties.store(new FileOutputStream(file), "");
- } catch (final FileNotFoundException ex) {
- throw new CloudRuntimeException("Cannot find the file: "
- + file.getAbsolutePath(), ex);
- } catch (final IOException ex) {
- throw new CloudRuntimeException("IOException in reading "
- + file.getAbsolutePath(), ex);
- }
- }
-
- @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 {
+ ServerResource {
+ private static final Logger s_logger = Logger
+ .getLogger(LibvirtComputingResource.class);
+
+ private String _modifyVlanPath;
+ private String _versionstringpath;
+ private String _patchdomrPath;
+ private String _createvmPath;
+ private String _manageSnapshotPath;
+ private String _createTmplPath;
+ private String _heartBeatPath;
+ private String _securityGroupPath;
+ private String _routerProxyPath;
+ private String _host;
+ private String _dcId;
+ private String _pod;
+ private String _clusterId;
+ private int _migrateSpeed;
+
+ private long _hvVersion;
+ private KVMHAMonitor _monitor;
+ private final String _SSHKEYSPATH = "/root/.ssh";
+ private final String _SSHPRVKEYPATH = _SSHKEYSPATH + File.separator
+ + "id_rsa.cloud";
+ private final String _SSHPUBKEYPATH = _SSHKEYSPATH + File.separator
+ + "id_rsa.pub.cloud";
+ private String _mountPoint = "/mnt";
+ StorageLayer _storage;
+ private KVMStoragePoolManager _storagePoolMgr;
+
+ private static final class KeyValueInterpreter extends OutputInterpreter {
+ private final Map<String, String> map = new HashMap<String, String>();
+
+ @Override
+ public String interpret(BufferedReader reader) throws IOException {
+ String line = null;
+ int numLines = 0;
+ while ((line = reader.readLine()) != null) {
+ String[] toks = line.trim().split("=");
+ if (toks.length < 2) {
+ s_logger.warn("Failed to parse Script output: " + line);
+ } else {
+ map.put(toks[0].trim(), toks[1].trim());
+ }
+ numLines++;
+ }
+ if (numLines == 0) {
+ s_logger.warn("KeyValueInterpreter: no output lines?");
+ }
+ return null;
+ }
+
+ public Map<String, String> getKeyValues() {
+ return map;
+ }
+ }
+
+ @Override
+ protected String getDefaultScriptsDir() {
+ return null;
+ }
+
+ protected static MessageFormat SnapshotXML = new MessageFormat(
+ " <domainsnapshot>" + " <name>{0}</name>" + " <domain>"
+ + " <uuid>{1}</uuid>" + " </domain>"
+ + " </domainsnapshot>");
+
+ protected String _hypervisorType;
+ protected String _hypervisorURI;
+ protected String _hypervisorPath;
+ protected String _sysvmISOPath;
+ protected String _privNwName;
+ protected String _privBridgeName;
+ protected String _linkLocalBridgeName;
+ protected String _publicBridgeName;
+ protected String _guestBridgeName;
+ protected String _privateIp;
+ protected String _pool;
+ protected String _localGateway;
+ private boolean _can_bridge_firewall;
+ protected String _localStoragePath;
+ protected String _localStorageUUID;
+ private Pair<String, String> _pifs;
+ private final Map<String, vmStats> _vmStats = new ConcurrentHashMap<String, vmStats>();
+
+ protected boolean _disconnected = true;
+ protected int _timeout;
+ protected int _cmdsTimeout;
+ protected int _stopTimeout;
+ protected static HashMap<DomainInfo.DomainState, State> s_statesTable;
+ static {
+ s_statesTable = new HashMap<DomainInfo.DomainState, State>();
+ s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_SHUTOFF,
+ State.Stopped);
+ s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_PAUSED,
+ State.Running);
+ s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_RUNNING,
+ State.Running);
+ s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_BLOCKED,
+ State.Running);
+ s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_NOSTATE,
+ State.Unknown);
+ s_statesTable.put(DomainInfo.DomainState.VIR_DOMAIN_SHUTDOWN,
+ State.Stopping);
+ }
+
+ protected HashMap<String, State> _vms = new HashMap<String, State>(20);
+ protected List<String> _vmsKilled = new ArrayList<String>();
+
+ private VirtualRoutingResource _virtRouterResource;
+
+ private String _pingTestPath;
+
+ private int _dom0MinMem;
+
+ protected enum defineOps {
+ UNDEFINE_VM, DEFINE_VM
+ }
+
+ private String getEndIpFromStartIp(String startIp, int numIps) {
+ String[] tokens = startIp.split("[.]");
+ assert (tokens.length == 4);
+ int lastbyte = Integer.parseInt(tokens[3]);
+ lastbyte = lastbyte + numIps;
+ tokens[3] = Integer.toString(lastbyte);
+ StringBuilder end = new StringBuilder(15);
+ end.append(tokens[0]).append(".").append(tokens[1]).append(".")
+ .append(tokens[2]).append(".").append(tokens[3]);
+ return end.toString();
+ }
+
+ private Map<String, Object> getDeveloperProperties()
+ throws ConfigurationException {
+ final File file = PropertiesUtil.findConfigFile("developer.properties");
+ if (file == null) {
+ throw new ConfigurationException(
+ "Unable to find developer.properties.");
+ }
+
+ s_logger.info("developer.properties found at " + file.getAbsolutePath());
+ Properties properties = new Properties();
+ try {
+ properties.load(new FileInputStream(file));
+
+ String startMac = (String) properties.get("private.macaddr.start");
+ if (startMac == null) {
+ throw new ConfigurationException(
+ "Developers must specify start mac for private ip range");
+ }
+
+ String startIp = (String) properties.get("private.ipaddr.start");
+ if (startIp == null) {
+ throw new ConfigurationException(
+ "Developers must specify start ip for private ip range");
+ }
+ final Map<String, Object> params = PropertiesUtil.toMap(properties);
+
+ String endIp = (String) properties.get("private.ipaddr.end");
+ if (endIp == null) {
+ endIp = getEndIpFromStartIp(startIp, 16);
+ params.put("private.ipaddr.end", endIp);
+ }
+ return params;
+ } catch (final FileNotFoundException ex) {
+ throw new CloudRuntimeException("Cannot find the file: "
+ + file.getAbsolutePath(), ex);
+ } catch (final IOException ex) {
+ throw new CloudRuntimeException("IOException in reading "
+ + file.getAbsolutePath(), ex);
+ }
+ }
+
+ protected String getDefaultNetworkScriptsDir() {
+ return "scripts/vm/network/vnet";
+ }
+
+ protected String getDefaultStorageScriptsDir() {
+ return "scripts/storage/qcow2";
+ }
+
+ private void saveProperties(Map<String, Object> params)
+ throws ConfigurationException {
+ final File file = PropertiesUtil.findConfigFile("agent.properties");
+ if (file == null) {
+ throw new ConfigurationException("Unable to find agent.properties.");
+ }
+
+ s_logger.info("agent.properties found at " + file.getAbsolutePath());
+
+ try {
+ Properties _properties = new Properties();
+ _properties.load(new FileInputStream(file));
+ Set<String> names = _properties.stringPropertyNames();
+ for (String key : params.keySet()) {
+ if (!names.contains(key)) {
+ _properties.setProperty(key, (String) params.get(key));
+ }
+ }
+ _properties.store(new FileOutputStream(file), "");
+ } catch (final FileNotFoundException ex) {
+ throw new CloudRuntimeException("Cannot find the file: "
+ + file.getAbsolutePath(), ex);
+ } catch (final IOException ex) {
+ throw new CloudRuntimeException("IOException in reading "
+ + file.getAbsolutePath(), ex);
+ }
+ }
+
+ @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");
+ }
+
+ _routerProxyPath = Script.findScript("scripts/network/domr/",
+ "router_proxy.sh");
+ if (_routerProxyPath == null) {
+ throw new ConfigurationException(
+ "Unable to find the router_proxy.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);
+ createControlNetwork();
- } 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;
- }
-
- private Pair<String, String> getPifs() {
- /* get pifs from bridge */
- String pubPif = null;
- String privPif = null;
- String vlan = null;
- if (_publicBridgeName != null) {
- pubPif = Script.runSimpleBashScript("brctl show | grep "
- + _publicBridgeName + " | awk '{print $4}'");
- vlan = Script.runSimpleBashScript("ls /proc/net/vlan/" + pubPif);
- if (vlan != null && !vlan.isEmpty()) {
- pubPif = Script
- .runSimpleBashScript("grep ^Device\\: /proc/net/vlan/"
- + pubPif + " | awk {'print $2'}");
- }
- }
- if (_guestBridgeName != null) {
- privPif = Script.runSimpleBashScript("brctl show | grep "
- + _guestBridgeName + " | awk '{print $4}'");
- vlan = Script.runSimpleBashScript("ls /proc/net/vlan/" + privPif);
- if (vlan != null && !vlan.isEmpty()) {
- privPif = Script
- .runSimpleBashScript("grep ^Device\\: /proc/net/vlan/"
- + privPif + " | awk {'print $2'}");
- }
- }
- return new Pair<String, String>(privPif, pubPif);
- }
-
- private boolean checkNetwork(String networkName) {
- if (networkName == null) {
- return true;
- }
-
- String name = Script.runSimpleBashScript("brctl show | grep "
- + networkName + " | awk '{print $4}'");
- if (name == null) {
- return false;
- } else {
- return true;
- }
- }
-
- private String getVnetId(String vnetId) {
- return vnetId;
- }
-
- private void patchSystemVm(String cmdLine, String dataDiskPath,
- String vmName) throws InternalErrorException {
- String result;
- final Script command = new Script(_patchdomrPath, _timeout, s_logger);
- command.add("-l", vmName);
- command.add("-t", "all");
- command.add("-d", dataDiskPath);
- command.add("-p", cmdLine.replaceAll(" ", "%"));
- result = command.execute();
- if (result != null) {
- throw new InternalErrorException(result);
- }
- }
-
- boolean isDirectAttachedNetwork(String type) {
- if ("untagged".equalsIgnoreCase(type)) {
- return true;
- } else {
- try {
- Long.valueOf(type);
- } catch (NumberFormatException e) {
- return true;
- }
- return false;
- }
- }
-
- protected String startDomain(Connect conn, String vmName, String domainXML)
- throws LibvirtException, InternalErrorException {
- /* No duplicated vm, we will success, or failed */
- boolean failed = false;
- Domain dm = null;
- try {
- dm = conn.domainDefineXML(domainXML);
- } catch (final LibvirtException e) {
- /* Duplicated defined vm */
- s_logger.warn("Failed to define domain " + vmName + ": "
- + e.getMessage());
- failed = true;
- } finally {
- try {
- if (dm != null) {
- dm.free();
- }
- } catch (final LibvirtException e) {
-
- }
- }
-
- /* If failed, undefine the vm */
- Domain dmOld = null;
- Domain dmNew = null;
- try {
- if (failed) {
- dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
- .getBytes()));
- dmOld.undefine();
- dmNew = conn.domainDefineXML(domainXML);
- }
- } catch (final LibvirtException e) {
- s_logger.warn("Failed to define domain (second time) " + vmName
- + ": " + e.getMessage());
- throw e;
- } catch (Exception e) {
- s_logger.warn("Failed to define domain (second time) " + vmName
- + ": " + e.getMessage());
- throw new InternalErrorException(e.toString());
- } finally {
- try {
- if (dmOld != null) {
- dmOld.free();
- }
- if (dmNew != null) {
- dmNew.free();
- }
- } catch (final LibvirtException e) {
-
- }
- }
-
- /* Start the VM */
- try {
- dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
- .getBytes()));
- dm.create();
- } catch (LibvirtException e) {
- s_logger.warn("Failed to start domain: " + vmName + ": "
- + e.getMessage());
- throw e;
- } finally {
- try {
- if (dm != null) {
- dm.free();
- }
- } catch (final LibvirtException e) {
-
- }
- }
- return null;
- }
-
- @Override
- public boolean stop() {
- try {
- Connect conn = LibvirtConnection.getConnection();
- conn.close();
- } catch (LibvirtException e) {
- }
-
- return true;
- }
-
- public static void main(String[] args) {
- s_logger.addAppender(new org.apache.log4j.ConsoleAppender(
- new org.apache.log4j.PatternLayout(), "System.out"));
- LibvirtComputingResource test = new LibvirtComputingResource();
- Map<String, Object> params = new HashMap<String, Object>();
- try {
- test.configure("test", params);
- } catch (ConfigurationException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- }
- String result = null;
- // String result = test.startDomainRouter("domr1",
- // "/var/lib/images/centos.5-4.x86-64/centos-small.img", 128, "0064",
- // "02:00:30:00:01:01", "00:16:3e:77:e2:a1", "02:00:30:00:64:01");
- boolean created = (result == null);
- s_logger.info("Domain " + (created ? " " : " not ") + " created");
-
- s_logger.info("Rule " + (created ? " " : " not ") + " created");
- test.stop();
- }
-
- @Override
- public Answer executeRequest(Command cmd) {
-
- try {
- if (cmd instanceof StopCommand) {
- return execute((StopCommand) cmd);
- } else if (cmd instanceof GetVmStatsCommand) {
- return execute((GetVmStatsCommand) cmd);
- } else if (cmd instanceof RebootRouterCommand) {
- return execute((RebootRouterCommand) cmd);
- } else if (cmd instanceof RebootCommand) {
- return execute((RebootCommand) cmd);
- } else if (cmd instanceof GetHostStatsCommand) {
- return execute((GetHostStatsCommand) cmd);
- } else if (cmd instanceof CheckStateCommand) {
- return executeRequest(cmd);
- } else if (cmd instanceof CheckHealthCommand) {
- return execute((CheckHealthCommand) cmd);
- } else if (cmd instanceof PrepareForMigrationCommand) {
- return execute((PrepareForMigrationCommand) cmd);
- } else if (cmd instanceof MigrateCommand) {
- return execute((MigrateCommand) cmd);
- } else if (cmd instanceof PingTestCommand) {
- return execute((PingTestCommand) cmd);
- } else if (cmd instanceof CheckVirtualMachineCommand) {
- return execute((CheckVirtualMachineCommand) cmd);
- } else if (cmd instanceof ReadyCommand) {
- return execute((ReadyCommand) cmd);
- } else if (cmd instanceof AttachIsoCommand) {
- return execute((AttachIsoCommand) cmd);
- } else if (cmd instanceof AttachVolumeCommand) {
- return execute((AttachVolumeCommand) cmd);
- } else if (cmd instanceof StopCommand) {
- return execute((StopCommand) cmd);
- } else if (cmd instanceof CheckConsoleProxyLoadCommand) {
- return execute((CheckConsoleProxyLoadCommand) cmd);
- } else if (cmd instanceof WatchConsoleProxyLoadCommand) {
- return execute((WatchConsoleProxyLoadCommand) cmd);
- } else if (cmd instanceof GetVncPortCommand) {
- return execute((GetVncPortCommand) cmd);
- } else if (cmd instanceof ModifySshKeysCommand) {
- return execute((ModifySshKeysCommand) cmd);
- } else if (cmd instanceof MaintainCommand) {
- return execute((MaintainCommand) cmd);
- } else if (cmd instanceof CreateCommand) {
- return execute((CreateCommand) cmd);
- } else if (cmd instanceof DestroyCommand) {
- return execute((DestroyCommand) cmd);
- } else if (cmd instanceof PrimaryStorageDownloadCommand) {
- return execute((PrimaryStorageDownloadCommand) cmd);
- } else if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) {
- return execute((CreatePrivateTemplateFromVolumeCommand) cmd);
- } else if (cmd instanceof GetStorageStatsCommand) {
- return execute((GetStorageStatsCommand) cmd);
- } else if (cmd instanceof ManageSnapshotCommand) {
- return execute((ManageSnapshotCommand) cmd);
- } else if (cmd instanceof BackupSnapshotCommand) {
- return execute((BackupSnapshotCommand) cmd);
- } else if (cmd instanceof CreateVolumeFromSnapshotCommand) {
- return execute((CreateVolumeFromSnapshotCommand) cmd);
- } else if (cmd instanceof CreatePrivateTemplateFromSnapshotCommand) {
- return execute((CreatePrivateTemplateFromSnapshotCommand) cmd);
- } else if (cmd instanceof UpgradeSnapshotCommand) {
- return execute((UpgradeSnapshotCommand) cmd);
- } else if (cmd instanceof CreateStoragePoolCommand) {
- return execute((CreateStoragePoolCommand) cmd);
- } else if (cmd instanceof ModifyStoragePoolCommand) {
- return execute((ModifyStoragePoolCommand) cmd);
- } else if (cmd instanceof SecurityGroupRulesCmd) {
- return execute((SecurityGroupRulesCmd) cmd);
- } else if (cmd instanceof DeleteStoragePoolCommand) {
- return execute((DeleteStoragePoolCommand) cmd);
- } else if (cmd instanceof FenceCommand) {
- return execute((FenceCommand) cmd);
- } else if (cmd instanceof StartCommand) {
- return execute((StartCommand) cmd);
- } else if (cmd instanceof IpAssocCommand) {
- return execute((IpAssocCommand) cmd);
- } else if (cmd instanceof NetworkElementCommand) {
- return _virtRouterResource.executeRequest(cmd);
- } else if (cmd instanceof CheckSshCommand) {
- return execute((CheckSshCommand) cmd);
- } else if (cmd instanceof NetworkUsageCommand) {
- return execute((NetworkUsageCommand) cmd);
- } else if (cmd instanceof NetworkRulesSystemVmCommand) {
- return execute((NetworkRulesSystemVmCommand) cmd);
- } else if (cmd instanceof CleanupNetworkRulesCmd) {
- return execute((CleanupNetworkRulesCmd) cmd);
- } else if (cmd instanceof CopyVolumeCommand) {
- return execute((CopyVolumeCommand) cmd);
- } else if (cmd instanceof CheckNetworkCommand) {
- return execute((CheckNetworkCommand) cmd);
- } else {
- s_logger.warn("Unsupported command ");
- return Answer.createUnsupportedCommandAnswer(cmd);
- }
- } catch (final IllegalArgumentException e) {
- return new Answer(cmd, false, e.getMessage());
- }
- }
-
- private CheckNetworkAnswer execute(CheckNetworkCommand cmd) {
- List<PhysicalNetworkSetupInfo> phyNics = cmd
- .getPhysicalNetworkInfoList();
- String errMsg = null;
- for (PhysicalNetworkSetupInfo nic : phyNics) {
- if (!checkNetwork(nic.getGuestNetworkName())) {
- errMsg = "Can not find network: " + nic.getGuestNetworkName();
- break;
- } else if (!checkNetwork(nic.getPrivateNetworkName())) {
- errMsg = "Can not find network: " + nic.getPrivateNetworkName();
- break;
- } else if (!checkNetwork(nic.getPublicNetworkName())) {
- errMsg = "Can not find network: " + nic.getPublicNetworkName();
- break;
- }
- }
-
- if (errMsg != null) {
- return new CheckNetworkAnswer(cmd, false, errMsg);
- } else {
- return new CheckNetworkAnswer(cmd, true, null);
- }
- }
-
- private CopyVolumeAnswer execute(CopyVolumeCommand cmd) {
- boolean copyToSecondary = cmd.toSecondaryStorage();
- String volumePath = cmd.getVolumePath();
- StorageFilerTO pool = cmd.getPool();
- String secondaryStorageUrl = cmd.getSecondaryStorageURL();
- KVMStoragePool secondaryStoragePool = null;
- try {
- KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(pool
- .getUuid());
- String volumeName = UUID.randomUUID().toString();
-
- if (copyToSecondary) {
- String destVolumeName = volumeName + ".qcow2";
- KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(cmd
- .getVolumePath());
- String volumeDestPath = "/volumes/" + cmd.getVolumeId()
- + File.separator;
- secondaryStoragePool = _storagePoolMgr
- .getStoragePoolByURI(secondaryStorageUrl);
- secondaryStoragePool.createFolder(volumeDestPath);
- secondaryStoragePool.delete();
- secondaryStoragePool = _storagePoolMgr
- .getStoragePoolByURI(secondaryStorageUrl
- + volumeDestPath);
- _storagePoolMgr.copyPhysicalDisk(volume, destVolumeName,
- secondaryStoragePool);
- return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
- } else {
- volumePath = "/volumes/" + cmd.getVolumeId() + File.separator;
- secondaryStoragePool = _storagePoolMgr
- .getStoragePoolByURI(secondaryStorageUrl + volumePath);
- KVMPhysicalDisk volume = secondaryStoragePool
- .getPhysicalDisk(cmd.getVolumePath() + ".qcow2");
- _storagePoolMgr.copyPhysicalDisk(volume, volumeName,
- primaryPool);
- return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
- }
- } catch (CloudRuntimeException e) {
- return new CopyVolumeAnswer(cmd, false, e.toString(), null, null);
- } finally {
- if (secondaryStoragePool != null) {
- secondaryStoragePool.delete();
- }
- }
- }
-
- protected Answer execute(DeleteStoragePoolCommand cmd) {
- try {
- _storagePoolMgr.deleteStoragePool(cmd.getPool().getUuid());
- return new Answer(cmd);
- } catch (CloudRuntimeException e) {
- return new Answer(cmd, false, e.toString());
- }
- }
-
- protected FenceAnswer execute(FenceCommand cmd) {
- ExecutorService executors = Executors.newSingleThreadExecutor();
- List<NfsStoragePool> pools = _monitor.getStoragePools();
- KVMHAChecker ha = new KVMHAChecker(pools, cmd.getHostIp());
- Future<Boolean> future = executors.submit(ha);
- try {
- Boolean result = future.get();
- if (result) {
- return new FenceAnswer(cmd, false, "Heart is still beating...");
- } else {
- return new FenceAnswer(cmd);
- }
- } catch (InterruptedException e) {
- s_logger.warn("Unable to fence", e);
- return new FenceAnswer(cmd, false, e.getMessage());
- } catch (ExecutionException e) {
- s_logger.warn("Unable to fence", e);
- return new FenceAnswer(cmd, false, e.getMessage());
- }
-
- }
-
- protected Storage.StorageResourceType getStorageResourceType() {
- return Storage.StorageResourceType.STORAGE_POOL;
- }
-
- protected Answer execute(CreateCommand cmd) {
- StorageFilerTO pool = cmd.getPool();
- DiskProfile dskch = cmd.getDiskCharacteristics();
- KVMPhysicalDisk BaseVol = null;
- KVMStoragePool primaryPool = null;
- KVMPhysicalDisk vol = null;
- long disksize;
- try {
- primaryPool = _storagePoolMgr.getStoragePool(pool.getUuid());
-
- if (cmd.getTemplateUrl() != null) {
-
- BaseVol = primaryPool.getPhysicalDisk(cmd.getTemplateUrl());
- vol = _storagePoolMgr.createDiskFromTemplate(BaseVol, UUID
- .randomUUID().toString(), primaryPool);
-
- if (vol == null) {
- return new Answer(cmd, false,
- " Can't create storage volume on storage pool");
- }
- disksize = vol.getSize();
- } else {
- disksize = dskch.getSize();
- vol = primaryPool.createPhysicalDisk(UUID.randomUUID()
- .toString(), dskch.getSize());
- }
- VolumeTO volume = new VolumeTO(cmd.getVolumeId(), dskch.getType(),
- pool.getType(), pool.getUuid(), pool.getPath(),
- vol.getName(), vol.getName(), disksize, null);
- return new CreateAnswer(cmd, volume);
- } catch (CloudRuntimeException e) {
- s_logger.debug("Failed to create volume: " + e.toString());
- return new CreateAnswer(cmd, e);
- }
- }
-
- public Answer execute(DestroyCommand cmd) {
- VolumeTO vol = cmd.getVolume();
-
- try {
- KVMStoragePool pool = _storagePoolMgr.getStoragePool(vol
- .getPoolUuid());
- pool.deletePhysicalDisk(vol.getPath());
-
- return new Answer(cmd, true, "Success");
- } catch (CloudRuntimeException e) {
- s_logger.debug("Failed to delete volume: " + e.toString());
- return new Answer(cmd, false, e.toString());
- }
- }
-
- private String getVlanIdFromBridge(String brName) {
- OutputInterpreter.OneLineParser vlanIdParser = new OutputInterpreter.OneLineParser();
- final Script cmd = new Script("/bin/bash", s_logger);
- cmd.add("-c");
- cmd.add("vlanid=$(brctl show |grep " + brName
- + " |awk '{print $4}' | cut -s -d. -f 2);echo $vlanid");
- String result = cmd.execute(vlanIdParser);
- if (result != null) {
- return null;
- }
- String vlanId = vlanIdParser.getLine();
- if (vlanId.equalsIgnoreCase("")) {
- return null;
- } else {
- return vlanId;
- }
- }
-
- private void VifHotPlug(Connect conn, String vmName, String vlanId,
- String macAddr) throws InternalErrorException, LibvirtException {
- NicTO nicTO = new NicTO();
- nicTO.setMac(macAddr);
- nicTO.setType(TrafficType.Public);
- if (vlanId == null) {
- nicTO.setBroadcastType(BroadcastDomainType.Native);
- } else {
- nicTO.setBroadcastType(BroadcastDomainType.Vlan);
- nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
- }
+ } 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;
+ }
+
+ private Pair<String, String> getPifs() {
+ /* get pifs from bridge */
+ String pubPif = null;
+ String privPif = null;
+ String vlan = null;
+ if (_publicBridgeName != null) {
+ pubPif = Script.runSimpleBashScript("brctl show | grep "
+ + _publicBridgeName + " | awk '{print $4}'");
+ vlan = Script.runSimpleBashScript("ls /proc/net/vlan/" + pubPif);
+ if (vlan != null && !vlan.isEmpty()) {
+ pubPif = Script
+ .runSimpleBashScript("grep ^Device\\: /proc/net/vlan/"
+ + pubPif + " | awk {'print $2'}");
+ }
+ }
+ if (_guestBridgeName != null) {
+ privPif = Script.runSimpleBashScript("brctl show | grep "
+ + _guestBridgeName + " | awk '{print $4}'");
+ vlan = Script.runSimpleBashScript("ls /proc/net/vlan/" + privPif);
+ if (vlan != null && !vlan.isEmpty()) {
+ privPif = Script
+ .runSimpleBashScript("grep ^Device\\: /proc/net/vlan/"
+ + privPif + " | awk {'print $2'}");
+ }
+ }
+ return new Pair<String, String>(privPif, pubPif);
+ }
+
+ private boolean checkNetwork(String networkName) {
+ if (networkName == null) {
+ return true;
+ }
+
+ String name = Script.runSimpleBashScript("brctl show | grep "
+ + networkName + " | awk '{print $4}'");
+ if (name == null) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ private String getVnetId(String vnetId) {
+ return vnetId;
+ }
+
+ private void patchSystemVm(String cmdLine, String dataDiskPath,
+ String vmName) throws InternalErrorException {
+ String result;
+ final Script command = new Script(_patchdomrPath, _timeout, s_logger);
+ command.add("-l", vmName);
+ command.add("-t", "all");
+ command.add("-d", dataDiskPath);
+ command.add("-p", cmdLine.replaceAll(" ", "%"));
+ result = command.execute();
+ if (result != null) {
+ throw new InternalErrorException(result);
+ }
+ }
+
+ boolean isDirectAttachedNetwork(String type) {
+ if ("untagged".equalsIgnoreCase(type)) {
+ return true;
+ } else {
+ try {
+ Long.valueOf(type);
+ } catch (NumberFormatException e) {
+ return true;
+ }
+ return false;
+ }
+ }
+
+ protected String startDomain(Connect conn, String vmName, String domainXML)
+ throws LibvirtException, InternalErrorException {
+ /* No duplicated vm, we will success, or failed */
+ boolean failed = false;
+ Domain dm = null;
+ try {
+ dm = conn.domainDefineXML(domainXML);
+ } catch (final LibvirtException e) {
+ /* Duplicated defined vm */
+ s_logger.warn("Failed to define domain " + vmName + ": "
+ + e.getMessage());
+ failed = true;
+ } finally {
+ try {
+ if (dm != null) {
+ dm.free();
+ }
+ } catch (final LibvirtException e) {
+
+ }
+ }
+
+ /* If failed, undefine the vm */
+ Domain dmOld = null;
+ Domain dmNew = null;
+ try {
+ if (failed) {
+ dmOld = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
+ .getBytes()));
+ dmOld.undefine();
+ dmNew = conn.domainDefineXML(domainXML);
+ }
+ } catch (final LibvirtException e) {
+ s_logger.warn("Failed to define domain (second time) " + vmName
+ + ": " + e.getMessage());
+ throw e;
+ } catch (Exception e) {
+ s_logger.warn("Failed to define domain (second time) " + vmName
+ + ": " + e.getMessage());
+ throw new InternalErrorException(e.toString());
+ } finally {
+ try {
+ if (dmOld != null) {
+ dmOld.free();
+ }
+ if (dmNew != null) {
+ dmNew.free();
+ }
+ } catch (final LibvirtException e) {
+
+ }
+ }
+
+ /* Start the VM */
+ try {
+ dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName
+ .getBytes()));
+ dm.create();
+ } catch (LibvirtException e) {
+ s_logger.warn("Failed to start domain: " + vmName + ": "
+ + e.getMessage());
+ throw e;
+ } finally {
+ try {
+ if (dm != null) {
+ dm.free();
+ }
+ } catch (final LibvirtException e) {
+
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public boolean stop() {
+ try {
+ Connect conn = LibvirtConnection.getConnection();
+ conn.close();
+ } catch (LibvirtException e) {
+ }
+
+ return true;
+ }
+
+ public static void main(String[] args) {
+ s_logger.addAppender(new org.apache.log4j.ConsoleAppender(
+ new org.apache.log4j.PatternLayout(), "System.out"));
+ LibvirtComputingResource test = new LibvirtComputingResource();
+ Map<String, Object> params = new HashMap<String, Object>();
+ try {
+ test.configure("test", params);
+ } catch (ConfigurationException e) {
+ System.out.println(e.getMessage());
+ e.printStackTrace();
+ }
+ String result = null;
+ // String result = test.startDomainRouter("domr1",
+ // "/var/lib/images/centos.5-4.x86-64/centos-small.img", 128, "0064",
+ // "02:00:30:00:01:01", "00:16:3e:77:e2:a1", "02:00:30:00:64:01");
+ boolean created = (result == null);
+ s_logger.info("Domain " + (created ? " " : " not ") + " created");
+
+ s_logger.info("Rule " + (created ? " " : " not ") + " created");
+ test.stop();
+ }
+
+ @Override
+ public Answer executeRequest(Command cmd) {
+
+ try {
+ if (cmd instanceof StopCommand) {
+ return execute((StopCommand) cmd);
+ } else if (cmd instanceof GetVmStatsCommand) {
+ return execute((GetVmStatsCommand) cmd);
+ } else if (cmd instanceof RebootRouterCommand) {
+ return execute((RebootRouterCommand) cmd);
+ } else if (cmd instanceof RebootCommand) {
+ return execute((RebootCommand) cmd);
+ } else if (cmd instanceof GetHostStatsCommand) {
+ return execute((GetHostStatsCommand) cmd);
+ } else if (cmd instanceof CheckStateCommand) {
+ return executeRequest(cmd);
+ } else if (cmd instanceof CheckHealthCommand) {
+ return execute((CheckHealthCommand) cmd);
+ } else if (cmd instanceof PrepareForMigrationCommand) {
+ return execute((PrepareForMigrationCommand) cmd);
+ } else if (cmd instanceof MigrateCommand) {
+ return execute((MigrateCommand) cmd);
+ } else if (cmd instanceof PingTestCommand) {
+ return execute((PingTestCommand) cmd);
+ } else if (cmd instanceof CheckVirtualMachineCommand) {
+ return execute((CheckVirtualMachineCommand) cmd);
+ } else if (cmd instanceof ReadyCommand) {
+ return execute((ReadyCommand) cmd);
+ } else if (cmd instanceof AttachIsoCommand) {
+ return execute((AttachIsoCommand) cmd);
+ } else if (cmd instanceof AttachVolumeCommand) {
+ return execute((AttachVolumeCommand) cmd);
+ } else if (cmd instanceof StopCommand) {
+ return execute((StopCommand) cmd);
+ } else if (cmd instanceof CheckConsoleProxyLoadCommand) {
+ return execute((CheckConsoleProxyLoadCommand) cmd);
+ } else if (cmd instanceof WatchConsoleProxyLoadCommand) {
+ return execute((WatchConsoleProxyLoadCommand) cmd);
+ } else if (cmd instanceof GetVncPortCommand) {
+ return execute((GetVncPortCommand) cmd);
+ } else if (cmd instanceof ModifySshKeysCommand) {
+ return execute((ModifySshKeysCommand) cmd);
+ } else if (cmd instanceof MaintainCommand) {
+ return execute((MaintainCommand) cmd);
+ } else if (cmd instanceof CreateCommand) {
+ return execute((CreateCommand) cmd);
+ } else if (cmd instanceof DestroyCommand) {
+ return execute((DestroyCommand) cmd);
+ } else if (cmd instanceof PrimaryStorageDownloadCommand) {
+ return execute((PrimaryStorageDownloadCommand) cmd);
+ } else if (cmd instanceof CreatePrivateTemplateFromVolumeCommand) {
+ return execute((CreatePrivateTemplateFromVolumeCommand) cmd);
+ } else if (cmd instanceof GetStorageStatsCommand) {
+ return execute((GetStorageStatsCommand) cmd);
+ } else if (cmd instanceof ManageSnapshotCommand) {
+ return execute((ManageSnapshotCommand) cmd);
+ } else if (cmd instanceof BackupSnapshotCommand) {
+ return execute((BackupSnapshotCommand) cmd);
+ } else if (cmd instanceof CreateVolumeFromSnapshotCommand) {
+ return execute((CreateVolumeFromSnapshotCommand) cmd);
+ } else if (cmd instanceof CreatePrivateTemplateFromSnapshotCommand) {
+ return execute((CreatePrivateTemplateFromSnapshotCommand) cmd);
+ } else if (cmd instanceof UpgradeSnapshotCommand) {
+ return execute((UpgradeSnapshotCommand) cmd);
+ } else if (cmd instanceof CreateStoragePoolCommand) {
+ return execute((CreateStoragePoolCommand) cmd);
+ } else if (cmd instanceof ModifyStoragePoolCommand) {
+ return execute((ModifyStoragePoolCommand) cmd);
+ } else if (cmd instanceof SecurityGroupRulesCmd) {
+ return execute((SecurityGroupRulesCmd) cmd);
+ } else if (cmd instanceof DeleteStoragePoolCommand) {
+ return execute((DeleteStoragePoolCommand) cmd);
+ } else if (cmd instanceof FenceCommand) {
+ return execute((FenceCommand) cmd);
+ } else if (cmd instanceof StartCommand) {
+ return execute((StartCommand) cmd);
+ } else if (cmd instanceof IpAssocCommand) {
+ return execute((IpAssocCommand) cmd);
+ } else if (cmd instanceof NetworkElementCommand) {
+ return _virtRouterResource.executeRequest(cmd);
+ } else if (cmd instanceof CheckSshCommand) {
+ return execute((CheckSshCommand) cmd);
+ } else if (cmd instanceof NetworkUsageCommand) {
+ return execute((NetworkUsageCommand) cmd);
+ } else if (cmd instanceof NetworkRulesSystemVmCommand) {
+ return execute((NetworkRulesSystemVmCommand) cmd);
+ } else if (cmd instanceof CleanupNetworkRulesCmd) {
+ return execute((CleanupNetworkRulesCmd) cmd);
+ } else if (cmd instanceof CopyVolumeCommand) {
+ return execute((CopyVolumeCommand) cmd);
+ } else if (cmd instanceof CheckNetworkCommand) {
+ return execute((CheckNetworkCommand) cmd);
+ } else {
+ s_logger.warn("Unsupported command ");
+ return Answer.createUnsupportedCommandAnswer(cmd);
+ }
+ } catch (final IllegalArgumentException e) {
+ return new Answer(cmd, false, e.getMessage());
+ }
+ }
+
+ private CheckNetworkAnswer execute(CheckNetworkCommand cmd) {
+ List<PhysicalNetworkSetupInfo> phyNics = cmd
+ .getPhysicalNetworkInfoList();
+ String errMsg = null;
+ for (PhysicalNetworkSetupInfo nic : phyNics) {
+ if (!checkNetwork(nic.getGuestNetworkName())) {
+ errMsg = "Can not find network: " + nic.getGuestNetworkName();
+ break;
+ } else if (!checkNetwork(nic.getPrivateNetworkName())) {
+ errMsg = "Can not find network: " + nic.getPrivateNetworkName();
+ break;
+ } else if (!checkNetwork(nic.getPublicNetworkName())) {
+ errMsg = "Can not find network: " + nic.getPublicNetworkName();
+ break;
+ }
+ }
+
+ if (errMsg != null) {
+ return new CheckNetworkAnswer(cmd, false, errMsg);
+ } else {
+ return new CheckNetworkAnswer(cmd, true, null);
+ }
+ }
+
+ private CopyVolumeAnswer execute(CopyVolumeCommand cmd) {
+ boolean copyToSecondary = cmd.toSecondaryStorage();
+ String volumePath = cmd.getVolumePath();
+ StorageFilerTO pool = cmd.getPool();
+ String secondaryStorageUrl = cmd.getSecondaryStorageURL();
+ KVMStoragePool secondaryStoragePool = null;
+ try {
+ KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(pool
+ .getUuid());
+ String volumeName = UUID.randomUUID().toString();
+
+ if (copyToSecondary) {
+ String destVolumeName = volumeName + ".qcow2";
+ KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(cmd
+ .getVolumePath());
+ String volumeDestPath = "/volumes/" + cmd.getVolumeId()
+ + File.separator;
+ secondaryStoragePool = _storagePoolMgr
+ .getStoragePoolByURI(secondaryStorageUrl);
+ secondaryStoragePool.createFolder(volumeDestPath);
+ secondaryStoragePool.delete();
+ secondaryStoragePool = _storagePoolMgr
+ .getStoragePoolByURI(secondaryStorageUrl
+ + volumeDestPath);
+ _storagePoolMgr.copyPhysicalDisk(volume, destVolumeName,
+ secondaryStoragePool);
+ return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
+ } else {
+ volumePath = "/volumes/" + cmd.getVolumeId() + File.separator;
+ secondaryStoragePool = _storagePoolMgr
+ .getStoragePoolByURI(secondaryStorageUrl + volumePath);
+ KVMPhysicalDisk volume = secondaryStoragePool
+ .getPhysicalDisk(cmd.getVolumePath() + ".qcow2");
+ _storagePoolMgr.copyPhysicalDisk(volume, volumeName,
+ primaryPool);
+ return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
+ }
+ } catch (CloudRuntimeException e) {
+ return new CopyVolumeAnswer(cmd, false, e.toString(), null, null);
+ } finally {
+ if (secondaryStoragePool != null) {
+ secondaryStoragePool.delete();
+ }
+ }
+ }
+
+ protected Answer execute(DeleteStoragePoolCommand cmd) {
+ try {
+ _storagePoolMgr.deleteStoragePool(cmd.getPool().getUuid());
+ return new Answer(cmd);
+ } catch (CloudRuntimeException e) {
+ return new Answer(cmd, false, e.toString());
+ }
+ }
+
+ protected FenceAnswer execute(FenceCommand cmd) {
+ ExecutorService executors = Executors.newSingleThreadExecutor();
+ List<NfsStoragePool> pools = _monitor.getStoragePools();
+ KVMHAChecker ha = new KVMHAChecker(pools, cmd.getHostIp());
+ Future<Boolean> future = executors.submit(ha);
+ try {
+ Boolean result = future.get();
+ if (result) {
+ return new FenceAnswer(cmd, false, "Heart is still beating...");
+ } else {
+ return new FenceAnswer(cmd);
+ }
+ } catch (InterruptedException e) {
+ s_logger.warn("Unable to fence", e);
+ return new FenceAnswer(cmd, false, e.getMessage());
+ } catch (ExecutionException e) {
+ s_logger.warn("Unable to fence", e);
+ return new FenceAnswer(cmd, false, e.getMessage());
+ }
+
+ }
+
+ protected Storage.StorageResourceType getStorageResourceType() {
+ return Storage.StorageResourceType.STORAGE_POOL;
+ }
+
+ protected Answer execute(CreateCommand cmd) {
+ StorageFilerTO pool = cmd.getPool();
+ DiskProfile dskch = cmd.getDiskCharacteristics();
+ KVMPhysicalDisk BaseVol = null;
+ KVMStoragePool primaryPool = null;
+ KVMPhysicalDisk vol = null;
+ long disksize;
+ try {
+ primaryPool = _storagePoolMgr.getStoragePool(pool.getUuid());
+
+ if (cmd.getTemplateUrl() != null) {
+
+ BaseVol = primaryPool.getPhysicalDisk(cmd.getTemplateUrl());
+ vol = _storagePoolMgr.createDiskFromTemplate(BaseVol, UUID
+ .randomUUID().toString(), primaryPool);
+
+ if (vol == null) {
+ return new Answer(cmd, false,
+ " Can't create storage volume on storage pool");
+ }
+ disksize = vol.getSize();
+ } else {
+ disksize = dskch.getSize();
+ vol = primaryPool.createPhysicalDisk(UUID.randomUUID()
+ .toString(), dskch.getSize());
+ }
+ VolumeTO volume = new VolumeTO(cmd.getVolumeId(), dskch.getType(),
+ pool.getType(), pool.getUuid(), pool.getPath(),
+ vol.getName(), vol.getName(), disksize, null);
+ return new CreateAnswer(cmd, volume);
+ } catch (CloudRuntimeException e) {
+ s_logger.debug("Failed to create volume: " + e.toString());
+ return new CreateAnswer(cmd, e);
+ }
+ }
+
+ public Answer execute(DestroyCommand cmd) {
+ VolumeTO vol = cmd.getVolume();
+
+ try {
+ KVMStoragePool pool = _storagePoolMgr.getStoragePool(vol
+ .getPoolUuid());
+ pool.deletePhysicalDisk(vol.getPath());
+
+ return new Answer(cmd, true, "Success");
+ } catch (CloudRuntimeException e) {
+ s_logger.debug("Failed to delete volume: " + e.toString());
+ return new Answer(cmd, false, e.toString());
+ }
+ }
+
+ private String getVlanIdFromBridge(String brName) {
+ OutputInterpreter.OneLineParser vlanIdParser = new OutputInterpreter.OneLineParser();
+ final Script cmd = new Script("/bin/bash", s_logger);
+ cmd.add("-c");
+ cmd.add("vlanid=$(brctl show |grep " + brName
+ + " |awk '{print $4}' | cut -s -d. -f 2);echo $vlanid");
+ String result = cmd.execute(vlanIdParser);
+ if (result != null) {
+ return null;
+ }
+ String vlanId = vlanIdParser.getLine();
+ if (vlanId.equalsIgnoreCase("")) {
+ return null;
+ } else {
+ return vlanId;
+ }
+ }
+
+ private void VifHotPlug(Connect conn, String vmName, String vlanId,
+ String macAddr) throws InternalErrorException, LibvirtException {
+ NicTO nicTO = new NicTO();
+ nicTO.setMac(macAddr);
+ nicTO.setType(TrafficType.Public);
+ if (vlanId == null) {
+ nicTO.setBroadcastType(BroadcastDomainType.Native);
+ } else {
+ nicTO.setBroadcastType(BroadcastDomainType.Vlan);
+ nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId));
+ }
- InterfaceDef nic = createVif(conn, nicTO, InterfaceDef.nicModel.VIRTIO);
+ InterfaceDef nic = createVif(nicTO, InterfaceDef.nicModel.VIRTIO);
- Domain vm = getDomain(conn, vmName);
- vm.attachDevice(nic.toString());
- }
-
- public Answer execute(IpAssocCommand cmd) {
- String routerName = cmd
- .getAccessDetail(NetworkElementCommand.ROUTER_NAME);
- String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
- String[] results = new String[cmd.getIpAddresses().length];
- Connect conn;
- try {
- conn = LibvirtConnection.getConnection();
- List<InterfaceDef> nics = getInterfaces(conn, routerName);
- Map<String, Integer> vlanAllocatedToVM = new HashMap<String, Integer>();
- Integer nicPos = 0;
- for (InterfaceDef nic : nics) {
- if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
- vlanAllocatedToVM.put("LinkLocal", nicPos);
- } else {
- String vlanId = getVlanIdFromBridge(nic.getBrName());
- if (vlanId != null) {
- vlanAllocatedToVM.put(vlanId, nicPos);
- } else {
- vlanAllocatedToVM.put(Vlan.UNTAGGED, nicPos);
- }
- }
- nicPos++;
- }
- IpAddressTO[] ips = cmd.getIpAddresses();
- int i = 0;
- String result = null;
- int nicNum = 0;
- for (IpAddressTO ip : ips) {
- if (!vlanAllocatedToVM.containsKey(ip.getVlanId())) {
- /* plug a vif into router */
- VifHotPlug(conn, routerName, ip.getVlanId(),
- ip.getVifMacAddress());
- vlanAllocatedToVM.put(ip.getVlanId(), nicPos++);
- }
- nicNum = vlanAllocatedToVM.get(ip.getVlanId());
- networkUsage(routerIp, "addVif", "eth" + nicNum);
- result = _virtRouterResource.assignPublicIpAddress(routerName,
- routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(),
- ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(),
- ip.getVlanNetmask(), ip.getVifMacAddress(),
- ip.getGuestIp(), nicNum);
-
- if (result != null) {
- results[i++] = IpAssocAnswer.errorResult;
- } else {
- results[i++] = ip.getPublicIp() + " - success";
- ;
- }
- }
- return new IpAssocAnswer(cmd, results);
- } catch (LibvirtException e) {
- return new IpAssocAnswer(cmd, results);
- } catch (InternalErrorException e) {
- return new IpAssocAnswer(cmd, results);
- }
- }
-
- protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) {
- String snapshotName = cmd.getSnapshotName();
- String snapshotPath = cmd.getSnapshotPath();
- String vmName = cmd.getVmName();
- try {
- Connect conn = LibvirtConnection.getConnection();
- DomainInfo.DomainState state = null;
- Domain vm = null;
- if (vmName != null) {
- try {
- vm = getDomain(conn, cmd.getVmName());
- state = vm.getInfo().state;
- } catch (LibvirtException e) {
-
- }
- }
-
- KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd
- .getPool().getUuid());
+ Domain vm = getDomain(conn, vmName);
+ vm.attachDevice(nic.toString());
+ }
+
+ public Answer execute(IpAssocCommand cmd) {
+ String routerName = cmd
+ .getAccessDetail(NetworkElementCommand.ROUTER_NAME);
+ String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP);
+ String[] results = new String[cmd.getIpAddresses().length];
+ Connect conn;
+ try {
+ conn = LibvirtConnection.getConnection();
+ List<InterfaceDef> nics = getInterfaces(conn, routerName);
+ Map<String, Integer> vlanAllocatedToVM = new HashMap<String, Integer>();
+ Integer nicPos = 0;
+ for (InterfaceDef nic : nics) {
+ if (nic.getBrName().equalsIgnoreCase(_linkLocalBridgeName)) {
+ vlanAllocatedToVM.put("LinkLocal", nicPos);
+ } else {
+ String vlanId = getVlanIdFromBridge(nic.getBrName());
+ if (vlanId != null) {
+ vlanAllocatedToVM.put(vlanId, nicPos);
+ } else {
+ vlanAllocatedToVM.put(Vlan.UNTAGGED, nicPos);
+ }
+ }
+ nicPos++;
+ }
+ IpAddressTO[] ips = cmd.getIpAddresses();
+ int i = 0;
+ String result = null;
+ int nicNum = 0;
+ for (IpAddressTO ip : ips) {
+ if (!vlanAllocatedToVM.containsKey(ip.getVlanId())) {
+ /* plug a vif into router */
+ VifHotPlug(conn, routerName, ip.getVlanId(),
+ ip.getVifMacAddress());
+ vlanAllocatedToVM.put(ip.getVlanId(), nicPos++);
+ }
+ nicNum = vlanAllocatedToVM.get(ip.getVlanId());
+ networkUsage(routerIp, "addVif", "eth" + nicNum);
+ result = _virtRouterResource.assignPublicIpAddress(routerName,
+ routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(),
+ ip.isSourceNat(), ip.getVlanId(), ip.getVlanGateway(),
+ ip.getVlanNetmask(), ip.getVifMacAddress(),
+ ip.getGuestIp(), nicNum);
+
+ if (result != null) {
+ results[i++] = IpAssocAnswer.errorResult;
+ } else {
+ results[i++] = ip.getPublicIp() + " - success";
+ ;
+ }
+ }
+ return new IpAssocAnswer(cmd, results);
+ } catch (LibvirtException e) {
+ return new IpAssocAnswer(cmd, results);
+ } catch (InternalErrorException e) {
+ return new IpAssocAnswer(cmd, results);
+ }
+ }
+
+ protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) {
+ String snapshotName = cmd.getSnapshotName();
+ String snapshotPath = cmd.getSnapshotPath();
+ String vmName = cmd.getVmName();
+ try {
+ Connect conn = LibvirtConnection.getConnection();
+ DomainInfo.DomainState state = null;
+ Domain vm = null;
+ if (vmName != null) {
+ try {
+ vm = getDomain(conn, cmd.getVmName());
+ state = vm.getInfo().state;
+ } catch (LibvirtException e) {
+
+ }
+ }
+
+ KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd
+ .getPool().getUuid());
if (primaryPool.getType() == StoragePoolType.RBD) {
s_logger.debug("Snapshots are not supported on RBD volumes");
@@@ -1694,960 -1694,958 +1694,960 @@@
templFo.close();
}
- Map<String, Object> params = new HashMap<String, Object>();
- params.put(StorageLayer.InstanceConfigKey, _storage);
- Processor qcow2Processor = new QCOW2Processor();
-
- qcow2Processor.configure("QCOW2 Processor", params);
-
- FormatInfo info = qcow2Processor.process(tmpltPath, null,
- cmd.getUniqueName());
-
- TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
- loc.create(1, true, cmd.getUniqueName());
- loc.addFormat(info);
- loc.save();
-
- return new CreatePrivateTemplateAnswer(cmd, true, null,
- templateInstallFolder + cmd.getUniqueName() + ".qcow2",
- info.virtualSize, info.size, cmd.getUniqueName(),
- ImageFormat.QCOW2);
- } catch (LibvirtException e) {
- s_logger.debug("Failed to get secondary storage pool: "
- + e.toString());
- return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
- } catch (InternalErrorException e) {
- return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
- } catch (IOException e) {
- return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
- } catch (ConfigurationException e) {
- return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
- } catch (CloudRuntimeException e) {
- return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
- } finally {
- if (secondaryStorage != null) {
- secondaryStorage.delete();
- }
- }
- }
-
- protected PrimaryStorageDownloadAnswer execute(
- final PrimaryStorageDownloadCommand cmd) {
- String tmplturl = cmd.getUrl();
- int index = tmplturl.lastIndexOf("/");
- String mountpoint = tmplturl.substring(0, index);
- String tmpltname = null;
- if (index < tmplturl.length() - 1) {
- tmpltname = tmplturl.substring(index + 1);
- }
-
- KVMPhysicalDisk tmplVol = null;
- KVMStoragePool secondaryPool = null;
- try {
- secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
-
- /* Get template vol */
- if (tmpltname == null) {
- secondaryPool.refresh();
- List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
- if (disks == null || disks.isEmpty()) {
- return new PrimaryStorageDownloadAnswer(
- "Failed to get volumes from pool: "
- + secondaryPool.getUuid());
- }
- for (KVMPhysicalDisk disk : disks) {
- if (disk.getName().endsWith("qcow2")) {
- tmplVol = disk;
- break;
- }
- }
- if (tmplVol == null) {
- return new PrimaryStorageDownloadAnswer(
- "Failed to get template from pool: "
- + secondaryPool.getUuid());
- }
- } else {
- tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
- }
-
- /* Copy volume to primary storage */
- KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd
- .getPoolUuid());
-
- KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(
- tmplVol, UUID.randomUUID().toString(), primaryPool);
-
- return new PrimaryStorageDownloadAnswer(primaryVol.getName(),
- primaryVol.getSize());
- } catch (CloudRuntimeException e) {
- return new PrimaryStorageDownloadAnswer(e.toString());
- } finally {
- if (secondaryPool != null) {
- secondaryPool.delete();
- }
- }
- }
-
- protected Answer execute(CreateStoragePoolCommand cmd) {
- return new Answer(cmd, true, "success");
- }
-
- protected Answer execute(ModifyStoragePoolCommand cmd) {
- KVMStoragePool storagepool = _storagePoolMgr.createStoragePool(cmd
+ Map<String, Object> params = new HashMap<String, Object>();
+ params.put(StorageLayer.InstanceConfigKey, _storage);
+ Processor qcow2Processor = new QCOW2Processor();
+
+ qcow2Processor.configure("QCOW2 Processor", params);
+
+ FormatInfo info = qcow2Processor.process(tmpltPath, null,
+ cmd.getUniqueName());
+
+ TemplateLocation loc = new TemplateLocation(_storage, tmpltPath);
+ loc.create(1, true, cmd.getUniqueName());
+ loc.addFormat(info);
+ loc.save();
+
+ return new CreatePrivateTemplateAnswer(cmd, true, null,
+ templateInstallFolder + cmd.getUniqueName() + ".qcow2",
+ info.virtualSize, info.size, cmd.getUniqueName(),
+ ImageFormat.QCOW2);
+ } catch (LibvirtException e) {
+ s_logger.debug("Failed to get secondary storage pool: "
+ + e.toString());
+ return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
+ } catch (InternalErrorException e) {
+ return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
+ } catch (IOException e) {
+ return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
+ } catch (ConfigurationException e) {
+ return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
+ } catch (CloudRuntimeException e) {
+ return new CreatePrivateTemplateAnswer(cmd, false, e.toString());
+ } finally {
+ if (secondaryStorage != null) {
+ secondaryStorage.delete();
+ }
+ }
+ }
+
+ protected PrimaryStorageDownloadAnswer execute(
+ final PrimaryStorageDownloadCommand cmd) {
+ String tmplturl = cmd.getUrl();
+ int index = tmplturl.lastIndexOf("/");
+ String mountpoint = tmplturl.substring(0, index);
+ String tmpltname = null;
+ if (index < tmplturl.length() - 1) {
+ tmpltname = tmplturl.substring(index + 1);
+ }
+
+ KVMPhysicalDisk tmplVol = null;
+ KVMStoragePool secondaryPool = null;
+ try {
+ secondaryPool = _storagePoolMgr.getStoragePoolByURI(mountpoint);
+
+ /* Get template vol */
+ if (tmpltname == null) {
+ secondaryPool.refresh();
+ List<KVMPhysicalDisk> disks = secondaryPool.listPhysicalDisks();
+ if (disks == null || disks.isEmpty()) {
+ return new PrimaryStorageDownloadAnswer(
+ "Failed to get volumes from pool: "
+ + secondaryPool.getUuid());
+ }
+ for (KVMPhysicalDisk disk : disks) {
+ if (disk.getName().endsWith("qcow2")) {
+ tmplVol = disk;
+ break;
+ }
+ }
+ if (tmplVol == null) {
+ return new PrimaryStorageDownloadAnswer(
+ "Failed to get template from pool: "
+ + secondaryPool.getUuid());
+ }
+ } else {
+ tmplVol = secondaryPool.getPhysicalDisk(tmpltname);
+ }
+
+ /* Copy volume to primary storage */
+ KVMStoragePool primaryPool = _storagePoolMgr.getStoragePool(cmd
+ .getPoolUuid());
+
+ KVMPhysicalDisk primaryVol = _storagePoolMgr.copyPhysicalDisk(
+ tmplVol, UUID.randomUUID().toString(), primaryPool);
+
+ return new PrimaryStorageDownloadAnswer(primaryVol.getName(),
+ primaryVol.getSize());
+ } catch (CloudRuntimeException e) {
+ return new PrimaryStorageDownloadAnswer(e.toString());
+ } finally {
+ if (secondaryPool != null) {
<TRUNCATED>
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/ce9f1afa/server/src/com/cloud/configuration/Config.java
----------------------------------------------------------------------