[Esbox-commits] r1589 - in trunk: org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox
eswartz at garage.maemo.org
eswartz at garage.maemo.org
Tue May 12 22:28:41 EEST 2009
Author: eswartz
Date: 2009-05-12 22:28:40 +0300 (Tue, 12 May 2009)
New Revision: 1589
Added:
trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/TestVirtualBoxUtils.java
Modified:
trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/AllTests.java
trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java
trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java
Log:
Merge revs 1586:1588 from work_Ed branch
Modified: trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/AllTests.java
===================================================================
--- trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/AllTests.java 2009-05-12 19:26:22 UTC (rev 1588)
+++ trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/AllTests.java 2009-05-12 19:28:40 UTC (rev 1589)
@@ -25,6 +25,7 @@
//$JUnit-BEGIN$
//$JUnit-END$
suite.addTest(new JUnit4TestAdapter(TestVMPreferences.class));
+ suite.addTest(new JUnit4TestAdapter(TestVirtualBoxUtils.class));
return suite;
}
Copied: trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/TestVirtualBoxUtils.java (from rev 1588, branches/work_Ed/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/TestVirtualBoxUtils.java)
===================================================================
--- trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/TestVirtualBoxUtils.java (rev 0)
+++ trunk/org.maemo.esbox.vm.tests/src/org/maemo/esbox/vm/tests/TestVirtualBoxUtils.java 2009-05-12 19:28:40 UTC (rev 1589)
@@ -0,0 +1,77 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Nokia Corporation
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ed Swartz (Nokia) - initial API and implementation
+ *******************************************************************************/
+
+package org.maemo.esbox.vm.tests;
+
+import static org.junit.Assert.*;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+import org.maemo.esbox.internal.vm.virtualbox.VirtualBoxUtils;
+import org.maemo.esbox.internal.vm.virtualbox.VirtualBoxUtils.VBoxNetworkInterface;
+
+/**
+ * @author eswartz
+ *
+ */
+public class TestVirtualBoxUtils {
+ @Test
+ public void testNetworkInterfaces() {
+ Map<String, String> info = new HashMap<String, String>();
+
+ info.put("NIC 1", "MAC: 080027C8E004, Attachment: NAT, Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps");
+
+ VBoxNetworkInterface[] ifs = VirtualBoxUtils.getNetworkInterfaceInfo(info);
+
+ assertEquals(1, ifs.length);
+
+ VBoxNetworkInterface i = ifs[0];
+ assertEquals(VirtualBoxUtils.NETWORK_CONFIG_NAT, i.getNetworkType());
+ assertEquals("", i.getNetworkInterfaceName());
+
+ ////
+
+ info.put("NIC 5", "MAC: 080027C8E004, Attachment: Host Interface 'en0: Ethernet', Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps");
+
+ ifs = VirtualBoxUtils.getNetworkInterfaceInfo(info);
+
+ assertEquals(2, ifs.length); // add NIC 5 at end
+ i = ifs[1];
+
+ assertEquals(VirtualBoxUtils.NETWORK_CONFIG_BRIDGED, i.getNetworkType());
+ assertEquals("en0", i.getNetworkInterfaceName());
+ ////
+
+ info.put("NIC 5", "MAC: 080027E40803, Attachment: Bridged Interface 'Intel(R) PRO/1000 PL Network Connection', Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps");
+
+ ifs = VirtualBoxUtils.getNetworkInterfaceInfo(info);
+
+ assertEquals(2, ifs.length); // rewrote NIC 5
+ i = ifs[1];
+
+ assertEquals(VirtualBoxUtils.NETWORK_CONFIG_BRIDGED, i.getNetworkType());
+ assertEquals("Intel(R) PRO/1000 PL Network Connection", i.getNetworkInterfaceName());
+
+ ////////
+
+ info.put("NIC 2", "MAC: 0800001111, Attachment: Host-only Interface 'VirtualBox Host-Only Ethernet Adapter', Cable connected: on, Trace: off (file: none), Type: Am79C973, Reported speed: 0 Mbps");
+
+ ifs = VirtualBoxUtils.getNetworkInterfaceInfo(info);
+
+ assertEquals(3, ifs.length); // added NIC 2 between 1 and 5
+ i = ifs[1];
+
+ assertEquals(VirtualBoxUtils.NETWORK_CONFIG_HOST_ONLY, i.getNetworkType());
+ assertEquals("VirtualBox Host-Only Ethernet Adapter", i.getNetworkInterfaceName());
+ }
+}
Modified: trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java
===================================================================
--- trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java 2009-05-12 19:26:22 UTC (rev 1588)
+++ trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java 2009-05-12 19:28:40 UTC (rev 1589)
@@ -17,6 +17,7 @@
import java.util.Map;
import java.util.Set;
+import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.preference.IPreferenceStore;
import org.maemo.esbox.internal.api.vm.ui.preferences.AddressProvider;
@@ -27,10 +28,13 @@
import org.maemo.esbox.internal.api.vm.ui.preferences.IVirtualMachineSettingsPreferencePage;
import org.maemo.esbox.internal.api.vm.ui.preferences.LocalhostTargetInterfaceAddressProvider;
import org.maemo.esbox.internal.api.vm.ui.preferences.SameSubnetHostTargetAddressConciler;
+import org.maemo.esbox.internal.vm.virtualbox.VirtualBoxUtils.VBoxNetworkInterface;
import org.maemo.esbox.vm.core.IVirtualMachine;
import org.maemo.esbox.vm.core.IVirtualMachineConfiguration;
import org.maemo.esbox.vm.virtualbox.IVirtualBoxConfiguration;
import org.maemo.mica.common.core.IProgressReporter;
+import org.maemo.mica.common.core.MicaException;
+import org.maemo.mica.common.core.machine.NetworkUtils;
/**
* Configure VMware preferences. This page is NOT registered at the top level
@@ -124,37 +128,99 @@
reporter.log(Activator.createErrorStatus("Could not find any info on the machine " +
configuration.getMachineName() +"; guessing from defaults", null));
} else {
-
- boolean hasNAT = VirtualBoxUtils.machineUsesNATNetworking(machineInfo);
+ boolean detected = false;
+
+ // find the first NAT connection or a Bridged connection
+ VBoxNetworkInterface[] intfs = VirtualBoxUtils.getNetworkInterfaceInfo(machineInfo);
- if (hasNAT) {
- int foundPorts = 0;
- machineInfo = VirtualBoxUtils.getVirtualMachineExtraData(configuration);
- for (Map.Entry<String, String> entry : machineInfo.entrySet()) {
- if (entry.getKey().endsWith("guestssh/GuestPort")) {
- hostPort = entry.getValue();
- foundPorts++;
- } else if (entry.getKey().endsWith("guestssh/HostPort")) {
- targetPort = entry.getValue();
- foundPorts++;
+
+ for (VBoxNetworkInterface intf : intfs) {
+ if (intf.getNetworkType().equals(VirtualBoxUtils.NETWORK_CONFIG_NAT)) {
+ int foundPorts = 0;
+ machineInfo = VirtualBoxUtils.getVirtualMachineExtraData(configuration);
+ for (Map.Entry<String, String> entry : machineInfo.entrySet()) {
+ if (entry.getKey().endsWith("guestssh/GuestPort")) {
+ hostPort = entry.getValue();
+ foundPorts++;
+ } else if (entry.getKey().endsWith("guestssh/HostPort")) {
+ targetPort = entry.getValue();
+ foundPorts++;
+ }
}
+
+ if (foundPorts < 2) {
+ reporter.log(Activator.createErrorStatus("Did not find all the expected NAT info; guessing from defaults", null));
+ }
+
+ reporter.logInfo("Using NAT configuration");
+ reporter.logInfo("Setting Target Address to " + target);
+ detected = true;
+ break;
}
-
- if (foundPorts < 2) {
- reporter.log(Activator.createErrorStatus("Did not find all the expected NAT info; guessing from defaults", null));
+
+ if (intf.getNetworkType().equals(VirtualBoxUtils.NETWORK_CONFIG_BRIDGED)) {
+ reporter.logInfo("Using bridged networking");
+
+ InetAddress hostAddr = null;
+
+ try {
+ // get the host that matches the interface mentioned in the configuration
+ InetAddress[] locals = null;
+ try {
+ locals = NetworkUtils.getInterfaceAddressesNamed(intf.getNetworkInterfaceName());
+ } catch (MicaException e) {
+ reporter.log(Activator.createErrorStatus("Could not match up NIC configuration with network interfaces", e));
+ }
+
+ if (locals != null && locals.length > 0) {
+ hostAddr = locals[0];
+ } else {
+ // fallback
+ hostAddr = InetAddress.getLocalHost();
+ reporter.log(Activator.createErrorStatus("Could not find network adapter referenced in NIC configuration; guessing", null));
+ }
+ host = hostAddr.getHostAddress();
+
+ // assume the target address is on the same network segment
+ InetAddress targetAddr = hostAddr;
+ if (targetAddr instanceof Inet4Address) {
+ byte[] addrBytes = targetAddr.getAddress();
+ addrBytes[3] = 0;
+ targetAddr = InetAddress.getByAddress(addrBytes);
+ }
+ target = targetAddr.getHostAddress();
+ targetPort = "22";
+
+ reporter.logInfo("Set Target Address to likely DHCP address " + targetAddr);
+ reporter.log(Activator.createStatus(IStatus.WARNING,
+ "You will need to manually edit the Target Address after the VM boots, by inspecting the output of 'sudo dhclient3' or '/sbin/ifconfig'."));
+ } catch (UnknownHostException e) {
+ reporter.log(Activator.createErrorStatus(
+ "Could not guess the LAN address",
+ e));
+ }
+
+ detected = true;
+ break;
}
}
+
+ if (!detected) {
+ reporter.log(Activator.createErrorStatus(
+ "The machine either has no networking configured or none of the interfaces is appropriate for ESbox; please use NAT or Bridged/Host Interface networking. Guessing from defaults...",
+ null));
+ }
+
}
+
try {
commonPrefPage.getSSHTargetAddr().setAddress(
InetAddress.getByName(target));
- reporter.logInfo("Setting Target Address to " + target);
} catch (UnknownHostException e) {
- reporter.log(Activator.createErrorStatus("Failed to create stock " + target + "address", e));
+ reporter.log(Activator.createErrorStatus("Failed to create " + target + "address", e));
}
-
commonPrefPage.getSSHTargetPort().setStringValue(targetPort);
reporter.logInfo("Setting SSH Target Port to " + targetPort);
@@ -166,7 +232,7 @@
InetAddress.getByName(host));
reporter.logInfo("Setting Host Address to " + host);
} catch (UnknownHostException e) {
- reporter.log(Activator.createErrorStatus("Failed to create stock " + host + " address", e));
+ reporter.log(Activator.createErrorStatus("Failed to create " + host + " address", e));
}
}
Modified: trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java
===================================================================
--- trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java 2009-05-12 19:26:22 UTC (rev 1588)
+++ trunk/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java 2009-05-12 19:28:40 UTC (rev 1589)
@@ -11,6 +11,7 @@
package org.maemo.esbox.internal.vm.virtualbox;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -40,8 +41,16 @@
static final Pattern namePattern = Pattern.compile("Name:\\s*(.*)");
static final Pattern nameUUID22Pattern = Pattern.compile("\"([^\"]+)\"\\s*\\{(.*)\\}");
static final Pattern uuidPattern = Pattern.compile("UUID:\\s*([0-9a-fA-F-]{36})");
- static final Pattern keyValuePattern = Pattern.compile("(.+):\\s*(.+)");
+ static final Pattern keyValuePattern = Pattern.compile("([^:]+):\\s*(.+)");
static final Pattern extradataPattern = Pattern.compile("Key:\\s*(.+),\\s*Value:\\s*(.+)");
+ public static final String NETWORK_CONFIG_NAT = "NAT";
+ public static final String NETWORK_CONFIG_BRIDGED = "bridged";
+ public static final String NETWORK_CONFIG_HOST_ONLY = "host-only";
+
+ final static Pattern nicAttachmentPattern = Pattern.compile(".*Attachment:\\s*(([^,]+)('[^']+')?),?.*");
+ // "bridged" was called "Host Interface" before 2.2
+ final static Pattern nicBridgedPattern = Pattern.compile("(?:Bridged|Host Interface)[^']*'([^']*)'");
+ final static Pattern nicHostOnlyPattern = Pattern.compile("(?:Host-only)[^']*'([^']*)'");
/**
@@ -203,30 +212,84 @@
return info;
}
+
+ static public class VBoxNetworkInterface {
+ private String networkType;
+ private String networkInterfaceName;
+ public VBoxNetworkInterface(String networkType, String networkInterfaceName) {
+ this.networkType = networkType;
+ this.networkInterfaceName = networkInterfaceName;
+ }
+ public String getNetworkType() {
+ return networkType;
+ }
+ public String getNetworkInterfaceName() {
+ return networkInterfaceName;
+ }
+ }
+
/**
- * Tell if the machine appears to use NAT.
- * @param hasNAT
+ * Get the information about configured network interfaces.
* @param machineInfo
- * @return
+ * @return non-<code>null</code> array
*/
- public static boolean machineUsesNATNetworking(Map<String, String> machineInfo) {
- boolean hasNAT = false;
- Pattern nicAttachmentPattern = Pattern.compile(".*Attachment:\\s*(.+),?");
+ public static VBoxNetworkInterface[] getNetworkInterfaceInfo(Map<String, String> machineInfo) {
+ List<VBoxNetworkInterface> ifs = new ArrayList<VBoxNetworkInterface>();
- for (String key : new String[] { "NIC 1", "NIC 2", "NIC 3", "NIC 4" }) {
+ for (int inum = 1; inum <= 8; inum++) {
+ String key = "NIC " + inum;
String nicInfo = machineInfo.get(key);
if (nicInfo != null) {
+
Matcher matcher = nicAttachmentPattern.matcher(nicInfo);
- if (matcher.matches() && matcher.group(1).equals("NAT")) {
- hasNAT = true;
- break;
+ if (matcher.matches()) {
+ String attachment = matcher.group(1);
+ if (attachment.equals("NAT")) {
+ ifs.add(new VBoxNetworkInterface(NETWORK_CONFIG_NAT, ""));
+ continue;
+ }
+
+ Matcher matcher2 = nicBridgedPattern.matcher(attachment);
+ if (matcher2.matches()) {
+ String ifaceName = matcher2.group(1);
+ int idx = ifaceName.indexOf(':');
+ if (idx > 0)
+ ifaceName = ifaceName.substring(0, idx);
+ ifs.add(new VBoxNetworkInterface(NETWORK_CONFIG_BRIDGED, ifaceName));
+ continue;
+ }
+
+ matcher2 = nicHostOnlyPattern.matcher(attachment);
+ if (matcher2.matches()) {
+ String ifaceName = matcher2.group(1);
+ int idx = ifaceName.indexOf(':');
+ if (idx > 0)
+ ifaceName = ifaceName.substring(0, idx);
+ ifs.add(new VBoxNetworkInterface(NETWORK_CONFIG_HOST_ONLY, ifaceName));
+ continue;
+ }
}
}
}
- return hasNAT;
+ return (VBoxNetworkInterface[]) ifs.toArray(new VBoxNetworkInterface[ifs.size()]);
}
/**
+ * Tell if the machine appears to use NAT.
+ * @param hasNAT
+ * @param machineInfo
+ * @return
+ */
+ public static boolean machineUsesNATNetworking(Map<String, String> machineInfo) {
+ VBoxNetworkInterface[] ifs = getNetworkInterfaceInfo(machineInfo);
+ for (VBoxNetworkInterface intf : ifs) {
+ if (intf.getNetworkType().equals(NETWORK_CONFIG_NAT))
+ return true;
+ }
+ return false;
+ }
+
+ /**
* Get the list of registered VMs
* @return collection of human-readable names
*/
More information about the Esbox-commits
mailing list