From e7d98e8f3aabd8ac906b73bfa2e301e38d490be8 Mon Sep 17 00:00:00 2001 From: Dinko Korunic Date: Mon, 18 May 2009 12:39:33 +0200 Subject: [PATCH] - grupa workaroundova oko potrganog groot i kopt parametra na nekim GRUB instalacijama: import GRUB funkcija u odvojenom fajlu, parsiranje menu.lst, popravljanje groot i kopt, itd. --- debian/dirs | 1 + debian/install | 1 + debian/postinst | 15 ++- debian/postrm | 2 +- debian/preinst | 40 ++++++++ grub-functions.sh | 289 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 346 insertions(+), 2 deletions(-) create mode 100644 debian/dirs create mode 100644 debian/install create mode 100755 debian/preinst create mode 100755 grub-functions.sh diff --git a/debian/dirs b/debian/dirs new file mode 100644 index 0000000..ba5e80a --- /dev/null +++ b/debian/dirs @@ -0,0 +1 @@ +usr/share/kernel-2.6-cn diff --git a/debian/install b/debian/install new file mode 100644 index 0000000..07aa51a --- /dev/null +++ b/debian/install @@ -0,0 +1 @@ +grub-functions.sh usr/share/kernel-2.6-cn diff --git a/debian/postinst b/debian/postinst index b3f9b68..e22b539 100755 --- a/debian/postinst +++ b/debian/postinst @@ -1,5 +1,5 @@ #!/bin/sh -# postinst script for spamassassin-cn +# postinst script for kernel-2.6-cn # # see: dh_installdeb(1) @@ -30,6 +30,9 @@ esac # import CN-functions . /usr/share/carnet-tools/functions.sh +# import GRUB helper functions +. /usr/share/kernel-2.6-cn/grub-functions.sh + ################################################################################ # starting up backup @@ -152,6 +155,16 @@ if ! grub-install --no-floppy '(hd0)' >/dev/null 2>&1; then fi echo -n " grub4" +# fix possible wrong params in menu.lst +cp_check_and_sed '^# groot=' \ + "s;^# groot=.*;# groot=$grub_root_device;g" \ + /boot/grub/menu.lst || true +cp_check_and_sed '^# kopt=' \ + "s;^# kopt=\(.*\)root=[^[:space:]]*\(.*\);# kopt=\1root=$root_device\2;g" \ + /boot/grub/menu.lst || true +/usr/sbin/update-grub >/dev/null 2>&1 || true +echo -n " grub5" + # restore execshield state if [ ! -z "$SHIELD" ]; then sysctl -w -e "kernel.exec-shield=$SHIELD" >/dev/null 2>&1 diff --git a/debian/postrm b/debian/postrm index ca2fee4..5c733a3 100755 --- a/debian/postrm +++ b/debian/postrm @@ -1,5 +1,5 @@ #!/bin/sh -# postrm script for bind9-cn +# postrm script for kernel-2.6-cn # # see: dh_installdeb(1) diff --git a/debian/preinst b/debian/preinst new file mode 100755 index 0000000..6cc6e1f --- /dev/null +++ b/debian/preinst @@ -0,0 +1,40 @@ +#!/bin/sh +# preinst script for kernel-2.6-cn +# +# see: dh_installdeb(1) + +set -e + +# summary of how this script can be called: +# * `install' +# * `install' +# * `upgrade' +# * `abort-upgrade' +# for details, see http://www.debian.org/doc/debian-policy/ or +# the debian-policy package + + +case "$1" in + install|upgrade) + ;; + + abort-upgrade) + ;; + + *) + echo "preinst called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# make GRUB template directory +if [ ! -d /boot/grub ]; then + mkdir -p /boot/grub +fi + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 diff --git a/grub-functions.sh b/grub-functions.sh new file mode 100755 index 0000000..feb2481 --- /dev/null +++ b/grub-functions.sh @@ -0,0 +1,289 @@ +#!/bin/bash +# +# Insert a list of installed kernels in a grub config file +# Copyright 2001 Wichert Akkerman +# +# 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 +# David B.Harris +# Marc Haber +# Crispin Flowerday + +# 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 -- 1.7.10.4