36907a08971018ba82652e66bdcbb18d13313bcb
[nubigate] / createvbox.sh
1 #!/bin/sh
2 #
3 # Syntax: createvbox.sh [<vdi>] [<name>] [start]
4
5 vdi=$PWD/nubigate-unreleased.vdi
6 vmname="NubiGate"
7 if [ ! -z $1 ] && [ "$1" != "start" ]; then
8         vdi=$1
9         shift
10 fi
11 if [ ! -z $1 ] && [ "$1" != "start" ]; then
12         vmname=$1
13         shift
14 fi
15 autostart=$1
16
17 if [ "$vdi" = "$PWD/nubigate-unreleased.vdi" ]; then
18         if [ -f nubigate-unreleased.img ]; then
19                 rm -f nubigate-unreleased.vdi
20                 VBoxManage convertfromraw -format VDI nubigate-unreleased.img "$vdi"
21         else
22                 echo "Error: Need to run createimg.sh first." >&2
23                 exit 1
24         fi
25 fi
26
27 # redir(protocol,guestport,hostport)
28 redir()
29 {
30         adapter=e1000 # pcnet
31
32         VBoxManage setextradata "$vmname" "VBoxInternal/Devices/$adapter/0/LUN#0/Config/$1/Protocol" TCP
33         VBoxManage setextradata "$vmname" "VBoxInternal/Devices/$adapter/0/LUN#0/Config/$1/GuestPort" $2
34         VBoxManage setextradata "$vmname" "VBoxInternal/Devices/$adapter/0/LUN#0/Config/$1/HostPort" $3
35 }
36
37 VBoxManage list vms | grep -q "$vmname"
38 if [ $? -ne 0 ]; then
39         # FIXME: Debian_64 depending on the architecture
40         VBoxManage createvm --name "$vmname" --ostype Debian_64 --register
41         VBoxManage modifyvm "$vmname" --memory 2048 --cpus 4
42         VBoxManage storagectl "$vmname" --name SATA-Controller --add sata
43
44         baseport=20000
45         if [ -f ~/.vboxautoconfig ]; then
46                 baseport=`cat ~/.vboxautoconfig`
47         fi
48         VBoxManage modifyvm "$vmname" --vrdeauthtype external
49         VBoxManage modifyvm "$vmname" --vrdeport $((baseport))
50         redir ssh 22 $(($baseport+1))
51         redir web 22 $(($baseport+2))
52         redir tomcat 8080 $(($baseport+3))
53         baseport=$(($baseport+10))
54         echo $baseport > ~/.vboxautoconfig
55 else
56         VBoxManage discardstate "$vmname"
57         VBoxManage storageattach "$vmname" --storagectl SATA-Controller --port 0 --device 0 --medium none
58         VBoxManage closemedium disk $vdi
59 fi
60
61 VBoxManage storageattach "$vmname" --storagectl SATA-Controller --port 0 --device 0 --medium "$vdi" --type hdd
62
63 if [ "$autostart" = "start" ]; then
64         ps a | grep -v grep | grep -q /usr/bin/X
65         if [ $? = 0 ]; then
66                 virtualbox --fullscreen --startvm "$vmname"
67         else
68                 VBoxHeadless --startvm "$vmname"
69         fi
70 fi
71