64 lines
1.3 KiB
Python
Executable file
64 lines
1.3 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
import os, sys, time, traceback
|
|
import subprocess
|
|
|
|
Popen = subprocess.Popen
|
|
|
|
def usage():
|
|
sys.stderr.write("""\
|
|
Usage:
|
|
startvm <vmname>
|
|
""")
|
|
|
|
def run():
|
|
if len(sys.argv) != 2:
|
|
usage()
|
|
return
|
|
|
|
vmstart = sys.argv[1]
|
|
fd = open('/srv/vmlist', 'r')
|
|
found = False
|
|
for l in fd:
|
|
l = l.strip()
|
|
if not l:
|
|
continue
|
|
name, mac, port, autostart = l.split()
|
|
if name == vmstart:
|
|
found = True
|
|
break
|
|
|
|
if not found:
|
|
sys.stderr.write("No VM with that name\n\n")
|
|
usage()
|
|
return
|
|
|
|
try:
|
|
os.mkdir("/var/run/vbox")
|
|
except OSError:
|
|
pass
|
|
|
|
pidfile = "/var/run/vbox/%s" % (name,)
|
|
if os.path.exists(pidfile):
|
|
fdpid = open(pidfile, "r")
|
|
oldpid = int(fdpid.read())
|
|
fdpid.close()
|
|
procfile = "/proc/%s/cmdline" % (oldpid,)
|
|
if os.path.exists(procfile):
|
|
fdcmd = open(procfile, "r")
|
|
oldcmd = fdcmd.read().replace('\x00', ' ')
|
|
fdcmd.close()
|
|
sys.stderr.write("Already running: PID %s, CMD %s\n" % (oldpid, oldcmd))
|
|
return
|
|
|
|
fdo = open("/var/log/vbox/%s.log" % (name,), "a")
|
|
fdo.write("--- Starting VirtualBox at %s ---\n" % (time.strftime("%Y-%m-%d %H:%M:%S"),))
|
|
fdpid = open(pidfile, "w")
|
|
p = Popen(["vboxheadless", "-s", name, "-n", "-m", port], stdout=fdo, stderr=fdo)
|
|
fdpid.write(str(p.pid))
|
|
fdpid.close()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run()
|