xref: /illumos-gate/usr/src/cmd/boot/scripts/update_grub.ksh (revision 2aeafac3612e19716bf8164f89c3c9196342979c)
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# Copyright 2016 Nexenta Systems, Inc.
27#
28
29PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
30ALT_ROOT=
31
32while getopts R: OPT 2>/dev/null
33do
34	case $OPT in
35	R)	ALT_ROOT="$OPTARG"
36		;;
37	?)	echo "Usage: ${0##*/}: [-R \<root\>]"
38		;;
39	esac
40done
41
42ARCH=`uname -p`
43
44is_pcfs_boot=yes
45is_zfs_boot=no
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
56check_zfs_boot()
57{
58	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
59		dev=`grep '^PHYS_SLICE=' "$ALT_ROOT"/etc/lu/GRUB_slice |
60		    cut -d= -f2`
61		if [ "`fstyp $dev`" = "zfs" ]; then
62			is_zfs_boot=yes
63		fi
64	else
65		rootfstype=`df -n ${ALT_ROOT:-/} | awk '{print $3}'`
66		if [ "$rootfstype" = "zfs" ]; then
67			is_zfs_boot=yes
68		fi
69
70	fi
71}
72
73#
74# Return the list of raw devices
75#
76get_rootdev_list()
77{
78	if [ -f "$ALT_ROOT"/etc/lu/GRUB_slice ]; then
79		dev=`grep '^PHYS_SLICE' "$ALT_ROOT"/etc/lu/GRUB_slice |
80		    cut -d= -f2`
81		if [ "$is_zfs_boot" = "yes" ]; then
82			fstyp -a "$dev" | grep 'path: ' | grep -v phys_path: |
83			    cut -d"'" -f2 | sed 's+/dsk/+/rdsk/+'
84		else
85			echo "$dev"
86		fi
87		return
88	elif [ "$is_zfs_boot" = "yes" ]; then
89		rootpool=`df -k ${ALT_ROOT:-/} | tail +2 | cut -d/ -f1`
90		rootdevlist=`LC_ALL=C zpool iostat -v "$rootpool" | tail +5 |
91		    egrep -v "mirror|spare|replacing" |
92		    sed -n -e '/--/q' -e p | awk '{print $1}'`
93	else
94		dev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | \
95		    grep "[      ]/[    ]" | nawk '{print $2}'`
96		if [[ $dev = /dev/rdsk/* ]]; then
97			rootdevlist=`basename "$dev"`
98		fi
99	fi
100	for rootdev in $rootdevlist
101	do
102		echo /dev/rdsk/`basename $rootdev`
103	done
104}
105
106#
107# multiboot: install grub on the boot slice
108#
109install_grub()
110{
111	# Stage 2 blocks must remain untouched
112	STAGE1="$ALT_ROOT"/boot/grub/stage1
113	STAGE2="$ALT_ROOT"/boot/grub/stage2
114
115	if [ $is_pcfs_boot = yes ]; then
116		#
117		# Note: /stubboot/boot/grub/stage2 must stay untouched.
118		#
119		mkdir -p "$ALT_ROOT"/stubboot/boot/grub
120		cp "$ALT_ROOT"/boot/grub/menu.lst "$ALT_ROOT"/stubboot/boot/grub
121		bootdev=`grep -v "^#" "$ALT_ROOT"/etc/vfstab | grep pcfs | \
122			grep "[	 ]/stubboot[ 	]" | nawk '{print $1}'`
123		rpcfsdev=`echo "$bootdev" | sed -e "s/dev\/dsk/dev\/rdsk/"`
124		if [ X"$rpcfsdev" != X ]; then
125			print "Installing grub on $rpcfsdev"
126			"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rpcfsdev
127		fi
128	fi
129
130	grubdevlist=`get_rootdev_list`
131	zfsarg=""
132	if [ "$is_zfs_boot" = "yes" ]; then
133		zfsarg="-Z"
134	fi
135
136	for rootdev in $grubdevlist
137	do
138		if [ X"$rpcfsdev" != X ]; then
139			echo "create GRUB menu in "$ALT_ROOT"/stubboot"
140			"$ALT_ROOT"/sbin/bootadm update-menu $zfsarg\
141			    -R "$ALT_ROOT"/stubboot -o $rootdev,"$ALT_ROOT"
142		else
143			echo "Creating GRUB menu in ${ALT_ROOT:-/}"
144			$ALT_ROOT/sbin/bootadm update-menu -R ${ALT_ROOT:-/} \
145			    $zfsarg -o $rootdev
146		fi
147		print "Installing grub on $rootdev"
148		"$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rootdev
149	done
150}
151
152if [ -f "$ALT_ROOT"/platform/i86pc/multiboot -a "$ARCH" = i386 ] ; then
153	check_pcfs_boot
154	check_zfs_boot
155	install_grub
156fi
157
158exit 0
159