Make the listvms remctl include domains that get stuck in pygrub
[invirt/packages/invirt-remote.git] / host / usr / sbin / invirt-listvms
index 0f9d5dc..aeea6d7 100755 (executable)
@@ -11,13 +11,27 @@ def live_vms():
     domids.discard('0')
 
     vms = dict()
+    xenserver = None
 
     for domid in domids:
         try:
             name, data = get_dom(int(domid))
         except (xs.Error, TypeError):
             continue # went down since we started
-        if name is not None and name.startswith('d_'):
+        if name is None: # xenstore may not be fully populated if it hung early
+            try:
+                if xenserver is None:
+                    from xen.xm.main import SERVER_LEGACY_XMLRPC, parseServer
+                    from xen.util.xmlrpcclient import ServerProxy
+                    serverType, serverURI = parseServer()
+                    if serverType != SERVER_LEGACY_XMLRPC:
+                        continue # we can't handle a XenAPI server yet
+                    xenserver = ServerProxy(serverURI)
+                name = filter(lambda x: isinstance(x, list) and x[0]=='name',
+                              xenserver.xend.domain(int(domid)))[0][1]
+            except:
+                continue # we have nothing we can show about this domain
+        if name.startswith('d_'):
             name = name[2:]
             vms[name] = data
     return vms
@@ -30,15 +44,27 @@ def get_dom(domid):
     data['console'] = xsc.read('', '/local/domain/%d/device/vfb/0/state' % domid)
     # uptime
     data['vm'] = xsc.read('', '/local/domain/%d/vm' % domid)
-    data['start_time'] = float(xsc.read('', '%s/start_time' % data['vm']))
-    data['uptime'] = time.time()-data['start_time']
-    # CD, if there is one
+    start_time = xsc.read('', '%s/start_time' % data['vm'])
+    if start_time is not None:
+        data['start_time'] = float(start_time)
+        data['uptime'] = time.time()-data['start_time']
+    else:
+        data['uptime'] = 0
+    # CD or autoinstall, if we're using either
     vbds = xsc.ls('', '/local/domain/%d/device/vbd' % domid)
-    for vbd in vbds:
-        dev = '/local/domain/%d/device/vbd/%s' % (domid, vbd)
-        if xsc.read('', '%s/device-type' % dev) == 'cdrom':
-            data['cdrom'] = xsc.read('', '%s/params' % xsc.read('', '%s/backend' % dev))
-    
+    if vbds is not None:
+        for vbd in vbds:
+            dev = '/local/domain/%d/device/vbd/%s' % (domid, vbd)
+            dev_type = xsc.read('', '%s/device-type' % dev)
+            dev_params = xsc.read('', '%s/params' % xsc.read('', '%s/backend' % dev))
+            if dev_type == 'cdrom':
+                data['cdrom'] = dev_params
+            elif dev_type == 'disk' and dev_params == '/dev/xenvg/s_install_hda':
+                data['autoinstall'] = True
+    cmdline = xsc.read('', '%s/image/cmdline' % data['vm'])
+    if cmdline is not None and 'auto=true' in cmdline:
+        data['autoinstall'] = True
+
     return name, data
 
 if __name__ == '__main__':