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

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