xref: /titanic_51/usr/src/cmd/boot/scripts/update_grub.ksh (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28# ident	"%Z%%M%	%I%	%E% SMI"
29
30PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
31ALT_ROOT=
32
33while getopts R: OPT 2>/dev/null
34do
35	case $OPT in
36	R)	ALT_ROOT="$OPTARG"
37		;;
38	?)	echo "Usage: ${0##*/}: [-R \<root\>]"
39		;;
40	esac
41done
42
43ARCH=`uname -p`
44
45is_pcfs_boot=yes
46is_zfs_boot=no
47
48check_pcfs_boot()
49{
50	bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs \
51	    | grep "[       ]/stubboot[      ]" | nawk '{print $1}'`
52	if [ X"$bootdev" = "X" ]; then
53		is_pcfs_boot=no
54	fi
55}
56
57check_zfs_boot()
58{
59	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
60		dev=`grep '^PHYS_SLICE=' "$ALT_ROOT"/etc/lu/GRUB_slice |
61		    cut -d= -f2`
62		if [ "`fstyp $dev`" = "zfs" ]; then
63			is_zfs_boot=yes
64		fi
65	else
66		rootfstype=`df -n ${ALT_ROOT:-/} | awk '{print $3}'`
67		if [ "$rootfstype" = "zfs" ]; then
68			is_zfs_boot=yes
69		fi
70
71	fi
72}
73
74#
75# Detect SVM root and return the list of raw devices under the mirror
76#
77get_rootdev_list()
78{
79	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
80		dev=`grep '^PHYS_SLICE' "$ALT_ROOT"/etc/lu/GRUB_slice |
81		    cut -d= -f2`
82		if [ "$is_zfs_boot" = "yes" ]; then
83			fstyp -a "$dev" | grep 'path: ' | grep -v phys_path: |
84			    cut -d"'" -f2 | sed 's+/dsk/+/rdsk/+'
85		else
86			echo "$dev"
87		fi
88		return
89	elif [ "$is_zfs_boot" = "yes" ]; then
90		rootpool=`df -k ${ALT_ROOT:-/} | tail +2 | cut -d/ -f1`
91		rootdevlist=`zpool iostat -v "$rootpool" | tail +5 |
92		    grep -v mirror | sed -n -e '/--/q' -e p | awk '{print $1}'`
93	else
94		metadev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | \
95		    grep "[	 ]/[ 	]" | nawk '{print $2}'`
96		if [[ $metadev = /dev/rdsk/* ]]; then
97			rootdevlist=`echo "$metadev" | sed -e "s#/dev/rdsk/##"`
98		elif [[ $metadev = /dev/md/rdsk/* ]]; then
99			metavol=`echo "$metadev" | sed -e "s#/dev/md/rdsk/##"`
100			rootdevlist=`metastat -p $metavol |\
101			    grep -v "^$metavol[	 ]" | nawk '{print $4}'`
102		fi
103	fi
104	for rootdev in $rootdevlist
105	do
106		echo /dev/rdsk/$rootdev
107	done
108}
109
110#
111# multiboot: install grub on the boot slice
112#
113install_grub()
114{
115	# Stage 2 blocks must remain untouched
116	STAGE1="$ALT_ROOT"/boot/grub/stage1
117	STAGE2="$ALT_ROOT"/boot/grub/stage2
118
119	if [ $is_pcfs_boot = yes ]; then
120		#
121		# Note: /stubboot/boot/grub/stage2 must stay untouched.
122		#
123		mkdir -p "$ALT_ROOT"/stubboot/boot/grub
124		cp "$ALT_ROOT"/boot/grub/menu.lst "$ALT_ROOT"/stubboot/boot/grub
125		bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs | \
126			grep "[	 ]/stubboot[ 	]" | nawk '{print $1}'`
127		rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`
128		if [ X"$rpcfsdev" != X ]; then
129			print "Installing grub on $rpcfsdev"
130			"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rpcfsdev
131		fi
132	fi
133
134	grubdevlist=`get_rootdev_list`
135	zfsarg=""
136	if [ "$is_zfs_boot" = "yes" ]; then
137		zfsarg="-Z"
138	fi
139
140	for rootdev in $grubdevlist
141	do
142		if [ X"$rpcfsdev" != X ]; then
143			echo "create GRUB menu in "$ALT_ROOT"/stubboot"
144			"$ALT_ROOT"/sbin/bootadm update-menu $zfsarg\
145			    -R "$ALT_ROOT"/stubboot -o $rootdev,"$ALT_ROOT"
146		else
147			echo "Creating GRUB menu in ${ALT_ROOT:-/}"
148			$ALT_ROOT/sbin/bootadm update-menu -R ${ALT_ROOT:-/} \
149			    $zfsarg -o $rootdev
150		fi
151		print "Installing grub on $rootdev"
152		"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rootdev
153	done
154}
155
156if [ -f "$ALT_ROOT"/platform/i86pc/multiboot -a "$ARCH" = i386 ] ; then
157	check_pcfs_boot
158	check_zfs_boot
159	install_grub
160fi
161
162exit 0
163