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

remove --prompt mode
[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       ;;
82   esac
83   shift
84 done
85
86 if [ -z "${HOSTNAME:-}" ]; then
87   echo >&2 'E: hostname is required'
88   exit 2
89 fi
90
91 BASEDIR=$(cd ${0%/*}; pwd)
92
93 tmpdir=$(mktemp -d)
94 tar -C $BASEDIR/preseed/$SUITE -cf $tmpdir/commands.tar commands
95
96 extra_args="auto $CONSOLE \
97 hostname=${HOSTNAME%%.*} \
98 domain=${HOSTNAME#*.}"
99
100 if [ -n "$IPADDRESS" ]; then
101   if [ -z "$GATEWAY" ] && [ -x $(command -v ipcalc) ]; then
102     # default to the first IP in the network
103     GATEWAY=$(ipcalc $IPADDRESS | grep HostMin | awk '{print $2}')
104   fi
105   NETMASK="${IPADDRESS#*/}"
106   IPADDRESS="${IPADDRESS%/*}"
107   : ${NAMESERVER:=$GATEWAY}
108
109   extra_args="$extra_args netcfg/disable_dhcp=true \
110   netcfg/get_ipaddress=$IPADDRESS \
111   netcfg/get_netmask=$NETMASK \
112   netcfg/get_gateway=$GATEWAY \
113   netcfg/get_nameservers=$NAMESERVER"
114
115   BRIDGE=wan
116
117 else
118   extra_args="$extra_args netcfg/disable_dhcp=false"
119   BRIDGE=virt-br
120 fi
121
122 if [ -z "$ARCH" ]; then
123   ARCH=$(dpkg --print-architecture)
124 fi
125
126 cat >&2 <<_eof
127   hostname: $HOSTNAME
128       arch: $ARCH
129        ram: $RAM MiB
130       disk: $DISK GiB
131    console: $CONSOLE
132 net bridge: $BRIDGE
133 _eof
134 if [ -n "$IPADDRESS" ]; then
135   cat >&2 <<_eof
136  ipaddress: $IPADDRESS
137    netmask: $NETMASK
138    gateway: $GATEWAY
139 nameserver: $NAMESERVER
140 _eof
141 else
142   cat >&2 <<_eof
143       dhcp: true
144 _eof
145 fi
146
147 echo >&2
148
149 set -v
150 virsh --connect=$QEMU_URI vol-create-as $STORAGE_POOL virt-$HOSTNAME ${DISK}G
151
152 [ $DEBUG = 1 ] && DEBUGFLAG=--debug
153
154 virt-install --connect=$QEMU_URI \
155   ${DEBUGFLAG:-} \
156   -n $HOSTNAME \
157   -r $RAM \
158   --disk vol=$STORAGE_POOL/virt-$HOSTNAME \
159   -w bridge=$BRIDGE \
160   --graphics=vnc \
161   --serial=pty \
162   --watchdog i6300esb,action=reset \
163   --os-variant=debian$SUITE \
164   -l $BASEDIR/installer-amd64 \
165   --initrd-inject=$BASEDIR/preseed/$SUITE/preseed.cfg \
166   --initrd-inject=$tmpdir/commands.tar \
167   --noautoconsole --autostart \
168   --extra-args="$extra_args" \
169   "$@"
170
171 virsh --connect=$QEMU_URI console $HOSTNAME
172 virsh --connect=$QEMU_URI "start $HOSTNAME; console $HOSTNAME"