recording grub mod for system 76 laptops
[feisty_meow.git] / infobase / configuration / grub / system76_grub_mod / 10_linux
1 #! /bin/sh
2 set -e
3
4 # grub-mkconfig helper script.
5 # Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
6 #
7 # GRUB is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # GRUB is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
19
20 prefix="/usr"
21 exec_prefix="/usr"
22 datarootdir="/usr/share"
23 ubuntu_recovery="1"
24 quiet_boot="1"
25 quick_boot="1"
26 gfxpayload_dynamic="1"
27 # 2019-02-14: fred moved vt_handoff from 1 to 0 to try to address sleep freezes 
28 # 2021-01-05: this seems to help a lot, but doesn't totally eliminate the freezes.
29 vt_handoff="0"
30
31 . "$pkgdatadir/grub-mkconfig_lib"
32
33 export TEXTDOMAIN=grub
34 export TEXTDOMAINDIR="${datarootdir}/locale"
35
36 CLASS="--class gnu-linux --class gnu --class os"
37 SUPPORTED_INITS="sysvinit:/lib/sysvinit/init systemd:/lib/systemd/systemd upstart:/sbin/upstart"
38
39 if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
40   OS=GNU/Linux
41 else
42   case ${GRUB_DISTRIBUTOR} in
43     Ubuntu|Kubuntu)
44       OS="${GRUB_DISTRIBUTOR}"
45       ;;
46     *)
47       OS="${GRUB_DISTRIBUTOR} GNU/Linux"
48       ;;
49   esac
50   CLASS="--class $(echo ${GRUB_DISTRIBUTOR} | tr 'A-Z' 'a-z' | cut -d' ' -f1|LC_ALL=C sed 's,[^[:alnum:]_],_,g') ${CLASS}"
51 fi
52
53 # loop-AES arranges things so that /dev/loop/X can be our root device, but
54 # the initrds that Linux uses don't like that.
55 case ${GRUB_DEVICE} in
56   /dev/loop/*|/dev/loop[0-9])
57     GRUB_DEVICE=`losetup ${GRUB_DEVICE} | sed -e "s/^[^(]*(\([^)]\+\)).*/\1/"`
58     # We can't cope with devices loop-mounted from files here.
59     case ${GRUB_DEVICE} in
60       /dev/*) ;;
61       *) exit 0 ;;
62     esac
63   ;;
64 esac
65
66 # btrfs may reside on multiple devices. We cannot pass them as value of root= parameter
67 # and mounting btrfs requires user space scanning, so force UUID in this case.
68 if [ "x${GRUB_DEVICE_UUID}" = "x" ] || [ "x${GRUB_DISABLE_LINUX_UUID}" = "xtrue" ] \
69     || ! test -e "/dev/disk/by-uuid/${GRUB_DEVICE_UUID}" \
70     || ( test -e "${GRUB_DEVICE}" && uses_abstraction "${GRUB_DEVICE}" lvm ); then
71   LINUX_ROOT_DEVICE=${GRUB_DEVICE}
72 else
73   LINUX_ROOT_DEVICE=UUID=${GRUB_DEVICE_UUID}
74 fi
75
76 case x"$GRUB_FS" in
77     xbtrfs)
78         rootsubvol="`make_system_path_relative_to_its_root /`"
79         rootsubvol="${rootsubvol#/}"
80         if [ "x${rootsubvol}" != x ]; then
81             GRUB_CMDLINE_LINUX="rootflags=subvol=${rootsubvol} ${GRUB_CMDLINE_LINUX}"
82         fi;;
83     xzfs)
84         rpool=`${grub_probe} --device ${GRUB_DEVICE} --target=fs_label 2>/dev/null || true`
85         bootfs="`make_system_path_relative_to_its_root / | sed -e "s,@$,,"`"
86         LINUX_ROOT_DEVICE="ZFS=${rpool}${bootfs}"
87         ;;
88 esac
89
90 title_correction_code=
91
92 if [ -x /lib/recovery-mode/recovery-menu ]; then
93     GRUB_CMDLINE_LINUX_RECOVERY=recovery
94 else
95     GRUB_CMDLINE_LINUX_RECOVERY=single
96 fi
97 if [ "$ubuntu_recovery" = 1 ]; then
98     GRUB_CMDLINE_LINUX_RECOVERY="$GRUB_CMDLINE_LINUX_RECOVERY nomodeset"
99 fi
100
101 if [ "$vt_handoff" = 1 ]; then
102   for word in $GRUB_CMDLINE_LINUX_DEFAULT; do
103     if [ "$word" = splash ]; then
104       GRUB_CMDLINE_LINUX_DEFAULT="$GRUB_CMDLINE_LINUX_DEFAULT \$vt_handoff"
105     fi
106   done
107 fi
108
109 linux_entry ()
110 {
111   os="$1"
112   version="$2"
113   type="$3"
114   args="$4"
115
116   if [ -z "$boot_device_id" ]; then
117       boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
118   fi
119   if [ x$type != xsimple ] ; then
120       case $type in
121           recovery)
122               title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")" ;;
123           init-*)
124               title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "${type#init-}")" ;;
125           *)
126               title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
127       esac
128       if [ x"$title" = x"$GRUB_ACTUAL_DEFAULT" ] || [ x"Previous Linux versions>$title" = x"$GRUB_ACTUAL_DEFAULT" ]; then
129           replacement_title="$(echo "Advanced options for ${OS}" | sed 's,>,>>,g')>$(echo "$title" | sed 's,>,>>,g')"
130           quoted="$(echo "$GRUB_ACTUAL_DEFAULT" | grub_quote)"
131           title_correction_code="${title_correction_code}if [ \"x\$default\" = '$quoted' ]; then default='$(echo "$replacement_title" | grub_quote)'; fi;"
132           grub_warn "$(gettext_printf "Please don't use old title \`%s' for GRUB_DEFAULT, use \`%s' (for versions before 2.00) or \`%s' (for 2.00 or later)" "$GRUB_ACTUAL_DEFAULT" "$replacement_title" "gnulinux-advanced-$boot_device_id>gnulinux-$version-$type-$boot_device_id")"
133       fi
134       echo "menuentry '$(echo "$title" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-$version-$type-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
135   else
136       echo "menuentry '$(echo "$os" | grub_quote)' ${CLASS} \$menuentry_id_option 'gnulinux-simple-$boot_device_id' {" | sed "s/^/$submenu_indentation/"
137   fi      
138   if [ "$quick_boot" = 1 ]; then
139       echo "    recordfail" | sed "s/^/$submenu_indentation/"
140   fi
141   if [ x$type != xrecovery ] ; then
142       save_default_entry | grub_add_tab
143   fi
144
145   # Use ELILO's generic "efifb" when it's known to be available.
146   # FIXME: We need an interface to select vesafb in case efifb can't be used.
147   if [ "x$GRUB_GFXPAYLOAD_LINUX" = x ]; then
148       echo "    load_video" | sed "s/^/$submenu_indentation/"
149   else
150       if [ "x$GRUB_GFXPAYLOAD_LINUX" != xtext ]; then
151           echo "        load_video" | sed "s/^/$submenu_indentation/"
152       fi
153   fi
154   if ([ "$ubuntu_recovery" = 0 ] || [ x$type != xrecovery ]) && \
155      ([ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 1 ]); then
156       echo "    gfxmode \$linux_gfx_mode" | sed "s/^/$submenu_indentation/"
157   fi
158
159   echo "        insmod gzio" | sed "s/^/$submenu_indentation/"
160   echo "        if [ x\$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi" | sed "s/^/$submenu_indentation/"
161
162   if [ x$dirname = x/ ]; then
163     if [ -z "${prepare_root_cache}" ]; then
164       prepare_root_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | grub_add_tab)"
165     fi
166     printf '%s\n' "${prepare_root_cache}" | sed "s/^/$submenu_indentation/"
167   else
168     if [ -z "${prepare_boot_cache}" ]; then
169       prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE_BOOT} | grub_add_tab)"
170     fi
171     printf '%s\n' "${prepare_boot_cache}" | sed "s/^/$submenu_indentation/"
172   fi
173   if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
174     message="$(gettext_printf "Loading Linux %s ..." ${version})"
175     sed "s/^/$submenu_indentation/" << EOF
176         echo    '$(echo "$message" | grub_quote)'
177 EOF
178   fi
179   if test -d /sys/firmware/efi && test -e "${linux}.efi.signed"; then
180     sed "s/^/$submenu_indentation/" << EOF
181         linux   ${rel_dirname}/${basename}.efi.signed root=${linux_root_device_thisversion} ro ${args}
182 EOF
183   else
184     if [ x"$GRUB_FORCE_PARTUUID" = x ]; then
185         sed "s/^/$submenu_indentation/" << EOF
186         linux   ${rel_dirname}/${basename} root=${linux_root_device_thisversion} ro ${args}
187 EOF
188     else
189         sed "s/^/$submenu_indentation/" << EOF
190         linux   ${rel_dirname}/${basename} root=PARTUUID=${GRUB_FORCE_PARTUUID} ro ${args}
191 EOF
192     fi
193   fi
194   if test -n "${initrd}" && [ x"$GRUB_DISABLE_INITRD" != xtrue ]; then
195     # TRANSLATORS: ramdisk isn't identifier. Should be translated.
196     if [ x"$quiet_boot" = x0 ] || [ x"$type" != xsimple ]; then
197       message="$(gettext_printf "Loading initial ramdisk ...")"
198       sed "s/^/$submenu_indentation/" << EOF
199         echo    '$(echo "$message" | grub_quote)'
200 EOF
201     fi
202     sed "s/^/$submenu_indentation/" << EOF
203         initrd  ${rel_dirname}/${initrd}
204 EOF
205   fi
206   sed "s/^/$submenu_indentation/" << EOF
207 }
208 EOF
209 }
210
211 machine=`uname -m`
212 case "x$machine" in
213     xi?86 | xx86_64)
214         list=
215         for i in /boot/vmlinuz-* /vmlinuz-* /boot/kernel-* ; do
216             if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
217         done ;;
218     *) 
219         list=
220         for i in /boot/vmlinuz-* /boot/vmlinux-* /vmlinuz-* /vmlinux-* /boot/kernel-* ; do
221                   if grub_file_is_not_garbage "$i" ; then list="$list $i" ; fi
222         done ;;
223 esac
224
225 case "$machine" in
226     i?86) GENKERNEL_ARCH="x86" ;;
227     mips|mips64) GENKERNEL_ARCH="mips" ;;
228     mipsel|mips64el) GENKERNEL_ARCH="mipsel" ;;
229     arm*) GENKERNEL_ARCH="arm" ;;
230     *) GENKERNEL_ARCH="$machine" ;;
231 esac
232
233 prepare_boot_cache=
234 prepare_root_cache=
235 boot_device_id=
236 title_correction_code=
237
238 cat << 'EOF'
239 function gfxmode {
240         set gfxpayload="${1}"
241 EOF
242 if [ "$vt_handoff" = 1 ]; then
243   cat << 'EOF'
244         if [ "${1}" = "keep" ]; then
245                 set vt_handoff=vt.handoff=1
246         else
247                 set vt_handoff=
248         fi
249 EOF
250 fi
251 cat << EOF
252 }
253 EOF
254
255 # Use ELILO's generic "efifb" when it's known to be available.
256 # FIXME: We need an interface to select vesafb in case efifb can't be used.
257 if [ "x$GRUB_GFXPAYLOAD_LINUX" != x ] || [ "$gfxpayload_dynamic" = 0 ]; then
258   echo "set linux_gfx_mode=$GRUB_GFXPAYLOAD_LINUX"
259 else
260   cat << EOF
261 if [ "\${recordfail}" != 1 ]; then
262   if [ -e \${prefix}/gfxblacklist.txt ]; then
263     if hwmatch \${prefix}/gfxblacklist.txt 3; then
264       if [ \${match} = 0 ]; then
265         set linux_gfx_mode=keep
266       else
267         set linux_gfx_mode=text
268       fi
269     else
270       set linux_gfx_mode=text
271     fi
272   else
273     set linux_gfx_mode=keep
274   fi
275 else
276   set linux_gfx_mode=text
277 fi
278 EOF
279 fi
280 cat << EOF
281 export linux_gfx_mode
282 EOF
283
284 # Extra indentation to add to menu entries in a submenu. We're not in a submenu
285 # yet, so it's empty. In a submenu it will be equal to '\t' (one tab).
286 submenu_indentation=""
287
288 is_top_level=true
289 while [ "x$list" != "x" ] ; do
290   linux=`version_find_latest $list`
291   case $linux in
292     *.efi.signed)
293       # We handle these in linux_entry.
294       list=`echo $list | tr ' ' '\n' | grep -vx $linux | tr '\n' ' '`
295       continue
296       ;;
297   esac
298   gettext_printf "Found linux image: %s\n" "$linux" >&2
299   basename=`basename $linux`
300   dirname=`dirname $linux`
301   rel_dirname=`make_system_path_relative_to_its_root $dirname`
302   version=`echo $basename | sed -e "s,^[^0-9]*-,,g"`
303   alt_version=`echo $version | sed -e "s,\.old$,,g"`
304   linux_root_device_thisversion="${LINUX_ROOT_DEVICE}"
305
306   initrd=
307   for i in "initrd.img-${version}" "initrd-${version}.img" "initrd-${version}.gz" \
308            "initrd-${version}" "initramfs-${version}.img" \
309            "initrd.img-${alt_version}" "initrd-${alt_version}.img" \
310            "initrd-${alt_version}" "initramfs-${alt_version}.img" \
311            "initramfs-genkernel-${version}" \
312            "initramfs-genkernel-${alt_version}" \
313            "initramfs-genkernel-${GENKERNEL_ARCH}-${version}" \
314            "initramfs-genkernel-${GENKERNEL_ARCH}-${alt_version}"; do
315     if test -e "${dirname}/${i}" ; then
316       initrd="$i"
317       break
318     fi
319   done
320
321   config=
322   for i in "${dirname}/config-${version}" "${dirname}/config-${alt_version}" "/etc/kernels/kernel-config-${version}" ; do
323     if test -e "${i}" ; then
324       config="${i}"
325       break
326     fi
327   done
328
329   initramfs=
330   if test -n "${config}" ; then
331       initramfs=`grep CONFIG_INITRAMFS_SOURCE= "${config}" | cut -f2 -d= | tr -d \"`
332   fi
333
334   if test -n "${initrd}" ; then
335     gettext_printf "Found initrd image: %s\n" "${dirname}/${initrd}" >&2
336   elif test -z "${initramfs}" ; then
337     # "UUID=" and "ZFS=" magic is parsed by initrd or initramfs.  Since there's
338     # no initrd or builtin initramfs, it can't work here.
339     linux_root_device_thisversion=${GRUB_DEVICE}
340   fi
341
342   if [ "x$is_top_level" = xtrue ] && [ "x${GRUB_DISABLE_SUBMENU}" != xy ]; then
343     linux_entry "${OS}" "${version}" simple \
344     "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
345
346     submenu_indentation="$grub_tab"
347     
348     if [ -z "$boot_device_id" ]; then
349         boot_device_id="$(grub_get_device_id "${GRUB_DEVICE}")"
350     fi
351     # TRANSLATORS: %s is replaced with an OS name
352     echo "submenu '$(gettext_printf "Advanced options for %s" "${OS}" | grub_quote)' \$menuentry_id_option 'gnulinux-advanced-$boot_device_id' {"
353     is_top_level=false
354   fi
355
356   linux_entry "${OS}" "${version}" advanced \
357               "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
358   for supported_init in ${SUPPORTED_INITS}; do
359     init_path="${supported_init#*:}"
360     if [ -x "${init_path}" ] && [ "$(readlink -f /sbin/init)" != "$(readlink -f "${init_path}")" ]; then
361       linux_entry "${OS}" "${version}" "init-${supported_init%%:*}" \
362                   "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT} init=${init_path}"
363     fi
364   done
365   if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
366     linux_entry "${OS}" "${version}" recovery \
367                 "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
368   fi
369
370   list=`echo $list | tr ' ' '\n' | fgrep -vx "$linux" | tr '\n' ' '`
371 done
372
373 # If at least one kernel was found, then we need to
374 # add a closing '}' for the submenu command.
375 if [ x"$is_top_level" != xtrue ]; then
376   echo '}'
377 fi
378
379 echo "$title_correction_code"