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