scripts: Add autocluster-test-ctdb script
[tridge/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     installdir="`dirname \"$0\"`"
25 else
26     autocluster=`which $0`
27     installdir="`dirname \"$autocluster\"`"
28 fi
29 ##END-INSTALLDIR-MAGIC##
30
31 ####################
32 # show program usage
33 usage ()
34 {
35     cat <<EOF
36 Usage: autocluster [OPTION] ... <COMMAND>
37   options:
38      -c <file>                   specify config file (default is "config")
39 EOF
40
41     local releases=$(echo $(cd $installdir/releases && ls))
42     releases="\"${releases// /\", \"}\""
43
44     usage_smart_display \
45         defconf "WITH_RELEASE" "" \
46         "<string>" "specify kickstart and yum templates using a release version string.  Possible values are: ${releases}."
47
48 cat <<EOF
49      -x                          enable script debugging
50      --dump                      dump config settings and exit
51
52   configuration options:
53 EOF
54
55     usage_config_options
56
57     cat <<EOF
58
59   commands:
60      create base
61            create a base image
62
63      create cluster CLUSTERNAME
64            create a full cluster
65
66      create node CLUSTERNAME NODENUMBER
67            create one cluster node
68
69      create tsm CLUSTERNAME
70            create a TSM server node
71
72      mount DISK
73            mount a qemu disk on mnt/
74
75      unmount
76            unmount a qemu disk from mnt/
77
78      bootbase
79            boot the base image
80
81      testproxy
82            test your proxy setup
83 EOF
84     exit 1
85 }
86
87 ###############################
88 # common node creation stuff
89 create_node_common() {
90     local node="$1"
91
92     local nodenumber
93     local template_file
94     if [ "$node" = "tsm" ] ; then
95         nodenumber=0
96         NAME="${CLUSTER}tsm"
97         template_file=$TSM_TEMPLATE
98         echo "Creating TSM cluster node $NAME"
99     else
100         nodenumber=$node
101         NAME="${CLUSTER}n${nodenumber}"
102         template_file=$NODE_TEMPLATE
103         echo "Creating cluster node $NAME"
104     fi
105
106     IPNUM=$(($FIRSTIP + $nodenumber))
107     DISK="${VIRTBASE}/${CLUSTER}/${NAME}.qcow2"
108
109     mkdir -p $VIRTBASE/$CLUSTER tmp
110
111     echo "Creating the disk"
112     rm -f "$DISK"
113     qemu-img create -b "$VIRTBASE/$BASENAME.img" -f qcow2 "$DISK"
114
115     mount_disk $DISK
116     setup_base
117     setup_network
118     unmount_disk
119
120     set_macaddrs $CLUSTER $nodenumber
121     UUID=`uuidgen`
122     
123     echo "Creating $NAME.xml"
124     substitute_vars $template_file tmp/$NAME.xml
125     
126     # install the XML file
127     $VIRSH undefine $NAME > /dev/null 2>&1 || true
128     $VIRSH define tmp/$NAME.xml
129 }
130
131 ###############################
132 # create a single node
133 create_node() {
134     CLUSTER="$1"
135     local nodenumber="$2"
136
137     # first node might need more memory for SoFS GUI
138     if [ "$WITH_SOFS_GUI" = 1 -a $nodenumber = 1 -a $GUIMEM -gt $MEM ]; then
139         NODEMEM=$GUIMEM
140     else
141         NODEMEM=$MEM
142     fi
143
144     create_node_common "$nodenumber"
145 }
146
147 ###############################
148 # create a TSM node
149 create_tsm() {
150     CLUSTER="$1"
151
152     # This must be before create_node_common.
153     TSMDISK="${VIRTBASE}/${CLUSTER}/tsmstorage.qcow2"
154
155     create_node_common "tsm"
156
157     echo "Creating tsm disk"
158     qemu-img create -f qcow2 "$TSMDISK" $TSMDISKSIZE
159 }
160
161 ###############################
162 # create a whole cluster
163 create_cluster() {
164     CLUSTER="$1"
165
166     mkdir -p $VIRTBASE/$CLUSTER tmp
167     mkdir -p $KVMLOG
168
169     echo "Creating 3 shared disks"
170     for i in `seq 1 3`; do
171         # setup a nice ID at the start of the disk
172         qemu-img create -f raw $VIRTBASE/$CLUSTER/shared$i $SHAREDDISKSIZE
173         echo "SOFS-`uuidgen`" > tmp/diskid
174         dd if=tmp/diskid of=$VIRTBASE/$CLUSTER/shared$i conv=notrunc bs=1 > /dev/null 2>&1
175     done
176
177     echo "Creating $NUMNODES base nodes"
178     for i in `seq 1 $NUMNODES`; do
179         create_node "$CLUSTER" $i
180     done
181
182     if [ $WITH_TSM_NODE -eq 1 ]; then
183       echo "Creating TSM server node"
184       create_tsm "$CLUSTER"
185     fi
186
187     echo "# autocluster $CLUSTER" > hosts.$CLUSTER
188     [ $WITH_TSM_NODE -eq 1 ] && {
189         echo "$IPBASE.0.$FIRSTIP ${CLUSTER}tsm.$LOWDOMAIN ${CLUSTER}tsm" >> hosts.$CLUSTER
190     }
191     for i in `seq 1 $NUMNODES`; do
192         echo "$IPBASE.0.`expr $FIRSTIP + $i` ${CLUSTER}n$i.$LOWDOMAIN ${CLUSTER}n$i" >> hosts.$CLUSTER
193     done
194     echo >> hosts.$CLUSTER
195
196     echo "Cluster $CLUSTER created"
197     echo "You may want to add this to your /etc/hosts file:"
198     cat hosts.$CLUSTER
199
200     echo
201 }
202
203 ###############################
204 # test the proxy setup
205 test_proxy() {
206     export http_proxy=$WEBPROXY
207     wget -O /dev/null $INSTALL_SERVER || {
208         echo "Your WEBPROXY setting \"$WEBPROXY\" is not working"
209         exit 1
210     }
211     echo "Proxy OK"
212 }
213
214 ###################
215 # create base image
216 create_base() {
217
218     NAME="$BASENAME"
219     DISK="$VIRTBASE/$NAME.img"
220
221     mkdir -p $KVMLOG
222
223     echo "Testing WEBPROXY $WEBPROXY"
224     test_proxy
225
226     echo "Creating the disk"
227     qemu-img create -f $BASE_FORMAT "$DISK" $DISKSIZE
228
229     rm -rf tmp
230     mkdir -p mnt tmp tmp/ISO
231
232     setup_timezone
233     setup_indirects
234
235     echo "Creating kickstart file from template"
236     substitute_vars "$KICKSTART" "tmp/ks.cfg"
237
238     if [ $INSTALLKEY = "--skip" ]; then
239         cat <<EOF
240 --------------------------------------------------------------------------------------
241 WARNING: You have not entered an install key. Some RHEL packages will not be installed.
242
243 Please enter a valid RHEL install key in your config file like this:
244
245   INSTALLKEY="1234-5678-0123-4567"
246
247 The install will continue without an install key in 5 seconds
248 --------------------------------------------------------------------------------------
249 EOF
250         sleep 5
251     fi
252     
253     echo "Creating kickstart floppy"
254     dd if=/dev/zero of=tmp/floppy.img bs=1024 count=1440
255     mkdosfs tmp/floppy.img
256     mount -o loop -t msdos tmp/floppy.img mnt
257     cp tmp/ks.cfg mnt
258     mount -o loop,ro $ISO tmp/ISO
259     
260     echo "Setting up bootloader"
261     cp tmp/ISO/isolinux/isolinux.bin tmp
262     cp tmp/ISO/isolinux/vmlinuz tmp
263     cp tmp/ISO/isolinux/initrd.img tmp
264     umount tmp/ISO
265     umount mnt
266
267     UUID=`uuidgen`
268
269     substitute_vars $INSTALL_TEMPLATE tmp/$NAME.xml
270
271     rm -f $KVMLOG/serial.$NAME
272
273     # boot the install CD
274     $VIRSH create tmp/$NAME.xml
275
276     echo "Waiting for install to start"
277     sleep 2
278     
279     # wait for the install to finish
280     if ! waitfor $KVMLOG/serial.$NAME "you may safely reboot your system" 3600; then
281         $VIRSH destroy $NAME
282         echo "Failed to create base image $DISK"
283         exit 1
284     fi
285     
286     $VIRSH destroy $NAME
287
288     ls -l $DISK
289     cat <<EOF
290
291 Install finished, base image $DISK created
292
293 You may wish to run
294    chattr +i $DISK
295 To ensure that this image does not change
296
297 Note that the root password has been set to $ROOTPASSWORD
298
299 EOF
300 }
301
302 ###############################
303 # boot the base disk
304 boot_base() {
305     CLUSTER="$1"
306
307     NAME="$BASENAME"
308     DISK="$VIRTBASE/$NAME.img"
309
310     rm -rf tmp
311     mkdir -p tmp
312
313     IPNUM=$FIRSTIP
314     CLUSTER="base"
315
316     mount_disk $DISK
317     setup_base
318     unmount_disk
319
320     UUID=`uuidgen`
321     
322     echo "Creating $NAME.xml"
323     substitute_vars $BOOT_TEMPLATE tmp/$NAME.xml
324     
325     # boot the base system
326     $VIRSH create tmp/$NAME.xml
327 }
328
329 ######################################################################
330
331 # various functions...
332
333 # Set some MAC address variables based on a hash of the cluster name
334 # plus the node number and each adapter number.
335 set_macaddrs () {
336     local cname="$1"
337     local nodenumber="$2"
338
339     local md5=$(echo $cname | md5sum)
340     local nh=$(printf "%02x" $nodenumber)
341     local mac_prefix="02:${md5:0:2}:${md5:2:2}:00:${nh}:"
342
343     MAC1="${mac_prefix}01"
344     MAC2="${mac_prefix}02"
345     MAC3="${mac_prefix}03"
346     MAC4="${mac_prefix}04"
347     MAC5="${mac_prefix}05"
348     MAC6="${mac_prefix}06"
349 }
350
351 # mount a qemu image via nbd
352 connect_nbd() {    
353     echo "Connecting nbd to $1"
354     mkdir -p mnt
355     modprobe nbd
356     killall -9 -q $QEMU_NBD || true
357     $QEMU_NBD -p 1300 $1 &
358     sleep 1
359     [ -r $NBD_DEVICE ] || {
360         mknod $NBD_DEVICE b 43 0
361     }
362     umount mnt 2> /dev/null || true
363     nbd-client -d $NBD_DEVICE > /dev/null 2>&1 || true
364     killall -9 -q nbd-client || true
365     nbd-client localhost 1300 $NBD_DEVICE > /dev/null 2>&1 || true &
366     sleep 1
367 }
368
369 # disconnect nbd
370 disconnect_nbd() {
371     echo "Disconnecting nbd"
372     sync; sync
373     nbd-client -d $NBD_DEVICE > /dev/null 2>&1 || true
374     killall -9 -q nbd-client || true
375     killall -q $QEMU_NBD || true
376 }
377
378 # mount a qemu image via nbd
379 mount_disk() {    
380     connect_nbd $1
381     echo "Mounting disk $1"
382     mount_ok=0
383     for i in `seq 1 5`; do
384         mount -o offset=32256 $NBD_DEVICE mnt && {
385             mount_ok=1
386             break
387         }
388         umount mnt 2>/dev/null || true
389         sleep 1
390     done
391     [ $mount_ok = 1 ] || {
392         echo "Failed to mount $1"
393         exit 1
394     }
395     [ -d mnt/root ] || {
396         echo "Mounted directory does not look like a root filesystem"
397         ls -latr mnt
398         exit 1
399     }
400 }
401
402 # unmount a qemu image
403 unmount_disk() {
404     echo "Unmounting disk"
405     sync; sync;
406     umount mnt || umount mnt || true
407     disconnect_nbd
408 }
409
410 # setup the files from $BASE_TEMPLATES/, substituting any variables
411 # based on the config
412 setup_base() {
413     umask 022
414     setup_indirects
415     echo "Copy base files"
416     for f in `cd $BASE_TEMPLATES && find . \! -name '*~'`; do
417         if [ -d "$BASE_TEMPLATES/$f" ]; then
418             mkdir -p mnt/"$f"
419         else 
420             substitute_vars "$BASE_TEMPLATES/$f" "mnt/$f"
421         fi
422         chmod --reference="$BASE_TEMPLATES/$f" "mnt/$f"
423     done
424     # this is needed as git doesn't store file permissions other
425     # than execute
426     chmod 600 mnt/etc/ssh/*key mnt/root/.ssh/*
427     chmod 700 mnt/etc/ssh mnt/root/.ssh mnt/root
428     if [ -r "$HOME/.ssh/id_rsa.pub" ]; then
429        echo "Adding $HOME/.ssh/id_rsa.pub to ssh authorized_keys"
430        cat "$HOME/.ssh/id_rsa.pub" >> mnt/root/.ssh/authorized_keys
431     fi
432     if [ -r "$HOME/.ssh/id_dsa.pub" ]; then
433        echo "Adding $HOME/.ssh/id_dsa.pub to ssh authorized_keys"
434        cat "$HOME/.ssh/id_dsa.pub" >> mnt/root/.ssh/authorized_keys
435     fi
436     echo "Adjusting grub.conf"
437     local o="$EXTRA_KERNEL_OPTIONS"
438     sed -e "s/console=ttyS0,19200/console=ttyS0,115200/"  \
439         -e "s/ nodmraid//" -e "s/ nompath//"  \
440         -e "s/quiet/divider=10${o:+ }${o}/g" mnt/boot/grub/grub.conf -i.org
441 }
442
443 setup_indirects() {
444     # Order is very important here, since postinstall.sh can use
445     # @@YUM_REPOS@@.
446
447     substitute_vars $YUM_TEMPLATE tmp/SOFS.repo
448     YUM_REPOS="`cat tmp/SOFS.repo`"
449
450     substitute_vars $POSTINSTALL_TEMPLATE tmp/postinstall.sh
451     EXTRA_POSTINSTALL="`cat tmp/postinstall.sh`"
452
453     substitute_vars "$installdir/templates/force-net.sh" tmp/force-net.sh
454     AUTOCLUSTER_POSTINSTALL="`cat tmp/force-net.sh`"
455 }
456
457 # setup various networking components
458 setup_network() {
459     echo "Setting up networks"
460
461     for i in `seq 1 $NUMNODES`; do
462         echo "$IPBASE.0.`expr $FIRSTIP + $i` ${CLUSTER}n$i.$LOWDOMAIN ${CLUSTER}n$i" >> mnt/etc/hosts
463     done
464
465     echo "Setting up /etc/ctdb/nodes"
466     mkdir -p mnt/etc/ctdb
467     rm -f mnt/etc/ctdb/nodes
468     for i in `seq 1 $NUMNODES`; do
469         echo "$IPBASE.0.`expr $FIRSTIP + $i`" >> mnt/etc/ctdb/nodes
470     done
471
472     [ "$WEBPROXY" = "" ] || {
473         echo "export http_proxy=$WEBPROXY" >> mnt/etc/bashrc
474     }
475
476     echo "Enabling nfs mount of $NFSSHARE"
477     mkdir -p mnt/root/SOFS
478     echo "$NFSSHARE /root/SOFS nfs intr" >> mnt/etc/fstab
479
480     mkdir -p mnt/etc/yum.repos.d
481     substitute_vars "$YUM_TEMPLATE" mnt/etc/yum.repos.d/SOFS.repo
482 }
483
484 setup_timezone() {
485     [ -z "$TIMEZONE" ] && {
486         [ -r /etc/timezone ] && {
487             TIMEZONE=`cat /etc/timezone`
488         }
489         [ -r /etc/sysconfig/clock ] && {
490             . /etc/sysconfig/clock
491             TIMEZONE="$ZONE"
492         }
493     }
494     if [ "$TIMEZONE" = "" ]; then
495         echo "Unable to determine TIMEZONE - please set in config"
496         exit 1
497     fi
498 }
499
500 # substite a set of variables of the form @@XX@@ for the shell
501 # variables $XX in a file
502 substitute_vars() {(
503         infile="$1"
504         outfile="$2"
505         delimiter="@"
506         delim2="$delimiter$delimiter"
507
508         # get the list of variables used in the template
509         VARS=`tr -cs "A-Z0-9_$delimiter" '\012' < "$infile" | 
510               sort -u |
511               egrep "^$delim2.*$delim2$" |
512               cut -d$delimiter -f3`
513
514         rm -f sed.$$
515         touch sed.$$
516         for v in $VARS; do
517             # variable variables are fun .....
518             [ "${!v+x}" ] || {
519                 echo "ERROR: No substitution given for ${delim2}$v${delim2} in $infile"
520                 rm -f sed.$$
521                 exit 1
522             }
523             s=${!v}
524             # escape some pesky chars
525             s=${s//
526 /\\n}
527             s=${s//#/\\#}
528             s=${s//&/\\&}
529             echo "s#@@$v@@#$s#g" >> sed.$$
530         done
531
532         sed -f sed.$$ < "$infile" > "$outfile" || exit 1
533         rm -f sed.$$
534 )}
535
536
537 check_command() {
538     if which $1 > /dev/null; then
539         # echo "$1 is installed: OK"
540         :
541     else
542         echo "Please install $1 to continue"
543         exit 1
544     fi
545 }
546
547 # Set a variable if it isn't already set.  This allows environment
548 # variables to override default config settings.
549 defconf() {
550     local v="$1"
551     local e="$2"
552
553     [ "${!v+x}" ] || eval "$v=\"$e\""
554 }
555
556 # Print the list of config variables defined in config.default.
557 get_config_options() {( # sub-shell for local declaration of defconf()
558         local options=
559         defconf() { options="$options $1" ; }
560         . "$installdir/config.default"
561         echo $options
562 )}
563
564 # Produce a list of long options, suitable for use with getopt, that
565 # represent the config variables defined in config.default.
566 getopt_config_options() {
567     local x=$(get_config_options | tr 'A-Z_' 'a-z-')
568     echo "${x// /:,}:"
569 }
570
571 # Unconditionally set the config variable associated with the given
572 # long option.
573 setconf_longopt() {
574     local longopt="$1"
575     local e="$2"
576
577     local v=$(echo "${longopt#--}" | tr 'a-z-' 'A-Z_')
578     # unset so defconf will set it
579     eval "unset $v"
580     defconf "$v" "$e"
581 }
582
583 # Dump all of the current config variables.
584 dump_config() {
585     local o
586     for o in $(get_config_options) ; do
587         echo "${o}=\"${!o}\""
588     done
589     exit 0
590 }
591
592 # Print text assuming it starts after other text in $startcol and
593 # needs to wrap before $fillcol.  Subsequent lines start at $startcol.
594 # Long "words" will extend past $fillcol.
595 fill_text() {
596     local startcol="$1"
597     local fillcol="$2"
598     local text="$3"
599
600     local width=$(($fillcol - $startcol))
601     [ $width -lt 0 ] && width=$((78 - $startcol))
602
603     local out=""
604
605     local padding=$(printf "\n%${startcol}s" " ")
606
607     while [ -n "$text" ] ; do
608         local orig="$text"
609
610         # If we already have output then arrange padding on the next line.
611         [ -n "$out" ] && out="${out}${padding}"
612
613         # Break the text at $width.
614         out="${out}${text:0:${width}}"
615         text="${text:${width}}"
616
617         # If we have left over text then the line break may be ugly,
618         # so let's check and try to break it on a space.
619         if [ -n "$text" ] ; then
620             if [ "${text:0:1}" != " " -a "${text: -1:1}" != " " ] ; then
621                 # We didn't break on a space.  Arrange for the
622                 # beginning of the broken "word" to appear on the next
623                 # line but not if it will make us loop infinitely.
624                 if [ "${orig}" != "${out##* }${text}" ] ; then
625                     text="${out##* }${text}"
626                     out="${out% *}"
627                 else
628                     # Hmmm, doing that would make us loop, so add the
629                     # rest of the word from the remainder of the text
630                     # to this line and let it extend past $fillcol.
631                     out="${out}${text%% *}"
632                     if [ "${text# *}" != "$text" ] ; then
633                         # Remember the text after the next space for next time.
634                         text="${text# *}"
635                     else
636                         # No text after next space.
637                         text=""
638                     fi
639                 fi
640             else
641                 # We broke on a space.  If it will be at the beginning
642                 # of the next line then remove it.
643                 text="${text# }"
644             fi
645         fi
646     done
647
648     echo "$out"
649 }
650
651 # Display usage text, trying these approaches in order.
652 # 1. See if it all fits on one line before $fillcol.
653 # 2. See if splitting before the default value and indenting it
654 #    to $startcol means that nothing passes $fillcol.
655 # 3. Treat the message and default value as a string and just us fill_text()
656 #    to format it. 
657 usage_display_text() {
658     local startcol="$1"
659     local fillcol="$2"
660     local desc="$3"
661     local default="$4"
662     
663     local width=$(($fillcol - $startcol))
664
665     # To avoid clutter, only quote values that need it.
666     #local q=
667     #[ -z "$default" -o "$(echo $default)" = "${default// /}" ] || q="\""
668     default="(default \"$default\")"
669
670     if [ $((${#desc} + 1 + ${#default})) -le $width ] ; then
671         echo "${desc} ${default}"
672     else
673         local padding=$(printf "%${startcol}s" " ")
674
675         if [ ${#desc} -lt $width -a ${#default} -lt $width ] ; then
676             echo "$desc"
677             echo "${padding}${default}"
678         else
679             fill_text $startcol $fillcol "${desc} ${default}"
680         fi
681     fi
682 }
683
684 # Display usage information for long config options.
685 usage_smart_display () {( # sub-shell for local declaration of defconf()
686         local def_fillcol=78
687         local fillcol=$def_fillcol
688         local rows=$(stty size | sed -e 's@.* @@')
689         [ -n "$rows" ] && fillcol=$(($rows - 2))
690
691         local startcol=33
692
693         # We need to have at least one column... negative is also a problem.
694         [ $fillcol -le $startcol ] && fillcol=$def_fillcol
695
696         defconf() {
697             local local longopt=$(echo "$1" | tr 'A-Z_' 'a-z-')
698
699             printf "     --%-25s " "${longopt}=${3}"
700
701             usage_display_text $startcol $fillcol "$4" "$2"
702         }
703
704         "$@"
705 )}
706
707
708 # Display usage information for long config options.
709 usage_config_options(){
710     usage_smart_display . "$installdir/config.default"
711 }
712
713 ######################################################################
714
715 . "$installdir/config.default"
716
717 ############################
718 # parse command line options
719 long_opts=$(getopt_config_options)
720 getopt_output=$(getopt -n autocluster -o "c:xh" -l help,dump,with-release -l "$long_opts" -- "$@")
721 [ $? != 0 ] && usage
722
723 use_default_config=true
724
725 # We do 2 passes of the options.  The first time we just handle usage
726 # and check whether -c is being used.
727 eval set -- "$getopt_output"
728 while true ; do
729     case "$1" in
730         -c) shift 2 ; use_default_config=false ;;
731         --) shift ; break ;;
732         --with-release) shift 2 ;; # Don't set use_default_config=false!!!
733         --dump|-x) shift ;;
734         -h|--help) usage ;; # Usage should be shown here for real defaults.
735         --*) shift 2 ;; # Assume other long opts are valid and take an arg.
736         *) usage ;; # shouldn't happen, so this is reasonable.
737     esac
738 done
739
740 config="./config"
741 $use_default_config && [ -r "$config" ] && . "$config"
742
743 eval set -- "$getopt_output"
744
745 while true ; do
746     case "$1" in
747         -c) . "`dirname $2`/$2" ; shift 2 ;;
748         --with-release)
749             # This simply loads an extra config file from $installdir/releases
750             f="${installdir}/releases/$2"
751             if [ -r "$f" ] ; then
752                 . "$f"
753             else
754                 echo "Unknown release \"$2\" specified to --with-release"
755                 exit 1
756             fi
757             shift 2
758             ;;
759         -x) set -x; shift ;;
760         --dump) dump_config ;;
761         --) shift ; break ;;
762         -h|--help) usage ;; # Redundant.
763         --*)
764             # Putting --opt1|opt2|... into a variable and having case
765             # match against it as a pattern doesn't work.  The | is
766             # part of shell syntax, so we need to do this.  Look away
767             # now to stop your eyes from bleeding! :-)
768             x=",${long_opts}" # Now each option is surrounded by , and :
769             if [ "$x" != "${x#*,${1#--}:}" ] ; then
770                 # Our option, $1, surrounded by , and : was in $x, so is legal.
771                 setconf_longopt "$1" "$2"; shift 2
772             else
773                 usage
774             fi
775             ;;
776         *) usage ;; # shouldn't happen, so this is reasonable.
777     esac
778 done
779
780 # catch errors
781 set -e
782 set -E
783 trap 'es=$?; 
784       echo ERROR: failed in function \"${FUNCNAME}\" at line ${LINENO} of ${BASH_SOURCE[0]} with code $es; 
785       exit $es' ERR
786
787 LOWDOMAIN=`echo $DOMAIN | tr A-Z a-z`
788
789 # check for needed programs 
790 check_command nbd-client
791 check_command expect
792 check_command $QEMU_NBD
793
794 [ $# -lt 1 ] && usage
795
796 command="$1"
797 shift
798
799 case $command in
800     create)
801         type=$1
802         shift
803         case $type in
804             base)
805                 [ $# != 0 ] && usage
806                 create_base;
807                 ;;
808             cluster)
809                 [ $# != 1 ] && usage
810                 create_cluster "$1";
811                 ;;
812             node)
813                 [ $# != 2 ] && usage
814                 create_node "$1" "$2";
815                 ;;
816             tsm)
817                 [ $# != 1 ] && usage
818                 create_tsm "$1";
819                 ;;
820             *)
821                 usage;
822                 ;;
823         esac
824         ;;
825     mount)
826         [ $# != 1 ] && usage
827         mount_disk "$1"
828         ;;
829     unmount)
830         [ $# != 0 ] && usage
831         unmount_disk
832         ;;
833     bootbase)
834         boot_base;
835         ;;
836     testproxy)
837         test_proxy;
838         ;;
839     *)
840         usage;
841         ;;
842 esac