Index: vws ================================================================== --- vws +++ vws @@ -57,17 +57,17 @@ try: sock.recv(64, socket.MSG_DONTWAIT) except socket.error as ex: if ex.errno != errno.EAGAIN and ex.errno != errno.EWOULDBLOCK: raise ex - sock.send(command + "\n") + sock.send((command + "\n").encode("utf-8")) answer = "" while not answer.endswith("(qemu) "): chunk = sock.recv(1024) - if chunk == '': + if chunk == b'': raise IOError("Unexpected EOF from monitor") - answer += chunk + answer += chunk.decode("utf-8") finally: fcntl.flock(sock, fcntl.LOCK_UN) return answer def spiceurl(sock): @@ -108,11 +108,11 @@ def parse_arp(iface): """ Returns map which maps mac addresses to IPs for specified interface" """ addr_map = {} - pipe = os.popen("%s -n -u %s" % (config.get("tools", "arp"), iface), "r") + pipe = os.popen("%s -n -i %s" % (config.get("tools", "arp"), iface), "r") for line in pipe: data = line.split() mac = data[2] if mac == "HWAddress": continue @@ -145,11 +145,11 @@ 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) + match = re.search("-net nic,(?:\S*,)?macaddr=(\\S+) -net ([^, ]+)", line) if match: f = {"mac":match.group(1)} if match.group(2) == "user": f["iface"] = "user" elif match.group(2) == "bridge": @@ -437,17 +437,10 @@ sock.shutdown(socket.SHUT_RDWR) sock.close() f["state"] = "running" return f -def matches(name, patterns): - """ checks if name matches one of patterns """ - import fnmatch - for pattern in patterns: - if fnmatch.fnmatch(name, pattern): - return True - return False def add_ip_address(listing): """ Adds IP addresses from ARP into VM listing """ bridges = set() for vminfo in listing: @@ -460,15 +453,16 @@ if "mac" in vminfo and not "ip" in vminfo: if vminfo["mac"] in arp_data: vminfo["ip"] = arp_data[vminfo["mac"]] else: vminfo["ip"] = "-" -def all_vms(): +def all_vms(patterns=["*"]): """ Returns list of tuples vmname, vmtype, directory for all vms """ + import fnmatch search_path = [("private", os.path.join(pwd.getpwuid(os.getuid()).pw_dir, "VWs")), ("shared", config.get("directories", "SharedVMs")), ("autostart", config.get("directories", "AutostartVMs"))] vmlist = [] @@ -476,20 +470,24 @@ if not os.access(dirname, os.X_OK): continue for vmname in os.listdir(dirname): if not os.access(os.path.join(dirname, vmname, "start"), os.X_OK): continue - vmlist.append((vmname, vmtype, os.path.join(dirname, vmname))) + matches = False + for pattern in patterns: + if fnmatch.fnmatch(vmname, pattern): + matches = True + break + if matches: + vmlist.append((vmname, vmtype, os.path.join(dirname, vmname))) return vmlist def cmd_list(options): """ vws list """ count = 0 maxlen = 0 vms = [] - for vmname, vmtype, dirname in all_vms(): - if not matches(vmname, options.pattern): - continue + for vmname, vmtype, dirname in all_vms(options.pattern): count += 1 if maxlen < len(vmname): maxlen = len(vmname) if options.state: vms.append(make_vm_listing(vmname, dirname, vmtype)) @@ -560,10 +558,13 @@ os.chmod(i, 0o664) return 0 def cmd_snapshots(options): """ vws snapshots - list existing snapshots """ + if not options.stopped: + print("Cannot list snapshots of running VW", file=sys.stderr) + sys.exit(1) os.chdir(options.dir) drives = get_drives(options.dir) lst = [] info = {} with os.popen("qemu-img info --backing-chain " + drives[0], "r") as f: @@ -573,11 +574,12 @@ if val != "": info[var] = val elif line[0] == '\n': lst.append(info) info = {} - lst.append(info) + if info: + lst.append(info) for snap in lst: print("%-30s %+8s %+8s" % (snap["image"], snap["virtual size"], snap["disk size"]))