initial import

--HG--
branch : vmtools
This commit is contained in:
cecilkorik 2011-03-05 08:34:41 +00:00
commit 7b51226796
9 changed files with 347 additions and 0 deletions

20
autostartvm Executable file
View file

@ -0,0 +1,20 @@
#!/usr/bin/python
import os, sys, subprocess, time
os.environ['VBOX_USER_HOME'] = '/srv/vbox'
devnull = open('/dev/null', 'wb')
vml = open('/srv/vmlist', 'r')
for vmd in vml:
vmd = vmd.strip()
if not vmd:
continue
name, mac, vnc, autostart = vmd.split(' ')
if autostart == '1':
subprocess.Popen(['/usr/local/sbin/startvm', name], env=os.environ).wait()
time.sleep(2)
vml.close()

14
autostopvm Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/python
import os, sys, subprocess
os.environ['VBOX_USER_HOME'] = '/srv/vbox'
if not os.path.exists('/var/run/vbox'):
sys.exit(0)
devnull = open('/dev/null', 'wb')
for name in os.listdir('/var/run/vbox'):
subprocess.Popen(['/usr/local/sbin/stopvm', name], env=os.environ).wait()

54
killvm Executable file
View file

@ -0,0 +1,54 @@
#!/usr/bin/python
import os, sys, time, traceback
def vbox_soft_kill(name, pid):
def vbox_running(pid):
try:
fd = open("/proc/%d/cmdline" % (pid,), "r")
except IOError, OSError:
return False
return True
if not vbox_running(pid):
return
os.system("vboxmanage controlvm %s poweroff" % (name,))
for i in xrange(30):
time.sleep(0.333)
if not vbox_running(pid):
return
os.system("kill %s" % (pid,))
for i in xrange(30):
time.sleep(0.333)
if not vbox_running(pid):
return
os.system("kill -9 %s" % (pid,))
def usage():
sys.stderr.write("""\
Usage:
killvm <vmname>
""")
def run():
if len(sys.argv) != 2:
sys.stderr.write("Invalid arguments\n\n")
usage()
return
name = sys.argv[1]
if not os.path.exists('/var/run/vbox/%s' % (name,)):
sys.stderr.write("Invalid VM name\n\n")
usage()
return
fd = open('/var/run/vbox/%s' % (name,), 'r')
pid = int(fd.read())
fd.close()
vbox_soft_kill(name, pid)
if __name__ == "__main__":
run()

49
makevm Executable file
View file

@ -0,0 +1,49 @@
#!/bin/bash
usage() {
cat << EEOF
Usage:
makevm <newvm> [clonevm]
EEOF
}
if [ ! $1 ]; then
usage
exit 1
fi
NEWVM=$1
CLONEVM=$2
SYSDISK=/dev/vg0/$NEWVM
SYSDISKFILE=$VBOX_USER_HOME/Disks/$NEWVM/$NEWVM.vmdk
CLONEDISK=/dev/vg0/$CLONEVM
CLONEDISKFILE=$VBOX_USER_HOME/Disks/$CLONEVM/$CLONEVM.vmdk
NEWVNC=`python nextvnc.py`
if [ $NEWVNC -lt 5900 ]; then
echo "Python error $NEWVNC"
exit 99
fi
NEWMAC_LASTPART=`python -c "print '%.2d' % ($(($NEWVNC - ($NEWVNC / 100 * 100))),)"`
NEWMAC=aa:aa:aa:00:$(($NEWVNC / 100)):$NEWMAC_LASTPART
VBOXMAC=`echo $NEWMAC | sed -e 's/://g'`
if [ $CLONEVM ]; then
#lvcreate -L 10G -n $NEWVM vg0
vboxmanage internalcommands createrawvmdk -filename $SYSDISKFILE -rawdisk $SYSDISK || exit 250
vboxmanage clonehd $CLONEDISKFILE $SYSDISKFILE --existing --remember || exit 250
else
lvcreate -L 10G -n $NEWVM vg0 || exit 250
vboxmanage internalcommands createrawvmdk -filename $SYSDISKFILE -rawdisk $SYSDISK || exit 250
#vboxmanage createhd --filename $SYSDISK --size 10000 --variant Fixed --remember || exit 250
fi
vboxmanage createvm --name $NEWVM --ostype Ubuntu_64 --register || exit 250
vboxmanage modifyvm $NEWVM --memory 512 --nic1 bridged --nictype1 Am79C973 --nicspeed1 1000000 --bridgeadapter1 eth0 --macaddress1 $VBOXMAC --boot1 disk --boot2 dvd --boot3 net || exit 250
vboxmanage storagectl $NEWVM --name sata0 --add sata --sataportcount 30 || exit 250
vboxmanage storageattach $NEWVM --storagectl sata0 --port 1 --device 0 --type hdd --medium $SYSDISKFILE || exit 250
echo $NEWVM $NEWMAC $NEWVNC 0 >> /srv/vmlist
echo "Done!"
echo "Machine '$NEWVM' with MAC '$NEWMAC' on port '$NEWVNC' created successfully!"

