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

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

Add grub2 configuration files

  • Property mode set to 100755
File size: 5.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.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   if [ -e "/etc/defaults/pcbsd" ] ; then
133      NICK="PC-BSD"
134   else
135      NICK="TrueOS"
136   fi
137
138   # Get list of beadm datasets
139   for b in `beadm list 2>/dev/null| grep -v "Created" | cut -d ' ' -f 1`
140   do
141      # Got a beadm snapshot, lets get the complete dataset name
142      cdataset=`beadm list -a | grep "/ROOT/${b}" | awk '{print $1}'`
143      ztank=`echo $cdataset | cut -d '/' -f 1`
144      shortdataset="/`echo $cdataset | cut -d '/' -f 2-5`"
145
146      # Lets echo the main guts of the BE loader
147      cat << EOF
148# Start TrueOS BE
149menuentry "$NICK - Boot Environment ($b)" {
150  insmod zfs
151  search -s -l $ztank
152  kfreebsd ${shortdataset}/@/boot/kernel/kernel
153  kfreebsd_module ${shortdataset}/@/boot/zfs/zpool.cache type=/boot/zfs/zpool.cache
154  set kFreeBSD.vfs.root.mountfrom=zfs:$cdataset
155EOF
156     
157      # Now lets look for options in loader.conf to load
158      loadPrefix="${shortdataset}"
159      # If this is the current mounted dataset, we can skip mounting it
160      mount | grep -q "$cdataset on / ("
161      if [ $? -eq 0 ] ; then
162        display_loaderopts
163      else
164        display_loaderopts $cdataset
165      fi
166
167      # Lastly lets close the section
168      cat << EOF
169}
170# End TrueOS BE
171
172EOF
173   done
174
175}
176
177# Detect our types of disk layouts
178detect_beadm
179
Note: See TracBrowser for help on using the repository browser.