xref: /illumos-gate/usr/src/cmd/boot/scripts/update_grub.ksh (revision 942c5e3c2dd127463517e5cc1694ee94ca45e021)
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 2007 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
46
47check_pcfs_boot()
48{
49	bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs \
50	    | grep "[       ]/stubboot[      ]" | nawk '{print $1}'`
51	if [ X"$bootdev" = "X" ]; then
52		is_pcfs_boot=no
53	fi
54}
55
56#
57# Detect SVM root and return the list of raw devices under the mirror
58#
59get_rootdev_list()
60{
61	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
62		grep '^PHYS_SLICE' "$ALT_ROOT"/etc/lu/GRUB_slice | cut -d= -f2
63	else
64		metadev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | \
65		    grep "[	 ]/[ 	]" | nawk '{print $2}'`
66		if [[ $metadev = /dev/rdsk/* ]]; then
67			rootdevlist=`echo "$metadev" | sed -e "s#/dev/rdsk/##"`
68		elif [[ $metadev = /dev/md/rdsk/* ]]; then
69			metavol=`echo "$metadev" | sed -e "s#/dev/md/rdsk/##"`
70			rootdevlist=`metastat -p $metavol |\
71			    grep -v "^$metavol[	 ]" | nawk '{print $4}'`
72		fi
73		for rootdev in $rootdevlist
74		do
75			echo /dev/rdsk/$rootdev
76		done
77	fi
78}
79
80#
81# multiboot: install grub on the boot slice
82#
83install_grub()
84{
85	# Stage 2 blocks must remain untouched
86	STAGE1="$ALT_ROOT"/boot/grub/stage1
87	STAGE2="$ALT_ROOT"/boot/grub/stage2
88
89	if [ $is_pcfs_boot = yes ]; then
90		#
91		# Note: /stubboot/boot/grub/stage2 must stay untouched.
92		#
93		mkdir -p "$ALT_ROOT"/stubboot/boot/grub
94		cp "$ALT_ROOT"/boot/grub/menu.lst "$ALT_ROOT"/stubboot/boot/grub
95		bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs | \
96			grep "[	 ]/stubboot[ 	]" | nawk '{print $1}'`
97		rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`
98		if [ X"$rpcfsdev" != X ]; then
99			print "Installing grub on $rpcfsdev"
100			"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rpcfsdev
101		fi
102	fi
103
104	get_rootdev_list | while read rootdev
105	do
106		if [ X"$rpcfsdev" != X ]; then
107			echo "create GRUB menu in "$ALT_ROOT"/stubboot"
108			"$ALT_ROOT"/sbin/bootadm update-menu \
109			    -R "$ALT_ROOT"/stubboot -o $rootdev,"$ALT_ROOT"
110		else
111			echo "Creating GRUB menu in ${ALT_ROOT:-/}"
112			$ALT_ROOT/sbin/bootadm update-menu -R ${ALT_ROOT:-/} \
113			    -o $rootdev
114		fi
115		print "Installing grub on $rootdev"
116		"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rootdev
117	done
118}
119
120if [ -f "$ALT_ROOT"/platform/i86pc/multiboot -a "$ARCH" = i386 ] ; then
121	check_pcfs_boot
122	install_grub
123fi
124
125exit 0
126