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

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