- script to turn IMG into VBOX, adapted from SPACEflight
authorJosef Spillner <josef@bomba-ssd>
Sun, 13 Apr 2014 15:26:28 +0000 (17:26 +0200)
committerJosef Spillner <josef@bomba-ssd>
Sun, 13 Apr 2014 15:26:28 +0000 (17:26 +0200)
createvbox.sh [new file with mode: 0755]

diff --git a/createvbox.sh b/createvbox.sh
new file mode 100755 (executable)
index 0000000..36907a0
--- /dev/null
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# Syntax: createvbox.sh [<vdi>] [<name>] [start]
+
+vdi=$PWD/nubigate-unreleased.vdi
+vmname="NubiGate"
+if [ ! -z $1 ] && [ "$1" != "start" ]; then
+       vdi=$1
+       shift
+fi
+if [ ! -z $1 ] && [ "$1" != "start" ]; then
+       vmname=$1
+       shift
+fi
+autostart=$1
+
+if [ "$vdi" = "$PWD/nubigate-unreleased.vdi" ]; then
+       if [ -f nubigate-unreleased.img ]; then
+               rm -f nubigate-unreleased.vdi
+               VBoxManage convertfromraw -format VDI nubigate-unreleased.img "$vdi"
+       else
+               echo "Error: Need to run createimg.sh first." >&2
+               exit 1
+       fi
+fi
+
+# redir(protocol,guestport,hostport)
+redir()
+{
+       adapter=e1000 # pcnet
+
+       VBoxManage setextradata "$vmname" "VBoxInternal/Devices/$adapter/0/LUN#0/Config/$1/Protocol" TCP
+       VBoxManage setextradata "$vmname" "VBoxInternal/Devices/$adapter/0/LUN#0/Config/$1/GuestPort" $2
+       VBoxManage setextradata "$vmname" "VBoxInternal/Devices/$adapter/0/LUN#0/Config/$1/HostPort" $3
+}
+
+VBoxManage list vms | grep -q "$vmname"
+if [ $? -ne 0 ]; then
+       # FIXME: Debian_64 depending on the architecture
+       VBoxManage createvm --name "$vmname" --ostype Debian_64 --register
+       VBoxManage modifyvm "$vmname" --memory 2048 --cpus 4
+       VBoxManage storagectl "$vmname" --name SATA-Controller --add sata
+
+       baseport=20000
+       if [ -f ~/.vboxautoconfig ]; then
+               baseport=`cat ~/.vboxautoconfig`
+       fi
+       VBoxManage modifyvm "$vmname" --vrdeauthtype external
+       VBoxManage modifyvm "$vmname" --vrdeport $((baseport))
+       redir ssh 22 $(($baseport+1))
+       redir web 22 $(($baseport+2))
+       redir tomcat 8080 $(($baseport+3))
+       baseport=$(($baseport+10))
+       echo $baseport > ~/.vboxautoconfig
+else
+       VBoxManage discardstate "$vmname"
+       VBoxManage storageattach "$vmname" --storagectl SATA-Controller --port 0 --device 0 --medium none
+       VBoxManage closemedium disk $vdi
+fi
+
+VBoxManage storageattach "$vmname" --storagectl SATA-Controller --port 0 --device 0 --medium "$vdi" --type hdd
+
+if [ "$autostart" = "start" ]; then
+       ps a | grep -v grep | grep -q /usr/bin/X
+       if [ $? = 0 ]; then
+               virtualbox --fullscreen --startvm "$vmname"
+       else
+               VBoxHeadless --startvm "$vmname"
+       fi
+fi
+