30
nextvnc.py Executable file
View file

@ -0,0 +1,30 @@
#!/usr/bin/python
import os, sys
import traceback
if not os.path.exists('/srv/vmlist'):
print "5900"
sys.exit(0)
try:
f = open('/srv/vmlist', 'r')
vnclist = []
for l in f:
l = l.strip()
if not l:
continue
vm, mac, vnc, autostart = l.split()
vnc = int(vnc)
vnclist.append(vnc)
vnclist.sort()
vncstart = 5900
for i in xrange(len(vnclist)):
if vncstart + i != vnclist[i]:
print vncstart + i
print vncstart + len(vnclist)
except:
traceback.print_exc()
sys.exit(1)

2
rebootvm Executable file
View file

@ -0,0 +1,2 @@
#!/bin/bash
vboxmanage controlvm $1 reset

64
startvm Executable file
View file

@ -0,0 +1,64 @@
#!/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()

75
stopvm Executable file
View file

@ -0,0 +1,75 @@
#!/usr/bin/python
import os, sys, time, traceback
def vbox_soft_kill(name, pid):
def vbox_running(pid):
try:
fd = open("/proc/%d/cmdline" % (pid,), "r")
except IOError, OSError:
return False
return True
if not vbox_running(pid):
return
os.system("vboxmanage controlvm %s acpipowerbutton" % (name,))
for i in xrange(360):
time.sleep(0.333)
if not vbox_running(pid):
return
os.system("vboxmanage controlvm %s poweroff" % (name,))
for i in xrange(30):
time.sleep(0.333)
if not vbox_running(pid):
return
os.system("kill %s" % (pid,))
for i in xrange(30):
time.sleep(0.333)
if not vbox_running(pid):
return
os.system("kill -9 %s" % (pid,))
def usage():
sys.stderr.write("""\
Usage:
stopvm <vmname>
""")
def run():
if len(sys.argv) != 2:
sys.stderr.write("Invalid arguments\n\n")
usage()
return
name = sys.argv[1]
vml = open('/srv/vmlist', 'r')
found = False
for vmn in vml:
vmn = vmn.strip()
if not vmn:
continue
vmn = vmn.split()[0]
if vmn == name:
found = True
break
if not found:
sys.stderr.write("Invalid VM name\n\n")
usage()
return
if not os.path.exists('/var/run/vbox/%s' % (name,)):
sys.stderr.write("VM is not running\n\n")
usage()
return
fd = open('/var/run/vbox/%s' % (name,), 'r')
pid = int(fd.read())
fd.close()
vbox_soft_kill(name, pid)
if __name__ == "__main__":
run()

39
vmlist Executable file
View file

@ -0,0 +1,39 @@
#!/usr/bin/python
import os, sys
fd = open('/srv/vmlist', 'r')
auto_only = False
running_only = False
show_pids = False
if len(sys.argv) > 1 and sys.argv[1] == '-a':
auto_only = True
if len(sys.argv) > 1 and sys.argv[1] == '-r':
running_only = True
if len(sys.argv) > 1 and sys.argv[1] == '-p':
show_pids = True
for line in fd:
line = line.strip()
if not line:
continue
ls = line.split(' ')
if auto_only and ls[3] == '0':
continue
pid = None
if running_only or show_pids:
if not os.path.exists('/var/run/vbox/%s' % (ls[0],)):
continue
pid = open('/var/run/vbox/%s' % (ls[0],), 'r').read()
procfile = "/proc/%s/cmdline" % (pid,)
if not os.path.exists(procfile):
continue
if show_pids:
print "%s %s" % (ls[0], pid)
else:
print ls[0]
fd.close()