[Esbox-commits] r1586 - branches/work_Ed/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 21:25:56 EEST 2009
Author: eswartz
Date: 2009-05-12 21:25:55 +0300 (Tue, 12 May 2009)
New Revision: 1586
Modified:
branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java
branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java
Log:
Fix bug 4027 (VirtualBox bridged network autoselection)
Modified: branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java
===================================================================
--- branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java 2009-05-12 14:21:28 UTC (rev 1585)
+++ branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxPreferencePage.java 2009-05-12 18:25:55 UTC (rev 1586)
@@ -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,24 +128,76 @@
reporter.log(Activator.createErrorStatus("Could not find any info on the machine " +
configuration.getMachineName() +"; guessing from defaults", null));
} else {
-
- boolean hasNAT = VirtualBoxUtils.machineUsesNATNetworking(machineInfo);
+
+ // 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);
+ 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));
+ }
+
+ break;
}
}
}
@@ -149,12 +205,10 @@
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 +220,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: branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java
===================================================================
--- branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java 2009-05-12 14:21:28 UTC (rev 1585)
+++ branches/work_Ed/org.maemo.esbox.vm.virtualbox/src/org/maemo/esbox/internal/vm/virtualbox/VirtualBoxUtils.java 2009-05-12 18:25:55 UTC (rev 1586)
@@ -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,12 @@
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*(.+)");
+ static final String NETWORK_CONFIG_NAT = "NAT";
+ static final String NETWORK_CONFIG_BRIDGED = "bridged";
+ final static Pattern nicBridgedPattern = Pattern.compile("Bridged.*'([^']*)'");
+ final static Pattern nicAttachmentPattern = Pattern.compile(".*Attachment:\\s*([^,]+),?.*");
/**
@@ -203,30 +208,69 @@
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, ""));
+ break;
+ } else {
+ Matcher matcher2 = nicBridgedPattern.matcher(attachment);
+ if (matcher2.matches()) {
+ ifs.add(new VBoxNetworkInterface(NETWORK_CONFIG_BRIDGED, matcher2.group(1)));
+ }
+ }
+ }
}
}
- 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