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

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

Update GRUB generation, read in device.hints directly now, also display
a message to hold "Left-Shift" to get into the boot-menu.

  • Property mode set to 100755
File size: 8.3 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
30ROOTFS=`mount | awk '/ \/ / {print $1}'`
31BEDS="$( echo ${ROOTFS} | awk -F '/' '{print $2}' )"
32if [ "$BEDS" = "dev" ] ; then BEDS="ROOT"; fi
33
34
35display_loaderopts()
36{
37  # Optional ARG1, set to a ZFS dataset to mount and read values from
38  if [ -n "$1" ] ; then
39     fMnt="/mnt.$$"
40     mkdir $fMnt
41     mount -t zfs  ${1} $fMnt >/dev/null
42     if [ $? -ne 0 ] ; then
43        echo "Failed to mount ${1}" >&2
44        return
45     fi
46  else
47     fMnt=""
48  fi
49 
50  # Create our placeholder to save _load entries to parse
51  touch /tmp/.lRObjs.$$
52  touch /tmp/.lSysCtls.$$
53
54  # Loader files, in order of which to read them
55  lFiles="${fMnt}/boot/loader.conf.local ${fMnt}/boot/loader.conf ${fMnt}/boot/loader.conf.trueos ${fMnt}/boot/loader.conf.pcbsd"
56  for f in $lFiles
57  do
58     if [ ! -e "$f" ] ; then continue ; fi
59
60     # Lets parse any of the _load= lines
61     grep "_load=" ${f} | grep -v "^#" >/tmp/.lObjs.$$
62     while read line
63     do
64       loadVal="`echo $line | cut -d '=' -f 1`"
65       # Is this value already set in a higher priority file?
66       grep -q "^${loadVal}" /tmp/.lRObjs.$$
67       if [ $? -eq 0 ];then continue; fi
68
69       # Save this value for later
70       echo "$line" >> /tmp/.lRObjs.$$
71       haveObjs=1
72
73     done < /tmp/.lObjs.$$
74     rm /tmp/.lObjs.$$
75
76     # Lets look for any sysctls to set
77     grep "." ${f} | grep "=" | grep -v "^#" | grep -v "_load" >/tmp/.sObjs.$$
78     while read line
79     do
80       loadVal="`echo $line | cut -d '=' -f 1`"
81       # Is this value already set in a higher priority file?
82       grep -q "^${loadVal}" /tmp/.lSysCtls.$$
83       if [ $? -eq 0 ];then continue; fi
84
85       # Save this value for later
86       echo "$line" >> /tmp/.lSysCtls.$$
87       haveSysCtls=1
88
89     done < /tmp/.sObjs.$$
90     rm /tmp/.sObjs.$$
91  done
92
93  # Now lets echo out the modules to load
94  if [ "$haveObjs" = "1" ] ; then
95    while read line
96    do
97      echo "$line" | grep -q '"YES"'
98      if [ $? -ne 0 ] ; then continue ; fi
99      module="`echo $line | cut -d '=' -f 1 | sed 's|_load||g'`"
100      # Try to locate module now
101      if [ -e "${fMnt}/boot/kernel/${module}.ko" ] ; then
102        mPath="kernel"
103      elif [ -e "${fMnt}/boot/modules/${module}.ko" ] ; then
104        mPath="modules"
105      else
106        # This isn't a module that we can see, lets set it as a variable
107        #echo "No such module $line, setting as a variable" >&2
108        echo "$line" >> /tmp/.lSysCtls.$$
109        continue
110      fi
111      echo "    kfreebsd_module_elf ${loadPrefix}/@/boot/${mPath}/${module}.ko"
112    done < /tmp/.lRObjs.$$
113  fi
114
115  # Any sysctls to set?
116  if [ "$haveSysCtls" = "1" ] ; then
117    while read line
118    do
119      # Strip out the vfs.root.mountfrom, we set that elsewhere
120      echo "$line" | grep -q "vfs.root.mountfrom"
121      if [ $? -eq 0 ] ; then continue ; fi
122
123      val="`echo $line | sed 's|"||g'`"
124      echo "    set kFreeBSD.${val}"
125    done < /tmp/.lSysCtls.$$
126  fi
127
128  rm /tmp/.lRObjs.$$
129  rm /tmp/.lSysCtls.$$
130  if [ -n "$1" ] ; then
131     umount /mnt.$$ >/dev/null
132     rmdir /mnt.$$ >/dev/null
133  fi
134}
135
136detect_beadm()
137{
138   which -s beadm
139   if [ $? -ne 0 ] ; then return 0; fi
140
141   # Check if we are running from the installer and use its beadm
142   if [ -e "/root/beadm.install" ] ; then
143      BEADM="/root/beadm.install"
144   else
145      BEADM="`which beadm`"
146   fi
147
148   ${BEADM} list >/dev/null 2>/dev/null
149   if [ $? -ne 0 ] ; then return 0; fi
150
151
152   if [ -e "/etc/defaults/pcbsd" ] ; then
153      NICK="PC-BSD"
154   else
155      NICK="TrueOS"
156   fi
157
158   # Get list of beadm datasets
159   for b in `${BEADM} list 2>/dev/null| grep -v "Created" | cut -d ' ' -f 1`
160   do
161      # Got a beadm snapshot, lets get the complete dataset name
162      beLine=`${BEADM} list -a | grep "/$BEDS/${b}"`
163      cdataset=`echo $beLine | awk '{print $1}'`
164      cdatadate=`echo $beLine | awk '{print $5}'`
165      cdatatime=`echo $beLine | awk '{print $6}'`
166      ztank=`echo $cdataset | cut -d '/' -f 1`
167      shortdataset="/`echo $cdataset | cut -d '/' -f 2-5`"
168
169      # First part of this dataset
170      cat > /tmp/.grubdataset.$$.1 << EOF
171    insmod zfs
172    search -s -l $ztank
173EOF
174
175      # Second part of loader to save
176      cat > /tmp/.grubdataset.$$.2 << EOF
177    kfreebsd_module ${shortdataset}/@/boot/zfs/zpool.cache type=/boot/zfs/zpool.cache
178    set kFreeBSD.vfs.root.mountfrom=zfs:$cdataset
179EOF
180     
181      # Now lets look for options in loader.conf to load
182      loadPrefix="${shortdataset}"
183      # If this is the current mounted dataset, we can skip mounting it
184      mount | grep -q -e "$cdataset on / (" -e "$cdataset on /mnt ("
185      if [ $? -eq 0 ] ; then
186        display_loaderopts >>/tmp/.grubdataset.$$.2
187      else
188        display_loaderopts $cdataset >> /tmp/.grubdataset.$$.2
189      fi
190
191      # Lets start a submenu for each BE
192cat << EOF
193# Start TrueOS BE Submenu
194submenu "${NICK} ($b) - ${cdatadate} ${cdatatime}" {
195EOF
196 
197  # Lets do the default entry first
198  #################################
199  cat << EOF
200  menuentry "Normal Bootup" {
201EOF
202  # Get the dataset guts
203  cat /tmp/.grubdataset.$$.1
204  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
205  echo "    kfreebsd_loadenv ${shortdataset}@/boot/device.hints"
206  cat /tmp/.grubdataset.$$.2
207
208  # Set any options
209  cat << EOF
210  }
211EOF
212
213  # Next lets do single user mode
214  #################################
215  cat << EOF
216  menuentry "Single User Mode" {
217EOF
218  # Get the dataset guts
219  cat /tmp/.grubdataset.$$.1
220  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel -s"
221  echo "    kfreebsd_loadenv ${shortdataset}@/boot/device.hints"
222  cat /tmp/.grubdataset.$$.2
223
224  # Set any options
225  cat << EOF
226  }
227EOF
228
229  # Next lets do verbose mode
230  #################################
231  cat << EOF
232  menuentry "Verbose Mode" {
233EOF
234  # Get the dataset guts
235  cat /tmp/.grubdataset.$$.1
236  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel -v"
237  echo "    kfreebsd_loadenv ${shortdataset}@/boot/device.hints"
238  cat /tmp/.grubdataset.$$.2
239
240  # Set any options
241  cat << EOF
242  }
243EOF
244
245  if [ -e "/etc/defaults/pcbsd" ] ; then
246    # Next lets do display wizard
247    #################################
248    cat << EOF
249  menuentry "Run the Display Wizard" {
250EOF
251    # Get the dataset guts
252    cat /tmp/.grubdataset.$$.1
253    echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
254    echo "    kfreebsd_loadenv ${shortdataset}@/boot/device.hints"
255    cat /tmp/.grubdataset.$$.2
256
257    # Set any options
258    cat << EOF
259    set kFreeBSD.runwiz=YES
260  }
261EOF
262    # Now for vesa mode
263    #################################
264    cat << EOF
265  menuentry "Run X in vesa mode" {
266EOF
267    # Get the dataset guts
268    cat /tmp/.grubdataset.$$.1
269    echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
270    echo "    kfreebsd_loadenv ${shortdataset}@/boot/device.hints"
271    cat /tmp/.grubdataset.$$.2
272
273    # Set any options
274    cat << EOF
275    set kFreeBSD.xvesa=YES
276  }
277EOF
278  fi
279
280
281      # Lastly lets close the submenu section
282      cat << EOF
283}
284# End TrueOS BE
285
286EOF
287   done
288
289   # Cleanup after ourselves
290   if [ -e "/tmp/.grubdataset.$$.1" ] ; then
291     rm /tmp/.grubdataset.$$.1
292     rm /tmp/.grubdataset.$$.2
293   fi
294}
295
296# Detect our types of disk layouts
297detect_beadm
298
Note: See TracBrowser for help on using the repository browser.