Diff
Not logged in

Differences From Artifact [e599101b46]:

To Artifact [f9837316dc]:


85
86
87
88
89
90
91

















92
93
94
95
96
97
98
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







            if idx <= 0:
                continue
            name = line[:idx]
            if name == "bridge name":
                continue
            lst.append(name)
    return lst

def parse_arp(iface):
    """
    Returns map which maps mac addresses to IPs for specified interface"
    """
    addr_map = {}
    pipe = os.popen(config.get("tools","arp")+" -n -i "+iface, "r")
    for line in pipe:
        data = line.split() 
        mac=data[2]
        if mac == "HWAddress":
            continue
        if mac == iface:
            # Foind line with (incomplete) entry
            continue
        addr_map[data[2]]=data[0]
    return addr_map

def validate_size(size):
    """ Checks if size argument has proper format """
    return re.match('\\d+[KMG]', size) is not None

def get_drives(vm_dir):
    """ Return list of drive files in the VW directory """
107
108
109
110
111
112
113
114





























115
116
117
118
119
120
121
124
125
126
127
128
129
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166







-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    return result

def snapshot_mode(sock):
    """ Returns True if VM is running in snapshot mode """
    answer = send_command(sock, "info block")
    return re.search(": /tmp", answer) is not None


def read_netinfo(filename):
    """ Reads network information from start script """
    with open(filename,"r") as f:
       for line in f:
          match=re.search("-net nic,macaddr=(\\S+) -net ([^,]+)",line)
          if match:
              f={"mac":match.group(1)}
              if match.group(2) == "user":
                   f["iface"]="user"
              elif match.group(2) == "bridge":
                   f["iface"]=re.search("br=(\\S+)",line).group(1)
              else:
                   f["iface"]="unknown";
              return f
    return {"iface":"unknown","mac":"?","ip":"?"}
def get_netinfo(sock):
    """ Gets network information from the running VM """
    answer = send_command(sock, "info network")
    match=re.search("bridge\\.0:.*,br=(\\S+).*macaddr=(\\S+)", answer, re.S)
    if match:
        return {"iface":match.group(1), "mac":match.group(2)}
    else:
        match = re.search("user.0:.*net=([^,]+).*\n.*macaddr=(\\S+)",answer)
        if match:
           return {"iface":"user", "ip":match.group(1), 
                    "mac":match.group(2)}
        else:
           print >>sys.stderr,answer
           return {"iface":"unknown","ip":"?","mac":"?","card":"?"}
#
# command implementation
#

def cmd_spiceuri(options):
    """ vws spiceuri """
    print spiceurl(options.sock)
305
306
307
308
309
310
311

312
313
314
315
316
317
318

319
320
321
322

323
324
325
326


327
328




329
330
331

332
333
334



335
336
337









338
339

340
341
342
343
344
345
346
350
351
352
353
354
355
356
357
358
359
360
361
362
363

364
365
366
367

368
369
370


371
372
373
374
375
376
377
378
379
380

381


382
383
384
385



386
387
388
389
390
391
392
393
394
395

396
397
398
399
400
401
402
403







+






-
+



-
+


-
-
+
+


+
+
+
+


-
+
-
-

+
+
+
-
-
-
+
+
+
+
+
+
+
+
+

-
+







    count = 0
    search_path = [("private",os.environ['HOME'] + "/VWs"),
                   ("shared",config.get("directories", "SharedVMs")),
                   ("autostart",config.get("directories", "AutostartVMs"))]

    maxlen = 0
    vms = []
    bridges = set()
    for (vmtype,dirname) in search_path:
        if not os.access(dirname + "/.", os.X_OK):
            continue
        for vmname in os.listdir(dirname):
            if os.access(dirname + "/" + vmname + "/start", os.X_OK):
                count += 1
                f = [vmname]
                f = {"name":vmname}
                if maxlen < len(vmname):
                    maxlen = len(vmname)
                if options.state:
                    f.append(vmtype)
                    f["type"]=vmtype
                    sock = connect_vm(dirname + "/" + vmname)
                    if sock is None:
                        state = "stopped"
                        uri="-"
                        f.update({"state":"stopped","uri":"-","ip":"-"})
                        f.update(read_netinfo(dirname + "/" + vmname + "/start"))
                    else:
                        uri=spiceurl(sock)
                        f["uri"]=uri[uri.rindex(":")+1:]
                        f.update(get_netinfo(sock))
                        if "ip" not in f:
                            bridges.add(f["iface"])
                        sock.shutdown(socket.SHUT_RDWR)
                        sock.close()
                        state = "running"
                        f["state"] = "running"
                    f.append(state)
                    f.append(uri)
                vms.append(f)
    arp_data={}
    for bridge in bridges:
        arp_data.update(parse_arp(bridge))
    for f in sorted(vms):
        if len(f) == 4:
            print "%*s %s   %-9s %s" % (-maxlen, f[0], f[2], f[1], f[3])
    for f in sorted(vms,key=lambda x: x["name"]):
        if "state" in f:
            if "mac" in f and not "ip" in f:
                if f["mac"] in arp_data:
                    f["ip"] = arp_data[f["mac"]]
                else:
                    f["ip"] = "-"
            f["name"] = f["name"].ljust(maxlen)
            print "%(name)s %(state)s   %(type)-9s %(uri)-4s %(iface)-5s %(mac)s %(ip)s " % f
        else:
            print f[0]
            print f["name"]
    if not count:
        sys.exit(1)

def cmd_screenshot(options):
    """ vws screenshot """
    from os.path import abspath
    filename = abspath(options.filename)
698
699
700
701
702
703
704

705
706
707
708
709
710
711
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769







+







                      ('diskif', 'virtio'), ('sound', 'hda'), ('arch', arch),
                      ('vga', 'qxl')]:
    config.set('create options', option, value)
config.add_section('tools')
config.set('tools', 'viewer', 'remote-viewer %s')
config.set('tools', 'bridge_list', '/sbin/brctl show')
config.set('tools', 'lsusb', 'lsusb')
config.set('tools', 'arp', '/usr/sbin/arp')
config.add_section('permissions')
config.set('permissions','vm_group','kvm')
config.set('permissions','autostart_user','root')
config.set('permissions','setgid_vm','yes')
# Read configration files
config.read(['/etc/vws.conf', os.environ['HOME'] + '/.vwsrc'])
# Parse argument