4 # Copyright (C) 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 readonly self=$(readlink -f $0)
25 readonly ensure_dirs=@PKGLIBDIR@/ensure-dirs
26 readonly action_shortcuts=( start stop restart status watcher )
27 readonly default_nodecount=5
28 readonly default_instcount=10
29 readonly default_netprefix=192.0.2
30 readonly default_netdev=eth0
31 readonly default_initscript=@SYSCONFDIR@/init.d/ganeti
32 readonly cluster_name=cluster
37 # Instances: .100-.254
38 readonly first_node_ipaddr_octet=10
39 readonly first_inst_ipaddr_octet=100
41 readonly max_node_count=$((first_inst_ipaddr_octet - first_node_ipaddr_octet))
42 readonly max_instance_count=$((255 - first_inst_ipaddr_octet))
45 echo "Usage: $0 [-E] [-N] [-c <number>] [-i <number>] [-p <prefix>]"\
46 '[-n <netdev>] [-I <path>] <directory>'
49 echo " -c Number of virtual nodes (defaults to $default_nodecount)"
50 echo " -i Number of instances (defaults to $default_instcount)"
51 echo " -p IPv4 network prefix (defaults to $default_netprefix)"
52 echo ' -n Network device for virtual IP addresses (defaults to'\
54 echo " -I Path to init script (defaults to $default_initscript)"
55 echo ' -E Do not modify /etc/hosts'
56 echo ' -N Do not configure networking'
59 # Variables for options
60 nodecount=$default_nodecount
61 instcount=$default_instcount
62 netprefix=$default_netprefix
63 netdev=$default_netdev
64 initscript=$default_initscript
69 while getopts :hENc:p:n:i:I: opt; do
77 if [[ "$nodecount" != +([0-9]) ]]; then
78 echo "Invalid node count number: $nodecount" >&2
80 elif (( nodecount > max_node_count )); then
81 echo "Node count must be $max_node_count or lower" >&2
87 if [[ "$instcount" != +([0-9]) ]]; then
88 echo "Invalid instance count number: $instcount" >&2
90 elif (( instcount > max_instance_count )); then
91 echo "Instance count must be $max_instance_count or lower" >&2
97 if [[ "$netprefix" != +([0-9]).+([0-9]).+([0-9]) ]]; then
98 echo "Invalid network prefix: $netprefix" >&2
104 if ! ip link show $netdev >/dev/null; then
105 echo "Invalid network device: $netdev" >&2
111 if [[ ! -x $initscript ]]; then
112 echo "Init script '$initscript' is not executable" >&2
123 echo "Invalid option: -$OPTARG" >&2
128 echo "Option -$OPTARG requires an argument" >&2
135 shift $((OPTIND - 1))
137 if [[ "$#" != 1 ]]; then
142 readonly rootdir=$1; shift
144 if [[ ! -d "$rootdir" ]]; then
145 echo "Directory '$rootdir' does not exist!" >&2
149 if (( $nodecount < 1 )); then
150 echo "Must create at least one node, currently requested $nodecount" >&2
157 echo "node$((number + 1))"
160 instance_hostname() {
163 echo "instance$((number + 1))"
169 echo "$netprefix.$((first_node_ipaddr_octet + number))"
175 echo "$netprefix.$((first_inst_ipaddr_octet + number))"
180 local -r nodedir=$rootdir/$(node_hostname $number)
182 echo "Setting up node '$(node_hostname $number)' ..." >&2
184 if [[ ! -d $nodedir ]]; then
189 $nodedir@SYSCONFDIR@/default \
190 $nodedir@LOCALSTATEDIR@/lock\
191 $nodedir@LOCALSTATEDIR@/{lib,log,run}/ganeti
193 GANETI_HOSTNAME=$(node_hostname $number) \
194 GANETI_ROOTDIR=$nodedir \
197 local -r daemon_args="-b $(node_ipaddr $number)"
199 cat > $nodedir/etc/default/ganeti <<EOF
200 # Default settings for virtual node $i
201 NODED_ARGS='--no-mlock $daemon_args'
203 RAPI_ARGS='$daemon_args'
204 CONFD_ARGS='$daemon_args'
206 export GANETI_ROOTDIR='$nodedir'
207 export GANETI_HOSTNAME='$(node_hostname $number)'
210 cat > $nodedir/cmd <<EOF
213 export GANETI_ROOTDIR='$nodedir'
214 export GANETI_HOSTNAME='$(node_hostname $number)'
218 chmod +x $nodedir/cmd
222 for ((i=0; i < nodecount; ++i)); do
228 echo 'Configuring /etc/hosts ...' >&2
231 local -r tmpfile=$(mktemp /etc/hosts.vcluster.XXXXX)
232 trap "rm -f $tmpfile" EXIT
234 egrep -v "^$netprefix.[[:digit:]]+[[:space:]]" /etc/hosts
235 echo "$netprefix.1 $cluster_name"
236 for ((i=0; i < nodecount; ++i)); do
237 echo "$(node_ipaddr $i) $(node_hostname $i)"
239 for ((i=0; i < instcount; ++i)); do
240 echo "$(instance_ipaddr $i) $(instance_hostname $i)"
243 chmod 0644 $tmpfile && \
244 mv $tmpfile /etc/hosts && \
249 setup_network_interfaces() {
250 echo 'Configuring network ...' >&2
251 for ((i=0; i < nodecount; ++i)); do
252 local ipaddr="$(node_ipaddr $i)/32"
253 ip addr del "$ipaddr" dev "$netdev" || :
254 ip addr add "$ipaddr" dev "$netdev"
259 echo 'Configuring helper scripts ...' >&2
260 for action in "${action_shortcuts[@]}"; do
263 for ((i=0; i < nodecount; ++i)); do
264 local name=$(node_hostname $i)
265 if [[ $action = watcher ]]; then
266 echo "echo 'Running watcher for virtual node \"$name\" ..."
267 echo "$name/cmd ganeti-watcher \"\$@\""
269 echo "echo 'Action \"$action\" for virtual node \"$name\" ...'"
270 echo "$name/cmd $initscript $action \"\$@\""
273 } > $rootdir/$action-all
274 chmod +x $rootdir/$action-all
280 Virtual cluster setup is complete.
282 Root directory: $rootdir
283 Cluster name: $cluster_name
286 echo 'Nodes:' $(for ((i=0; i < nodecount; ++i)); do node_hostname $i; done)
291 cd $rootdir && node1/cmd gnt-cluster init --no-etc-hosts \\
292 --no-ssh-init --no-lvm-storage --no-drbd-storage $cluster_name
294 Change cluster settings:
295 cd $rootdir && node1/cmd gnt-cluster modify \\
296 --enabled-hypervisors=fake --specs-nic-count=min=0 \\
297 --specs-disk-size=min=0 --specs-disk-count=min=0
300 cd $rootdir && node1/cmd gnt-node add --no-ssh-key-check node2
305 if [[ -n "$etchosts" ]]; then
308 if [[ -n "$networking" ]]; then
309 setup_network_interfaces
316 # vim: set sw=2 sts=2 et :