- grupa workaroundova oko potrganog groot i kopt parametra na nekim GRUB instalacijam...
[kernel-cn.git] / grub-functions.sh
diff --git a/grub-functions.sh b/grub-functions.sh
new file mode 100755 (executable)
index 0000000..feb2481
--- /dev/null
@@ -0,0 +1,289 @@
+#!/bin/bash
+#
+# Insert a list of installed kernels in a grub config file
+#   Copyright 2001 Wichert Akkerman <wichert@linux.com>
+#
+# This file is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# Contributors:
+#      Jason Thomas <jason@debian.org>
+#      David B.Harris <dbarclay10@yahoo.ca>
+#      Marc Haber <mh@zugschlus.de>
+#      Crispin Flowerday <crispin@zeus.com>
+
+# Abort on errors
+set -e
+
+host_os=`uname -s | tr '[A-Z]' '[a-z]'`
+
+find_grub_dir ()
+{
+       for d in $grub_dirs ; do
+               if [ -d "$d" ] ; then
+                       grub_dir="$d"
+                       break
+               fi
+       done
+       
+       if [ -z "$grub_dir" ] ; then
+        grub_dir="/boot/grub"
+       fi
+
+       echo $grub_dir
+}
+
+find_device ()
+{
+       mount_point=$1
+
+       # Autodetect current root device
+       device=
+       if [ -f /etc/fstab ] ; then
+               while read DEV MNT FOO; do
+                       if `echo "$DEV" | grep -q "^#"`; then
+                               continue
+                       fi
+                       if [ "$MNT" = "$mount_point" ]; then
+                               device="$DEV";
+                       fi
+               done < /etc/fstab
+       fi
+
+       if [ -n "$device" ] ; then
+               case "$device" in
+                       LABEL=* | UUID=*)
+                               device=`readlink -f "$(findfs $device)"`
+                       ;;
+                       *)
+                               device=`readlink -f "$device"`
+                       ;;
+               esac
+       fi
+
+       echo $device
+}
+
+find_root_device ()
+{
+       device=$(find_device "/")
+
+       if [ -z "$device" ]; then
+               echo "Cannot determine root device.  Assuming /dev/hda1" >&2
+               echo "This error is probably caused by an invalid /etc/fstab" >&2
+               device=/dev/hda1
+       fi
+
+       echo $device
+}
+
+# Usage: convert_raid1 os_device
+# Checks if os_device is a software raid1.
+# If so, converts to first physical device in array.
+convert_raid1 ()
+{
+    case $1 in
+        /dev/md[0-9])
+            : ;; # Continue
+        *)
+            return 1 ;;
+    esac
+
+    [ -x /sbin/mdadm ] || return 1
+
+    # Check that the raid device is raid1
+    raidlevel=$(mdadm -D -b $1 | grep "^ARRAY" | \
+            sed "s/^.*level=//" | cut -d" " -f1)
+    [ "$raidlevel" = "raid1" ] || return 1
+    
+    # Take only the first device that makes up the raid
+    raiddev=$(mdadm -D $1 | grep -A1 "Number" | grep "dev" \
+                          | sed "s/^.*\(\/dev\/.*\)$/\1/")
+    [ -n "$raiddev" ] || return 1
+
+    echo $raiddev
+    return 0
+}
+
+# Usage: convert os_device
+# Convert an OS device to the corresponding GRUB drive.
+# This part is OS-specific.
+convert () {
+    # First, check if the device file exists.
+    if test -e "$1"; then
+               :
+    else
+               echo "$1: Not found or not a block device." 1>&2
+               exit 1
+    fi
+
+       host_os=`uname -s | tr '[[:upper:]]' '[[:lower:]]'`
+
+    # Break the device name into the disk part and the partition part.
+    case "$host_os" in
+    linux)
+               tmp_disk=`echo "$1" | sed -e 's%\([sh]d[[:lower:]]\)[0-9]*$%\1%' \
+                                 -e 's%\(fd[0-9]*\)$%\1%' \
+                                 -e 's%/part[0-9]*$%/disc%' \
+                                 -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
+               tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[[:lower:]]\([0-9]*\)$%\1%' \
+                                 -e 's%.*/fd[0-9]*$%%' \
+                                 -e 's%.*/floppy/[0-9]*$%%' \
+                                 -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
+                                 -e 's%.*c[0-7]d[0-9]*p*%%'`
+       ;;
+    gnu)
+               tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
+               tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
+    freebsd|*/kfreebsd)
+               tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%\1%' \
+                           | sed 's%r\{0,1\}\(da[0-9]*\).*$%\1%'`
+               tmp_part=`echo "$1" \
+                       | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
+                       | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
+       ;;
+    netbsd|*/knetbsd)
+               tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
+                       | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
+               tmp_part=`echo "$1" \
+                       | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
+       ;;
+    *)
+               echo "update-grub does not support your OS yet." 1>&2
+               exit 1 ;;
+    esac
+
+    # Get the drive name.
+    tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
+                       | sed 's%.*\(([hf]d[0-9][a-z0-9,]*)\).*%\1%'`
+
+    # If not found, print an error message and exit.
+    if test "x$tmp_drive" = x; then
+               echo "$1 does not have any corresponding BIOS drive." 1>&2
+               exit 1
+    fi
+
+    if test "x$tmp_part" != x; then
+               # If a partition is specified, we need to translate it into the
+               # GRUB's syntax.
+               case "$host_os" in
+               linux)
+                 echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
+               gnu)
+                 if echo $tmp_part | grep "^s" >/dev/null; then
+                               tmp_pc_slice=`echo $tmp_part \
+                               | sed "s%s\([0-9]*\)[a-z]*$%\1%"`
+                               tmp_drive=`echo "$tmp_drive" \
+                               | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
+                 fi
+                 if echo $tmp_part | grep "[a-z]$" >/dev/null; then
+                               tmp_bsd_partition=`echo "$tmp_part" \
+                               | sed "s%[^a-z]*\([a-z]\)$%\1%"`
+                               tmp_drive=`echo "$tmp_drive" \
+                               | sed "s%)%,$tmp_bsd_partition)%"`
+                 fi
+                 echo "$tmp_drive" ;;
+               freebsd|*/kfreebsd)
+                 if echo $tmp_part | grep "^s" >/dev/null; then
+                               tmp_pc_slice=`echo $tmp_part \
+                               | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
+                               tmp_drive=`echo "$tmp_drive" \
+                               | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
+                 fi
+                 if echo $tmp_part | grep "[a-h]$" >/dev/null; then
+                               tmp_bsd_partition=`echo "$tmp_part" \
+                               | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
+                               tmp_drive=`echo "$tmp_drive" \
+                               | sed "s%)%,$tmp_bsd_partition)%"`
+                 fi
+                 echo "$tmp_drive" ;;
+               netbsd|*/knetbsd)
+                 if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
+                               tmp_bsd_partition=`echo "$tmp_part" \
+                               | sed "s%\([a-p]\)$%\1%"`
+                               tmp_drive=`echo "$tmp_drive" \
+                               | sed "s%)%,$tmp_bsd_partition)%"`
+                 fi
+                 echo "$tmp_drive" ;;
+               esac
+    else
+               # If no partition is specified, just print the drive name.
+               echo "$tmp_drive"
+    fi
+}
+
+# Usage: convert_default os_device
+# Convert an OS device to the corresponding GRUB drive.
+# Calls OS-specific convert, and returns a default of
+# (hd0,0) if anything goes wrong
+convert_default () {
+       # Check if device is software raid1 array
+       if tmp_dev=$(convert_raid1 $1 2>/dev/null) ; then
+               : # Use device returned by convert_raid1
+       else
+               tmp_dev=$1
+       fi
+
+       if tmp=$(convert $tmp_dev 2>/dev/null) ; then
+               echo $tmp
+       else
+               echo "(hd0,0)"
+       fi
+}
+
+## Configuration Options
+# directory's to look for the grub installation and the menu file
+grub_dirs="/boot/grub /boot/boot/grub"
+
+# The grub installation directory
+grub_dir=$(find_grub_dir)
+
+# Full path to the menu.lst
+menu_file_basename=menu.lst
+menu_file=$grub_dir/$menu_file_basename
+
+# Full path to the default file
+default_file_basename=default
+default_file=$grub_dir/$default_file_basename
+
+# the device for the / filesystem
+root_device=$(find_root_device)
+
+# the device for the /boot filesystem
+boot_device=$(find_device "/boot")
+
+# Full path to the device.map
+device_map=$grub_dir/device.map
+
+# Default kernel options, overidden by the kopt statement in the menufile.
+kopt="root=$root_device ro"
+
+# Title
+title="Debian GNU/`uname -s | sed -e s,GNU/,,g`"
+
+# should update-grub remember the default entry
+updatedefaultentry="false"
+
+# Drive(in GRUB terms) where the kernel is located. Overridden by the
+# kopt statement in menufile.
+# if we don't have a device.map then we can't use the convert function.
+if test -f "$device_map" ; then
+       if test -z "$boot_device" ; then
+               grub_root_device=$(convert_default "$root_device")
+       else
+               grub_root_device=$(convert_default "$boot_device")
+       fi
+else
+       grub_root_device="(hd0,0)"
+fi