]> git.donarmstrong.com Git - neurodebian.git/blob - vm/tools/nd_createappliance
NF: fully (nearly) automated construction of VM image
[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 set -eu
6
7 # TODO: arguments later on to become cmdline args
8 #iso=debian-squeeze-di-beta1-amd64-businesscard.iso
9 iso=debian-testing-i386-businesscard.iso
10 di_cd=$(readlink -f $PWD/../../../neurodebian-images/$iso)
11 di_host=hydra                   # where to look for di preseed
12
13 build_dir=$PWD/build
14 dist_dir=$PWD/dist
15 vendor="NeuroDebian"
16 vm_version="6.0.1"
17 vm_ostype=Debian
18
19 # Generic definitions
20 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.
21
22 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."
23
24
25 # Computed settings
26 #vm_basepath=
27
28 # By default 32bit unless installer image has amd64
29 # TODO: might need to make more robust?
30 vm_arch=i386
31 vm_arch_name=" (32bit)"
32 if file $di_cd | grep -q ' amd64 '; then
33     vm_arch=amd64
34     vm_ostype+="_64"
35     vm_arch_name=" (64bit)"
36 fi
37
38 vm_disk="$build_dir/nd-${vm_version}_${vm_arch}.vdi"
39 vm_name="${vendor} VirtualMachine${vm_arch_name}"
40 vm_ovf="${dist_dir}/${vendor}_${vm_version}_${vm_arch}.ovf"
41 product_name="${vendor} VirtualMachine (${vm_arch})"
42
43 _info() {
44     echo "I: $*"
45 }
46
47 clean_buildvm() {
48 # TODO: remove whenever done
49 VBoxManage storagectl "${vm_name}" \
50     --name "SATA Controller" --remove || :
51 VBoxManage storagectl "${vm_name}" \
52     --name "IDE Controller" --remove || :
53
54 VBoxManage closemedium disk "${vm_disk}" || :
55 VBoxManage unregistervm "${vm_name}" --delete || :
56 rm -f ${vm_disk}
57 }
58
59 clean_buildvm                   # clean things up
60
61 # Check that no previous VM was left (can happen if previous failures
62 # were "valid")
63 if VBoxManage list vms | grep -q "^\"${vm_name}\".*"; then
64     echo >&2, "VM $vm_name still exists -- something is wrong, Can't continue"
65     exit 1
66 fi
67
68 _info Assure build directory
69 mkdir -p $build_dir
70
71 _info Create HardDisk for the VM
72 VBoxManage createhd --filename $vm_disk \
73     --size 20000 --format VDI
74   # documented but not implemented:
75   #  --comment "Drive for NeuroDebian VM installer"
76
77 _info Create VM
78 VBoxManage createvm --name "${vm_name}" --register \
79     --ostype "${vm_ostype}" --basefolder $build_dir
80
81 _info Tune VM
82 VBoxManage modifyvm "${vm_name}" \
83     --audio alsa \
84     --audiocontroller  ac97 \
85     --boot1 disk \
86     --boot2 dvd \
87     --cpus 1 \
88     --ioapic  on \
89     --memory 1048 \
90     --mouse usbtablet \
91     --nic1 nat \
92     --rtcuseutc on \
93     --usb on \
94     --vram 32
95
96 # Add HD controllers
97 VBoxManage storagectl "${vm_name}" \
98     --name "IDE Controller" \
99     --add ide \
100     --controller "PIIX4"
101 VBoxManage storageattach "${vm_name}" \
102     --storagectl "IDE Controller" \
103     --port 0 \
104     --device 0 \
105     --type dvddrive \
106     --medium  $di_cd
107
108 VBoxManage storagectl "${vm_name}" \
109     --name "SATA Controller" \
110     --add sata \
111     --controller "IntelAHCI"
112
113 VBoxManage storageattach  "${vm_name}" \
114     --storagectl "SATA Controller" \
115     --port 0 \
116     --device 0 \
117     --type hdd \
118     --medium "${vm_disk}"
119
120 VBoxManage showvminfo  "${vm_name}"
121
122 _info "Run Debian Installer"
123 # When boot menu appears you will have to
124 # press Esc
125 # type  auto url=$di_host
126 # press Enter"
127
128 VBoxManage startvm "${vm_name}"
129
130 sleep 5                         # give some time to make sure we get to menu
131 # Send our sequence -- cruel way
132 #VBoxManage controlvm "${vm_name}" keyboardputscancode \
133 #    01 81 \
134 #    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 \
135 #    1c 9c # ESCAPE, auto url=hydra, ENTER
136 echo "typeGuest \"${vm_name}\" \"&ESC;Wauto url=${di_host}&ENTER;\"" | /usr/lib/virtualbox/vboxshell.py
137
138 : "
139 oops -- asked either I want to store Partitioning on the disk...
140
141 didn't happen before
142 "
143
144 # wait for it to finish! -- found no cleaner way :-/
145 sleep 10
146 while VBoxManage showvminfo "${vm_name}" | grep -q running; do
147     sleep 5
148 done
149
150 _info Compacting VDI
151 sudo ./nd_compactvdi "${vm_disk}"
152
153 _info Exporting the appliance
154
155 VBoxManage export "${vm_name}" -o "$vm_ovf" \
156         --vsys 0 \
157         --product "${vendor} VirtualMachine (${vm_arch})" \
158         --producturl "http://neuro.debian.net/vm.html" \
159         --vendor "${vendor}" \
160         --vendorurl "http://neuro.debian.net" \
161         --version "$vm_version" \
162         --eula "$eula"
163
164 _info Cleaning after ourselves
165 clean_buildvm
166
167 _info Testing import of the appliance
168 VBoxManage import "$vm_ovf" --vsys 0 --eula accept
169
170 _info Starting imported machine
171 VBoxManage startvm "${vm_name}"