Blob Blame History Raw
changeset:   165:5b37d71c88b6
user:        "Daniel P. Berrange <berrange@redhat.com>"
date:        Tue May 01 11:31:32 2007 -0400
summary:     Automatically pick sensible default networking if no arg is listed, rather than defaulting to xenbr0. Pick sensible connect URI based on host OS

diff -r cdbbe109cf8d -r 5b37d71c88b6 virt-install
--- a/virt-install	Mon Apr 30 11:44:00 2007 -0400
+++ b/virt-install	Tue May 01 11:31:32 2007 -0400
@@ -42,11 +42,6 @@ def prompt_for_input(prompt = "", val = 
     print prompt + " ", 
     return sys.stdin.readline().strip()
 
-
-def check_xen():
-    if not os.path.isdir("/proc/xen"):
-        print >> sys.stderr, "Can only install guests if running under a Xen kernel!"
-        sys.exit(1)
 
 ### General input gathering functions
 def get_full_virt():
@@ -216,7 +211,8 @@ def get_networks(macs, bridges, networks
             macs = [ None ] * len(networks)
     else:
         if os.getuid() == 0:
-            networks = ["bridge:" + virtinst.util.default_bridge()]
+            net = virtinst.util.default_network()
+            networks = [net[0] + ":" + net[1]]
         else:
             networks = ["user"]
         if macs != None:
@@ -274,9 +270,12 @@ def get_paravirt_extraargs(extra, guest)
 
 
 ### fullvirt input gathering functions
-def get_fullvirt_cdrom(cdpath, guest):
-    while 1:
-        cdpath = prompt_for_input("What would you like to use for the virtual CD image?", cdpath)
+def get_fullvirt_cdrom(cdpath, location, guest):
+    if cdpath is None and location is not None:
+        cdpath = location
+
+    while 1:
+        cdpath = prompt_for_input("What is the virtual CD image, CD device or install location?", cdpath)
         try:
             guest.location = cdpath
             break
@@ -353,7 +352,8 @@ def parse_args():
                       help="Use kernel acceleration capabilities")
     parser.add_option("", "--connect", type="string", dest="connect",
                       action="callback", callback=check_before_store,
-                      help="Connect to hypervisor with URI")
+                      help="Connect to hypervisor with URI",
+                      default=virtinst.util.default_connection())
     
     # fullvirt options
     parser.add_option("-v", "--hvm", action="store_true", dest="fullvirt",
@@ -484,7 +484,6 @@ def main():
     # check to ensure we're really on a xen kernel
     if conn.getType() == "Xen":
         type = "xen"
-        check_xen()
 
         if os.geteuid() != 0:
             print >> sys.stderr, "Must be root to install guests"
@@ -544,7 +543,7 @@ def main():
         get_paravirt_extraargs(options.extra, guest)
         continue_inst = False
     else:
-        get_fullvirt_cdrom(options.cdrom, guest)
+        get_fullvirt_cdrom(options.cdrom, options.location, guest)
         if options.noacpi:
             guest.features["acpi"] = False
         if options.noapic:
diff -r cdbbe109cf8d -r 5b37d71c88b6 virtinst/util.py
--- a/virtinst/util.py	Mon Apr 30 11:44:00 2007 -0400
+++ b/virtinst/util.py	Tue May 01 11:31:32 2007 -0400
@@ -16,7 +16,7 @@ import os.path
 import os.path
 from sys import stderr
 
-def default_bridge():
+def default_route():
     route_file = "/proc/net/route"
     d = file(route_file)
 
@@ -30,12 +30,47 @@ def default_bridge():
         try:
             route = int(info[1],16)
             if route == 0:
-                defn = int(info[0][-1])
-                break 
+                return info[0]
         except ValueError:
             continue
+    return None
 
-    return "xenbr%d"%(defn)
+# Legacy for compat only.
+def default_bridge():
+    rt = default_route()
+    defn = int(rt[-1])
+
+    if defn is None:
+        return "xenbr0"
+    else:
+        return "xenbr%d"%(defn)
+
+def default_network():
+    dev = default_route()
+
+    if dev is not None:
+        # New style peth0 == phys dev, eth0 == bridge, eth0 == default route
+        if os.path.exists("/sys/class/net/%s/bridge" % dev):
+            return ["bridge", dev]
+
+        # Old style, peth0 == phys dev, eth0 == netloop, xenbr0 == bridge,
+        # vif0.0 == netloop enslaved, eth0 == default route
+        defn = int(dev[-1])
+        if os.path.exists("/sys/class/net/peth%d/brport" % defn) and \
+           os.path.exists("/sys/class/net/xenbr%d/bridge" % defn):
+            return ["bridge", "xenbr%d" % defn]
+
+    return ["network", "default"]
+
+def default_connection():
+    if os.path.exists("/var/lib/xend") and os.path.exists("/proc/xen"):
+        return "xen"
+    elif os.path.exists("/usr/bin/qemu"):
+        if os.getuid() == 0:
+            return "qemu:///system"
+        else:
+            return "qemu:///session"
+    return None
 
 def get_cpu_flags():
     f = open("/proc/cpuinfo")