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

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