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 )
27 readonly default_nodecount=5
28 readonly default_instcount=10
29 readonly default_netprefix=192.0.2
30 readonly default_netdev=eth0
31 readonly cluster_name=cluster
36 # Instances: .100-.254
37 readonly first_node_ipaddr_octet=10
38 readonly first_inst_ipaddr_octet=100
40 readonly max_node_count=$((first_inst_ipaddr_octet - first_node_ipaddr_octet))
41 readonly max_instance_count=$((255 - first_inst_ipaddr_octet))
44 echo "Usage: $0 [-E] [-N] [-c <number>] [-i <number>] [-p <prefix>]"\
45 '[-n <netdev>] <directory>'
48 echo " -c Number of virtual nodes (defaults to $default_nodecount)"
49 echo " -i Number of instances (defaults to $default_instcount)"
50 echo " -p IPv4 network prefix (defaults to $default_netprefix)"
51 echo ' -n Network device for virtual IP addresses (defaults to'\
53 echo ' -E Do not modify /etc/hosts'
54 echo ' -N Do not configure networking'
57 # Variables for options
58 nodecount=$default_nodecount
59 instcount=$default_instcount
60 netprefix=$default_netprefix
61 netdev=$default_netdev
66 while getopts :hENc:p:n:i: opt; do
74 if [[ "$nodecount" != +([0-9]) ]]; then
75 echo "Invalid node count number: $nodecount" >&2
77 elif (( nodecount > max_node_count )); then
78 echo "Node count must be $max_node_count or lower" >&2
84 if [[ "$instcount" != +([0-9]) ]]; then
85 echo "Invalid instance count number: $instcount" >&2
87 elif (( instcount > max_instance_count )); then
88 echo "Instance count must be $max_instance_count or lower" >&2
94 if [[ "$netprefix" != +([0-9]).+([0-9]).+([0-9]) ]]; then
95 echo "Invalid network prefix: $netprefix" >&2
101 if ! ip link show $netdev >/dev/null; then
102 echo "Invalid network device: $netdev" >&2
113 echo "Invalid option: -$OPTARG" >&2
118 echo "Option -$OPTARG requires an argument" >&2
125 shift $((OPTIND - 1))
127 if [[ "$#" != 1 ]]; then
132 readonly rootdir=$1; shift
134 if [[ ! -d "$rootdir" ]]; then
135 echo "Directory '$rootdir' does not exist!" >&2
139 if (( $nodecount < 1 )); then
140 echo "Must create at least one node, currently requested $nodecount" >&2
147 echo "node$((number + 1))"
150 instance_hostname() {
153 echo "instance$((number + 1))"
159 echo "$netprefix.$((first_node_ipaddr_octet + number))"
165 echo "$netprefix.$((first_inst_ipaddr_octet + number))"
170 local -r nodedir=$rootdir/$(node_hostname $number)
172 echo "Setting up node '$(node_hostname $number)' ..." >&2
174 if [[ ! -d $nodedir ]]; then
179 $nodedir@SYSCONFDIR@/default \
180 $nodedir@LOCALSTATEDIR@/lock\
181 $nodedir@LOCALSTATEDIR@/{lib,log,run}/ganeti
183 GANETI_HOSTNAME=$(node_hostname $number) \
184 GANETI_ROOTDIR=$nodedir \
187 local -r daemon_args="-b $(node_ipaddr $number)"
189 cat > $nodedir/etc/default/ganeti <<EOF
190 # Default settings for virtual node $i
191 NODED_ARGS='--no-mlock $daemon_args'
193 RAPI_ARGS='$daemon_args'
194 CONFD_ARGS='$daemon_args'
196 export GANETI_ROOTDIR='$nodedir'
197 export GANETI_HOSTNAME='$(node_hostname $number)'
200 cat > $nodedir/cmd <<EOF
203 export GANETI_ROOTDIR='$nodedir'
204 export GANETI_HOSTNAME='$(node_hostname $number)'
208 chmod +x $nodedir/cmd
212 for ((i=0; i < nodecount; ++i)); do
218 echo 'Configuring /etc/hosts ...' >&2
221 local -r tmpfile=$(mktemp /etc/hosts.vcluster.XXXXX)
222 trap "rm -f $tmpfile" EXIT
224 egrep -v "^$netprefix.[[:digit:]]+[[:space:]]" /etc/hosts
225 echo "$netprefix.1 $cluster_name"
226 for ((i=0; i < nodecount; ++i)); do
227 echo "$(node_ipaddr $i) $(node_hostname $i)"
229 for ((i=0; i < instcount; ++i)); do
230 echo "$(instance_ipaddr $i) $(instance_hostname $i)"
233 chmod 0644 $tmpfile && \
234 mv $tmpfile /etc/hosts && \
239 setup_network_interfaces() {
240 echo 'Configuring network ...' >&2
241 for ((i=0; i < nodecount; ++i)); do
242 local ipaddr="$(node_ipaddr $i)/32"
243 ip addr del "$ipaddr" dev "$netdev" || :
244 ip addr add "$ipaddr" dev "$netdev"
249 echo 'Configuring helper scripts ...' >&2
250 for action in "${action_shortcuts[@]}"; do
253 for ((i=0; i < nodecount; ++i)); do
254 local name=$(node_hostname $i)
255 echo "echo 'Action \"$action\" for virtual node \"$name\" ...'"
256 echo "$name/cmd /etc/init.d/ganeti $action"
258 } > $rootdir/$action-all
259 chmod +x $rootdir/$action-all
265 Virtual cluster setup is complete.
267 Root directory: $rootdir
268 Cluster name: $cluster_name
271 echo 'Nodes:' $(for ((i=0; i < nodecount; ++i)); do node_hostname $i; done)
276 cd $rootdir && node1/cmd gnt-cluster init --no-etc-hosts \\
277 --no-ssh-init --no-lvm-storage --no-drbd-storage $cluster_name
279 Change cluster settings:
280 cd $rootdir && node1/cmd gnt-cluster modify \\
281 --enabled-hypervisors=fake --specs-nic-count=min=0 \\
282 --specs-disk-size=min=0 --specs-disk-count=min=0
285 cd $rootdir && node1/cmd gnt-node add --no-ssh-key-check node2
290 if [[ -n "$etchosts" ]]; then
293 if [[ -n "$networking" ]]; then
294 setup_network_interfaces
301 # vim: set sw=2 sts=2 et :