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