]> git.madduck.net Git - code/vinst.git/blob - vinst

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

wget -c with -m makes no sense
[code/vinst.git] / vinst
1 #!/bin/sh
2
3 set -e
4
5 # defaults
6 RAM=512
7 DISK=20
8 CPUS=1
9 STORAGE_POOL=default
10 QEMU_URI=qemu:///system
11 SUITE=wheezy
12 CONSOLE='console=tty0 console=ttyS0,115200n8'
13 DEBUG=0
14 WIPE=0
15 RESTART=0
16
17 while [ -n "$1" ]; do
18   case "$1" in
19     (--debug)
20       DEBUG=1
21       ;;
22     (-S|--suite)
23       shift
24       if [ -z "$1" ]; then
25         echo >&2 'E: --suite/-S needs suite argument'
26         exit 1
27       fi
28       SUITE="$1"
29       ;;
30     (-n|--name)
31       shift
32       if [ -z "$1" ]; then
33         echo >&2 'E: --name/-n needs hostname argument'
34         exit 1
35       fi
36       HOSTNAME="$1"
37       ;;
38     (-i|--ipaddress)
39       shift
40       if [ -z "$1" ]; then
41         echo >&2 'E: --ipaddress/-i needs IP address/netmask'
42         exit 1
43       fi
44       case "$1" in
45         (*/*) :;;
46         (*)
47           echo >&2 'E: missing netmask'
48           exit 1
49           ;;
50       esac
51       IPADDRESS="$1"
52       ;;
53     (-d|--dns)
54       shift
55       if [ -z "$1" ]; then
56         echo >&2 'E: --dns/-d needs IP address'
57         exit 1
58       fi
59       NAMESERVER="$1"
60       ;;
61     (-g|--gateway)
62       shift
63       if [ -z "$1" ]; then
64         echo >&2 'E: --gateway/-g needs IP address'
65         exit 1
66       fi
67       GATEWAY="$1"
68       ;;
69     (-r|--ram)
70       shift
71       if [ -z "$1" ]; then
72         echo >&2 'E: --ram/-r needs number (megabytes)'
73         exit 1
74       fi
75       RAM="$1"
76       ;;
77     (-s|--size)
78       shift
79       if [ -z "$1" ]; then
80         echo >&2 'E: --size/-s needs size argument'
81         exit 1
82       fi
83       DISK="$1"
84       ;;
85     (-a|--arch)
86       shift
87       if [ -z "$1" ]; then
88         echo >&2 'E: --arch/-a needs arch argument'
89         exit 1
90       fi
91       ARCH="$1"
92       ;;
93     (-m|--mac)
94       shift
95       if [ -z "$1" ]; then
96         echo >&2 'E: --mac/-m needs mac address argument'
97         exit 1
98       fi
99       MAC="$1"
100       ;;
101     (-c|--cpus)
102       shift
103       if [ -z "$1" ]; then
104         echo >&2 'E: --cpus/-c needs integer argument'
105         exit 1
106       fi
107       CPUS="$1"
108       ;;
109     (--debug)
110       DEBUG=1
111       set -vx
112       ;;
113     (--wipe)
114       WIPE=1
115       ;;
116     (--restart)
117       RESTART=1
118       ;;
119   esac
120   shift
121 done
122
123 if [ -z "${HOSTNAME:-}" ]; then
124   echo >&2 'E: hostname is required'
125   exit 2
126 fi
127
128 BASEDIR=$(cd ${0%/*}; pwd)
129
130 tmpdir=$(mktemp -d)
131 tar -C $BASEDIR/preseed/$SUITE -cf $tmpdir/commands.tar commands
132
133 extra_kargs="auto $CONSOLE \
134 netcfg/hostname=${HOSTNAME%%.*} \
135 netcfg/domain=${HOSTNAME#*.}"
136
137 extra_opts=
138 if [ -n "$DEBUG" ]; then
139   extra_opts='--debug'
140 fi
141
142 if [ -n "$IPADDRESS" ]; then
143   if [ -z "$GATEWAY" ] && [ -x $(command -v ipcalc) ]; then
144     # default to the first IP in the network
145     GATEWAY=$(ipcalc $IPADDRESS | grep HostMin | awk '{print $2}')
146   fi
147   NETMASK="${IPADDRESS#*/}"
148   IPADDRESS="${IPADDRESS%/*}"
149   : ${NAMESERVER:=$GATEWAY}
150
151   extra_kargs="$extra_kargs netcfg/disable_dhcp=true \
152   netcfg/get_ipaddress=$IPADDRESS \
153   netcfg/get_netmask=$NETMASK \
154   netcfg/get_gateway=$GATEWAY \
155   netcfg/get_nameservers=$NAMESERVER"
156
157   BRIDGE=wan
158
159 else
160   extra_kargs="$extra_kargs netcfg/disable_dhcp=false"
161   BRIDGE=virt-br
162 fi
163
164 if [ -z "$ARCH" ]; then
165   ARCH=$(dpkg --print-architecture)
166 fi
167
168 if [ -z "$MAC" ]; then
169   bytes=$(dd status=noxfer if=/dev/urandom bs=3 count=1 | hexdump -v -e '/1 ":%02X"')
170   MAC=52:54:00$bytes
171 fi
172
173 cat >&2 <<_eof
174   hostname: $HOSTNAME
175       arch: $ARCH
176      suite: $SUITE
177        ram: $RAM MiB
178       disk: $DISK GiB
179       cpus: $CPUS
180       wipe: $WIPE
181    console: $CONSOLE
182    restart: $RESTART
183 net bridge: $BRIDGE
184 macaddress: $MAC
185 _eof
186 if [ -n "$IPADDRESS" ]; then
187   cat >&2 <<_eof
188  ipaddress: $IPADDRESS
189    netmask: $NETMASK
190    gateway: $GATEWAY
191 nameserver: $NAMESERVER
192 _eof
193 else
194   cat >&2 <<_eof
195       dhcp: true
196 _eof
197 fi
198
199 echo >&2
200
201 virsh --connect=$QEMU_URI vol-create-as $STORAGE_POOL virt-$HOSTNAME ${DISK}G
202 if [ $WIPE = 1 ]; then
203   echo >&2 "Wiping disk…"
204   virsh --connect=$QEMU_URI vol-wipe --pool $STORAGE_POOL --algorithm zero virt-$HOSTNAME
205 fi
206
207 if [ $DEBUG = 1 ]; then
208   DEBUGFLAG=--debug
209   extra_args="$extra_args DEBCONF_DEBUG=5"
210 fi
211
212 virt-install --connect=$QEMU_URI \
213   ${DEBUGFLAG:-} \
214   -n $HOSTNAME \
215   --vcpus=$CPUS \
216   -r $RAM \
217   --disk vol=$STORAGE_POOL/virt-$HOSTNAME \
218   --network bridge=$BRIDGE,mac=$MAC \
219   --graphics=vnc \
220   --serial=pty \
221   --watchdog i6300esb,action=reset \
222   --os-variant=debianwheezy \
223   --initrd-inject=$BASEDIR/preseed/$SUITE/preseed.cfg \
224   --initrd-inject=$tmpdir/commands.tar \
225   --extra-args="$extra_kargs" \
226   --prompt --noautoconsole --autostart $extra_opts \
227   -l $BASEDIR/installer-amd64/$SUITE \
228   "$@"
229
230 virsh --connect=$QEMU_URI console $HOSTNAME
231
232 if [ $RESTART = 1 ]; then
233   sleep 5
234   virsh --connect=$QEMU_URI "start $HOSTNAME; console $HOSTNAME"
235 fi