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

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