xref: /onnv-gate/usr/src/cmd/boot/scripts/update_grub.ksh (revision 8176:9c0201f1c3b6)
13446Smrj#!/bin/ksh -p
23446Smrj#
33446Smrj# CDDL HEADER START
43446Smrj#
53446Smrj# The contents of this file are subject to the terms of the
63446Smrj# Common Development and Distribution License (the "License").
73446Smrj# You may not use this file except in compliance with the License.
83446Smrj#
93446Smrj# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
103446Smrj# or http://www.opensolaris.org/os/licensing.
113446Smrj# See the License for the specific language governing permissions
123446Smrj# and limitations under the License.
133446Smrj#
143446Smrj# When distributing Covered Code, include this CDDL HEADER in each
153446Smrj# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
163446Smrj# If applicable, add the following below this CDDL HEADER, with the
173446Smrj# fields enclosed by brackets "[]" replaced with your own identifying
183446Smrj# information: Portions Copyright [yyyy] [name of copyright owner]
193446Smrj#
203446Smrj# CDDL HEADER END
213446Smrj#
223446Smrj
233446Smrj#
246423Sgw25295# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
253446Smrj# Use is subject to license terms.
263446Smrj#
273446Smrj
283446SmrjPATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
293446SmrjALT_ROOT=
303446Smrj
313446Smrjwhile getopts R: OPT 2>/dev/null
323446Smrjdo
333446Smrj	case $OPT in
343446Smrj	R)	ALT_ROOT="$OPTARG"
353446Smrj		;;
363446Smrj	?)	echo "Usage: ${0##*/}: [-R \<root\>]"
373446Smrj		;;
383446Smrj	esac
393446Smrjdone
403446Smrj
413446SmrjARCH=`uname -p`
423446Smrj
433446Smrjis_pcfs_boot=yes
446423Sgw25295is_zfs_boot=no
453446Smrj
463446Smrjcheck_pcfs_boot()
473446Smrj{
483446Smrj	bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs \
493446Smrj	    | grep "[       ]/stubboot[      ]" | nawk '{print $1}'`
503446Smrj	if [ X"$bootdev" = "X" ]; then
513446Smrj		is_pcfs_boot=no
523446Smrj	fi
533446Smrj}
543446Smrj
556423Sgw25295check_zfs_boot()
566423Sgw25295{
576423Sgw25295	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
586423Sgw25295		dev=`grep '^PHYS_SLICE=' "$ALT_ROOT"/etc/lu/GRUB_slice |
596423Sgw25295		    cut -d= -f2`
606423Sgw25295		if [ "`fstyp $dev`" = "zfs" ]; then
616423Sgw25295			is_zfs_boot=yes
626423Sgw25295		fi
636423Sgw25295	else
646423Sgw25295		rootfstype=`df -n ${ALT_ROOT:-/} | awk '{print $3}'`
656423Sgw25295		if [ "$rootfstype" = "zfs" ]; then
666423Sgw25295			is_zfs_boot=yes
676423Sgw25295		fi
686423Sgw25295
696423Sgw25295	fi
706423Sgw25295}
716423Sgw25295
723446Smrj#
733446Smrj# Detect SVM root and return the list of raw devices under the mirror
743446Smrj#
753446Smrjget_rootdev_list()
763446Smrj{
773446Smrj	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
786423Sgw25295		dev=`grep '^PHYS_SLICE' "$ALT_ROOT"/etc/lu/GRUB_slice |
796423Sgw25295		    cut -d= -f2`
806423Sgw25295		if [ "$is_zfs_boot" = "yes" ]; then
816423Sgw25295			fstyp -a "$dev" | grep 'path: ' | grep -v phys_path: |
826423Sgw25295			    cut -d"'" -f2 | sed 's+/dsk/+/rdsk/+'
836423Sgw25295		else
846423Sgw25295			echo "$dev"
856423Sgw25295		fi
866423Sgw25295		return
876423Sgw25295	elif [ "$is_zfs_boot" = "yes" ]; then
886423Sgw25295		rootpool=`df -k ${ALT_ROOT:-/} | tail +2 | cut -d/ -f1`
897965SGeorge.Wilson@Sun.COM		rootdevlist=`LC_ALL=C zpool iostat -v "$rootpool" | tail +5 |
907965SGeorge.Wilson@Sun.COM		    egrep -v "mirror|spare|replacing" |
917965SGeorge.Wilson@Sun.COM		    sed -n -e '/--/q' -e p | awk '{print $1}'`
923446Smrj	else
933446Smrj		metadev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | \
943446Smrj		    grep "[	 ]/[ 	]" | nawk '{print $2}'`
953446Smrj		if [[ $metadev = /dev/rdsk/* ]]; then
96*8176SJerry.Gilliam@Sun.COM			rootdevlist=`basename "$metadev"`
973446Smrj		elif [[ $metadev = /dev/md/rdsk/* ]]; then
98*8176SJerry.Gilliam@Sun.COM			metavol=`basename "$metadev"`
993446Smrj			rootdevlist=`metastat -p $metavol |\
1003446Smrj			    grep -v "^$metavol[	 ]" | nawk '{print $4}'`
1013446Smrj		fi
1023446Smrj	fi
1036423Sgw25295	for rootdev in $rootdevlist
1046423Sgw25295	do
105*8176SJerry.Gilliam@Sun.COM		echo /dev/rdsk/`basename $rootdev`
1066423Sgw25295	done
1073446Smrj}
1083446Smrj
1093446Smrj#
1103446Smrj# multiboot: install grub on the boot slice
1113446Smrj#
1123446Smrjinstall_grub()
1133446Smrj{
1143446Smrj	# Stage 2 blocks must remain untouched
1153446Smrj	STAGE1="$ALT_ROOT"/boot/grub/stage1
1163446Smrj	STAGE2="$ALT_ROOT"/boot/grub/stage2
1173446Smrj
1183446Smrj	if [ $is_pcfs_boot = yes ]; then
1193446Smrj		#
1203446Smrj		# Note: /stubboot/boot/grub/stage2 must stay untouched.
1213446Smrj		#
1223446Smrj		mkdir -p "$ALT_ROOT"/stubboot/boot/grub
1233446Smrj		cp "$ALT_ROOT"/boot/grub/menu.lst "$ALT_ROOT"/stubboot/boot/grub
1243446Smrj		bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs | \
1253446Smrj			grep "[	 ]/stubboot[ 	]" | nawk '{print $1}'`
1263446Smrj		rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`
1273446Smrj		if [ X"$rpcfsdev" != X ]; then
1283446Smrj			print "Installing grub on $rpcfsdev"
1293446Smrj			"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rpcfsdev
1303446Smrj		fi
1313446Smrj	fi
1323446Smrj
1336423Sgw25295	grubdevlist=`get_rootdev_list`
1346423Sgw25295	zfsarg=""
1356423Sgw25295	if [ "$is_zfs_boot" = "yes" ]; then
1366423Sgw25295		zfsarg="-Z"
1376423Sgw25295	fi
1386423Sgw25295
1396423Sgw25295	for rootdev in $grubdevlist
1403446Smrj	do
1413446Smrj		if [ X"$rpcfsdev" != X ]; then
1423446Smrj			echo "create GRUB menu in "$ALT_ROOT"/stubboot"
1436423Sgw25295			"$ALT_ROOT"/sbin/bootadm update-menu $zfsarg\
1443446Smrj			    -R "$ALT_ROOT"/stubboot -o $rootdev,"$ALT_ROOT"
1453446Smrj		else
1463446Smrj			echo "Creating GRUB menu in ${ALT_ROOT:-/}"
1473446Smrj			$ALT_ROOT/sbin/bootadm update-menu -R ${ALT_ROOT:-/} \
1486423Sgw25295			    $zfsarg -o $rootdev
1493446Smrj		fi
1503446Smrj		print "Installing grub on $rootdev"
1513446Smrj		"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rootdev
1523446Smrj	done
1533446Smrj}
1543446Smrj
1553446Smrjif [ -f "$ALT_ROOT"/platform/i86pc/multiboot -a "$ARCH" = i386 ] ; then
1563446Smrj	check_pcfs_boot
1576423Sgw25295	check_zfs_boot
1583446Smrj	install_grub
1593446Smrjfi
1603446Smrj
1613446Smrjexit 0
162