source: src-sh/pc-extractoverlay/ports-overlay/usr/local/etc/grub.d/10_ktrueos @ 293f64e

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since 293f64e was 293f64e, checked in by Kris Moore <kris@…>, 19 months ago

Make grub beadm detection script able to cope with installing from
chrooted /mnt, such as during system installation

  • Property mode set to 100755
File size: 5.7 KB
Line 
1#!/bin/sh
2#
3# Copyright 2013 iXsystems (Kris Moore)
4# All rights reserved
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted providing that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26
27# Script to detect TrueOS / PC-BSD disk installations and create grub
28# entries for them
29
30display_loaderopts()
31{
32  # Optional ARG1, set to a ZFS dataset to mount and read values from
33  if [ -n "$1" ] ; then
34     fMnt="/mnt.$$"
35     mkdir $fMnt
36     mount -t zfs  ${1} $fMnt >/dev/null
37     if [ $? -ne 0 ] ; then
38        echo "Failed to mount ${1}" >&2
39        return
40     fi
41  else
42     fMnt=""
43  fi
44 
45  # Create our placeholder to save _load entries to parse
46  touch /tmp/.lRObjs.$$
47  touch /tmp/.lSysCtls.$$
48
49  # Loader files, in order of which to read them
50  lFiles="${fMnt}/boot/device.hints ${fMnt}/boot/loader.conf.local ${fMnt}/boot/loader.conf ${fMnt}/boot/loader.conf.pcbsd"
51  for f in $lFiles
52  do
53     if [ ! -e "$f" ] ; then continue ; fi
54
55     # Lets parse any of the _load= lines
56     grep "_load=" ${f} | grep -v "^#" >/tmp/.lObjs.$$
57     while read line
58     do
59       loadVal="`echo $line | cut -d '=' -f 1`"
60       # Is this value already set in a higher priority file?
61       grep -q "^${loadVal}" /tmp/.lRObjs.$$
62       if [ $? -eq 0 ];then continue; fi
63
64       # Save this value for later
65       echo "$line" >> /tmp/.lRObjs.$$
66       haveObjs=1
67
68     done < /tmp/.lObjs.$$
69     rm /tmp/.lObjs.$$
70
71     # Lets look for any sysctls to set
72     grep "." ${f} | grep "=" | grep -v "^#" | grep -v "_load" >/tmp/.sObjs.$$
73     while read line
74     do
75       loadVal="`echo $line | cut -d '=' -f 1`"
76       # Is this value already set in a higher priority file?
77       grep -q "^${loadVal}" /tmp/.lSysCtls.$$
78       if [ $? -eq 0 ];then continue; fi
79
80       # Save this value for later
81       echo "$line" >> /tmp/.lSysCtls.$$
82       haveSysCtls=1
83
84     done < /tmp/.sObjs.$$
85     rm /tmp/.sObjs.$$
86  done
87
88  # Now lets echo out the modules to load
89  if [ "$haveObjs" = "1" ] ; then
90    while read line
91    do
92      echo "$line" | grep -q '"YES"'
93      if [ $? -ne 0 ] ; then continue ; fi
94      module="`echo $line | cut -d '=' -f 1 | sed 's|_load||g'`"
95      # Try to locate module now
96      if [ -e "${fMnt}/boot/kernel/${module}.ko" ] ; then
97        mPath="kernel"
98      elif [ -e "${fMnt}/boot/modules/${module}.ko" ] ; then
99        mPath="modules"
100      else
101        echo "Unable to locate module ${module}" >&2
102      fi
103      echo "  kfreebsd_module_elf ${loadPrefix}/@/boot/${mPath}/${module}.ko"
104    done < /tmp/.lRObjs.$$
105  fi
106
107  # Any sysctls to set?
108  if [ "$haveSysCtls" = "1" ] ; then
109    while read line
110    do
111      # Strip out the vfs.root.mountfrom, we set that elsewhere
112      echo "$line" | grep -q "vfs.root.mountfrom"
113      if [ $? -eq 0 ] ; then continue ; fi
114
115      val="`echo $line | sed 's|"||g'`"
116      echo "  set kFreeBSD.${val}"
117    done < /tmp/.lSysCtls.$$
118  fi
119
120  rm /tmp/.lRObjs.$$
121  rm /tmp/.lSysCtls.$$
122  if [ -n "$1" ] ; then
123     umount /mnt.$$ >/dev/null
124     rmdir /mnt.$$ >/dev/null
125  fi
126}
127
128detect_beadm()
129{
130   which -s beadm
131   if [ $? -ne 0 ] ; then return 0; fi
132
133   # Check if we are running from the installer and use its beadm
134   if [ -e "/root/beadm.install" ] ; then
135      BEADM="/root/beadm.install"
136   else
137      BEADM="`which beadm`"
138   fi
139
140   ${BEADM} list >/dev/null 2>/dev/null
141   if [ $? -ne 0 ] ; then return 0; fi
142
143
144   if [ -e "/etc/defaults/pcbsd" ] ; then
145      NICK="PC-BSD"
146   else
147      NICK="TrueOS"
148   fi
149
150   # Get list of beadm datasets
151   for b in `${BEADM} list 2>/dev/null| grep -v "Created" | cut -d ' ' -f 1`
152   do
153      # Got a beadm snapshot, lets get the complete dataset name
154      cdataset=`${BEADM} list -a | grep "/ROOT/${b}" | awk '{print $1}'`
155      ztank=`echo $cdataset | cut -d '/' -f 1`
156      shortdataset="/`echo $cdataset | cut -d '/' -f 2-5`"
157
158      # Lets echo the main guts of the BE loader
159      cat << EOF
160# Start TrueOS BE
161menuentry "$NICK - Boot Environment ($b)" {
162  insmod zfs
163  search -s -l $ztank
164  kfreebsd ${shortdataset}/@/boot/kernel/kernel
165  kfreebsd_module ${shortdataset}/@/boot/zfs/zpool.cache type=/boot/zfs/zpool.cache
166  set kFreeBSD.vfs.root.mountfrom=zfs:$cdataset
167EOF
168     
169      # Now lets look for options in loader.conf to load
170      loadPrefix="${shortdataset}"
171      # If this is the current mounted dataset, we can skip mounting it
172      mount | grep -q -e "$cdataset on / (" -e "$cdataset on /mnt ("
173      if [ $? -eq 0 ] ; then
174        display_loaderopts
175      else
176        display_loaderopts $cdataset
177      fi
178
179      # Lastly lets close the section
180      cat << EOF
181}
182# End TrueOS BE
183
184EOF
185   done
186
187}
188
189# Detect our types of disk layouts
190detect_beadm
191
Note: See TracBrowser for help on using the repository browser.