8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / boot / scripts / update_grub.ksh
blob8148717e1c4fd35439d1309189ea578f31b14c67
1 #!/bin/ksh -p
3 # CDDL HEADER START
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.
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.
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]
20 # CDDL HEADER END
24 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
26 # Copyright 2016 Nexenta Systems, Inc.
29 PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
30 ALT_ROOT=
32 while getopts R: OPT 2>/dev/null
34 case $OPT in
35 R) ALT_ROOT="$OPTARG"
37 ?) echo "Usage: ${0##*/}: [-R \<root\>]"
39 esac
40 done
42 ARCH=`uname -p`
44 is_pcfs_boot=yes
45 is_zfs_boot=no
47 check_pcfs_boot()
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
56 check_zfs_boot()
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
64 else
65 rootfstype=`df -n ${ALT_ROOT:-/} | awk '{print $3}'`
66 if [ "$rootfstype" = "zfs" ]; then
67 is_zfs_boot=yes
74 # Return the list of raw devices
76 get_rootdev_list()
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"
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"`
100 for rootdev in $rootdevlist
102 echo /dev/rdsk/`basename $rootdev`
103 done
107 # multiboot: install grub on the boot slice
109 install_grub()
111 # Stage 2 blocks must remain untouched
112 STAGE1="$ALT_ROOT"/boot/grub/stage1
113 STAGE2="$ALT_ROOT"/boot/grub/stage2
115 if [ $is_pcfs_boot = yes ]; then
117 # Note: /stubboot/boot/grub/stage2 must stay untouched.
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
130 grubdevlist=`get_rootdev_list`
131 zfsarg=""
132 if [ "$is_zfs_boot" = "yes" ]; then
133 zfsarg="-Z"
136 for rootdev in $grubdevlist
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
147 print "Installing grub on $rootdev"
148 "$ALT_ROOT"/sbin/installgrub $STAGE1 $STAGE2 $rootdev
149 done
152 if [ -f "$ALT_ROOT"/platform/i86pc/multiboot -a "$ARCH" = i386 ] ; then
153 check_pcfs_boot
154 check_zfs_boot
155 install_grub
158 exit 0