#!/bin/sh # QA Network OS Installation Configuration Script # @(#)jump 7/march/09 # 01/03/09 FIND="/usr/local/bin/find" FIND_MAXDEPTH="5" IMAGE_DIR="/export/fast/images" NETWORK="10.16.22.0" OLDDIR=`pwd` # JUMPSTART OPTIONS PROFILESERVER="10.16.22.10" PROFILEDIR="/export/mirror/jumpstart" KICKDIR="/export/mirror/jes/kickstart" SINSTNM="tgm" SINSTIP4="10.16.22.10" SROOTNM="tgm" SROOTIP4="10.16.22.10" SJUMPSCF="$PROFILESERVER:$PROFILEDIR" SIGHUP=1 SIGINT=2 SIGQUIT=3 SIGTERM=15 welcome() { SCRIPTNAME=$0 ID=`id` USER=`expr "${ID}" : 'uid=\([^(]*\).*'` trap abort $SIGHUP $SIGINT $SIGQUIT $SIGTERM # Verify user ID before proceeding - must be root if [ "${USER}" != "0" ]; then echo "You must be root to run $0" cleanup exit 1 else echo "=============================================================" echo " Welcome to Jump " echo " OS Installation Configuration Utility " echo " G11n Team in Prague " echo "=============================================================" fi } abort() { echo "${SCRIPTNAME}: Aborted" cleanup exit 1 } check_find() { if [ -f $FIND ]; then $FIND /tmp -maxdepth 1 >/dev/null 2>&1 RET=$? if [ $RET -ne 0 ]; then echo "Don't have GNU find, using default. Performance will be lower." GNUFIND=0 else # echo "Using GNU find." GNUFIND=1 fi else echo "The file '$FIND' does not exist. Using /usr/bin/find instead." echo "Performance will be lower." FIND="/usr/bin/find" GNUFIND=0 fi } get_os() { echo; echo "Select OS:" echo "1. Solaris" echo "2. OpenSolaris" echo "3. RedHat Linux" echo printf "Choose OS [1]: " read OS case $OS in 1) LOGOS="Solaris"; solaris;; 2) LOGOS="OpenSolaris"; opensolaris;; 3) LOGOS="RedHat"; LOGPLATFORM="x86"; redhat;; *) LOGOS="Solaris"; solaris;; esac } get_platform() { echo; echo "Select platform:" echo "1. X86" echo "2. SPARC" printf "Choose platform [1]: " read PLATFORM case $PLATFORM in 1) LOGPLATFORM="x86"; sol_x86;; 2) LOGPLATFORM="sparc"; sol_sparc;; *) LOGPLATFORM="x86"; PLATFORM=1; sol_x86;; esac } get_hosts() { cat /etc/ethers | grep "^[0-9]*:" | awk '{ print $2 }' | sed "/lepra/d" >> /tmp/hosts.$$ } show_hosts() { HOSTS=`cat /tmp/hosts.$$` N=1 for i in $HOSTS do echo "$N. $i" >> /tmp/hosts_menu.$$ N=`expr $N + 1` done cat /tmp/hosts_menu.$$ } get_host_mac() { # get the MAC, IP and MAC in DHCP format for the target host HOSTMAC=`cat /etc/ethers | egrep "^[0-9].*" | egrep ":[0-9a-f][0-9a-f][ ]*${USEHOST}[ ]*(#.*)*$" | awk '{ print $1 }'` if [ "$HOSTMAC" = "" ]; then printf "\nError: MAC adress for host '$USEHOST' not found in /etc/ethers!\n\n" exit 3 else echo "MAC: $HOSTMAC" fi } get_host_ip() { HOSTIP=`cat /etc/hosts | grep "${USEHOST}[ ]*$" | awk '{ print $1 }'` if [ "$HOSTIP" = "" ]; then printf "Error: IP adress for host '$USEHOST' not found in /etc/hosts!\n\n" exit 4 else echo "IP: $HOSTIP" fi } # delete DHCP network table for specific machine delete_dhcp_entry() { echo "Deleting DHCP network table for" $USEHOST /usr/sbin/pntadm -D $USEHOST $NETWORK 2>/dev/null } # create DHCP network table for specific machine create_dhcp_entry() { echo "Creating DHCP network table for" $USEHOST /usr/sbin/pntadm -A $USEHOST -f PERMANENT -c $USEHOST -i $CLIENTID -m $CLIENTID $NETWORK } get_client_id() { CLIENTID=01`echo $HOSTMAC |perl -pe 's/:/\n/g' | sed 's/^\([0-9]\)$/0\1/' | sed 's/^\([A-Fa-f]\)$/0\1/' | perl -pe 's/\n//' | tr [:lower:] [:upper:]` delete_dhcp_entry create_dhcp_entry } select_host() { echo; echo "Select host for JumpStart setup:" get_hosts show_hosts USEHOST="" while [ "$USEHOST" = "" ]; do echo; printf "Choose host: " read SELECTION if [ "x`echo ${SELECTION}|egrep '^[a-zA-Z][-a-zA-Z0-9]*$'`" != "x" ]; then USEHOST=`cat /tmp/hosts_menu.$$|egrep "^[0-9]+[.] ${SELECTION}$"| awk '{ print $2 }'` elif [ "x`echo ${SELECTION}|egrep '^[0-9]+$'`" != "x" ]; then USEHOST=`cat /tmp/hosts_menu.$$ | egrep "^${SELECTION}[.] [-0-9a-zA-Z]+$" | awk '{ print $2 }' ` else USEHOST="" fi if [ "$USEHOST" = "" ]; then echo "Invalid selection." fi done echo; echo "Host $USEHOST selected." } get_sol_images() { if [ $GNUFIND -eq 1 ]; then $FIND $IMAGE_DIR -maxdepth $FIND_MAXDEPTH -name .cdtoc > /tmp/search.$$ 2>/dev/null else $FIND $IMAGE_DIR -name .cdtoc > /tmp/search.$$ 2>/dev/null fi sed "s:.cdtoc::g" /tmp/search.$$ > /tmp/images.$$ } get_osol_images() { echo "$PLATFORM" find /net/lepra.czech.sun.com/repo/export/tftp/$PLATFORM -type f > /tmp/images.$$ } filter_img_platf_osol() { cat /tmp/images.$$ | while read LINE do IMGPLATFORM=`head -1 $LINE` if [ "$IMGPLATFORM" = "sparc" ]; then echo $LINE >> /tmp/images_sparc.$$ else echo $LINE >> /tmp/images_x86.$$ fi done } filter_img_platf() { cat /tmp/images.$$ | while read LINE do VOLINF=`cat $LINE/.volume.inf | grep -i sparc` if [ "$VOLINF" = "" ]; then echo $LINE >> /tmp/images_x86.$$ else echo $LINE >> /tmp/images_sparc.$$ fi done } show_images() { case $PLATFORM in 1) IMAGES=`cat /tmp/images_x86.$$`;; 2) IMAGES=`cat /tmp/images_sparc.$$`;; esac N=1 for i in $IMAGES do echo "$N. $i" >> /tmp/images_menu.$$ N=`expr $N + 1` done cat /tmp/images_menu.$$ } cleanup() { rm /tmp/*.$$ 2>/dev/null cd $OLDDIR } get_sol_toolsdir() { DIR1=`ls $USEIMAGE | grep -i "solaris_[0-9]*"` TOOLSDIR="$USEIMAGE/$DIR1/Tools" if [ ! -d $TOOLSDIR ]; then echo "No directory $TOOLSDIR found." exit 2 fi } select_sol_image() { echo; echo "Select image for JumpStart setup:" show_images echo; printf "Choose image: " read SELECTION if [ "$SELECTION" = "" ]; then echo "Invalid selection." exit 1 fi USEIMAGE=`cat /tmp/images_menu.$$ | grep "^${SELECTION}[.] .*" | awk '{ print $2 }' ` echo; echo "Image $USEIMAGE selected." if [ "$LOGOS" != "OpenSolaris" ]; then get_sol_toolsdir else OSOLMACRO=`tail -1 $USEIMAGE` fi } select_sol_boot_loader() { echo; echo "Select network boot loader:" echo "1. PXE Grub (S10U1 and above)" echo "2. Solaris Network Boot (S10 and below)" printf "Choose network boot loader [1]: " read BLOADER case $BLOADER in 1) LOGBLOADER="grub"; sol_x86_grub;; 2) LOGBLOADER="nbp"; sol_x86_nbp;; *) BLOADER=1; LOGBLOADER="grub"; sol_x86_grub;; esac } netboot_sol_x86() { echo; echo "Do you want to configure network boot?" echo "1. Yes" echo "2. No" echo "if you're installing OpenSolaris, you must choose 1" printf "Choose option [1]: " read WANTNETBOOT case $WANTNETBOOT in 1) LOGNETBOOT="Yes"; select_sol_boot_loader;; 2) LOGNETBOOT="No"; sol_x86_nonet;; *) LOGNETBOOT="Yes"; select_sol_boot_loader;; esac } # parameter $1 ... macro name to delete delete_dhcp_macro() { /usr/sbin/dhtadm -P | awk '{ print $1 }' | grep $1 >/dev/null RET=$? case $RET in 0) /usr/sbin/dhtadm -D -m $1;; *) echo;echo "Macro $1 not yet defined.";; esac } # parameter $1 ... macro name to add add_dhcp_macro_sol_grub() { echo "Creating DHCP macro: $1" /usr/sbin/dhtadm -A -m $1 -d \ ":BootFile=\"$1\":SjumpsCF=\"$SJUMPSCF\":SsysidCF=\"$SJUMPSCF/$USEHOST\":" >/dev/null 2>&1 RET=$? case $RET in 0) /usr/bin/pkill -1 in.dhcpd;; *) echo "Error modifying DHCP table."; exit $RET;; esac } # parameter $1 ... macro name to add. $2 is macro to add add_dhcp_macro_osol_grub() { echo "Creating DHCP macro: $1" /usr/sbin/dhtadm -A -m $1 -d \ "$2" >/dev/null 2>&1 RET=$? case $RET in 0) /usr/bin/pkill -1 in.dhcpd;; *) echo "Error modifying DHCP table."; exit $RET;; esac } # parameter $1 ... macro name to add add_dhcp_macro_sol_nbp() { echo "Creating DHCP macro: $1" echo "..avoiding dhcp option overflow" # hack specific for S9x86 nbp - DHCP is limited for dhcp-request 43 (vendor-options packed) upto 255bytes # for now, we can try to remove symlinks as it might not break anything NBP_SJUMPSCF=`echo $SJUMPSCF|sed -e's/\/mirror//'` NBP_USEIMAGE=`echo $USEIMAGE|sed -e's/\/fast//'` NBP_TOOLSDIR=`echo $TOOLSDIR|sed -e's/\/fast//'` /usr/sbin/dhtadm -A -m $1 -d ":SinstNM=\"$SINSTNM\":SinstIP4=$SINSTIP4:SinstPTH=\"$NBP_USEIMAGE\":SrootNM=\"$SROOTNM\":SrootIP4=$SROOTIP4:SrootPTH=\"$NBP_TOOLSDIR/Boot\":BootFile=\"nbp.$1\":SbootURI=\"tftp://$SINSTIP4/$1\":SjumpsCF=\"$NBP_SJUMPSCF\":SsysidCF=\"$NBP_SJUMPSCF/$USEHOST\":" #>/dev/null 2>&1 /usr/bin/pkill -1 in.dhcpd RET=$? case $RET in 0) ;; *) echo "Error modifying DHCP table."; exit $RET;; esac } get_jump_options() { AWKTMP=/tmp/jump_awkmenu.$$ AWKTAR=$MENU_TMPFILE # init options to differ x86/sparc Solaris if [ $PLATFORM -eq 1 ]; then # Solaris x86 AWKPAT='kernel\/unix install' AWKDEL=',' else # Solaris sparc AWKPAT="^$USEHOST " AWKDEL=' ' fi # read options from file grep -v '^#' $PROFILEDIR/$USEHOST/bootenv.rc | grep -v '^[ ]*$' | while read LINE do printf "%s%s" "$AWKDEL" "$LINE" >> $AWKTMP done AWKOPT=`cat $AWKTMP` if [ "X$AWKOPT" = "X" ]; then echo "No JumpStart options found or accessible."; exit 7 fi # run awk to update files AWKCMD="/$AWKPAT/ {print \$0\"$AWKOPT\"; next}; {print}" cp $AWKTAR $AWKTAR.orig.$$ awk "$AWKCMD" $AWKTAR.orig.$$ > $AWKTAR case $RET in 0) ;; *) echo "Error!"; exit $RET;; esac } solaris() { echo; echo "========== S O L A R I S ==========" echo; echo "Searching for available images ..." get_sol_images filter_img_platf get_platform } opensolaris() { echo; echo "========== O P E N S O L A R I S ==========" echo; echo "Searching for available images ..." get_osol_images filter_img_platf_osol get_platform } sol_x86() { echo; echo "========== S O L A R I S x 8 6 ==========" select_sol_image select_host netboot_sol_x86 } sol_x86_grub() { get_host_mac get_host_ip get_client_id if [ "$LOGOS" != "OpenSolaris" ]; then cd $TOOLSDIR # run add_install_client. It will modify /etc/bootparams, create boot files under /tftpboot # and create a menu file for grub in /tftpboot/menu.lst. ./add_install_client -d -e $HOSTMAC -c ${SJUMPSCF} \ -p ${SJUMPSCF}/$USEHOST i86pc #>/dev/null 2>&1 if [ $? -ne 0 ]; then printf "\nError encoutered! The host '$USEHOST' has not been set up for JumpStart.\n\n" exit 6 fi # modify DHCP table delete_dhcp_macro $CLIENTID add_dhcp_macro_sol_grub $CLIENTID # 1. modify the menu file to automatically perform the JumpStart # by including "install" as a kernel parameter # 2. create second menu entry for manual install MENUFILE="/tftpboot/menu.lst.$CLIENTID" HUMANMENUFILE="/tftpboot/menu.lst.$USEHOST" MENU_TMPFILE="/tmp/menu.lst.$$" if [ -f $MENU_TMPFILE ]; then /bin/rm $MENU_TMPFILE fi TITLE=`cat $MENUFILE | grep "[ \t]*title "` # KERNEL=`cat $MENUFILE | grep "[ \t]*kernel "` # MODULE=`cat $MENUFILE | grep "[ \t]*module "` KERNEL=`cat $MENUFILE | grep "[ \t]*kernel[ $]"` MODULE=`cat $MENUFILE | grep "[ \t]*module[ $]"` TITLE_NEW=`echo $TITLE | sed -e 's/\(.*\)Jumpstart/\1Manual Install/'` sed -e 's/kernel\/unix/kernel\/unix install/' -e 's/default=0/default=2/' $MENUFILE > $MENU_TMPFILE printf "\n$TITLE_NEW\n$KERNEL\n$MODULE\n" >> $MENU_TMPFILE # add jumpstart options via PXE bootpath if [ -f $PROFILEDIR/$USEHOST/bootenv.rc ]; then get_jump_options case $RET in 0) echo "Done!";; *) echo "Error in jump_opt_x86!"; exit $RET;; esac fi # add SunGhost entry: USEETH=`cat $KICKDIR/ks_ethlist | grep -i $USEHOST | awk '{ print $2; }'` if [ "x$USEETH" = "x" ]; then USEETH="eth0" fi cat <> $MENU_TMPFILE title HDD First partition rootnoverify (hd0) chainloader +1 title SunGhost (DHCP) root (nd) kernel (nd)/sg.img ramdisk_size=80000 sg_net=eth0:dhcp sg_net=eth1:dhcp sg_net=eth2:dhcp sg_net=eth3:dhcp quiet initrd (nd)/sg.gz EOF # ------------------ mv $MENU_TMPFILE $MENUFILE if [ ! -L $HUMANMENUFILE ]; then /bin/rm -f $HUMANMENUFILE ln -s $MENUFILE $HUMANMENUFILE > /dev/null 2>&1 fi chmod 666 $MENUFILE else # modify DHCP table delete_dhcp_macro $CLIENTID add_dhcp_macro_osol_grub $CLIENTID $OSOLMACRO fi printf "\nDone! Please, reboot the machine $USEHOST and boot it using PXE.\n" printf "(Press F8 or F12 after reboot and select MBA UNDI as the boot device.)\n\n" } sol_x86_nbp() { get_host_mac get_host_ip get_client_id cd $TOOLSDIR ./add_install_client -d -e $HOSTMAC -c ${SJUMPSCF} \ -p ${SJUMPSCF}/$USEHOST i86pc > /tmp/add_install_client_out.$$ # -b "input-device=keyboard" \ # -b "output-device=screen" i86pc #>/dev/null # parse BootFile & BootURI from the output of add_install_client script BOOTID=`cat /tmp/add_install_client_out.$$ | grep "\(BootFile\)" | awk -F: '{ print $2 }' | head -1 | sed 's/nbp[.]//'` # modify DHCP table delete_dhcp_macro $CLIENTID delete_dhcp_macro $BOOTID #add_dhcp_macro_sol_nbp $CLIENTID add_dhcp_macro_sol_nbp $BOOTID printf "\nDone! Please, reboot the machine $USEHOST and boot it using PXE.\n" printf "(Press F8 or F12 after reboot and select MBA UNDI as the boot device.)\n\n" } sol_x86_nonet() { get_host_mac get_host_ip cd $TOOLSDIR ./add_install_client -c ${SJUMPSCF} -p ${SJUMPSCF}/$USEHOST $USEHOST i86pc >/dev/null printf "\nDone! Please, reboot the machine $USEHOST and boot it using CD/DVD media.\n" printf "Choose Network Install - Jumpstart as the installation method.\n\n" } sol_sparc() { echo; echo "========== S O L A R I S S P A R C ==========" select_sol_image select_host if [ "$LOGOS" != "OpenSolaris" ]; then cd $TOOLSDIR ./add_install_client -c ${SJUMPSCF} -p ${SJUMPSCF}/$USEHOST $USEHOST sun4u RET=$? case $RET in 0) echo "Done!";; *) echo "Error!"; exit $RET;; esac # add jumpstart options via bootparams if [ -f $PROFILEDIR/$USEHOST/bootenv.rc ]; then MENU_TMPFILE=/tmp/jump_bootparams.$$ cp /etc/bootparams $MENU_TMPFILE get_jump_options case $RET in 0) ;; *) echo "Error in jump_opt_sparc!"; exit $RET;; esac mv /etc/bootparams /etc/bootparams.orig mv $MENU_TMPFILE /etc/bootparams fi else get_host_mac get_host_ip get_client_id # modify DHCP table delete_dhcp_macro $CLIENTID add_dhcp_macro_osol_grub $CLIENTID $OSOLMACRO printf "\nDone! Please, reboot the machine $USEHOST with command \"reboot net:dhcp\"\n" printf "(or press \"Stop + a\" on the machine $USEHOST and enter \"boot net:dhcp\")\n\n" fi } show_kickstart() { KSFILES=`ls -1A $KICKDIR/*ks_${USEHOST}* 2>/dev/null` RET=$? case $RET in 0) ;; *) echo "No config found."; exit $RET;; esac N=1 for i in $KSFILES do echo "$N. $i" >> /tmp/kickstart_menu.$$ N=`expr $N + 1` done cat /tmp/kickstart_menu.$$ } select_kickstart() { echo; echo "Select kickstart configuration:" show_kickstart echo; printf "Choose config: " read SELECTION if [ "$SELECTION" = "" ]; then echo "Invalid selection." exit 1 fi USECFG=`cat /tmp/kickstart_menu.$$ | grep "^${SELECTION}[.] .*" | awk '{ print $2 }' ` echo; echo "Config $USECFG selected." case $USECFG in *rh30_x86*) USEIMG=rh3x86;USETITLE='Redhat 3.0 x86';; *rh30u6_x86*) USEIMG=rh3u6x86;USETITLE='Redhat 3.0 ur6 x86';; *rh40_x86*) USEIMG=rh4x86;USETITLE='Redhat 4.0 x86';; *rh40_amd64*) USEIMG=rh4amd64;USETITLE='Redhat 4.0 amd64';; *rh4u6_x64*) USEIMG=rh4u6_x64;USETITLE='Redhat 4.0u6 amd64';; *rh40u3_x86*) USEIMG=rh4u3x86;USETITLE='Redhat 4.0 ur3 x86';; *rh40u4_x86*) USEIMG=rh4u4x86;USETITLE='Redhat 4.0 ur4 x86';; *rh50_x86*) USEIMG=rh5x86;USETITLE='Redhat 5.0 x86';; *rh52_x86*) USEIMG=rh52x86;USETITLE='Redhat 5.2 x86';; *rh52_x64*) USEIMG=rh52x64;USETITLE='Redhat 5.2 x64';; esac } redhat() { echo;echo "========== R E D H A T L I N U X ==========" select_host select_kickstart get_host_mac get_host_ip get_client_id #modify DHCP table delete_dhcp_macro $CLIENTID add_dhcp_macro_sol_grub $CLIENTID MENUFILE="/tftpboot/menu.lst.$CLIENTID" HUMANMENUFILE="/tftpboot/menu.lst.$USEHOST" MENU_TMPFILE="/tmp/menu.lst.$$" if [ -f $MENU_TMPFILE ]; then /bin/rm $MENU_TMPFILE fi USEETH=`cat $KICKDIR/ks_ethlist | grep -i $USEHOST | awk '{ print $2; }'` if [ "x$USEETH" = "x" ]; then USEETH="eth0" fi cat << RHEOF > $MENU_TMPFILE default=2 timeout=30 title HDD First partition rootnoverify (hd0) chainloader +1 title SunGhost (DHCP) root (nd) kernel (nd)/sg.img ramdisk_size=80000 sg_net=eth0:dhcp sg_net=eth1:dhcp sg_net=eth2:dhcp sg_net=eth3:dhcp quiet initrd (nd)/sg.gz title $USETITLE root (nd) kernel (nd)/ks_vmlinuz_$USEIMG ks=nfs:10.16.22.10:$USECFG ksdevice=${USEETH} initrd (nd)/ks_initrd_$USEIMG RHEOF #--------------------------------------- mv $MENU_TMPFILE $MENUFILE if [ ! -L $HUMANMENUFILE ]; then /bin/rm -f $HUMANMENUFILE ln -s $MENUFILE $HUMANMENUFILE > /dev/null 2>&1 fi chmod 666 $MENUFILE printf "\nDone! Please, reboot the machine $USEHOST and boot it using PXE.\n" printf "(Press F8 or F12 after reboot and select MBA UNDI as the boot device.)\n\n" } # Delete exiting macro in 10 minutes delete_macro_after_timeout() { if [ $PLATFORM = 1 ]; then #platform is x86 echo "Do you wish to delete created macro in 5 minutes? [yes]" echo " Yes - You have only 5 minutes to jumpstart selected PC" echo " No - You can jumpstart the PC without any time restriction" read ANSWER ANSWER=`echo $ANSWER | tr [:upper:] [:lower:]` case $ANSWER in no) exit 10 ;; *) echo "Created macro will be deleted in 5 minutes" echo "/usr/sbin/dhtadm -D -m $CLIENTID && /usr/bin/pkill -1 in.dhcpd" >/tmp/tmpfile.$$ echo "rm /tmp/tmpfile.$$" >>/tmp/tmpfile.$$ at -f /tmp/tmpfile.$$ now + 5 minutes 2>/dev/null ;; esac fi } # ==================== MAIN ==================== welcome check_find get_os cleanup who am i | sed 's/^\([^ ]*\ \).*/\1/' > /export/jes/scripts/machines/$USEHOST date +%b\%e\ %T >> /export/jes/scripts/machines/$USEHOST echo "$USEIMAGE$USECFG" >> /export/jes/scripts/machines/$USEHOST echo "$LOGPLATFORM" >> /export/jes/scripts/machines/$USEHOST echo "$LOGOS" >> /export/jes/scripts/machines/$USEHOST echo "$HOSTIP" >> /export/jes/scripts/machines/$USEHOST echo "$LOGNETBOOT" >> /export/jes/scripts/machines/$USEHOST echo "$LOGBLOADER" >> /export/jes/scripts/machines/$USEHOST chmod 0666 /export/jes/scripts/machines/$USEHOST delete_macro_after_timeout