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

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since ca660df was ca660df, checked in by Kris Moore <kris@…>, 7 months ago

Fix an issue during installation resulting in an incorrect grub.cfg

  • Property mode set to 100755
File size: 8.0 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/device.hints ${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  cat /tmp/.grubdataset.$$.2
206
207  # Set any options
208  cat << EOF
209  }
210EOF
211
212  # Next lets do single user mode
213  #################################
214  cat << EOF
215  menuentry "Single User Mode" {
216EOF
217  # Get the dataset guts
218  cat /tmp/.grubdataset.$$.1
219  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel -s"
220  cat /tmp/.grubdataset.$$.2
221
222  # Set any options
223  cat << EOF
224  }
225EOF
226
227  # Next lets do verbose mode
228  #################################
229  cat << EOF
230  menuentry "Verbose Mode" {
231EOF
232  # Get the dataset guts
233  cat /tmp/.grubdataset.$$.1
234  echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel -v"
235  cat /tmp/.grubdataset.$$.2
236
237  # Set any options
238  cat << EOF
239  }
240EOF
241
242  if [ -e "/etc/defaults/pcbsd" ] ; then
243    # Next lets do display wizard
244    #################################
245    cat << EOF
246  menuentry "Run the Display Wizard" {
247EOF
248    # Get the dataset guts
249    cat /tmp/.grubdataset.$$.1
250    echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
251    cat /tmp/.grubdataset.$$.2
252
253    # Set any options
254    cat << EOF
255    set kFreeBSD.runwiz=YES
256  }
257EOF
258    # Now for vesa mode
259    #################################
260    cat << EOF
261  menuentry "Run X in vesa mode" {
262EOF
263    # Get the dataset guts
264    cat /tmp/.grubdataset.$$.1
265    echo "    kfreebsd ${shortdataset}/@/boot/kernel/kernel"
266    cat /tmp/.grubdataset.$$.2
267
268    # Set any options
269    cat << EOF
270    set kFreeBSD.xvesa=YES
271  }
272EOF
273  fi
274
275
276      # Lastly lets close the submenu section
277      cat << EOF
278}
279# End TrueOS BE
280
281EOF
282   done
283
284   # Cleanup after ourselves
285   if [ -e "/tmp/.grubdataset.$$.1" ] ; then
286     rm /tmp/.grubdataset.$$.1
287     rm /tmp/.grubdataset.$$.2
288   fi
289}
290
291# Detect our types of disk layouts
292detect_beadm
293
Note: See TracBrowser for help on using the repository browser.