]> 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:

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