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

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