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

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

Add submenus to GRUB for each boot-environment, now you can select
Normal, Single User, Verbose, or the Display wizard (on PC-BSD)

  • Property mode set to 100755
File size: 7.4 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.trueos ${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      beLine=`${BEADM} list -a | grep "/ROOT/${b}"`
155      cdataset=`echo $beLine | awk '{print $1}'`
156      cdatadate=`echo $beLine | awk '{print $5}'`
157      cdatatime=`echo $beLine | awk '{print $6}'`
158      ztank=`echo $cdataset | cut -d '/' -f 1`
159      shortdataset="/`echo $cdataset | cut -d '/' -f 2-5`"
160
161      # First part of this dataset
162      cat > /tmp/.grubdataset.$$.1 << EOF
163    insmod zfs
164    search -s -l $ztank
165EOF
166
167      # Second part of loader to save
168      cat > /tmp/.grubdataset.$$.2 << EOF
169    kfreebsd_module ${shortdataset}/@/boot/zfs/zpool.cache type=/boot/zfs/zpool.cache
170    set kFreeBSD.vfs.root.mountfrom=zfs:$cdataset
171EOF
172     
173      # Now lets look for options in loader.conf to load
174      loadPrefix="${shortdataset}"
175      # If this is the current mounted dataset, we can skip mounting it
176      mount | grep -q -e "$cdataset on / (" -e "$cdataset on /mnt ("
177      if [ $? -eq 0 ] ; then
178        display_loaderopts >>/tmp/.grubdataset.$$.2
179      else
180        display_loaderopts $cdataset >> /tmp/.grubdataset.$$.2
181      fi
182
183      # Lets start a submenu for each BE
184cat << EOF
185# Start TrueOS BE Submenu
186submenu "${NICK} ($b) - ${cdatadate} ${cdatatime}" {
187EOF
188 
189  # Lets do the default entry first
190  #################################
191  cat << EOF
192  menuentry "Normal Bootup" {
193EOF
194  # Get the dataset guts
195  cat /tmp/.grubdataset.$$.1
196  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
197  cat /tmp/.grubdataset.$$.2
198
199  # Set any options
200  cat << EOF
201  }
202EOF
203
204  # Next lets do single user mode
205  #################################
206  cat << EOF
207  menuentry "Single User Mode" {
208EOF
209  # Get the dataset guts
210  cat /tmp/.grubdataset.$$.1
211  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel -s"
212  cat /tmp/.grubdataset.$$.2
213
214  # Set any options
215  cat << EOF
216  }
217EOF
218
219  # Next lets do verbose mode
220  #################################
221  cat << EOF
222  menuentry "Verbose Mode" {
223EOF
224  # Get the dataset guts
225  cat /tmp/.grubdataset.$$.1
226  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel -v"
227  cat /tmp/.grubdataset.$$.2
228
229  # Set any options
230  cat << EOF
231  }
232EOF
233
234  if [ -e "/etc/defaults/pcbsd" ] ; then
235    # Next lets do display wizard
236    #################################
237    cat << EOF
238  menuentry "Run the Display Wizard" {
239EOF
240    # Get the dataset guts
241    cat /tmp/.grubdataset.$$.1
242    echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
243    cat /tmp/.grubdataset.$$.2
244
245    # Set any options
246    cat << EOF
247    set kFreeBSD.runwiz=YES
248  }
249EOF
250  fi
251
252
253      # Lastly lets close the submenu section
254      cat << EOF
255}
256# End TrueOS BE
257
258EOF
259   done
260
261   # Cleanup after ourselves
262   if [ -e "/tmp/.grubdataset.$$.1" ] ; then
263     rm /tmp/.grubdataset.$$.1
264     rm /tmp/.grubdataset.$$.2
265   fi
266}
267
268# Detect our types of disk layouts
269detect_beadm
270
Note: See TracBrowser for help on using the repository browser.