]> git.donarmstrong.com Git - neurodebian.git/blob - vm/tools/nd_createappliance
ef2e32989eb93d53c16cc0d025f1da988fd3517a
[neurodebian.git] / vm / tools / nd_createappliance
1 #!/bin/bash
2 #emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*- 
3 #ex: set sts=4 ts=4 sw=4 noet:
4
5 # fail early
6 set -eu
7
8
9 function usage {
10 echo "Usage: $0 [-p PRESEED -i ISO -m MIRROR -t TZ]
11
12 Create ND VirtualBox appliance.
13
14 Options: 
15   -p PRESEED   path to preesed file for d-i
16   -i ISO       path to debian installation iso file
17   -m MIRROR    debian mirror to use for installation
18   -t TZ        time zone of virtual machine
19   -h           Show this help and exit
20 "
21 }
22
23 while getopts "p:" OPTION
24 do
25   case $OPTION in
26     "f")  $OPTARG;;
27     \?) exit 1;;
28   esac
29 done
30
31
32 # TODO: arguments later on to become cmdline args
33 #iso=debian-squeeze-di-beta1-amd64-businesscard.iso
34 #iso=debian-6.0.3-${ARCH:=amd64}-businesscard.iso
35 #iso=debian-wheezy-DI-a1-${ARCH:=amd64}-businesscard.iso
36
37 di_cd=/backup/isos/debian-wheezy-DI-b1-amd64-netinst.iso
38 # look here for more details about the default IP of the host:
39 # # http://www.virtualbox.org/manual/ch09.html#changenat
40 di_host=10.0.2.2              # where to look for di preseed
41 di_port=10100
42 di_preseed=/home/tiziano/git/neurodebian/vm/d-i/wheezy/preseed.cfg
43
44
45 build_dir=$PWD/build
46 dist_dir=$PWD/dist
47 vendor="NeuroDebian"
48 vm_version="6.999.20120827"
49 vm_ostype=Debian
50 vendor_url="http://neuro.debian.net"
51 product_url="${vendor_url}/vm.html"
52
53 # Generic definitions
54 eula="This virtual appliance contains Free and Open Source Software (FOSS) released under licenses compliant with the Debian Free Software Guidelines (DFSG, see http://www.debian.org/social_contract), such as, GPL, BSD, MIT, etc.  Such software is free to be used or customized for any purpose.
55
56 However, by default this virtual machine is also enabled to install additional software from Debian and NeuroDebian repositories that is distributed under more restrictive licenses (e.g. closed-source, non-commercial, research-only). It is the user's responsibility to adhere to the terms and conditions of any particular software that is installed and used in this virtual machine. Copyright and license details for any installed PACKAGE are available in /usr/share/doc/PACKAGE/copyright inside the virtual machine."
57
58
59 # Computed settings
60 #vm_basepath=
61
62 # By default 32bit unless installer image has amd64
63 # TODO: might need to make more robust?
64 vm_arch=i386
65 vm_arch_name=" (32bit)"
66 if file $di_cd | grep -q ' amd64 '; then
67     vm_arch=amd64
68     vm_ostype+="_64"
69     vm_arch_name=" (64bit)"
70 fi
71
72 vm_fprefix="${vendor}_${vm_version}_${vm_arch}" # common prefix for files
73 #vm_disk="$build_dir/nd-${vm_version}_${vm_arch}.vdi"
74 vm_disk="$build_dir/${vm_fprefix}.vdi"
75 vm_name="${vendor} ${vm_version} ${vm_arch_name}"
76 # Let's use OVA since 4.x
77 # vm_ovf="${dist_dir}/${vm_fprefix}.ovf"
78 vm_ova="${dist_dir}/${vm_fprefix}.ova"
79 product_name="${vendor} VirtualMachine (${vm_arch})"
80
81 _info() {
82     echo "I: $*"
83 }
84
85 clean_buildvm() {
86 # TODO: remove whenever done
87 VBoxManage storagectl "${vm_name}" \
88     --name "SATA Controller" --remove || :
89 VBoxManage storagectl "${vm_name}" \
90     --name "IDE Controller" --remove || :
91
92 VBoxManage closemedium disk "${vm_disk}" || :
93 VBoxManage unregistervm "${vm_name}" --delete || :
94 rm -f ${vm_disk}
95 }
96
97 clean_buildvm >&/dev/null                   # clean things up
98
99 #exit
100 # Check that no previous VM was left (can happen if previous failures
101 # were "valid")
102 if VBoxManage list vms | grep -q "^\"${vm_name}\".*"; then
103     echo "VM $vm_name still exists -- something is wrong, Can't continue" >&2
104     exit 1
105 fi
106
107 _info Assure build directory
108 mkdir -p $build_dir
109
110 _info Create HardDisk for the VM
111 VBoxManage createhd --filename $vm_disk \
112     --size 40960 --format VDI
113   # documented but not implemented:
114   #  --comment "Drive for NeuroDebian VM installer"
115
116 _info Create VM
117 VBoxManage createvm --name "${vm_name}" --register \
118     --ostype "${vm_ostype}" --basefolder $build_dir
119
120 _info Tune VM
121 VBoxManage modifyvm "${vm_name}" \
122     --audio alsa \
123     --audiocontroller  ac97 \
124     --boot1 disk \
125     --boot2 dvd \
126     --cpus 1 \
127     --ioapic  on \
128     --memory 1048 \
129     --mouse usbtablet \
130     --nic1 nat \
131     --rtcuseutc on \
132     --usb on \
133     --vram 32
134
135 # Add HD controllers
136 VBoxManage storagectl "${vm_name}" \
137     --name "IDE Controller" \
138     --add ide \
139     --controller "PIIX4"
140 VBoxManage storageattach "${vm_name}" \
141     --storagectl "IDE Controller" \
142     --port 0 \
143     --device 0 \
144     --type dvddrive \
145     --medium  $di_cd
146
147 VBoxManage storagectl "${vm_name}" \
148     --name "SATA Controller" \
149     --add sata \
150     --controller "IntelAHCI"
151
152 VBoxManage storageattach  "${vm_name}" \
153     --storagectl "SATA Controller" \
154     --port 0 \
155     --device 0 \
156     --type hdd \
157     --medium "${vm_disk}"
158
159 VBoxManage showvminfo  "${vm_name}"
160
161 _info "Run Debian Installer"
162 # When boot menu appears you will have to
163 # press Esc
164 # type  auto url=$di_host
165 # press Enter"
166
167 # start local web server to serve preseed file
168 cd $(dirname ${di_preseed}) && python -m SimpleHTTPServer ${di_port} >&/dev/null &
169
170 VBoxManage startvm "${vm_name}"
171
172 sleep 5                         # give some time to make sure we get to menu
173 # Send our sequence -- cruel way
174 #VBoxManage controlvm "${vm_name}" keyboardputscancode \
175 #    01 81 \
176 #    1e 9e 16 96 14 94 18 98 39 b9 16 96 13 93 26 a6 0d 8d 23 a3 15 95 20 a0 13 93 1e 9e \
177 #    1c 9c # ESCAPE, auto url=hydra, ENTER
178 echo "typeGuest \"${vm_name}\" \"&ESC;Wauto url=${di_host}|;${di_port}/$(basename ${di_preseed})&ENTER;\"" | /usr/lib/virtualbox/vboxshell.py
179
180 # wait for it to finish! -- found no cleaner way :-/
181 sleep 10
182 while VBoxManage showvminfo "${vm_name}" | grep -q running; do
183     sleep 5
184 done
185
186 # kill the web server
187 #kill ${webserver_pid}
188
189 _info Compacting VDI
190 sudo ./nd_compactvdi "${vm_disk}"
191
192 _info Exporting the appliance
193 if [ -e "$vm_ova" ]; then
194     vm_ova_old=$vm_ova.`date +"20%y%m%d%H%M"`
195     _info Previous OVA image exists, renaming it to $vm_ova_old
196     mv "$vm_ova" "$vm_ova_old"
197 fi
198 VBoxManage export "${vm_name}" -o "$vm_ova" \
199         --vsys 0 \
200         --product "${product_name}" \
201         --producturl "${product_url}" \
202         --vendor "${vendor}" \
203         --vendorurl "http://neuro.debian.net" \
204         --version "$vm_version" \
205         --eula "$eula"
206
207 _info Cleaning after ourselves
208 clean_buildvm
209
210 _info Testing import of the appliance
211 VBoxManage import "$vm_ova" --vsys 0 --eula accept
212
213 _info Starting imported machine
214 VBoxManage startvm "${vm_name}"