- grupa workaroundova oko potrganog groot i kopt parametra na nekim GRUB instalacijam...
[kernel-cn.git] / grub-functions.sh
1 #!/bin/bash
2 #
3 # Insert a list of installed kernels in a grub config file
4 #   Copyright 2001 Wichert Akkerman <wichert@linux.com>
5 #
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.
10 #
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.
15 #
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.
19 #
20 # Contributors:
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>
25
26 # Abort on errors
27 set -e
28
29 host_os=`uname -s | tr '[A-Z]' '[a-z]'`
30
31 find_grub_dir ()
32 {
33         for d in $grub_dirs ; do
34                 if [ -d "$d" ] ; then
35                         grub_dir="$d"
36                         break
37                 fi
38         done
39         
40         if [ -z "$grub_dir" ] ; then
41         grub_dir="/boot/grub"
42         fi
43
44         echo $grub_dir
45 }
46
47 find_device ()
48 {
49         mount_point=$1
50
51         # Autodetect current root device
52         device=
53         if [ -f /etc/fstab ] ; then
54                 while read DEV MNT FOO; do
55                         if `echo "$DEV" | grep -q "^#"`; then
56                                 continue
57                         fi
58                         if [ "$MNT" = "$mount_point" ]; then
59                                 device="$DEV";
60                         fi
61                 done < /etc/fstab
62         fi
63
64         if [ -n "$device" ] ; then
65                 case "$device" in
66                         LABEL=* | UUID=*)
67                                 device=`readlink -f "$(findfs $device)"`
68                         ;;
69                         *)
70                                 device=`readlink -f "$device"`
71                         ;;
72                 esac
73         fi
74
75         echo $device
76 }
77
78 find_root_device ()
79 {
80         device=$(find_device "/")
81
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
85                 device=/dev/hda1
86         fi
87
88         echo $device
89 }
90
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.
94 convert_raid1 ()
95 {
96     case $1 in
97         /dev/md[0-9])
98             : ;; # Continue
99         *)
100             return 1 ;;
101     esac
102
103     [ -x /sbin/mdadm ] || return 1
104
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
109     
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
114
115     echo $raiddev
116     return 0
117 }
118
119 # Usage: convert os_device
120 # Convert an OS device to the corresponding GRUB drive.
121 # This part is OS-specific.
122 convert () {
123     # First, check if the device file exists.
124     if test -e "$1"; then
125                 :
126     else
127                 echo "$1: Not found or not a block device." 1>&2
128                 exit 1
129     fi
130
131         host_os=`uname -s | tr '[[:upper:]]' '[[:lower:]]'`
132
133     # Break the device name into the disk part and the partition part.
134     case "$host_os" in
135     linux)
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*%%'`
145         ;;
146     gnu)
147                 tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
148                 tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
149     freebsd|*/kfreebsd)
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%"`
155         ;;
156     netbsd|*/knetbsd)
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%"`
161         ;;
162     *)
163                 echo "update-grub does not support your OS yet." 1>&2
164                 exit 1 ;;
165     esac
166
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%'`
170
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
174                 exit 1
175     fi
176
177     if test "x$tmp_part" != x; then
178                 # If a partition is specified, we need to translate it into the
179                 # GRUB's syntax.
180                 case "$host_os" in
181                 linux)
182                   echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
183                 gnu)
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\`)%"`
189                   fi
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)%"`
195                   fi
196                   echo "$tmp_drive" ;;
197                 freebsd|*/kfreebsd)
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\`)%"`
203                   fi
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)%"`
209                   fi
210                   echo "$tmp_drive" ;;
211                 netbsd|*/knetbsd)
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)%"`
217                   fi
218                   echo "$tmp_drive" ;;
219                 esac
220     else
221                 # If no partition is specified, just print the drive name.
222                 echo "$tmp_drive"
223     fi
224 }
225
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
230 convert_default () {
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
234         else
235                 tmp_dev=$1
236         fi
237
238         if tmp=$(convert $tmp_dev 2>/dev/null) ; then
239                 echo $tmp
240         else
241                 echo "(hd0,0)"
242         fi
243 }
244
245 ## Configuration Options
246 # directory's to look for the grub installation and the menu file
247 grub_dirs="/boot/grub /boot/boot/grub"
248
249 # The grub installation directory
250 grub_dir=$(find_grub_dir)
251
252 # Full path to the menu.lst
253 menu_file_basename=menu.lst
254 menu_file=$grub_dir/$menu_file_basename
255
256 # Full path to the default file
257 default_file_basename=default
258 default_file=$grub_dir/$default_file_basename
259
260 # the device for the / filesystem
261 root_device=$(find_root_device)
262
263 # the device for the /boot filesystem
264 boot_device=$(find_device "/boot")
265
266 # Full path to the device.map
267 device_map=$grub_dir/device.map
268
269 # Default kernel options, overidden by the kopt statement in the menufile.
270 kopt="root=$root_device ro"
271
272 # Title
273 title="Debian GNU/`uname -s | sed -e s,GNU/,,g`"
274
275 # should update-grub remember the default entry
276 updatedefaultentry="false"
277
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")
284         else
285                 grub_root_device=$(convert_default "$boot_device")
286         fi
287 else
288         grub_root_device="(hd0,0)"
289 fi