diff mbox

[KVM_AUTOTEST] added script for unattended install

Message ID 1243981484-6391-2-git-send-email-dhuff@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

David Huff June 2, 2009, 10:24 p.m. UTC
---
 client/tests/kvm_runtest_2/scripts/unattended.sh |  220 ++++++++++++++++++++++
 1 files changed, 220 insertions(+), 0 deletions(-)
 create mode 100755 client/tests/kvm_runtest_2/scripts/unattended.sh
diff mbox

Patch

diff --git a/client/tests/kvm_runtest_2/scripts/unattended.sh b/client/tests/kvm_runtest_2/scripts/unattended.sh
new file mode 100755
index 0000000..7928117
--- /dev/null
+++ b/client/tests/kvm_runtest_2/scripts/unattended.sh
@@ -0,0 +1,220 @@ 
+#!/bin/bash
+#
+# unattended.sh - sets up environment for an unattended install for kvm_autotest
+#
+# Copyright (C) 2009 Red Hat, Inc.
+# Written by:
+# 	David Huff <dhufff@redhat.com> 
+#	Darryl L. Pierce <dpierce@redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+
+ME=$(basename "$0")
+warn() { printf '%s: %s\n' "$ME" "$*" >&2; }
+die() {  warn "$*"; exit 1; }
+debug() { if $debugging; then log "[DEBUG] %s" "$*"; fi }
+
+set -e
+
+log () {
+    date=$(date)
+    printf "${date} $*\n"
+}
+
+usage () {
+    cat <<EOF
+Usage: $ME [-c unattended_config_file] [-n name] [LOGFILE]
+  -a: auto, pulls parameters from environment 
+  -c: unattended config file to use
+  -n: name to append to floppy.img
+  -p: pxeboot and use iso image provided to setup pxe tree
+  -k: extra kernel args to use when pxe booting
+  -d: enable more verbose output (default: disabled)
+  -v: enable tracing (default: disabled)
+  -h: display this help and exit
+EOF
+}
+
+# creates a folder to use as virtual floppy disk 
+# copys correct config file to this dir
+create_virtual_floppy_disk () {
+    local dir="floppy"
+    
+    if [ -d $dir ]; then
+    	rmdir $dir
+    fi
+    
+    mkdir $dir	
+
+    #copy config file to disk
+    if [[ "$CONFIG" =~ \.ks\$ ]] || [["$CONFIG" =~ \.cfg\$ ]]; then
+    	cp $CONFIG $dir/ks.cfg
+    else
+    	cp $CONFIG $dir
+    fi
+    
+    #change permissions
+    #chmod 755 $filename
+}
+
+
+# creates a floppy disk file and adds unattended config file
+# $CONFIG file to stick on floppy disk
+create_floppy_disk () {
+	local size="144"
+    local filename="images/floppy.img"
+    
+    #create floppy disk image   
+    debug "Creating floppy disk: filename=${filename} size=${size}"
+    qemu-img create -f raw $filename "${size}M" > /dev/null 2>&1
+    mkfs.msdos $filename
+    
+    #mount floppy disk image
+    mp=$(mktemp -d floppytmp.XXXXXX)
+    mount -t vfat -v -o loop $filename $mp
+    
+    #copy config file to disk
+    if [[ "$CONFIG" =~ \.ks\$ ]] || [["$CONFIG" =~ \.cfg\$ ]]; then
+    	cp $CONFIG $mp/ks.cfg
+    else
+    	cp $CONFIG $mp
+    fi
+    
+    #unmount floppy
+    df | grep $mp > /dev/null 2>&1 && umount -v $mp
+    rmdir $mp
+    
+    #change permissions
+    chmod 755 $filename
+}
+
+
+# setup pxeboot evironment
+# $PXE - iso image
+# $ARGS - kernel arguments
+setup_pxeboot () {
+	local workdir=$PWD/tftpboot
+	local iso=$PXE
+    local pxedefault=$workdir/pxelinux.cfg/default
+    local pxelinux="/usr/lib/syslinux/pxelinux.0"
+    local cdlocation="images/pxeboot/"
+    
+	# Check pxelinux.0 exists.
+	if [ ! -f /usr/share/syslinux/pxelinux.0 -a ! -f /usr/lib/syslinux/pxelinux.0 ]; then
+    	die "Warning: pxelinux.0 not found, Make sure syslinux or pxelinux is installed on this system."
+	fi
+	
+	#create clean tftpboot dir
+	if [ -d $workdir ]; then
+    log "$ME: subdirectory $workdir exists already.  overwriting"
+    rm -rf $workdir
+	fi
+	
+	mkdir -p $workdir/pxelinux.cfg  
+    	
+	# pxelinux bootloader.
+	if [ -f /usr/share/syslinux/pxelinux.0 ]; then
+    	cp /usr/share/syslinux/pxelinux.0 $workdir
+	elif [ -f /usr/lib/syslinux/pxelinux.0 ]; then
+    	cp /usr/lib/syslinux/pxelinux.0 $workdir
+	else
+    	die "Warning: pxelinux.0 not found, Make sure syslinux or pxelinux is installed on this system."
+	fi	
+	
+	# get vmlinz and initrd form image
+    mp=$(mktemp -d cdtmp.XXXXXX)
+    mount -v -o loop "$iso" $mp
+        
+	# Does it look like an ISO?
+	if [ ! -d $mp/$cdlocation ]; then
+	    die "The ISO image doesn't look like a ISO image to me."
+	fi
+	
+	cp $mp/$cdlocation/initrd.img $mp/$cdlocation/vmlinuz $workdir
+	df | grep $mp > /dev/null 2>&1 && umount -v $mp
+	rmdir $mp
+
+    # set default kernel arguments if none were provided
+    if [ -z "$ARGS" ]; then
+	local $ARGS=""
+    fi
+
+    local definition="DEFAULT pxeboot"
+    definition="${definition}\nTIMEOUT 20"
+    definition="${definition}\nPROMPT 0"
+    definition="${definition}\nLABEL pxeboot"
+    definition="${definition}\n     KERNEL vmlinuz"
+    definition="${definition}\n     IPAPPEND 2"
+    definition="${definition}\n     APPEND initrd=initrd.img $ARGS"
+
+    debug "pxeboot definition=\n${definition}"
+    printf "${definition}\n" > $pxedefault
+}
+
+debugging=false
+CONFIG=
+NAME=
+PXE=
+ARGS=
+
+while getopts adc:n:p:k:vh c; do
+    case $c in
+    a) CONFIG=$KVM_TEST_unattended_file
+       NAME=$KVM_TEST_image
+       PXE=isos/$KVM_TEST_cdrom
+       ARGS=$KVM_TEST_kernel_args;;
+	d) debugging=true;;
+	c) CONFIG=$OPTARG;;
+	n) NAME=$OPTARG;;
+	p) PXE=$OPTARG;;
+	k) ARGS=$OPTARG;;
+	v) set -v;;
+	h) usage; exit 0;;
+	'?') die "invalid option \`-$OPTARG'";;
+	:) die "missing argument to \`-$OPTARG' option";;
+	*) die "internal error";;
+    esac
+done
+
+shift $(($OPTIND - 1))
+
+set +u
+if [ $# -gt 0 -a -n "$1" ]; then RESULTS=$1; else RESULTS=unattended.log; fi
+set -u
+
+# check to see we are root
+if [ $( id -u ) -ne 0 ]; then
+    die "Must run as root"
+fi
+
+log "$ME: Logging results to file: ${RESULTS}"
+
+# Require "-o OUTPUT_FILE"
+if test -z $CONFIG; then
+	die "no config file for unattended install" 
+fi
+
+log "$ME: using unattended config file: $CONFIG"
+
+# create teh floppy image
+create_floppy_disk $CONFIG
+#create_virtual_floppy_disk $CONFIG
+
+#if [ $PXE ]; then 
+	debug "$ME: using $PXE to set up pxe environmet"
+	setup_pxeboot
+#fi