Dash:
[t2.git] / package / boot / grub / stone_mod_grub.sh
blob8f6aeacadbe22c1d3974b2e690f0dd7b4d998d62
1 # --- T2-COPYRIGHT-NOTE-BEGIN ---
2 # This copyright note is auto-generated by scripts/Create-CopyPatch.
3 #
4 # T2 SDE: package/*/grub/stone_mod_grub.sh
5 # Copyright (C) 2004 - 2021 The T2 SDE Project
6 # Copyright (C) 1998 - 2003 ROCK Linux Project
7 #
8 # More information can be found in the files COPYING and README.
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; version 2 of the License. A copy of the
13 # GNU General Public License can be found in the file COPYING.
14 # --- T2-COPYRIGHT-NOTE-END ---
16 # [MAIN] 70 grub GRUB Boot Loader Setup
17 # [SETUP] 90 grub
19 create_kernel_list() {
20 first=1
21 for x in `(cd /boot/; ls vmlinuz-*) | sort -r` ; do
22 ver=${x/vmlinuz-/}
23 if [ $first = 1 ] ; then
24 label=linux ; first=0
25 else
26 label=linux-$ver
29 cat << EOT
31 title $label
32 kernel $bootdrive$bootpath/$x root=$rootdev ro
33 initrd $bootdrive$bootpath/initrd-${ver}
34 EOT
35 done
38 create_device_map() {
39 gui_cmd '(Re-)Create GRUB Device Map' "$( cat << "EOT"
40 rm -vf /boot/grub/device.map
41 echo quit | grub --batch --device-map=/boot/grub/device.map
42 EOT
46 convert_device() {
47 local device="$1"
48 local root_part=
50 # extract device type (block) and major number for root drive
51 local major_minor=`ls -Ll $device |
52 awk '{if ($6 < 64) printf("%c%d0", $1, $5); else printf("%c%d1", $1, $5)}'`
54 # find the matching BIOS device
55 for bios_drive in `grep -v '^#' /boot/grub/device.map | awk '{print $2}'`
57 bios_major_minor=`ls -Ll $bios_drive 2>/dev/null |
58 awk '{if ($6 < 64) printf("%c%d0", $1, $5); else printf("%c%d1", $1, $5)}'`
60 if [ "$major_minor" = "$bios_major_minor" ]; then
61 # we found it
62 root_drive=`grep $bios_drive /boot/grub/device.map | awk '{print $1}'`
63 root_part=`ls -Ll $device | awk '{print $6}'`
64 root_part=$(( $root_part % 16 - 1 ))
65 break
67 done
69 drive=`echo $root_drive | sed "s:)$:,$root_part):"`
70 echo $drive
73 create_boot_menu() {
74 # determine /boot path, relative to the boot device
75 # (non local as used by create_kernel_list() ...)
77 if [ "$rootdrive" = "$bootdrive" ]
78 then bootpath="/boot"; else bootpath=""; fi
80 cat << EOT > /boot/grub/menu.lst
81 timeout 8
82 default 0
83 fallback 1
84 EOT
86 [ -f /boot/t2.xpm.gz ] && cat << EOT >> /boot/grub/menu.lst
88 foreground = FFFFFF
89 background = 09345F
90 splashimage $bootdrive$bootpath/t2.xpm.gz
91 EOT
93 create_kernel_list >> /boot/grub/menu.lst
95 [ -f /boot/memtest86.bin ] && cat << EOT >> /boot/grub/menu.lst
97 title MemTest86 (memory tester)
98 kernel $bootdrive$bootpath/memtest86.bin
99 EOT
101 gui_message "This is the new /boot/grub/menu.lst file:
103 $( cat /boot/grub/menu.lst )"
104 unset bootpath
107 grub_install() {
108 gui_cmd 'Installing GRUB' "echo -e 'root $bootdrive\\nsetup (hd0)\\nquit' | grub --batch --device-map=/boot/grub/device.map"
111 main() {
112 rootdev="`grep ' / ' /proc/mounts | tail -n 1 | sed 's, .*,,'`"
113 bootdev="`grep ' /boot ' /proc/mounts | tail -n 1 | sed 's, .*,,'`"
114 [ -z "$bootdev" ] && bootdev="$rootdev"
117 rootdev="$( cd `dirname $rootdev` ; pwd -P )/$( basename $rootdev )"
118 while [ -L $rootdev ] ; do
119 directory="$( cd `dirname $rootdev` ; pwd -P )"
120 rootdev="$( ls -l $rootdev | sed 's,.* -> ,,' )"
121 [ "${rootdev##/*}" ] && rootdev="$directory/$rootdev"
122 i=$(( $i + 1 )) ; [ $i -gt 20 ] && rootdev="Not found!"
123 done
126 bootdev="$( cd `dirname $bootdev` ; pwd -P )/$( basename $bootdev )"
127 while [ -L $bootdev ] ; do
128 directory="$( cd `dirname $bootdev` ; pwd -P )"
129 bootdev="$( ls -l $bootdev | sed 's,.* -> ,,' )"
130 [ "${bootdev##/*}" ] && bootdev="$directory/$bootdev"
131 i=$(( $i + 1 )) ; [ $i -gt 20 ] && bootdev="Not found!"
132 done
134 if [ ! -f /boot/grub/menu.lst ] ; then
135 if gui_yesno "GRUB does not appear to be configured.
136 Automatically install GRUB now?"; then
137 create_device_map
138 rootdrive=`convert_device $rootdev`
139 bootdrive=`convert_device $bootdev`
140 create_boot_menu
141 if ! grub_install; then
142 gui_message "There was an error while installing GRUB."
147 while
149 if [ -s /boot/grub/device.map ] ; then
150 rootdrive=`convert_device $rootdev`
151 bootdrive=`convert_device $bootdev`
152 else
153 rootdrive='No Device Map found!'
154 bootdrive='No Device Map found!'
157 gui_menu grub 'GRUB Boot Loader Setup' \
158 '(Re-)Create GRUB Device Map' 'create_device_map' \
159 "Root Device ... $rootdev" "" \
160 "Boot Drive .... $bootdrive$boot_path" "" \
161 '' '' \
162 '(Re-)Create boot menu with installed kernels' 'create_boot_menu' \
163 '(Re-)Install GRUB in MBR of (hd0)' 'grub_install' \
164 '' '' \
165 "Edit /boot/grub/device.map (Device Map)" \
166 "gui_edit 'GRUB Device Map' /boot/grub/device.map" \
167 "Edit /boot/grub/menu.lst (Boot Menu)" \
168 "gui_edit 'GRUB Boot Menu' /boot/grub/menu.lst"
169 do : ; done