7e259d2ad392853a90b4beaea898e03c16e1700b
[autocluster.git] / autocluster
1 #!/bin/bash
2 # main autocluster script
3 #
4 # Copyright (C) Andrew Tridgell  2008
5 # Copyright (C) Martin Schwenke  2008
6 #
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.
11 #   
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.
16 #   
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/>.
19
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.  :-)
23 if [ -f "$0" ]; then
24     autocluster="$0"
25 else
26     autocluster=$(which "$0")
27 fi
28 if [ -L "$autocluster" ] ; then
29     autocluster=$(readlink "$autocluster")
30 fi
31 installdir=$(dirname "$autocluster")
32 ##END-INSTALLDIR-MAGIC##
33
34 ####################
35 # show program usage
36 usage ()
37 {
38     cat <<EOF
39 Usage: autocluster [OPTION] ... <COMMAND>
40   options:
41      -c <file>                   specify config file (default is "config")
42      -e <expr>                   execute <expr> and exit
43      -E <expr>                   execute <expr> and continue
44      -x                          enable script debugging
45      --dump                      dump config settings and exit
46
47   configuration options:
48 EOF
49
50     usage_config_options
51
52     cat <<EOF
53
54   commands:
55      base [ create | boot ] ...
56
57      cluster [ build |
58                destroy | undefine |
59                create | update_hosts | boot | setup ] ...
60
61      create base
62            create a base image
63
64      create cluster [ CLUSTERNAME ]
65            create a full cluster
66
67      create node CLUSTERNAME IP_OFFSET
68            (re)create a single cluster node
69
70      mount DISK
71            mount a qemu disk on mnt/
72
73      unmount | umount
74            unmount a qemu disk from mnt/
75
76      bootbase
77            boot the base image
78 EOF
79     exit 1
80 }
81
82 ###############################
83
84 die () {
85     if [ "$no_sanity" = 1 ] ; then
86         fill_text 0 "WARNING: $*" >&2
87     else
88         fill_text 0 "ERROR: $*" >&2
89         exit 1
90     fi
91 }
92
93 announce ()
94 {
95     echo "######################################################################"
96     printf "# %-66s #\n" "$*"
97     echo "######################################################################"
98     echo ""
99 }
100
101 waitfor ()
102 {
103     local file="$1"
104     local msg="$2"
105     local timeout="$3"
106
107     local tmpfile=$(mktemp)
108
109     cat <<EOF >"$tmpfile"
110 spawn tail -n 10000 -f $file
111 expect -timeout $timeout -re "$msg"
112 EOF
113
114     export LANG=C
115     expect "$tmpfile"
116     rm -f "$tmpfile"
117
118     if ! grep -E "$msg" "$file" > /dev/null; then
119         echo "Failed to find \"$msg\" in \"$file\""
120         return 1
121     fi
122
123     return 0
124 }
125
126 ###############################
127
128 # Indirectly call a function named by ${1}_${2}
129 call_func () {
130     local func="$1" ; shift
131     local type="$1" ; shift
132
133     local f="${func}_${type}"
134     if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null ; then
135         "$f" "$@"
136     else
137         f="${func}_DEFAULT"
138         if type -t "$f" >/dev/null && ! type -P "$f" >/dev/null  ; then
139             "$f" "$type" "$@"
140         else
141             die "No function defined for \"${func}\" \"${type}\""
142         fi
143     fi
144 }
145
146 # Note that this will work if you pass "call_func f" because the first
147 # element of the node tuple is the node type.  Nice...  :-)
148 for_each_node ()
149 {
150     local n
151     for n in $NODES ; do
152         "$@" $(IFS=: ; echo $n)
153     done
154 }
155
156 node_is_ctdb_node_DEFAULT ()
157 {
158     echo 0
159 }
160
161 hack_one_node_with ()
162 {
163     local filter="$1" ; shift
164
165     local node_type="$1"
166     local ip_offset="$2"
167     local name="$3"
168     local ctdb_node="$4"
169
170     $filter
171
172     local item="${node_type}:${ip_offset}${name:+:}${name}${ctdb_node:+:}${ctdb_node}"
173     nodes="${nodes}${nodes:+ }${item}"
174 }
175
176 # This also gets used for non-filtering iteration.
177 hack_all_nodes_with ()
178 {
179     local filter="$1"
180
181     local nodes=""
182     for_each_node hack_one_node_with "$filter"
183     NODES="$nodes"
184 }
185
186 register_hook ()
187 {
188     local hook_var="$1"
189     local new_hook="$2"
190
191     eval "$hook_var=\"${!hook_var}${!hook_var:+ }${new_hook}\""
192 }
193
194 run_hooks ()
195 {
196     local hook_var="$1"
197     shift
198
199     local i
200     for i in ${!hook_var} ; do
201         $i "$@"
202     done
203 }
204
205 # Use with care, since this may clear some autocluster defaults.!
206 clear_hooks ()
207 {
208     local hook_var="$1"
209
210     eval "$hook_var=\"\""
211 }
212
213 ##############################
214
215 # These hooks are intended to customise the value of $DISK.  They have
216 # access to 1 argument ("base", "system", "shared") and the variables
217 # $VIRTBASE, $CLUSTER, $BASENAME (for "base"), $NAME (for "system"),
218 # $SHARED_DISK_NUM (for "shared").  A hook must be deterministic and
219 # should not be stateful, since they can be called multiple times for
220 # the same disk.
221 hack_disk_hooks=""
222
223 create_node_DEFAULT ()
224 {
225     local type="$1"
226     local ip_offset="$2"
227     local name="$3"
228     local ctdb_node="$4"
229
230     echo "Creating node \"$name\" (of type \"${type}\")"
231
232     create_node_COMMON "$name" "$ip_offset" "$type"
233 }
234
235 # common node creation stuff
236 create_node_COMMON ()
237 {
238     local NAME="$1"
239     local ip_offset="$2"
240     local type="$3"
241     local template_file="${4:-$NODE_TEMPLATE}"
242
243     if [ "$SYSTEM_DISK_FORMAT" != "qcow2" -a "$BASE_FORMAT" = "qcow2" ] ; then
244         die "Error: if BASE_FORMAT is \"qcow2\" then SYSTEM_DISK_FORMAT must also be \"qcow2\"."
245     fi
246
247     local IPNUM=$(($FIRSTIP + $ip_offset))
248     make_network_map
249
250     # Determine base image name.  We use $DISK temporarily to allow
251     # the path to be hacked.
252     local DISK="${VIRTBASE}/${BASENAME}.${BASE_FORMAT}"
253     if [ "$BASE_PER_NODE_TYPE" = "yes" ] ; then
254         DISK="${VIRTBASE}/${BASENAME}-${type}.${BASE_FORMAT}"
255     fi
256     run_hooks hack_disk_hooks "base"
257     local base_disk="$DISK"
258
259     # Determine the system disk image name.
260     DISK="${VIRTBASE}/${CLUSTER}/${NAME}.${SYSTEM_DISK_FORMAT}"
261     run_hooks hack_disk_hooks "system"
262
263     local di="$DISK"
264     if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
265         di=$(readlink "$DISK")
266     fi
267     rm -f "$di"
268     local di_dirname="${di%/*}"
269     mkdir -p "$di_dirname"
270
271     case "$SYSTEM_DISK_FORMAT" in
272         qcow2)
273             echo "Creating the disk..."
274             qemu-img create -b "$base_disk" -f qcow2 "$di"
275             create_node_configure_image "$DISK" "$type"
276             ;;
277         raw)
278             echo "Creating the disk..."
279             cp -v --sparse=always "$base_disk" "$di"
280             create_node_configure_image "$DISK" "$type"
281             ;;
282         reflink)
283             echo "Creating the disk..."
284             cp -v --reflink=always "$base_disk" "$di"
285             create_node_configure_image "$DISK" "$type"
286             ;;
287         mmclone)
288             echo "Creating the disk (using mmclone)..."
289             local base_snap="${base_disk}.snap"
290             [ -f "$base_snap" ] || mmclone snap "$base_disk" "$base_snap"
291             mmclone copy "$base_snap" "$di"
292             create_node_configure_image "$DISK" "$type"
293             ;;
294         none)
295             echo "Skipping disk image creation as requested"
296             ;;
297         *)
298             die "Error: unknown SYSTEM_DISK_FORMAT=\"${SYSTEM_DISK_FORMAT}\"."
299     esac
300
301     # Pull the UUID for this node out of the map.
302     UUID=$(awk "\$1 == $ip_offset {print \$2}" $uuid_map)
303     
304     mkdir -p tmp
305
306     echo "Creating $NAME.xml"
307     substitute_vars $template_file tmp/$NAME.xml
308     
309     # install the XML file
310     $VIRSH undefine $NAME > /dev/null 2>&1 || true
311     $VIRSH define tmp/$NAME.xml
312 }
313
314 create_node_configure_image ()
315 {
316     local disk="$1"
317     local type="$2"
318
319     diskimage mount "$disk"
320     setup_base "$type"
321     diskimage unmount
322 }
323
324 hack_network_map_hooks=""
325
326 # Uses: CLUSTER, NAME, NETWORKS, FIRSTIP, ip_offset
327 make_network_map ()
328 {
329     network_map="tmp/network_map.$NAME"
330
331     if [ -n "$CLUSTER" ] ; then
332         local md5=$(echo "$CLUSTER" | md5sum)
333         local nh=$(printf "%02x" $ip_offset)
334         local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
335     else
336         local mac_prefix="02:42:42:00:00:"
337     fi
338
339     local n
340     local count=1
341     for n in $NETWORKS ; do
342         local ch=$(printf "%02x" $count)
343         local mac="${mac_prefix}${ch}"
344
345         set -- ${n//,/ }
346         local ip_bits="$1" ; shift
347         local dev="$1" ; shift
348         local opts="$*"
349
350         local net="${ip_bits%/*}"
351         local netname="acnet_${net//./_}"
352
353         local ip="${net%.*}.${IPNUM}"
354         local mask="255.255.255.0"
355
356         # This can be used to override the variables in the echo
357         # statement below.  The hook can use any other variables
358         # available in this function.
359         run_hooks hack_network_map_hooks
360
361         echo "${netname} ${dev} ${ip} ${mask} ${mac} ${opts}"
362         count=$(($count + 1))
363     done >"$network_map"
364 }
365
366 ##############################
367
368 expand_nodes ()
369 {
370     # Expand out any abbreviations in NODES.
371     local ns=""
372     local n
373     for n in $NODES ; do
374         local t="${n%:*}"
375         local ips="${n#*:}"
376         case "$ips" in
377             *,*)
378                 local i
379                 for i in ${ips//,/ } ; do
380                     ns="${ns}${ns:+ }${t}:${i}"
381                 done
382                 ;;
383             *-*)
384                 local i
385                 for i in $(seq ${ips/-/ }) ; do
386                     ns="${ns}${ns:+ }${t}:${i}"
387                 done
388                 ;;
389             *)
390                 ns="${ns}${ns:+ }${n}"
391         esac
392     done
393     NODES="$ns"
394
395     # Check IP addresses for duplicates.
396     local ip_offsets=":"
397     # This function doesn't modify anything...
398     get_ip_offset ()
399     {
400         [ "${ip_offsets/${ip_offset}}" != "$ip_offsets" ] && \
401             die "Duplicate IP offset in NODES - ${node_type}:${ip_offset}"
402         ip_offsets="${ip_offsets}${ip_offset}:"
403     }
404     hack_all_nodes_with get_ip_offset
405
406     # Determine node names and whether they're in the CTDB cluster
407     declare -A node_count
408     _get_name_ctdb_node ()
409     {
410         local count=$((${node_count[$node_type]:-0} + 1))
411         node_count[$node_type]=$count
412         name=$(call_func node_name_format "$node_type" "$CLUSTER" $count) || {
413             echo "ERROR: Node type \"${node_type}\" not defined!"
414             echo "Valid node types are:"
415             set | sed -n 's@^node_name_format_\(.*\) ().*@  \1@p'
416             exit 1
417         }
418         ctdb_node=$(call_func node_is_ctdb_node "$node_type")
419     }
420     hack_all_nodes_with _get_name_ctdb_node
421 }
422
423 ##############################
424
425 sanity_check_cluster_name ()
426 {
427     [ -z "${CLUSTER//[A-Za-z0-9]}" ] || \
428         die "Cluster names should be restricted to the characters A-Za-z0-9.  \
429 Some cluster filesystems have problems with other characters."
430 }
431
432 hosts_file=
433
434 cluster_nodelist_hacking ()
435 {
436     # Rework the NODES list
437     expand_nodes
438
439     # Build /etc/hosts and hack the names of the ctdb nodes
440     hosts_line_hack_name ()
441     {
442         local sname=""
443         local hosts_line
444         local ip_addr="${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
445
446         # Primary name for CTDB nodes is <CLUSTER>n<num>
447         if [ "$ctdb_node" = 1 ] ; then
448             num_ctdb_nodes=$(($num_ctdb_nodes + 1))
449             sname="${CLUSTER}n${num_ctdb_nodes}"
450             hosts_line="$ip_addr ${sname}.${ld} ${name}.${ld} $name $sname"
451             name="$sname"
452         else
453             hosts_line="$ip_addr ${name}.${ld} $name"
454         fi
455
456         # This allows you to add a function to your configuration file
457         # to modify hostnames (and other aspects of nodes).  This
458         # function can access/modify $name (the existing name),
459         # $node_type and $ctdb_node (1, if the node is a member of the
460         # CTDB cluster, 0 otherwise).
461         if [ -n "$HOSTNAME_HACKING_FUNCTION" ] ; then
462             local old_name="$name"
463             $HOSTNAME_HACKING_FUNCTION
464             if [ "$name" != "$old_name" ] ; then
465                 hosts_line="$ip_addr ${name}.${ld} $name"
466             fi
467         fi
468
469         echo "$hosts_line"
470     }
471     hosts_file="tmp/hosts.$CLUSTER"
472     {
473         local num_ctdb_nodes=0
474         local ld=$(echo $DOMAIN | tr A-Z a-z)
475         echo "# autocluster $CLUSTER"
476         hack_all_nodes_with hosts_line_hack_name
477         echo
478     } >$hosts_file
479
480     # Build /etc/ctdb/nodes
481     ctdb_nodes_line ()
482     {
483         [ "$ctdb_node" = 1 ] || return 0
484         echo "${NETWORK_PRIVATE_PREFIX}.$(($FIRSTIP + $ip_offset))"
485         num_nodes=$(($num_nodes + 1))
486     }
487     nodes_file="tmp/nodes.$CLUSTER"
488     local num_nodes=0
489     hack_all_nodes_with ctdb_nodes_line >$nodes_file
490
491     # Build UUID map
492     uuid_map="tmp/uuid_map.$CLUSTER"
493     uuid_map_line ()
494     {
495         echo "${ip_offset} $(uuidgen) ${node_type}"
496     }
497     hack_all_nodes_with uuid_map_line >$uuid_map
498 }
499
500 create_cluster_hooks=
501 cluster_created_hooks=
502
503 cluster_create ()
504 {
505     # Use $1.  If not set then use value from configuration file.
506     CLUSTER="${1:-${CLUSTER}}"
507     announce "cluster create \"${CLUSTER}\""
508     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
509
510     sanity_check_cluster_name
511
512     mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
513
514     # Run hooks before doing anything else.
515     run_hooks create_cluster_hooks
516
517     for_each_node call_func create_node
518
519     echo "Cluster $CLUSTER created"
520     echo ""
521
522     run_hooks cluster_created_hooks
523 }
524
525 cluster_created_hosts_message ()
526 {
527     echo "You may want to add this to your /etc/hosts file:"
528     cat $hosts_file
529 }
530
531 register_hook cluster_created_hooks cluster_created_hosts_message
532
533 cluster_destroy ()
534 {
535     announce "cluster destroy \"${CLUSTER}\""
536     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
537
538     vircmd destroy "$CLUSTER" || true
539 }
540
541 cluster_undefine ()
542 {
543     announce "cluster undefine \"${CLUSTER}\""
544     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
545
546     vircmd undefine "$CLUSTER" || true
547 }
548
549 cluster_update_hosts ()
550 {
551     announce "cluster update_hosts \"${CLUSTER}\""
552     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
553
554     [ -n "$hosts_file" ] || hosts_file="tmp/hosts.${CLUSTER}"
555     [ -r "$hosts_file" ] || die "Missing hosts file \"${hosts_file}\""
556
557     # Building a general node name regexp is a bit cumbersome.  :-)
558     local name_regexp="("
559     for i in $(set | sed -n -e "s@^\(node_name_format_.*\) ().*@\1@p") ; do
560         # Format node name with placeholders (remembering that "_" is
561         # not valid in a real cluster name)
562         local t=$("$i" "_" "0")
563         # now replace the placeholders with regexps - order is
564         # important here, since the cluster name can contain digits
565         t=$(sed -r -e "s@[[:digit:]]+@[[:digit:]]+@" -e "s@_@${CLUSTER}@" <<<"$t")
566         # add to the regexp
567         name_regexp="${name_regexp}${t}|"
568     done
569     name_regexp="${name_regexp}${CLUSTER}n[[:digit:]]+)"
570
571     local pat="# autocluster ${CLUSTER}\$|[[:space:]]${name_regexp}"
572
573     local t="/etc/hosts.${CLUSTER}"
574     grep -E "$pat" /etc/hosts >"$t" || true
575     if diff -B "$t" "$hosts_file" >/dev/null ; then
576         rm "$t"
577         return
578     fi
579
580     local old=/etc/hosts.old.autocluster
581     cp /etc/hosts "$old"
582     local new=/etc/hosts.new
583     grep -Ev "$pat" "$old" |
584     cat -s - "$hosts_file" >"$new"
585
586     mv "$new" /etc/hosts
587
588     echo "Made these changes to /etc/hosts:"
589     diff -u "$old" /etc/hosts || true
590 }
591
592 cluster_boot ()
593 {
594     [ -n "$CLUSTER_PATTERN" ] || CLUSTER_PATTERN="$CLUSTER"
595     announce "cluster boot \"${CLUSTER_PATTERN}\""
596     [ -n "$CLUSTER_PATTERN" ] || die "\$CLUSTER_PATTERN not set"
597
598     vircmd start "$CLUSTER_PATTERN"
599
600     local nodes=$(vircmd dominfo "$CLUSTER_PATTERN" 2>/dev/null | \
601         sed -n -e 's/Name: *//p')
602
603     # Wait for each node
604     local i
605     for i in $nodes ; do
606         waitfor "${KVMLOG}/serial.$i" "login:" 300 || {
607             vircmd destroy "$CLUSTER_PATTERN"
608             die "Failed to create cluster"
609         }
610     done
611
612     # Move past the last line of log output
613     echo ""
614 }
615
616 cluster_setup_tasks_DEFAULT ()
617 {
618     local stage="$1"
619
620     # By default nodes have no tasks
621     case "$stage" in
622         install_packages) echo "" ;;
623         setup_clusterfs)  echo "" ;;
624         setup_node)       echo "" ;;
625         setup_cluster)    echo "" ;;
626     esac
627 }
628
629 cluster_setup ()
630 {
631     announce "cluster setup \"${CLUSTER}\""
632     [ -n "$CLUSTER" ] || die "\$CLUSTER not set"
633
634     local ssh="ssh -o StrictHostKeyChecking=no"
635     local setup_clusterfs_done=false
636     local setup_cluster_done=false
637
638     _cluster_setup_do_stage ()
639     {
640         local stage="$1"
641         local type="$2"
642         local ip_offset="$3"
643         local name="$4"
644         local ctdb_node="$5"
645
646         local tasks=$(call_func cluster_setup_tasks "$type" "$stage")
647
648         if [ -n "$tasks" ] ; then
649             # These tasks are only done on 1 node
650             case "$stage" in
651                 setup_clusterfs)
652                     if $setup_clusterfs_done ; then
653                         return
654                     else
655                         setup_clusterfs_done=true
656                     fi
657                     ;;
658                 setup_cluster)
659                     if $setup_cluster_done ; then
660                         return
661                     else
662                         setup_cluster_done=true
663                     fi
664                     ;;
665             esac
666
667             $ssh "$name" ./scripts/cluster_setup.sh "$stage" $tasks
668         fi
669
670     }
671
672     local stages="install_packages setup_clusterfs setup_node setup_cluster"
673     local stage
674     for stage in $stages ; do
675         for_each_node _cluster_setup_do_stage "$stage"
676     done
677 }
678
679 create_one_node ()
680 {
681     CLUSTER="$1"
682     local single_node_ip_offset="$2"
683
684     sanity_check_cluster_name
685
686     mkdir -p $VIRTBASE/$CLUSTER $KVMLOG tmp
687
688     for n in $NODES ; do
689         set -- $(IFS=: ; echo $n)
690         [ $single_node_ip_offset -eq $2 ] || continue
691         call_func create_node "$@"
692         
693         echo "Requested node created"
694         echo ""
695         echo "You may want to update your /etc/hosts file:"
696         cat $hosts_file
697         
698         break
699     done
700 }
701
702 ###############################
703 # test the proxy setup
704 test_proxy() {
705     export http_proxy=$WEBPROXY
706     wget -O /dev/null $INSTALL_SERVER || \
707         die "Your WEBPROXY setting \"$WEBPROXY\" is not working"
708     echo "Proxy OK"
709 }
710
711 ###################
712
713 kickstart_floppy_create_hooks=
714
715 guess_install_network ()
716 {
717     # Figure out IP address to use during base install.  Default to
718     # the IP address of the 1st (private) network. If a gateway is
719     # specified then use the IP address associated with it.
720     INSTALL_IP=""
721     INSTALL_GW=""
722     local netname dev ip mask mac opts
723     while read netname dev ip mask mac opts; do
724         local o
725         for o in $opts ; do
726             case "$o" in
727                 gw\=*)
728                     INSTALL_GW="${o#gw=}"
729                     INSTALL_IP="${ip}${FIRSTIP}"
730             esac
731         done
732         [ -n "$INSTALL_IP" ] || INSTALL_IP="$ip"
733     done <"$network_map"
734 }
735
736 # create base image
737 base_create()
738 {
739     local NAME="$BASENAME"
740     local DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
741     run_hooks hack_disk_hooks "base"
742
743     mkdir -p $KVMLOG
744
745     echo "Testing WEBPROXY $WEBPROXY"
746     test_proxy
747
748     local di="$DISK"
749     if [ "$DISK_FOLLOW_SYMLINKS" = "yes" -a -L "$DISK" ] ; then
750         di=$(readlink "$DISK")
751     fi
752     rm -f "$di"
753     local di_dirname="${di%/*}"
754     mkdir -p "$di_dirname"
755
756     echo "Creating the disk"
757     qemu-img create -f $BASE_FORMAT "$di" $DISKSIZE
758
759     rm -rf tmp
760     mkdir -p mnt tmp tmp/ISO
761
762     setup_timezone
763
764     make_network_map
765
766     guess_install_network
767
768     echo "Creating kickstart file from template"
769     substitute_vars "$KICKSTART" "tmp/ks.cfg"
770
771     # $ISO gets $ISO_DIR prepended if it doesn't start with a leading '/'.
772     case "$ISO" in
773         (/*) : ;;
774         (*) ISO="${ISO_DIR}/${ISO}"
775     esac
776     
777     echo "Creating kickstart floppy"
778     dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
779     mkdosfs -n KICKSTART tmp/floppy.img
780     mount -o loop -t msdos tmp/floppy.img mnt
781     cp tmp/ks.cfg mnt
782     mount -o loop,ro $ISO tmp/ISO
783     
784     echo "Setting up bootloader"
785     cp tmp/ISO/isolinux/isolinux.bin tmp
786     cp tmp/ISO/isolinux/vmlinuz tmp
787     cp tmp/ISO/isolinux/initrd.img tmp
788
789     run_hooks kickstart_floppy_create_hooks
790
791     umount tmp/ISO
792     umount mnt
793
794     UUID=`uuidgen`
795
796     substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
797
798     rm -f $KVMLOG/serial.$NAME
799
800     # boot the install CD
801     $VIRSH create tmp/$NAME.xml
802
803     echo "Waiting for install to start"
804     sleep 2
805     
806     # wait for the install to finish
807     if ! waitfor $KVMLOG/serial.$NAME "$KS_DONE_MESSAGE" $CREATE_BASE_TIMEOUT ; then
808         $VIRSH destroy $NAME
809         die "Failed to create base image ${DISK} after waiting for ${CREATE_BASE_TIMEOUT} seconds.
810 You may need to increase the value of CREATE_BASE_TIMEOUT.
811 Alternatively, the install might have completed but KS_DONE_MESSAGE
812 (currently \"${KS_DONE_MESSAGE}\")
813 may not have matched anything at the end of the kickstart output."
814     fi
815     
816     $VIRSH destroy $NAME
817
818     ls -l $DISK
819     cat <<EOF
820
821 Install finished, base image $DISK created
822
823 You may wish to run
824    chcon -t virt_content_t $DISK
825    chattr +i $DISK
826 To ensure that this image does not change
827
828 Note that the root password has been set to $ROOTPASSWORD
829
830 EOF
831 }
832
833 ###############################
834 # boot the base disk
835 base_boot() {
836     rm -rf tmp
837     mkdir -p tmp
838
839     NAME="$BASENAME"
840     DISK="${VIRTBASE}/${NAME}.${BASE_FORMAT}"
841
842     IPNUM=$FIRSTIP
843
844     make_network_map
845
846     CLUSTER="base"
847
848     diskimage mount $DISK
849     setup_base
850     diskimage unmount
851
852     UUID=`uuidgen`
853     
854     echo "Creating $NAME.xml"
855     substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
856     
857     # boot the base system
858     $VIRSH create tmp/$NAME.xml
859 }
860
861 ######################################################################
862
863 # Updating a disk image...
864
865 diskimage ()
866 {
867     local func="$1"
868     shift
869     call_func diskimage_"$func" "$SYSTEM_DISK_ACCESS_METHOD" "$@"
870 }
871
872 # setup the files from $BASE_TEMPLATES/, substituting any variables
873 # based on the config
874 copy_base_dir_substitute_templates ()
875 {
876     local dir="$1"
877
878     local d="$BASE_TEMPLATES/$dir"
879     [ -d "$d" ] || return 0
880
881     local f
882     for f in $(cd "$d" && find . \! -name '*~' \( -type d -name .svn -prune -o -print \) ) ; do
883         f="${f#./}" # remove leading "./" for clarity
884         if [ -d "$d/$f" ]; then
885             # Don't chmod existing directory
886             if diskimage is_directory "/$f" ; then
887                 continue
888             fi
889             diskimage mkdir_p "/$f"
890         else
891             echo " Install: $f"
892             diskimage substitute_vars "$d/$f" "/$f"
893         fi
894         diskimage chmod_reference "$d/$f" "/$f"
895     done
896 }
897
898 setup_base_hooks=
899
900 setup_base_ssh_keys ()
901 {
902     # this is needed as git doesn't store file permissions other
903     # than execute
904     # Note that we protect the wildcards from the local shell.
905     diskimage chmod 600 "/etc/ssh/*key" "/root/.ssh/*"
906     diskimage chmod 700 "/etc/ssh" "/root/.ssh" "/root"
907     if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
908        echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
909        diskimage append_text_file "$HOME/.ssh/id_rsa.pub" "/root/.ssh/authorized_keys"
910     fi
911     if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
912        echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
913        diskimage append_text_file "$HOME/.ssh/id_dsa.pub" "/root/.ssh/authorized_keys"
914     fi
915 }
916
917 register_hook setup_base_hooks setup_base_ssh_keys
918
919 setup_base_grub_conf ()
920 {
921     echo "Adjusting grub.conf"
922     local o="$EXTRA_KERNEL_OPTIONS" # For readability.
923     local grub_configs="/boot/grub/grub.conf"
924     if ! diskimage is_file "$grub_configs" ; then
925         grub_configs="/etc/default/grub /boot/grub2/grub.cfg"
926     fi
927     local c
928     for c in $grub_configs ; do
929         diskimage sed "$c" \
930             -e "s/console=ttyS0,19200/console=ttyS0,115200/"  \
931             -e "s/ console=tty1//" -e "s/ rhgb/ norhgb/"  \
932             -e "s/ nodmraid//" -e "s/ nompath//"  \
933             -e "s/quiet/noapic divider=10${o:+ }${o}/g"
934     done
935 }
936
937 register_hook setup_base_hooks setup_base_grub_conf
938
939 setup_base()
940 {
941     local type="$1"
942
943     umask 022
944     echo "Copy base files"
945     copy_base_dir_substitute_templates "all"
946     if [ -n "$type" ] ; then
947         copy_base_dir_substitute_templates "$type"
948     fi
949
950     run_hooks setup_base_hooks
951 }
952
953 # setup various networking components
954 setup_network()
955 {
956     # This avoids doing anything when we're called from boot_base().
957     if [ -z "$hosts_file" ] ; then
958         echo "Skipping network-related setup"
959         return
960     fi
961
962     echo "Setting up networks"
963     diskimage append_text_file "$hosts_file" "/etc/hosts"
964
965     echo "Setting up /etc/ctdb/nodes"
966     diskimage mkdir_p "/etc/ctdb"
967     diskimage put "$nodes_file" "/etc/ctdb/nodes"
968
969     [ "$WEBPROXY" = "" ] || {
970         diskimage append_text "export http_proxy=$WEBPROXY" "/etc/bashrc"
971     }
972
973     if [ -n "$NFSSHARE" -a -n "$NFS_MOUNTPOINT" ] ; then
974         echo "Enabling nfs mount of $NFSSHARE"
975         diskimage mkdir_p "$NFS_MOUNTPOINT"
976         diskimage append_text "$NFSSHARE $NFS_MOUNTPOINT nfs nfsvers=3,intr 0 0" "/etc/fstab"
977     fi
978
979     diskimage mkdir_p "/etc/yum.repos.d"
980     echo '@@@YUM_TEMPLATE@@@' | diskimage substitute_vars - "/etc/yum.repos.d/autocluster.repo"
981
982     diskimage rm_rf "/etc/udev/rules.d/70-persistent-net.rules"
983
984     echo "Setting up network interfaces: "
985     local netname dev ip mask mac opts
986     while read netname dev ip mask mac opts; do
987         echo "  $dev"
988
989         local o gw
990         gw=""
991         for o in $opts ; do
992             case "$o" in
993                 gw\=*)
994                     gw="${o#gw=}"
995             esac
996         done
997
998         cat <<EOF | \
999             diskimage put - "/etc/sysconfig/network-scripts/ifcfg-${dev}"
1000 DEVICE=$dev
1001 ONBOOT=yes
1002 TYPE=Ethernet
1003 IPADDR=$ip
1004 NETMASK=$mask
1005 HWADDR=$mac
1006 ${gw:+GATEWAY=}${gw}
1007 EOF
1008
1009         # This goes to 70-persistent-net.rules
1010         cat <<EOF
1011 # Generated by autocluster
1012 SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="${mac}", ATTR{type}=="1", KERNEL=="eth*", NAME="${dev}"
1013
1014 EOF
1015     done <"$network_map" |
1016     diskimage put - "/etc/udev/rules.d/70-persistent-net.rules"
1017 }
1018
1019 register_hook setup_base_hooks setup_network
1020
1021 setup_base_cluster_setup_config ()
1022 {
1023     local f
1024     {
1025         echo "# Generated by autocluster"
1026         echo
1027         # This is a bit of a hack.  Perhaps these script belong
1028         # elsewhere, since they no longer have templates?
1029         for f in $(find "${BASE_TEMPLATES}/all/root/scripts" -type f |
1030             xargs grep -l '^#config:') ; do
1031
1032             b=$(basename "$f")
1033             echo "# $b"
1034             local vs v
1035             vs=$(sed -n 's@^#config: *@@p' "$f")
1036             for v in $vs ; do
1037                 # This could substitute the values in directly using
1038                 # ${!v} but then no sanity checking is done to make
1039                 # sure variables are set.
1040                 echo "${v}=\"@@${v}@@\""
1041             done
1042             echo
1043         done
1044     } | diskimage substitute_vars - "/root/scripts/cluster_setup.config"
1045 }
1046
1047 register_hook setup_base_hooks setup_base_cluster_setup_config
1048
1049 setup_timezone() {
1050     [ -z "$TIMEZONE" ] && {
1051         [ -r /etc/timezone ] && {
1052             TIMEZONE=`cat /etc/timezone`
1053         }
1054         [ -r /etc/sysconfig/clock ] && {
1055             . /etc/sysconfig/clock
1056             TIMEZONE="$ZONE"
1057         }
1058         TIMEZONE="${TIMEZONE// /_}"
1059     }
1060     [ -n "$TIMEZONE" ] || \
1061         die "Unable to determine TIMEZONE - please set in config"
1062 }
1063
1064 # substite a set of variables of the form @@XX@@ for the shell
1065 # variables $XX in a file.
1066 #
1067 # Indirect variables @@@XX@@@ (3 ats) specify that the variable should
1068 # contain a filename whose contents are substituted, with variable
1069 # substitution applied to those contents.  If filename starts with '|'
1070 # it is a command instead - however, quoting is extremely fragile.
1071 substitute_vars() {(
1072         infile="${1:-/dev/null}" # if empty then default to /dev/null
1073         outfile="$2" # optional
1074
1075         tmp_out=$(mktemp)
1076         cat "$infile" >"$tmp_out"
1077
1078         # Handle any indirects by looping until nothing changes.
1079         # However, only handle 10 levels of recursion.
1080         count=0
1081         while : ; do
1082             if ! _substitute_vars "$tmp_out" "@@@" ; then
1083                 rm -f "$tmp_out"
1084                 die "Failed to expand template $infile"
1085             fi
1086
1087             # No old version of file means no changes made.
1088             if [ ! -f "${tmp_out}.old" ] ; then
1089                 break
1090             fi
1091
1092             rm -f "${tmp_out}.old"
1093
1094             count=$(($count + 1))
1095             if [ $count -ge 10 ] ; then
1096                 rm -f "$tmp_out"
1097                 die "Recursion too deep in $infile - only 10 levels allowed!"
1098             fi
1099         done
1100
1101         # Now regular variables.
1102         if ! _substitute_vars "$tmp_out" "@@" ; then
1103             rm -f "$tmp_out"
1104             die "Failed to expand template $infile"
1105         fi
1106         rm -f "${tmp_out}.old"
1107
1108         if [ -n "$outfile" ] ; then
1109             mv "$tmp_out" "$outfile"
1110         else
1111             cat "$tmp_out"
1112             rm -f "$tmp_out"
1113         fi
1114 )}
1115
1116
1117 # Delimiter @@ means to substitute contents of variable.
1118 # Delimiter @@@ means to substitute contents of file named by variable.
1119 # @@@ supports leading '|' in variable value, which means to excute a
1120 # command.
1121 _substitute_vars() {(
1122         tmp_out="$1"
1123         delimiter="${2:-@@}"
1124
1125         # Get the list of variables used in the template.  The grep
1126         # gets rid of any blank lines and lines with extraneous '@'s
1127         # next to template substitutions.
1128         VARS=$(sed -n -e "s#[^@]*${delimiter}\([A-Z0-9_][A-Z0-9_]*\)${delimiter}[^@]*#\1\n#gp" "$tmp_out" |
1129             grep '^[A-Z0-9_][A-Z0-9_]*$' |
1130             sort -u)
1131
1132         tmp=$(mktemp)
1133         for v in $VARS; do
1134             # variable variables are fun .....
1135             [ "${!v+x}" ] || {
1136                 rm -f $tmp
1137                 die "No substitution given for ${delimiter}$v${delimiter} in $infile"
1138             }
1139             s=${!v}
1140
1141             if [ "$delimiter" = "@@@" ] ; then
1142                 f=${s:-/dev/null}
1143                 c="${f#|}" # Is is a command, signified by a leading '|'?
1144                 if [ "$c" = "$f" ] ; then
1145                     # No leading '|', cat file.
1146                     s=$(cat -- "$f")
1147                     [ $? -eq 0 ] || {
1148                         rm -f $tmp
1149                         die "Could not substitute contents of file $f"
1150                     }
1151                 else
1152                     # Leading '|', execute command.
1153                     # Quoting problems here - using eval "$c" doesn't help.
1154                     s=$($c)
1155                     [ $? -eq 0 ] || {
1156                         rm -f $tmp
1157                         die "Could not execute command $c"
1158                     }
1159                 fi
1160             fi
1161
1162             # escape some pesky chars
1163             # This first one can be too slow if done using a bash
1164             # variable pattern subsitution.
1165             s=$(echo -n "$s" | tr '\n' '\001' | sed -e 's/\o001/\\n/g')
1166             s=${s//#/\\#}
1167             s=${s//&/\\&}
1168             echo "s#${delimiter}${v}${delimiter}#${s}#g"
1169         done > $tmp
1170
1171         # Get the in-place sed to make a backup of the old file.
1172         # Remove the backup if it is the same as the resulting file -
1173         # this acts as a flag to the caller that no changes were made.
1174         sed -i.old -f $tmp "$tmp_out"
1175         if cmp -s "${tmp_out}.old" "$tmp_out" ; then
1176             rm -f "${tmp_out}.old"
1177         fi
1178
1179         rm -f $tmp
1180 )}
1181
1182 check_command() {
1183     which $1 > /dev/null || die "Please install $1 to continue"
1184 }
1185
1186 # Set a variable if it isn't already set.  This allows environment
1187 # variables to override default config settings.
1188 defconf() {
1189     local v="$1"
1190     local e="$2"
1191
1192     [ "${!v+x}" ] || eval "$v=\"$e\""
1193 }
1194
1195 load_config () {
1196     local i
1197
1198     for i in "${installdir}/config.d/"*.defconf ; do
1199         . "$i"
1200     done
1201 }
1202
1203 # Print the list of config variables defined in config.d/.
1204 get_config_options () {( # sub-shell for local declaration of defconf()
1205         local options=
1206         defconf() { options="$options $1" ; }
1207         load_config
1208         echo $options
1209 )}
1210
1211 # Produce a list of long options, suitable for use with getopt, that
1212 # represent the config variables defined in config.d/.
1213 getopt_config_options () {
1214     local x=$(get_config_options | tr 'A-Z_' 'a-z-')
1215     echo "${x// /:,}:"
1216 }
1217
1218 # Unconditionally set the config variable associated with the given
1219 # long option.
1220 setconf_longopt () {
1221     local longopt="$1"
1222     local e="$2"
1223
1224     local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
1225     # unset so defconf will set it
1226     eval "unset $v"
1227     defconf "$v" "$e"
1228 }
1229
1230 # Dump all of the current config variables.
1231 dump_config() {
1232     local o
1233     for o in $(get_config_options) ; do
1234         echo "${o}=\"${!o}\""
1235     done
1236     exit 0
1237 }
1238
1239 # $COLUMNS is set in interactive bash shells.  It probably isn't set
1240 # in this shell, so let's set it if it isn't.
1241 : ${COLUMNS:=$(stty size 2>/dev/null | sed -e 's@.* @@')}
1242 : ${COLUMNS:=80}
1243 export COLUMNS
1244
1245 # Print text assuming it starts after other text in $startcol and
1246 # needs to wrap before $COLUMNS - 2.  Subsequent lines start at $startcol.
1247 # Long "words" will extend past $COLUMNS - 2.
1248 fill_text() {
1249     local startcol="$1"
1250     local text="$2"
1251
1252     local width=$(($COLUMNS - 2 - $startcol))
1253     [ $width -lt 0 ] && width=$((78 - $startcol))
1254
1255     local out=""
1256
1257     local padding
1258     if [ $startcol -gt 0 ] ; then
1259         padding=$(printf "\n%${startcol}s" " ")
1260     else
1261         padding="
1262 "
1263     fi
1264
1265     while [ -n "$text" ] ; do
1266         local orig="$text"
1267
1268         # If we already have output then arrange padding on the next line.
1269         [ -n "$out" ] && out="${out}${padding}"
1270
1271         # Break the text at $width.
1272         out="${out}${text:0:${width}}"
1273         text="${text:${width}}"
1274
1275         # If we have left over text then the line break may be ugly,
1276         # so let's check and try to break it on a space.
1277         if [ -n "$text" ] ; then
1278             # The 'x's stop us producing a special character like '(',
1279             # ')' or '!'.  Yuck - there must be a better way.
1280             if [ "x${text:0:1}" != "x " -a "x${text: -1:1}" != "x " ] ; then
1281                 # We didn't break on a space.  Arrange for the
1282                 # beginning of the broken "word" to appear on the next
1283                 # line but not if it will make us loop infinitely.
1284                 if [ "${orig}" != "${out##* }${text}" ] ; then
1285                     text="${out##* }${text}"
1286                     out="${out% *}"
1287                 else
1288                     # Hmmm, doing that would make us loop, so add the
1289                     # rest of the word from the remainder of the text
1290                     # to this line and let it extend past $COLUMNS - 2.
1291                     out="${out}${text%% *}"
1292                     if [ "${text# *}" != "$text" ] ; then
1293                         # Remember the text after the next space for next time.
1294                         text="${text# *}"
1295                     else
1296                         # No text after next space.
1297                         text=""
1298                     fi
1299                 fi
1300             else
1301                 # We broke on a space.  If it will be at the beginning
1302                 # of the next line then remove it.
1303                 text="${text# }"
1304             fi
1305         fi
1306     done
1307
1308     echo "$out"
1309 }
1310
1311 # Display usage text, trying these approaches in order.
1312 # 1. See if it all fits on one line before $COLUMNS - 2.
1313 # 2. See if splitting before the default value and indenting it
1314 #    to $startcol means that nothing passes $COLUMNS - 2.
1315 # 3. Treat the message and default value as a string and just us fill_text()
1316 #    to format it. 
1317 usage_display_text () {
1318     local startcol="$1"
1319     local desc="$2"
1320     local default="$3"
1321     
1322     local width=$(($COLUMNS - 2 - $startcol))
1323     [ $width -lt 0 ] && width=$((78 - $startcol))
1324
1325     default="(default \"$default\")"
1326
1327     if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
1328         echo "${desc} ${default}"
1329     else
1330         local padding=$(printf "%${startcol}s" " ")
1331
1332         if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
1333             echo "$desc"
1334             echo "${padding}${default}"
1335         else
1336             fill_text $startcol "${desc} ${default}"
1337         fi
1338     fi
1339 }
1340
1341 # Display usage information for long config options.
1342 usage_smart_display () {( # sub-shell for local declaration of defconf()
1343         local startcol=33
1344
1345         defconf() {
1346             local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
1347
1348             printf "     --%-25s " "${longopt}=${3}"
1349
1350             usage_display_text $startcol "$4" "$2"
1351         }
1352
1353         "$@"
1354 )}
1355
1356
1357 # Display usage information for long config options.
1358 usage_config_options (){
1359     usage_smart_display load_config
1360 }
1361
1362 actions_init ()
1363 {
1364     actions=""
1365 }
1366
1367 actions_add ()
1368 {
1369     actions="${actions}${actions:+ }$*"
1370 }
1371
1372 actions_run ()
1373 {
1374     [ -n "$actions" ] || usage
1375
1376     local a
1377     for a in $actions ; do
1378         $a
1379     done
1380 }
1381
1382 ######################################################################
1383
1384 post_config_hooks=
1385
1386 ######################################################################
1387
1388 load_config
1389
1390 ############################
1391 # parse command line options
1392 long_opts=$(getopt_config_options)
1393 getopt_output=$(getopt -n autocluster -o "c:e:E:xh" -l help,dump -l "$long_opts" -- "$@")
1394 [ $? != 0 ] && usage
1395
1396 use_default_config=true
1397
1398 # We do 2 passes of the options.  The first time we just handle usage
1399 # and check whether -c is being used.
1400 eval set -- "$getopt_output"
1401 while true ; do
1402     case "$1" in
1403         -c) shift 2 ; use_default_config=false ;;
1404         -e) shift 2 ;;
1405         -E) shift 2 ;;
1406         --) shift ; break ;;
1407         --dump|-x) shift ;;
1408         -h|--help) usage ;; # Usage should be shown here for real defaults.
1409         --*) shift 2 ;; # Assume other long opts are valid and take an arg.
1410         *) usage ;; # shouldn't happen, so this is reasonable.
1411     esac
1412 done
1413
1414 config="./config"
1415 $use_default_config && [ -r "$config" ] && . "$config"
1416
1417 eval set -- "$getopt_output"
1418
1419 while true ; do
1420     case "$1" in
1421         -c)
1422             b=$(basename $2)
1423             # force at least ./local_file to avoid accidental file
1424             # from $PATH
1425             . "$(dirname $2)/${b}"
1426             # If $CLUSTER is unset then try to base it on the filename
1427             if [ ! -n "$CLUSTER" ] ; then
1428                 case "$b" in
1429                     *.autocluster)
1430                         CLUSTER="${b%.autocluster}"
1431                 esac
1432             fi
1433             shift 2
1434             ;;
1435         -e) no_sanity=1 ; run_hooks post_config_hooks ; eval "$2" ; exit ;;
1436         -E) eval "$2" ; shift 2 ;;
1437         -x) set -x; shift ;;
1438         --dump) no_sanity=1 ; run_hooks post_config_hooks ; dump_config ;;
1439         --) shift ; break ;;
1440         -h|--help) usage ;; # Redundant.
1441         --*)
1442             # Putting --opt1|opt2|... into a variable and having case
1443             # match against it as a pattern doesn't work.  The | is
1444             # part of shell syntax, so we need to do this.  Look away
1445             # now to stop your eyes from bleeding! :-)
1446             x=",${long_opts}" # Now each option is surrounded by , and :
1447             if [ "$x" != "${x#*,${1#--}:}" ] ; then
1448                 # Our option, $1, surrounded by , and : was in $x, so is legal.
1449                 setconf_longopt "$1" "$2"; shift 2
1450             else
1451                 usage
1452             fi
1453             ;;
1454         *) usage ;; # shouldn't happen, so this is reasonable.
1455     esac
1456 done
1457
1458 run_hooks post_config_hooks 
1459
1460 # catch errors
1461 set -e
1462 set -E
1463 trap 'es=$?; 
1464       echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es; 
1465       exit $es' ERR
1466
1467 # check for needed programs 
1468 check_command expect
1469
1470 [ $# -lt 1 ] && usage
1471
1472 t="$1"
1473 shift
1474
1475 case "$t" in
1476     base)
1477         actions_init
1478         for t in "$@" ; do
1479             case "$t" in
1480                 create|boot) actions_add "base_${t}" ;;
1481                 *) usage ;;
1482             esac
1483         done
1484         actions_run
1485         ;;
1486
1487     cluster)
1488         actions_init
1489         for t in "$@" ; do
1490             case "$t" in
1491                 destroy|undefine|create|update_hosts|boot|setup)
1492                     actions_add "cluster_${t}" ;;
1493                 build)
1494                     for t in destroy undefine create update_hosts boot setup ; do
1495                         actions_add "cluster_${t}"
1496                     done
1497                     ;;
1498                 *) usage ;;
1499             esac
1500         done
1501         cluster_nodelist_hacking
1502         actions_run
1503         ;;
1504
1505     create)
1506         t="$1"
1507         shift
1508         case "$t" in
1509             base)
1510                 [ $# != 0 ] && usage
1511                 base_create
1512                 ;;
1513             cluster)
1514                 [ $# != 1 ] && usage
1515                 cluster_create "$1"
1516                 ;;
1517             node)
1518                 [ $# != 2 ] && usage
1519                 create_one_node "$1" "$2"
1520                 ;;
1521             *)
1522                 usage;
1523                 ;;
1524         esac
1525         ;;
1526     mount)
1527         [ $# != 1 ] && usage
1528         diskimage mount "$1"
1529         ;;
1530     unmount|umount)
1531         [ $# != 0 ] && usage
1532         diskimage unmount
1533         ;;
1534     bootbase)
1535         base_boot;
1536         ;;
1537     *)
1538         usage;
1539         ;;
1540 esac