Hi Perry, Thanks for the create-wui-appliance.sh script. It nicely eliminates several annoyingly manual steps. Here are some small changes. I've tested the result. Barring objections I'll push soon. create-wui-appliance.sh: mostly-minor changes * wui-appliance/create-wui-appliance.sh: Remove unnecessary quotes in var=... RHS. Remove unnecessary braces in ${VAR_NAME}/. Don't redirect function definition. Use 'cat<<EOF' instead of repeated echo. Add quotes around various $VAR uses, in case they contains shell meta-characters. Hoist default arch and image dir definitions, and use them in usage. Use bash's built-in "getopts" function. Accept "-h" option (for help). Upon usage error, refer to -h, rather than printing full usage. diff --git a/wui-appliance/create-wui-appliance.sh b/wui-appliance/create-wui-appliance.sh index 0167576..4665c59 100755 --- a/wui-appliance/create-wui-appliance.sh +++ b/wui-appliance/create-wui-appliance.sh @@ -1,57 +1,60 @@ #!/bin/bash +ME=$(basename "$0") +warn() { printf "$ME: $@\n" >&2; } +try_h() { printf "Try \`$ME -h' for more information.\n" >&2; } +die() { warn "$@"; try_h; exit 1; } + NAME=developer RAM=512 -IMGNAME=${NAME}.img +IMGNAME=$NAME.img IMGSIZE=6 -usage() { - echo "usage: $0 -i install_iso [-d image_dir] [-a x86_64|i386] [-m MAC]" - echo " -i: location of installation ISO" - echo " -d: directory to place virtual disk (default: /var/lib/libvirt/images)" - echo " -a: architecture for the virtual machine (default: x86_64)" - echo " -m: specify fixed MAC address for the primary network interface" - exit 1 -} >&2 - MAC ISO-IMGDIR=/var/lib/libvirt/images -ARCH=x86_64 -for i ; do - case $1 in - -i) - [ $# -lt 2 ] && usage - ISO="$2" - shift; shift;; - -d) - [ $# -lt 2 ] && usage - IMGDIR="$2" - shift; shift;; - -a) - [ $# -lt 2 ] && usage - ARCH="$2" - shift; shift;; - -m) - [ $# -lt 2 ] && usage - MAC="$2" - shift; shift;; - -?|-*) - usage;; +IMGDIR_DEFAULT=/var/lib/libvirt/images +ARCH_DEFAULT=x86_64 + +ARCH=$ARCH_DEFAULT +IMGDIR=$IMGDIR_DEFAULT + +usage() { + case $# in 1) warn "$1"; try_h; exit 1;; esac + cat <<EOF +Usage: $ME -i install_iso [-d image_dir] [-a x86_64|i386] [-m MAC] + -i: location of installation ISO (required) + -d: directory to place virtual disk (default: $IMGDIR_DEFAULT) + -a: architecture for the virtual machine (default: $ARCH_DEFAULT) + -m: specify fixed MAC address for the primary network interface + -h: display this help and exit +EOF +} + +err=0 help=0 +while getopts :a:d:i:m:h c; do + case $c in + i) ISO=$OPTARG;; + d) IMGDIR=$OPTARG;; + a) ARCH=$OPTARG;; + m) MAC=$OPTARG;; + h) help=1;; + '?') err=1; warn "invalid option: \`-$OPTARG'";; + :) err=1; warn "missing argument to \`-$OPTARG' option";; + *) err=1; warn "internal error: \`-$OPTARG' not handled";; esac done +test $err = 1 && { try_h; exit 1; } +test $help = 1 && { usage; exit 0; } -if [ -z $ISO ]; then - echo "Please supply the location of the OS ISO" >&2 - usage -fi +test -z "$ISO" && usage "no ISO file specified" +test -r "$ISO" || usage "missing or unreadable ISO file: \`$ISO'" -if [[ "$ARCH" != "i386" && "$ARCH" != "x86_64" ]]; then - echo "Please specify a valid architecture" >&2 - usage -fi +case $ARCH in + i386|x86_64);; + *) usage "invalid architecture: \`$ARCH'";; +esac -if [ -n $MAC ]; then +if [ -n "$MAC" ]; then MAC="-m $MAC" fi @@ -59,8 +62,8 @@ mkdir -p $IMGDIR virsh destroy $NAME > /dev/null 2>&1 virsh undefine $NAME > /dev/null 2>&1 -virt-install -n $NAME -r $RAM -f $IMGDIR/$IMGNAME -s $IMGSIZE --vnc \ - --accelerate -v -c $ISO --os-type=linux --arch=$ARCH \ +virt-install -n $NAME -r $RAM -f "$IMGDIR/$IMGNAME" -s $IMGSIZE --vnc \ + --accelerate -v -c "$ISO" --os-type=linux --arch=$ARCH \ --noreboot $MAC ./ovirt-mod-xml.sh virsh start $NAME diff --git a/wui-appliance/ovirt-mod-xml.sh b/wui-appliance/ovirt-mod-xml.sh old mode 100755 new mode 100644 -- 1.5.5.rc3.1.gaece
Perry N. Myers
2008-Apr-04 16:10 UTC
[Ovirt-devel] create-wui-appliance.sh: useful script!
Jim Meyering wrote:> Hi Perry, > > Thanks for the create-wui-appliance.sh script. > It nicely eliminates several annoyingly manual steps. > > Here are some small changes. I've tested the result. > Barring objections I'll push soon. > > create-wui-appliance.sh: mostly-minor changes > * wui-appliance/create-wui-appliance.sh: Remove unnecessary > quotes in var=... RHS. Remove unnecessary braces in ${VAR_NAME}/. > Don't redirect function definition. Use 'cat<<EOF' instead of repeated > echo. Add quotes around various $VAR uses, in case they contains shell > meta-characters. Hoist default arch and image dir definitions, > and use them in usage. Use bash's built-in "getopts" function. > Accept "-h" option (for help). Upon usage error, refer to -h, > rather than printing full usage.Thanks for cleaning this up. :) I had no idea there was getopt for bash... I've been parsing script cmdline args manually for years. Lots of other good techniques in here for me to absorb too. ACK Perry
Jim Meyering wrote:> Thanks for the create-wui-appliance.sh script. > It nicely eliminates several annoyingly manual steps. > Here are some small changes. I've tested the result.Here's one more to create dummybridge via libvirt. You must remove manually created dummybridge first: - stop VMs - ifdown dummybridge; brctl delbr dummybridge; find /etc/sysconfig/ -name ifcfg-dummybridge -exec rm {} \; diff --git a/wui-appliance/create-wui-appliance.sh b/wui-appliance/create-wui-appliance.sh index 0167576..b152cc5 100755 --- a/wui-appliance/create-wui-appliance.sh +++ b/wui-appliance/create-wui-appliance.sh @@ -55,6 +55,15 @@ if [ -n $MAC ]; then MAC="-m $MAC" fi +TMPXML=$(mktemp) || exit 1 +cat > $TMPXML <<EOF +<network> <name>dummy</name> <bridge name="dummybridge" stp="off" forwardDelay="0" /> <ip address="192.168.50.1" netmask="255.255.255.0"/> </network> +EOF +virsh net-define $TMPXML +rm $TMPXML +virsh net-start dummy +virsh net-autostart dummy + mkdir -p $IMGDIR virsh destroy $NAME > /dev/null 2>&1