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

fixup! set default storage pool name
[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   esac
71   shift
72 done
73
74 if [ -z "${HOSTNAME:-}" ]; then
75   echo >&2 'E: hostname is required'
76   exit 2
77 fi
78
79 BASEDIR=$(cd ${0%/*}; pwd)
80
81 tmpdir=$(mktemp -d)
82 tar -C $BASEDIR/preseed/$SUITE -cf $tmpdir/commands.tar commands
83
84 extra_args="auto $CONSOLE \
85 hostname=${HOSTNAME%%.*} \
86 domain=${HOSTNAME#*.}"
87
88 if [ -n "$IPADDRESS" ]; then
89   if [ -z "$GATEWAY" ] && [ -x $(command -v ipcalc) ]; then
90     # default to the first IP in the network
91     GATEWAY=$(ipcalc $IPADDRESS | grep HostMin | awk '{print $2}')
92   fi
93   NETMASK="${IPADDRESS#*/}"
94   IPADDRESS="${IPADDRESS%/*}"
95   : ${NAMESERVER:=$GATEWAY}
96
97   extra_args="$extra_args netcfg/disable_dhcp=true \
98   netcfg/get_ipaddress=$IPADDRESS \
99   netcfg/get_netmask=$NETMASK \
100   netcfg/get_gateway=$GATEWAY \
101   netcfg/get_nameservers=$NAMESERVER"
102
103   BRIDGE=wan
104
105 else
106   extra_args="$extra_args netcfg/disable_dhcp=false"
107   BRIDGE=virt-br
108 fi
109
110 cat >&2 <<_eof
111   hostname: $HOSTNAME
112        ram: $RAM MiB
113       disk: $DISK GiB
114    console: $CONSOLE
115 net bridge: $BRIDGE
116 _eof
117 if [ -n "$IPADDRESS" ]; then
118   cat >&2 <<_eof
119  ipaddress: $IPADDRESS
120    netmask: $NETMASK
121    gateway: $GATEWAY
122 nameserver: $NAMESERVER
123 _eof
124 else
125   cat >&2 <<_eof
126       dhcp: true
127 _eof
128 fi
129
130 exit 0
131
132 exec virt-install --connect=$QEMU_URI \
133   -n $HOSTNAME \
134   -r $RAM \
135   --disk vol=$STORAGE_POOL/virt-$HOSTNAME \
136   -w bridge=$BRIDGE \
137   --graphics=vnc \
138   --serial=pty \
139   --watchdog i6300esb,action=reset \
140   --os-variant=debian$SUITE \
141   -l $BASEDIR/installer-amd64 \
142   --initrd-inject=$BASEDIR/preseed/$SUITE/preseed.cfg \
143   --initrd-inject=$tmpdir/commands.tar \
144   --extra-args="$extra_args" \
145   --autostart \
146   "$@"