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