summaryrefslogtreecommitdiff
path: root/tools/bhvm.py
diff options
context:
space:
mode:
Diffstat (limited to 'tools/bhvm.py')
-rw-r--r--tools/bhvm.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/tools/bhvm.py b/tools/bhvm.py
new file mode 100644
index 0000000..a78846c
--- /dev/null
+++ b/tools/bhvm.py
@@ -0,0 +1,119 @@
+#!/usr/bin/env python3
+"""Tiny client for bhyvemgrd's UNIX-socket JSON protocol.
+
+Why this exists: the only privileged channel on this host is bhyvemgrd (it runs
+as root via mdo). beatrice is in group wheel, and the socket is srwxrwx--- root
+wheel, so a plain user can ask the daemon to create/start/stop VMs and to
+create/attach tap devices -- no sudo needed.
+
+Protocol (from /ROM/bhyve/bhyvemgr-src/src/unit_socket.pas):
+ one JSON object per line, '\n' terminated
+ request : {"type":"task","id":<uuid>,"method":<m>,"params":{...}}
+ reply : {"type":"task","id":<uuid>,"success":true,...}
+ events : {"type":"event",...}
+ on connect the daemon first sends {"type":"snapshot","vms":{...}}
+
+Methods seen in the GUI source: vm.create (params: vmname), vm.start
+(vmname, isrebooting), vm.stop, vm.destroy, vm.delete, network.create_device
+(device, vmname, mtu), network.attach_bridge (bridge, device),
+network.destroy_device (ifname).
+
+Usage:
+ bhvm.py list
+ bhvm.py call <method> '<json params>'
+ bhvm.py create <name> # register an already-written VM dir
+ bhvm.py start <name>
+ bhvm.py destroy <name> # power off the instance (vm.stop is refused
+ # while vCPU threads are active; destroy works)
+ bhvm.py net <tap> <vmname> # ifconfig <tap> create + addm to bhyve0
+ bhvm.py watch # connect and print events for 10 s
+"""
+import json
+import select
+import socket
+import sys
+import time
+import uuid
+
+SOCK = "/var/run/bhyvemgrd/bhyvemgrd.sock"
+BRIDGE = "bhyve0"
+
+
+def client():
+ s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ s.connect(SOCK)
+ return s
+
+
+def drain(s, seconds, rid=None):
+ end = time.time() + seconds
+ buf = b""
+ done = False
+ while time.time() < end:
+ r, _, _ = select.select([s], [], [], 0.3)
+ if not r:
+ continue
+ d = s.recv(65536)
+ if not d:
+ break
+ buf += d
+ if rid and ('"id" : "%s"' % rid) in buf.decode("utf-8", "replace"):
+ # keep reading briefly for the follow-up event
+ done = True
+ end = min(end, time.time() + 0.6)
+ lines = [l for l in buf.decode("utf-8", "replace").split("\n") if l.strip()]
+ if done:
+ return lines
+ return lines
+
+
+def call(method, params=None, wait=6.0):
+ s = client()
+ rid = str(uuid.uuid4())
+ req = {"type": "task", "id": rid, "method": method, "params": params or {}}
+ s.sendall((json.dumps(req) + "\n").encode())
+ lines = drain(s, wait, rid)
+ s.close()
+ ok = any('"success" : true' in l and rid in l for l in lines)
+ for l in lines:
+ print(l[:800])
+ return ok
+
+
+def main():
+ if len(sys.argv) < 2:
+ print(__doc__)
+ return 2
+ cmd = sys.argv[1]
+ if cmd == "list":
+ s = client()
+ for l in drain(s, 2.0):
+ print(l[:2000])
+ s.close()
+ elif cmd == "call":
+ call(sys.argv[2], json.loads(sys.argv[3]) if len(sys.argv) > 3 else {})
+ elif cmd == "create":
+ call("vm.create", {"vmname": sys.argv[2]})
+ elif cmd == "start":
+ call("vm.start", {"vmname": sys.argv[2], "isrebooting": "False"}, wait=10.0)
+ elif cmd == "destroy":
+ call("vm.destroy", {"vmname": sys.argv[2]})
+ elif cmd == "delete":
+ call("vm.delete", {"vmname": sys.argv[2]})
+ elif cmd == "net":
+ tap, vm = sys.argv[2], sys.argv[3]
+ call("network.create_device", {"device": tap, "vmname": vm, "mtu": "1500"}, wait=3)
+ call("network.attach_bridge", {"bridge": BRIDGE, "device": tap}, wait=3)
+ elif cmd == "watch":
+ s = client()
+ for l in drain(s, float(sys.argv[2]) if len(sys.argv) > 2 else 10.0):
+ print(l[:2000])
+ s.close()
+ else:
+ print(__doc__)
+ return 2
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())