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

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