Home | History | Annotate | Line # | Download | only in binstall
binstall.sh revision 1.16
      1 #!/bin/sh
      2 #	$NetBSD: binstall.sh,v 1.16 2018/09/16 14:23:04 kre Exp $
      3 #
      4 
      5 vecho () {
      6 # echo if VERBOSE on
      7 	if [ "$VERBOSE" = "1" ]; then
      8 		echo "$@" 1>&2
      9 	fi
     10 	return 0
     11 }
     12 
     13 Options () {
     14 	echo "Options:"
     15 	echo "	-h		- display this message"
     16 	echo "	-u		- install sparc64 (UltraSPARC) boot block"
     17 	echo "	-U		- install sparc boot block"
     18 	echo "	-b<bootprog>	- second-stage boot program to install"
     19 	echo "	-f<pathname>	- path to device/file image for filesystem"
     20 	echo "	-m<path>	- Look for boot programs in <path> (default: /usr/mdec)"
     21 	echo "	-i<progname>	- Use the installboot program at <progname>"
     22 	echo "			  (default: /usr/sbin/installboot)"
     23 	echo "	-v		- verbose mode"
     24 	echo "	-t		- test mode (implies -v)"
     25 }
     26 
     27 Usage () {
     28 	echo "Usage: $0 [options] <"'"net"|"ffs"'"> <directory>"
     29 	Options
     30 	exit 1
     31 }
     32 
     33 Help () {
     34 	echo "This script copies the boot programs to one of several"
     35 	echo "commonly used places."
     36 	echo "When installing an \"ffs\" boot program, this script also runs"
     37 	echo "installboot(8) which installs the default proto bootblocks into"
     38 	echo "the appropriate filesystem partition or filesystem image."
     39 	Options
     40 	exit 0
     41 }
     42 
     43 Secure () {
     44 	echo "This script has to be run when the kernel is in 'insecure' mode,"
     45 	echo "or when applying bootblocks to a file image (ala vnd)."
     46 	echo "The best way is to run this script in single-user mode."
     47 	exit 1
     48 }
     49 
     50 PATH=/bin:/usr/bin:/sbin:/usr/sbin
     51 : ${MDEC:=/usr/mdec}
     52 : ${INSTALLBOOT:=/usr/sbin/installboot}
     53 : ${BOOTPROG:=boot}
     54 : ${OFWBOOTBLK:=ofwboot}
     55 if [ "`sysctl -n machdep.cpu_arch`" = 9 ]; then
     56 	ULTRASPARC=1
     57 else
     58 	ULTRASPARC=0
     59 fi
     60 
     61 set -- `getopt "b:hf:i:m:tUuv" "$@"`
     62 if [ $? -gt 0 ]; then
     63 	Usage
     64 fi
     65 
     66 for a in $*
     67 do
     68 	case $1 in
     69 	-h) Help; shift ;;
     70 	-u) ULTRASPARC=1; shift ;;
     71 	-U) ULTRASPARC=0; shift ;;
     72 	-b) BOOTPROG=$2; OFWBOOTBLK=$2; shift 2 ;;
     73 	-f) DEV=$2; shift 2 ;;
     74 	-m) MDEC=$2; shift 2 ;;
     75 	-i) INSTALLBOOT=$2; shift 2 ;;
     76 	-t) TEST=1; VERBOSE=1; shift ;;
     77 	-v) VERBOSE=1; shift ;;
     78 	--) shift; break ;;
     79 	esac
     80 done
     81 
     82 if [ "`sysctl -n kern.securelevel`" -gt 0 ] && [ ! -f "$DEV" ]; then
     83 	Secure
     84 fi
     85 
     86 DOIT=${TEST:+echo "=>"}
     87 
     88 if [ $# != 2 ]; then
     89 	Usage
     90 fi
     91 
     92 WHAT=$1
     93 DEST=$2
     94 
     95 if [ ! -d $DEST ]; then
     96 	echo "$DEST: not a directory"
     97 	Usage
     98 fi
     99 
    100 if [ "$ULTRASPARC" = "1" ]; then
    101 	machine=sparc64
    102 	targ=ofwboot
    103 	stage2=""
    104 	netboot=ofwboot
    105 	BOOTPROG=$OFWBOOTBLK
    106 	BOOTXX=${MDEC}/bootblk
    107 else
    108 	machine=sparc
    109 	targ=boot
    110 	stage2=${targ}
    111 	netboot=boot.net
    112 	BOOTXX=${MDEC}/bootxx
    113 fi
    114 
    115 case $WHAT in
    116 "ffs")
    117 	if [ "$DEV" = "" ]; then
    118 		# Lookup device mounted on DEST
    119 		DEV=`mount | while read line; do
    120 			set -- $line
    121 			vecho "Inspecting \"$line\""
    122 			if [ "$2" = "on" ] && [ "$3" = "$DEST" ]; then
    123 				if [ ! -b $1 ]; then
    124 					continue
    125 				fi
    126 				RAW=\`echo -n "$1" | sed -e 's;/dev/;/dev/r;'\`
    127 				if [ ! -c \$RAW ]; then
    128 					continue
    129 				fi
    130 				echo -n $RAW
    131 				break;
    132 			fi
    133 		done`
    134 		if [ "$DEV" = "" ]; then
    135 			echo "Cannot find \"$DEST\" in mount table"
    136 			exit 1
    137 		fi
    138 	fi
    139 
    140 	vecho Boot device: $DEV
    141 	vecho Primary boot program: $BOOTXX
    142 	vecho Secondary boot program: $DEST/$targ
    143 
    144 	$DOIT cp -p -f ${MDEC}/${BOOTPROG} $DEST/$targ
    145 	sync; sync; sync
    146 	vecho ${INSTALLBOOT} ${VERBOSE:+-v} -m $machine $DEV ${BOOTXX} $stage2
    147 	$DOIT ${INSTALLBOOT} ${VERBOSE:+-v} -m $machine $DEV ${BOOTXX} $stage2
    148 	;;
    149 
    150 "net")
    151 	vecho Network boot program: $DEST/$boot.${machine}.netbsd
    152 	$DOIT cp -p -f ${MDEC}/$netboot $DEST/$boot.${machine}.netbsd
    153 	;;
    154 
    155 *)
    156 	echo "$WHAT: not recognised"
    157 	exit 1
    158 	;;
    159 esac
    160 
    161 exit $?
    162