3 # Insert a list of installed kernels in a grub config file
4 # Copyright 2001 Wichert Akkerman <wichert@linux.com>
6 # This file is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # Jason Thomas <jason@debian.org>
22 # David B.Harris <dbarclay10@yahoo.ca>
23 # Marc Haber <mh@zugschlus.de>
24 # Crispin Flowerday <crispin@zeus.com>
29 host_os=`uname -s | tr '[A-Z]' '[a-z]'`
33 for d in $grub_dirs ; do
40 if [ -z "$grub_dir" ] ; then
51 # Autodetect current root device
53 if [ -f /etc/fstab ] ; then
54 while read DEV MNT FOO; do
55 if `echo "$DEV" | grep -q "^#"`; then
58 if [ "$MNT" = "$mount_point" ]; then
64 if [ -n "$device" ] ; then
67 device=`readlink -f "$(findfs $device)"`
70 device=`readlink -f "$device"`
80 device=$(find_device "/")
82 if [ -z "$device" ]; then
83 echo "Cannot determine root device. Assuming /dev/hda1" >&2
84 echo "This error is probably caused by an invalid /etc/fstab" >&2
91 # Usage: convert_raid1 os_device
92 # Checks if os_device is a software raid1.
93 # If so, converts to first physical device in array.
103 [ -x /sbin/mdadm ] || return 1
105 # Check that the raid device is raid1
106 raidlevel=$(mdadm -D -b $1 | grep "^ARRAY" | \
107 sed "s/^.*level=//" | cut -d" " -f1)
108 [ "$raidlevel" = "raid1" ] || return 1
110 # Take only the first device that makes up the raid
111 raiddev=$(mdadm -D $1 | grep -A1 "Number" | grep "dev" \
112 | sed "s/^.*\(\/dev\/.*\)$/\1/")
113 [ -n "$raiddev" ] || return 1
119 # Usage: convert os_device
120 # Convert an OS device to the corresponding GRUB drive.
121 # This part is OS-specific.
123 # First, check if the device file exists.
124 if test -e "$1"; then
127 echo "$1: Not found or not a block device." 1>&2
131 host_os=`uname -s | tr '[[:upper:]]' '[[:lower:]]'`
133 # Break the device name into the disk part and the partition part.
136 tmp_disk=`echo "$1" | sed -e 's%\([sh]d[[:lower:]]\)[0-9]*$%\1%' \
137 -e 's%\(fd[0-9]*\)$%\1%' \
138 -e 's%/part[0-9]*$%/disc%' \
139 -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
140 tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[[:lower:]]\([0-9]*\)$%\1%' \
141 -e 's%.*/fd[0-9]*$%%' \
142 -e 's%.*/floppy/[0-9]*$%%' \
143 -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
144 -e 's%.*c[0-7]d[0-9]*p*%%'`
147 tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
148 tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
150 tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%\1%' \
151 | sed 's%r\{0,1\}\(da[0-9]*\).*$%\1%'`
152 tmp_part=`echo "$1" \
153 | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
154 | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
157 tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
158 | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
159 tmp_part=`echo "$1" \
160 | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
163 echo "update-grub does not support your OS yet." 1>&2
167 # Get the drive name.
168 tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
169 | sed 's%.*\(([hf]d[0-9][a-z0-9,]*)\).*%\1%'`
171 # If not found, print an error message and exit.
172 if test "x$tmp_drive" = x; then
173 echo "$1 does not have any corresponding BIOS drive." 1>&2
177 if test "x$tmp_part" != x; then
178 # If a partition is specified, we need to translate it into the
182 echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
184 if echo $tmp_part | grep "^s" >/dev/null; then
185 tmp_pc_slice=`echo $tmp_part \
186 | sed "s%s\([0-9]*\)[a-z]*$%\1%"`
187 tmp_drive=`echo "$tmp_drive" \
188 | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
190 if echo $tmp_part | grep "[a-z]$" >/dev/null; then
191 tmp_bsd_partition=`echo "$tmp_part" \
192 | sed "s%[^a-z]*\([a-z]\)$%\1%"`
193 tmp_drive=`echo "$tmp_drive" \
194 | sed "s%)%,$tmp_bsd_partition)%"`
198 if echo $tmp_part | grep "^s" >/dev/null; then
199 tmp_pc_slice=`echo $tmp_part \
200 | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
201 tmp_drive=`echo "$tmp_drive" \
202 | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
204 if echo $tmp_part | grep "[a-h]$" >/dev/null; then
205 tmp_bsd_partition=`echo "$tmp_part" \
206 | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
207 tmp_drive=`echo "$tmp_drive" \
208 | sed "s%)%,$tmp_bsd_partition)%"`
212 if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
213 tmp_bsd_partition=`echo "$tmp_part" \
214 | sed "s%\([a-p]\)$%\1%"`
215 tmp_drive=`echo "$tmp_drive" \
216 | sed "s%)%,$tmp_bsd_partition)%"`
221 # If no partition is specified, just print the drive name.
226 # Usage: convert_default os_device
227 # Convert an OS device to the corresponding GRUB drive.
228 # Calls OS-specific convert, and returns a default of
229 # (hd0,0) if anything goes wrong
231 # Check if device is software raid1 array
232 if tmp_dev=$(convert_raid1 $1 2>/dev/null) ; then
233 : # Use device returned by convert_raid1
238 if tmp=$(convert $tmp_dev 2>/dev/null) ; then
245 ## Configuration Options
246 # directory's to look for the grub installation and the menu file
247 grub_dirs="/boot/grub /boot/boot/grub"
249 # The grub installation directory
250 grub_dir=$(find_grub_dir)
252 # Full path to the menu.lst
253 menu_file_basename=menu.lst
254 menu_file=$grub_dir/$menu_file_basename
256 # Full path to the default file
257 default_file_basename=default
258 default_file=$grub_dir/$default_file_basename
260 # the device for the / filesystem
261 root_device=$(find_root_device)
263 # the device for the /boot filesystem
264 boot_device=$(find_device "/boot")
266 # Full path to the device.map
267 device_map=$grub_dir/device.map
269 # Default kernel options, overidden by the kopt statement in the menufile.
270 kopt="root=$root_device ro"
273 title="Debian GNU/`uname -s | sed -e s,GNU/,,g`"
275 # should update-grub remember the default entry
276 updatedefaultentry="false"
278 # Drive(in GRUB terms) where the kernel is located. Overridden by the
279 # kopt statement in menufile.
280 # if we don't have a device.map then we can't use the convert function.
281 if test -f "$device_map" ; then
282 if test -z "$boot_device" ; then
283 grub_root_device=$(convert_default "$root_device")
285 grub_root_device=$(convert_default "$boot_device")
288 grub_root_device="(hd0,0)"