2 # main autocluster script
4 # Copyright (C) Andrew Tridgell 2008
5 # Copyright (C) Martin Schwenke 2008
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
20 ##BEGIN-INSTALLDIR-MAGIC##
21 # There are better ways of doing this but not if you still want to be
22 # able to run straight out of a git tree. :-)
24 installdir="`dirname \"$0\"`"
26 autocluster=`which $0`
27 installdir="`dirname \"$autocluster\"`"
29 ##END-INSTALLDIR-MAGIC##
36 Usage: autocluster [OPTION] ... <COMMAND>
38 -c <file> specify config file (default is "config")
41 releases=$(list_releases)
44 defconf "WITH_RELEASE" "" \
45 "<string>" "specify preset options for a release using a version string. Possible values are: ${releases}."
48 -e <expr> execute <expr> and exit
49 -E <expr> execute <expr> and continue
50 -x enable script debugging
51 --dump dump config settings and exit
53 configuration options:
64 create cluster CLUSTERNAME
67 create node CLUSTERNAME IP_OFFSET
68 (re)create a single cluster node
71 mount a qemu disk on mnt/
74 unmount a qemu disk from mnt/
85 ###############################
88 fill_text 0 "ERROR: $*" >&2
92 ###############################
94 # Indirectly call a function named by ${1}_${2}
96 local func="$1" ; shift
97 local type="$1" ; shift
99 local f="${func}_${type}"
100 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
104 if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
107 die "No function defined for \"${func}\" \"${type}\""
112 # Note that this will work if you pass "call_func f" because the first
113 # element of the node tuple is the node type. Nice... :-)
118 "$@" $(IFS=: ; echo $n)
122 hack_one_node_with ()
124 local filter="$1" ; shift
133 local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
134 nodes="${nodes}${nodes:+ }${item}"
137 # This also gets used for non-filtering iteration.
138 hack_all_nodes_with ()
143 for_each_node hack_one_node_with "$filter"
152 eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
160 for i in ${!hook_var} ; do
165 # Use with care, since this may clear some autocluster defaults.!
170 eval "$hook_var=\"\""
173 ##############################
175 # common node creation stuff
176 create_node_COMMON ()
181 local template_file="${4:-$NODE_TEMPLATE}"
183 if [ "$SYSTEM_DISK_FORMAT" = "raw" -a "$BASE_FORMAT" != "raw" ] ; then
184 die "Error: if SYSTEM_DISK_FORMAT is \"raw\" then BASE_FORMAT must also be \"raw\"."
187 IPNUM=$(($FIRSTIP + $ip_offset))
188 DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
189 local base_disk="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
191 if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
192 base_disk="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
195 mkdir -p $VIRTBASE/$CLUSTER tmp
198 case "$SYSTEM_DISK_FORMAT" in
200 echo "Creating the disk..."
201 qemu-img create -b "$base_disk" -f qcow2 "$DISK"
202 create_node_configure_image "$DISK" "$type"
205 echo "Creating the disk..."
206 cp -v --sparse=always "$base_disk" "$DISK"
207 create_node_configure_image "$DISK" "$type"
210 echo "Skipping disk image creation as requested"
213 die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
216 set_macaddrs $CLUSTER $ip_offset
218 # Pull the UUID for this node out of the map.
219 UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
221 echo "Creating $NAME.xml"
222 substitute_vars $template_file tmp/$NAME.xml
224 # install the XML file
225 $VIRSH undefine $NAME > /dev/null 2>&1 || true
226 $VIRSH define tmp/$NAME.xml
229 create_node_configure_image ()
239 # Provides an easy way of removing nodes from $NODE.
240 create_node_null () {
244 ##############################
246 hack_nodes_functions=
249 # Expand out any abbreviations in NODES.
258 for i in ${ips//,/ } ; do
259 ns="${ns}${ns:+ }${t}:${i}"
264 for i in $(seq ${ips/-/ }) ; do
265 ns="${ns}${ns:+ }${t}:${i}"
269 ns="${ns}${ns:+ }${n}"
274 # Apply nodes hacks. Some of this is about backward compatibility
275 # but the hacks also fill in the node names and whether they're
276 # part of the CTDB cluster. The order is the order that
277 # configuration modules register their hacks.
278 run_hooks hack_nodes_functions
280 if [ -n "$NUMNODES" ] ; then
281 # Attempt to respect NUMNODES. Reduce the number of CTDB
283 local numnodes=$NUMNODES
287 if [ "$ctdb_node" = 1 ] ; then
288 if [ $numnodes -gt 0 ] ; then
289 numnodes=$(($numnodes - 1))
297 hack_all_nodes_with hack_filter
299 [ $numnodes -gt 0 ] && \
300 die "Can't not use NUMNODES to increase the number of nodes over that specified by NODES. You need to set NODES instead - please read the documentation."
303 # Check IP addresses for duplicates.
305 # This function doesn't modify anything...
308 [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
309 die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
310 ip_offsets="${ip_offsets}${ip_offset}:"
312 hack_all_nodes_with get_ip_offset
315 ##############################
317 sanity_check_cluster_name ()
319 [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
320 die "Cluster names should be restricted to the characters A-Za-z0-9. \
321 Some cluster filesystems have problems with other characters."
326 common_nodelist_hacking ()
328 # Rework the NODES list
331 # Build /etc/hosts and hack the names of the ctdb nodes
332 hosts_line_hack_name ()
334 # Ignore nodes without names (e.g. "null")
335 [ "$node_type" != "null" -a -n "$name" ] || return 0
339 local ip_addr="$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
341 if [ "$ctdb_node" = 1 ] ; then
342 num_ctdb_nodes=$(($num_ctdb_nodes + 1))
343 sname="${CLUSTER}n${num_ctdb_nodes}"
344 hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
347 hosts_line="$ip_addr ${name}.${ld} $name"
350 # This allows you to add a function to your configuration file
351 # to modify hostnames (and other aspects of nodes). This
352 # function can access/modify $name (the existing name),
353 # $node_type and $ctdb_node (1, if the node is a member of the
354 # CTDB cluster, 0 otherwise).
355 if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
356 local old_name="$name"
357 $HOSTNAME_HACKING_FUNCTION
358 if [ "$name" != "$old_name" ] ; then
359 hosts_line="$ip_addr ${name}.${ld} $name"
365 hosts_file="tmp/hosts.$CLUSTER"
367 local num_ctdb_nodes=0
368 local ld=$(echo $DOMAIN | tr A-Z a-z)
369 echo "# autocluster $CLUSTER"
370 hack_all_nodes_with hosts_line_hack_name
374 # Build /etc/ctdb/nodes
377 [ "$ctdb_node" = 1 ] || return 0
378 echo "$IPBASE.$IPNET0.$(($FIRSTIP + $ip_offset))"
379 num_nodes=$(($num_nodes + 1))
381 nodes_file="tmp/nodes.$CLUSTER"
383 hack_all_nodes_with ctdb_nodes_line >$nodes_file
384 : "${NUMNODES:=${num_nodes}}" # Set $NUMNODES if necessary
387 uuid_map="tmp/uuid_map.$CLUSTER"
390 echo "${ip_offset} $(uuidgen) ${node_type}"
392 hack_all_nodes_with uuid_map_line >$uuid_map
395 create_cluster_hooks=
396 cluster_created_hooks=
402 sanity_check_cluster_name
404 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
406 # Run hooks before doing anything else.
407 run_hooks create_cluster_hooks
409 common_nodelist_hacking
411 for_each_node call_func create_node
413 echo "Cluster $CLUSTER created"
416 run_hooks cluster_created_hooks
419 cluster_created_hosts_message ()
421 echo "You may want to add this to your /etc/hosts file:"
425 register_hook cluster_created_hooks cluster_created_hosts_message
430 local single_node_ip_offset="$2"
432 sanity_check_cluster_name
434 mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
436 common_nodelist_hacking
439 set -- $(IFS=: ; echo $n)
440 [ $single_node_ip_offset -eq $2 ] || continue
441 call_func create_node "$@"
443 echo "Requested node created"
445 echo "You may want to update your /etc/hosts file:"
452 ###############################
453 # test the proxy setup
455 export http_proxy=$WEBPROXY
456 wget -O /dev/null $INSTALL_SERVER || \
457 die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
463 kickstart_floppy_create_hooks=
469 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
473 echo "Testing WEBPROXY $WEBPROXY"
476 echo "Creating the disk"
477 qemu-img create -f $BASE_FORMAT "$DISK" $DISKSIZE
480 mkdir -p mnt tmp tmp/ISO
484 echo "Creating kickstart file from template"
485 substitute_vars "$KICKSTART" "tmp/ks.cfg"
487 if [ $INSTALLKEY = "--skip" ]; then
489 --------------------------------------------------------------------------------------
490 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
492 Please enter a valid RHEL install key in your config file like this:
494 INSTALLKEY="1234-5678-0123-4567"
496 The install will continue without an install key in 5 seconds
497 --------------------------------------------------------------------------------------
502 # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
505 (*) ISO="${ISO_DIR}/${ISO}"
508 echo "Creating kickstart floppy"
509 dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
510 mkdosfs tmp/floppy.img
511 mount -o loop -t msdos tmp/floppy.img mnt
513 mount -o loop,ro $ISO tmp/ISO
515 echo "Setting up bootloader"
516 cp tmp/ISO/isolinux/isolinux.bin tmp
517 cp tmp/ISO/isolinux/vmlinuz tmp
518 cp tmp/ISO/isolinux/initrd.img tmp
520 run_hooks kickstart_floppy_create_hooks
527 substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
529 rm -f $KVMLOG/serial.$NAME
531 # boot the install CD
532 $VIRSH create tmp/$NAME.xml
534 echo "Waiting for install to start"
537 # wait for the install to finish
538 if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
540 die "Failed to create base image $DISK"
548 Install finished, base image $DISK created
552 To ensure that this image does not change
554 Note that the root password has been set to $ROOTPASSWORD
559 ###############################
565 DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
579 echo "Creating $NAME.xml"
580 substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
582 # boot the base system
583 $VIRSH create tmp/$NAME.xml
586 ######################################################################
588 # various functions...
590 # Set some MAC address variables based on a hash of the cluster name
591 # plus the node number and each adapter number.
596 local md5=$(echo $cname | md5sum)
597 local nh=$(printf "%02x" $ip_offset)
598 local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
600 MAC1="${mac_prefix}01"
601 MAC2="${mac_prefix}02"
602 MAC3="${mac_prefix}03"
603 MAC4="${mac_prefix}04"
604 MAC5="${mac_prefix}05"
605 MAC6="${mac_prefix}06"
608 # mount a qemu image via nbd
610 echo "Connecting nbd to $1"
613 killall -9 -q $QEMU_NBD || true
614 $QEMU_NBD -p 1300 $1 &
616 [ -r $NBD_DEVICE ] || {
617 mknod $NBD_DEVICE b 43 0
619 umount mnt 2> /dev/null || true
620 nbd-client -d $NBD_DEVICE > /dev/null 2>&1 || true
621 killall -9 -q nbd-client || true
622 nbd-client localhost 1300 $NBD_DEVICE > /dev/null 2>&1 || true &
628 echo "Disconnecting nbd"
630 nbd-client -d $NBD_DEVICE > /dev/null 2>&1 || true
631 killall -9 -q nbd-client || true
632 killall -q $QEMU_NBD || true
639 case "$SYSTEM_DISK_FORMAT" in
643 extra_mount_options=""
647 extra_mount_options=",loop"
650 die "Error: unknown SYSTEM_DISK_FORMAT=${SYSTEM_DISK_FORMAT}."
656 case "$SYSTEM_DISK_FORMAT" in
664 die "Error: unknown SYSTEM_DISK_FORMAT=${SYSTEM_DISK_FORMAT}."
668 # mount a qemu image via nbd
673 local device extra_mount_options
677 echo "Mounting disk ${disk}..."
679 for m in $SYSTEM_DISK_MOUNTS ; do
681 o="${m#*:}" # Offset is after colon
682 m="${m%:*}" # Mountpoint is before colon
683 echo " mount ${m} from offset ${o}"
685 for i in $(seq 1 5); do
686 mount -o offset=${o}${extra_mount_options} $device "mnt${m}" && {
690 umount mnt 2>/dev/null || true
693 [ $mount_ok = 1 ] || die "Failed to mount $disk"
697 echo "Mounted directory does not look like a root filesystem"
703 # unmount a qemu image
705 echo "Unmounting disk"
708 # umounts are done in reverse. Be lazy and construct a reverse
709 # list, since the shell will handle any strange whitespace for
713 for m in $SYSTEM_DISK_MOUNTS ; do
714 umounts="${m%:*}${umounts:+ }${umounts}"
716 for m in $umounts ; do
718 umount "mnt${m}" || umount "mnt${m}" || true
723 # setup the files from $BASE_TEMPLATES/, substituting any variables
724 # based on the config
725 copy_base_dir_substitute_templates ()
729 local d="$BASE_TEMPLATES/$dir"
730 [ -d "$d" ] || return 0
733 for f in $(cd "$d" && find . \! -name '*~') ; do
734 if [ -d "$d/$f" ]; then
737 substitute_vars "$d/$f" "mnt/$f"
739 chmod --reference="$d/$f" "mnt/$f"
745 setup_base_ssh_keys ()
747 # this is needed as git doesn't store file permissions other
749 chmod 600 mnt/etc/ssh/*key mnt/root/.ssh/*
750 chmod 700 mnt/etc/ssh mnt/root/.ssh mnt/root
751 if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
752 echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
753 cat "$HOME/.ssh/id_rsa.pub" >> mnt/root/.ssh/authorized_keys
755 if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
756 echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
757 cat "$HOME/.ssh/id_dsa.pub" >> mnt/root/.ssh/authorized_keys
761 register_hook setup_base_hooks setup_base_ssh_keys
763 setup_base_grub_conf ()
765 echo "Adjusting grub.conf"
766 local o="$EXTRA_KERNEL_OPTIONS" # For readability.
767 sed -e "s/console=ttyS0,19200/console=ttyS0,115200/" \
768 -e "s/ nodmraid//" -e "s/ nompath//" \
769 -e "s/quiet/noapic divider=10${o:+ }${o}/g" mnt/boot/grub/grub.conf -i.org
772 register_hook setup_base_hooks setup_base_grub_conf
779 echo "Copy base files"
780 copy_base_dir_substitute_templates "all"
781 if [ -n "$type" ] ; then
782 copy_base_dir_substitute_templates "$type"
785 run_hooks setup_base_hooks
788 # setup various networking components
791 # This avoids doing anything when we're called from boot_base().
792 if [ -z "$hosts_file" ] ; then
793 echo "Skipping network-related setup"
797 echo "Setting up networks"
798 cat $hosts_file >>mnt/etc/hosts
800 echo "Setting up /etc/ctdb/nodes"
801 mkdir -p mnt/etc/ctdb
802 cp $nodes_file mnt/etc/ctdb/nodes
804 [ "$WEBPROXY" = "" ] || {
805 echo "export http_proxy=$WEBPROXY" >> mnt/etc/bashrc
808 if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
809 echo "Enabling nfs mount of $NFSSHARE"
810 mkdir -p "mnt$NFS_MOUNTPOINT"
811 echo "$NFSSHARE $NFS_MOUNTPOINT nfs intr" >> mnt/etc/fstab
814 mkdir -p mnt/etc/yum.repos.d
815 echo '@@@YUM_TEMPLATE@@@' | substitute_vars - > mnt/etc/yum.repos.d/autocluster.repo
818 register_hook setup_base_hooks setup_network
821 [ -z "$TIMEZONE" ] && {
822 [ -r /etc/timezone ] && {
823 TIMEZONE=`cat /etc/timezone`
825 [ -r /etc/sysconfig/clock ] && {
826 . /etc/sysconfig/clock
829 TIMEZONE="${TIMEZONE// /_}"
831 [ -n "$TIMEZONE" ] || \
832 die "Unable to determine TIMEZONE - please set in config"
835 # substite a set of variables of the form @@XX@@ for the shell
836 # variables $XX in a file.
838 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
839 # contain a filename whose contents are substituted, with variable
840 # substitution applied to those contents. If filename starts with '|'
841 # it is a command instead - however, quoting is extremely fragile.
843 infile="${1:-/dev/null}" # if empty then default to /dev/null
844 outfile="$2" # optional
846 instring=$(cat $infile)
848 # Handle any indirects by looping until nothing changes.
849 # However, only handle 10 levels of recursion.
852 outstring=$(_substitute_vars "$instring" "@@@")
853 [ $? -eq 0 ] || die "Failed to expand template $infile"
855 [ "$instring" = "$outstring" ] && break
857 count=$(($count + 1))
858 [ $count -lt 10 ] || \
859 die "Recursion too deep in $infile - only 10 levels allowed!"
861 instring="$outstring"
864 # Now regular variables.
865 outstring=$(_substitute_vars "$instring" "@@")
866 [ $? -eq 0 ] || die "Failed to expand template $infile"
868 if [ -n "$outfile" ] ; then
869 echo "$outstring" > "$outfile"
876 # Delimiter @@ means to substitute contents of variable.
877 # Delimiter @@@ means to substitute contents of file named by variable.
878 # @@@ supports leading '|' in variable value, which means to excute a
880 _substitute_vars() {(
884 # Get the list of variables used in the template. The grep
885 # gets rid of any blank lines and lines with extraneous '@'s
886 # next to template substitutions.
887 VARS=$(echo "$instring" |
888 sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" |
889 grep '^[A-Z0-9_][A-Z0-9_]*$' |
894 # variable variables are fun .....
897 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
901 if [ "$delimiter" = "@@@" ] ; then
903 c="${f#|}" # Is is a command, signified by a leading '|'?
904 if [ "$c" = "$f" ] ; then
905 # No leading '|', cat file.
909 die "Could not substitute contents of file $f"
912 # Leading '|', execute command.
913 # Quoting problems here - using eval "$c" doesn't help.
917 die "Could not execute command $c"
922 # escape some pesky chars
923 # This first one can be too slow if done using a bash
924 # variable pattern subsitution.
925 s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
928 echo "s#${delimiter}${v}${delimiter}#${s}#g"
931 echo "$instring" | sed -f $tmp
937 which $1 > /dev/null || die "Please install $1 to continue"
940 # Set a variable if it isn't already set. This allows environment
941 # variables to override default config settings.
946 [ "${!v+x}" ] || eval "$v=\"$e\""
952 for i in "${installdir}/config.d/"*.defconf ; do
957 # Print the list of config variables defined in config.d/.
958 get_config_options () {( # sub-shell for local declaration of defconf()
960 defconf() { options="$options $1" ; }
965 # Produce a list of long options, suitable for use with getopt, that
966 # represent the config variables defined in config.d/.
967 getopt_config_options () {
968 local x=$(get_config_options | tr 'A-Z_' 'a-z-')
972 # Unconditionally set the config variable associated with the given
978 local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
979 # unset so defconf will set it
984 # Dump all of the current config variables.
987 for o in $(get_config_options) ; do
988 echo "${o}=\"${!o}\""
993 # $COLUMNS is set in interactive bash shells. It probably isn't set
994 # in this shell, so let's set it if it isn't.
995 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
999 # Print text assuming it starts after other text in $startcol and
1000 # needs to wrap before $COLUMNS - 2. Subsequent lines start at $startcol.
1001 # Long "words" will extend past $COLUMNS - 2.
1006 local width=$(($COLUMNS - 2 - $startcol))
1007 [ $width -lt 0 ] && width=$((78 - $startcol))
1012 if [ $startcol -gt 0 ] ; then
1013 padding=$(printf "\n%${startcol}s" " ")
1019 while [ -n "$text" ] ; do
1022 # If we already have output then arrange padding on the next line.
1023 [ -n "$out" ] && out="${out}${padding}"
1025 # Break the text at $width.
1026 out="${out}${text:0:${width}}"
1027 text="${text:${width}}"
1029 # If we have left over text then the line break may be ugly,
1030 # so let's check and try to break it on a space.
1031 if [ -n "$text" ] ; then
1032 # The 'x's stop us producing a special character like '(',
1033 # ')' or '!'. Yuck - there must be a better way.
1034 if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1035 # We didn't break on a space. Arrange for the
1036 # beginning of the broken "word" to appear on the next
1037 # line but not if it will make us loop infinitely.
1038 if [ "${orig}" != "${out##* }${text}" ] ; then
1039 text="${out##* }${text}"
1042 # Hmmm, doing that would make us loop, so add the
1043 # rest of the word from the remainder of the text
1044 # to this line and let it extend past $COLUMNS - 2.
1045 out="${out}${text%% *}"
1046 if [ "${text# *}" != "$text" ] ; then
1047 # Remember the text after the next space for next time.
1050 # No text after next space.
1055 # We broke on a space. If it will be at the beginning
1056 # of the next line then remove it.
1065 # Display usage text, trying these approaches in order.
1066 # 1. See if it all fits on one line before $COLUMNS - 2.
1067 # 2. See if splitting before the default value and indenting it
1068 # to $startcol means that nothing passes $COLUMNS - 2.
1069 # 3. Treat the message and default value as a string and just us fill_text()
1071 usage_display_text () {
1076 local width=$(($COLUMNS - 2 - $startcol))
1077 [ $width -lt 0 ] && width=$((78 - $startcol))
1079 default="(default \"$default\")"
1081 if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1082 echo "${desc} ${default}"
1084 local padding=$(printf "%${startcol}s" " ")
1086 if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1088 echo "${padding}${default}"
1090 fill_text $startcol "${desc} ${default}"
1095 # Display usage information for long config options.
1096 usage_smart_display () {( # sub-shell for local declaration of defconf()
1100 local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1102 printf " --%-25s " "${longopt}=${3}"
1104 usage_display_text $startcol "$4" "$2"
1111 # Display usage information for long config options.
1112 usage_config_options (){
1113 usage_smart_display load_config
1117 local releases=$(cd $installdir/releases && echo *.release)
1118 releases="${releases//.release}"
1119 releases="${releases// /\", \"}"
1120 echo "\"$releases\""
1125 shift # subsequent args are passed to release file
1127 # This simply loads an extra config file from $installdir/releases
1128 f="${installdir}/releases/${release}.release"
1129 if [ -r "$f" ] ; then
1132 f="${installdir}/releases/${release%%-*}.release"
1133 if [ -r "$f" ] ; then
1134 . "$f" "${release#*-}"
1136 echo "Unknown release \"${release}\" specified to --with-release"
1137 printf "%-25s" "Supported releases are: "
1138 fill_text 25 "$(list_releases)"
1145 has_public_addresses_DEFAULT ()
1150 # Build public address configuration.
1151 # * 1st public IP: unless specified, last octet is $FIRSTIP + $PUBLIC_IP_OFFSET
1152 # * Excluded nodes: unless specified via comma-separated list of IP offsets,
1153 # nodes are excluded via their node types
1154 # * Number of public addresses per interface is either specified or $NUMNODES.
1155 make_public_addresses () {
1156 local firstip="${1:-$(($FIRSTIP + $PUBLIC_IP_OFFSET))}"
1157 local excluded_nodes="$2"
1158 local num_addrs="${3:-${NUMNODES}}"
1160 # For delimiting matches.
1161 excluded_nodes="${excluded_nodes:+,}${excluded_nodes}${excluded_nodes:+,}"
1163 excluded_nodes="${excluded_nodes// /}"
1165 make_public_addresses_for_node ()
1167 [ "$ctdb_node" = 1 ] || return 0
1169 echo "[/etc/ctdb/public_addresses:${name}.${DOMAIN}]"
1171 if [ -n "$excluded_nodes" -a \
1172 "${excluded_nodes/,${ip_offset},}" = "$excluded_nodes" ] ||
1173 ([ -z "$excluded_nodes" ] &&
1174 call_func has_public_addresses "$node_type") ; then
1177 for e in $IPNET1 $IPNET2 ; do
1178 for i in $(seq $firstip $(($firstip + $num_addrs - 1))) ; do
1179 if [ $i -gt 254 ] ; then
1180 die "make_public_addresses: octet > 254 - consider setting PUBLIC_IP_OFFSET"
1182 printf "\t${IPBASE}.${e}.${i}/24 eth${e}\n"
1188 hack_all_nodes_with make_public_addresses_for_node
1191 ######################################################################
1195 ######################################################################
1199 ############################
1200 # parse command line options
1201 long_opts=$(getopt_config_options)
1202 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump,with-release: -l "$long_opts" -- "$@")
1203 [ $? != 0 ] && usage
1205 use_default_config=true
1207 # We do 2 passes of the options. The first time we just handle usage
1208 # and check whether -c is being used.
1209 eval set -- "$getopt_output"
1212 -c) shift 2 ; use_default_config=false ;;
1215 --) shift ; break ;;
1216 --with-release) shift 2 ;; # Don't set use_default_config=false!!!
1218 -h|--help) usage ;; # Usage should be shown here for real defaults.
1219 --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1220 *) usage ;; # shouldn't happen, so this is reasonable.
1225 $use_default_config && [ -r "$config" ] && . "$config"
1227 eval set -- "$getopt_output"
1231 # force at least ./local_file to avoid accidental file from $PATH
1232 -c) . "$(dirname $2)/$(basename $2)" ; shift 2 ;;
1233 -e) run_hooks post_config_hooks ; eval "$2" ; exit ;;
1234 -E) eval "$2" ; shift 2 ;;
1239 -x) set -x; shift ;;
1240 --dump) run_hooks post_config_hooks ; dump_config ;;
1241 --) shift ; break ;;
1242 -h|--help) usage ;; # Redundant.
1244 # Putting --opt1|opt2|... into a variable and having case
1245 # match against it as a pattern doesn't work. The | is
1246 # part of shell syntax, so we need to do this. Look away
1247 # now to stop your eyes from bleeding! :-)
1248 x=",${long_opts}" # Now each option is surrounded by , and :
1249 if [ "$x" != "${x#*,${1#--}:}" ] ; then
1250 # Our option, $1, surrounded by , and : was in $x, so is legal.
1251 setconf_longopt "$1" "$2"; shift 2
1256 *) usage ;; # shouldn't happen, so this is reasonable.
1260 run_hooks post_config_hooks
1266 echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es;
1269 # check for needed programs
1270 check_command expect
1271 if [ "$SYSTEM_DISK_FORMAT" != "raw" ] ; then
1272 check_command $QEMU_NBD
1273 check_command nbd-client
1276 [ $# -lt 1 ] && usage
1287 [ $# != 0 ] && usage
1291 [ $# != 1 ] && usage
1295 [ $# != 2 ] && usage
1296 create_one_node "$1" "$2"
1304 [ $# != 1 ] && usage
1308 [ $# != 0 ] && usage