Bug#239111: grub and xfs

David Everly David Everly <deckrider@gmail.com>, 239111@bugs.debian.org
Mon, 25 Apr 2005 10:58:19 -0600


--wLAMOaPNJ0fu1fTG
Content-Type: multipart/mixed; boundary="xo44VMWPx7vlQ2+2"
Content-Disposition: inline


--xo44VMWPx7vlQ2+2
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

I have a Debian/Sarge installation (completely up to date as of today)
on which I can reproduce the hanging problem.  I have only one XFS
partition on which to boot and run Debian/Sarge.

Please note that I don't understand a lot of this, but I made some
changes to /sbin/grub-install and it seems to no longer hang.  However,
my system was fairly quiet when I did the test.

Also it would be better to somehow test if /boot/grub is on an XFS
filesystem, and if so, run xfs_magic.  But I don't know how to do this.

My test was to 'rm -r /boot/grub' and then run 'grub-install /dev/hda'.

Here is my version of grub-install which seems to work.

--=20
Encrypted Mail Preferred:
    Key ID:  8527B9AF
    Key Fingerprint:  E1B6 40B6 B73F 695E 0D3B  644E 6427 DD74 8527 B9AF
    Information:  http://www.gnupg.org/
                                                                           =
    =20
ASCII ribbon campaign:
()  against HTML email
/\  against Microsoft attachments
    Information:  http://www.expita.com/nomime.html

--xo44VMWPx7vlQ2+2
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=grub-install
Content-Transfer-Encoding: quoted-printable

#! /bin/sh

# Install GRUB on your drive.
#   Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, I=
nc.
#
# 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.

# Initialize some variables.
prefix=3D/
exec_prefix=3D${prefix}
sbindir=3D${exec_prefix}/sbin
libdir=3D${exec_prefix}/lib
PACKAGE=3Dgrub
VERSION=3D0.95
host_cpu=3Di386
host_os=3Dlinux-gnu
host_vendor=3Dpc
pkglibdir=3D${libdir}/${PACKAGE}/${host_cpu}-${host_vendor}

grub_shell=3D${sbindir}/grub
log_file=3D/tmp/grub-install.log.$$
img_file=3D/tmp/grub-install.img.$$
rootdir=3D
grub_prefix=3D/boot/grub

install_device=3D
no_floppy=3D
force_lba=3D
recheck=3Dno
debug=3Dno

# look for secure tempfile creation wrappers on this platform
if test -x /bin/tempfile; then
    mklog=3D"/bin/tempfile --prefix=3Dgrub"
    mkimg=3D"/bin/tempfile --prefix=3Dgrub"
elif test -x /bin/mktemp; then
    mklog=3D"/bin/mktemp /tmp/grub-install.log.XXXXXX"
    mkimg=3D"/bin/mktemp /tmp/grub-install.img.XXXXXX"
else
    mklog=3D""
    mkimg=3D""
fi

# Usage: usage
# Print the usage.
usage () {
    cat <<EOF
Usage: grub-install [OPTION] install_device
Install GRUB on your drive.

  -h, --help              print this message and exit
  -v, --version           print the version information and exit
  --root-directory=3DDIR    install GRUB images under the directory DIR
                          instead of the root directory
  --grub-shell=3DFILE       use FILE as the grub shell
  --no-floppy             do not probe any floppy drive
  --force-lba             force GRUB to use LBA mode even for a buggy
                          BIOS
  --recheck               probe a device map even if it already exists

INSTALL_DEVICE can be a GRUB device name or a system device filename.

grub-install copies GRUB images into the DIR/boot directory specfied by
--root-directory, and uses the grub shell to install grub into the boot
sector.

Report bugs to <bug-grub@gnu.org>.
EOF
}

xfs_magic () {
   sync
   if which xfs_freeze > /dev/null ; then
      xfs_freeze -f ${grubdir}
      sleep 1
      xfs_freeze -u ${grubdir}
   fi
}

# Usage: getraid_mdadm mddevice
# Routine to find a physical device from an md device
# If found, the first grub BIOS device (from device.map) is returned=20
# If no BIOS drives match the RAID devices, the first device returned
# from mdadm -D is returned
getraid_mdadm() {
	device=3D$1
	mdadm=3D$(mdadm -D "$device") || {
		echo "$PROG: mdadm -D $device failed" >&2
		exit 1
	}
	eval "$(
		echo "$mdadm" | awk '
			$1 =3D=3D "Number" && $2 =3D=3D "Major" { start =3D 1; next }
			$1 =3D=3D "UUID" { print "uuid=3D" $3; start =3D 0; next }
			!start { next }
			$2 =3D=3D 0 && $3 =3D=3D 0 { next }
			{ devices =3D devices "\n" $NF }
			END { print "devices=3D'\''" devices "'\''" }
		'
	)"

	# Convert RAID devices list into a list of disks
	tmp_disks=3D`echo "$devices" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
					 -e 's%\(d[0-9]*\)p[0-9]*$%\1%' \
					 -e 's%\(fd[0-9]*\)$%\1%' \
					 -e 's%/part[0-9]*$%/disc%' \
					 -e 's%\(c[0-7]d[0-9]*\).*$%\1%' \
					 -e '/^$/d' |
				     sed -n '1h;2,$H;${g;s/\n/|/g;p}'`

	# Find first BIOS disk that's a member of the RAID array
	# Default to first RAID member if no tmp_disks are BIOS devices
	set -- `egrep $tmp_disks $device_map | \
		sort | \
		sed -n 1p `
	device=3D${2:-${tmp_disks%%|*}}

	# Return first partition on BIOS disk that's part of the RAID
	echo "$devices" | \
		sed -n "\:${device}:p" | \
		sed -n 1p
}

# 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

    # Break the device name into the disk part and the partition part.
    case "$host_os" in
    linux*)
	# Find an actual physical device if we're passed a RAID device
	case $1 in
		/dev/md*)  set -- `getraid_mdadm $1`
	esac
	tmp_disk=3D`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
				  -e 's%\(d[0-9]*\)p[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=3D`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
				  -e 's%.*d[0-9]*p*%%' \
				  -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=3D`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
	tmp_part=3D`echo "$1" | sed "s%$tmp_disk%%"` ;;
    freebsd*)
	tmp_disk=3D`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%r\1%' \
			    | sed 's%r\{0,1\}\(da[0-9]*\).*$%r\1%'`
	tmp_part=3D`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*)
	tmp_disk=3D`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=3D`echo "$1" \
	    | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
	;;
    *)
	echo "grub-install does not support your OS yet." 1>&2
	exit 1 ;;
    esac

    # Get the drive name.
    tmp_drive=3D`grep -v '^#' $device_map | grep "$tmp_disk *$" \
	| sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`

    # If not found, print an error message and exit.
    if test "x$tmp_drive" =3D x; then
	echo "$1 does not have any corresponding BIOS drive." 1>&2
	exit 1
    fi

    if test "x$tmp_part" !=3D 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=3D`echo $tmp_part \
		    | sed "s%s\([0-9]*\)[a-g]*$%\1%"`
		tmp_drive=3D`echo "$tmp_drive" \
		    | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
	    fi
	    if echo $tmp_part | grep "[a-g]$" >/dev/null; then
		tmp_bsd_partition=3D`echo "$tmp_part" \
		    | sed "s%[^a-g]*\([a-g]\)$%\1%"`
		tmp_drive=3D`echo "$tmp_drive" \
		    | sed "s%)%,$tmp_bsd_partition)%"`
	    fi
	    echo "$tmp_drive" ;;
	freebsd*)
	    if echo $tmp_part | grep "^s" >/dev/null; then
		tmp_pc_slice=3D`echo $tmp_part \
		    | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
		tmp_drive=3D`echo "$tmp_drive" \
		    | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
	    fi
	    if echo $tmp_part | grep "[a-h]$" >/dev/null; then
		tmp_bsd_partition=3D`echo "$tmp_part" \
		    | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
		tmp_drive=3D`echo "$tmp_drive" \
		    | sed "s%)%,$tmp_bsd_partition)%"`
	    fi
	    echo "$tmp_drive" ;;
	netbsd*)
	    if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
		tmp_bsd_partition=3D`echo "$tmp_part" \
		    | sed "s%\([a-p]\)$%\1%"`
		tmp_drive=3D`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: resolve_symlink file
# Find the real file/device that file points at
resolve_symlink () {
	tmp_fname=3D$1
	# Resolve symlinks
	while test -L $tmp_fname; do
		tmp_new_fname=3D`ls -al $tmp_fname | sed -n 's%.*-> \(.*\)%\1%p'`
		if test -z "$tmp_new_fname"; then
			echo "Unrecognized ls output" 2>&1
			exit 1
		fi

		# Convert relative symlinks
		case $tmp_new_fname in
			/*) tmp_fname=3D"$tmp_new_fname"
			;;
			*) tmp_fname=3D"`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname"
			;;
		esac
	done
	echo "$tmp_fname"
}

# Usage: find_device file
# Find block device on which the file resides.
find_device () {
    # For now, this uses the program `df' to get the device name, but is
    # this really portable?
    tmp_fname=3D`df $1/ | sed -n 's%.*\(/dev/[^ 	]*\).*%\1%p'`

    if test -z "$tmp_fname"; then
	echo "Could not find device for $1" 2>&1
	exit 1
    fi

	tmp_fname=3D`resolve_symlink $tmp_fname`

    echo "$tmp_fname"
}

# Check the arguments.
for option in "$@"; do
    case "$option" in
    -h | --help)
	usage
	exit 0 ;;
    -v | --version)
	echo "grub-install (GNU GRUB ${VERSION})"
	exit 0 ;;
    --root-directory=3D*)
	rootdir=3D`echo "$option" | sed 's/--root-directory=3D//'` ;;
    --grub-shell=3D*)
	grub_shell=3D`echo "$option" | sed 's/--grub-shell=3D//'` ;;
    --no-floppy)
	no_floppy=3D"--no-floppy" ;;
    --force-lba)
	force_lba=3D"--force-lba" ;;
    --recheck)
	recheck=3Dyes ;;
    # This is an undocumented feature...
    --debug)
	debug=3Dyes ;;
    -*)
	echo "Unrecognized option \`$option'" 1>&2
	usage
	exit 1
	;;
    *)
	if test "x$install_device" !=3D x; then
	    echo "More than one install_devices?" 1>&2
	    usage
	    exit 1
	fi
	install_device=3D"${option}" ;;
    esac
done

if test "x$install_device" =3D x; then
    echo "install_device not specified." 1>&2
    usage
    exit 1
fi

# If the debugging feature is enabled, print commands.
if test $debug =3D yes; then
    set -x
fi

# Initialize these directories here, since ROOTDIR was initialized.
case "$host_os" in
netbsd*)
    # Because /boot is used for the boot block in NetBSD, use /grub
    # instead of /boot/grub.
    grub_prefix=3D/grub
    bootdir=3D${rootdir}
    ;;
*)
    # Use /boot/grub by default.
    bootdir=3D${rootdir}/boot
    ;;
esac

grubdir=3D${bootdir}/grub
device_map=3D${grubdir}/device.map

# Check if GRUB is installed.
# This is necessary, because the user can specify "grub --read-only".
set $grub_shell dummy
if test -f "$1"; then
    :
else
    echo "$1: Not found." 1>&2
    exit 1
fi

if test -f "$pkglibdir/stage1"; then
    :
else
    echo "${pkglibdir}/stage1: Not found." 1>&2
    exit 1
fi

if test -f "$pkglibdir/stage2"; then
    :
else
    echo "${pkglibdir}/stage2: Not found." 1>&2
    exit 1
fi

# Don't check for *stage1_5, because it is not fatal even if any
# Stage 1.5 does not exist.

# Create the GRUB directory if it is not present.
test -d "$bootdir" || mkdir "$bootdir" || exit 1
test -d "$grubdir" || mkdir "$grubdir" || exit 1

# If --recheck is specified, remove the device map, if present.
if test $recheck =3D yes; then
    rm -f $device_map
fi

# Create the device map file if it is not present.
if test -f "$device_map"; then
    :
else
    # Create a safe temporary file.
    test -n "$mklog" && log_file=3D`$mklog`

    xfs_magic

    $grub_shell --batch $no_floppy --device-map=3D$device_map <<EOF >$log_f=
ile
quit
EOF
    xfs_magic
    if grep "Error [0-9]*: " $log_file >/dev/null; then
	cat $log_file 1>&2
	exit 1
    fi

    rm -f $log_file
fi

# Make sure that there is no duplicated entry.
tmp=3D`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
    | sort | uniq -d | sed -n 1p`
if test -n "$tmp"; then
    echo "The drive $tmp is defined multiple times in the device map $devic=
e_map" 1>&2
    exit 1
fi

# Check for INSTALL_DEVICE.
case "$install_device" in
/dev/*)
    install_device=3D`resolve_symlink "$install_device"`
    install_drive=3D`convert "$install_device"`
    # I don't know why, but some shells wouldn't die if exit is
    # called in a function.
    if test "x$install_drive" =3D x; then
	exit 1
    fi ;;
\([hf]d[0-9]*\))
    install_drive=3D"$install_device" ;;
[hf]d[0-9]*)
    # The GRUB format with no parenthesis.
    install_drive=3D"($install_device)" ;;
*)
    echo "Format of install_device not recognized." 1>&2
    usage
    exit 1 ;;
esac

# Get the root drive.
root_device=3D`find_device ${rootdir}`
bootdir_device=3D`find_device ${bootdir}`

# Check if the boot directory is in the same device as the root directory.
if test "x$root_device" !=3D "x$bootdir_device"; then
    # Perhaps the user has a separate boot partition.
    root_device=3D$bootdir_device
    grub_prefix=3D"/grub"
fi

# Convert the root device to a GRUB drive.
root_drive=3D`convert "$root_device"`
if test "x$root_drive" =3D x; then
    exit 1
fi

# Check if the root directory exists in the same device as the grub
# directory.
grubdir_device=3D`find_device ${grubdir}`

if test "x$grubdir_device" !=3D "x$root_device"; then
    # For now, cannot deal with this situation.
    cat <<EOF 1>&2
You must set the root directory by the option --root-directory, because
$grubdir does not exist in the root device $root_device.
EOF
    exit 1
fi

# Copy the GRUB images to the GRUB directory.
for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
    rm -f $file || exit 1
done
for file in \
    ${pkglibdir}/stage1 ${pkglibdir}/stage2 ${pkglibdir}/*stage1_5; do
    cp -f $file ${grubdir} || exit 1
done

# Make sure that GRUB reads the same images as the host OS.
test -n "$mkimg" && img_file=3D`$mkimg`
test -n "$mklog" && log_file=3D`$mklog`

for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
    count=3D5
    tmp=3D`echo $file | sed "s|^${grubdir}|${grub_prefix}|"`
    while test $count -gt 0; do
	xfs_magic
	$grub_shell --batch $no_floppy --device-map=3D$device_map <<EOF >$log_file
dump ${root_drive}${tmp} ${img_file}
quit
EOF
	xfs_magic
	if grep "Error [0-9]*: " $log_file >/dev/null; then
	    :
	elif cmp $file $img_file >/dev/null; then
	    break
	fi
	count=3D`expr $count - 1`   =20
    done
    if test $count -eq 0; then
	echo "The file $file not read correctly." 1>&2
	exit 1
    fi
done

rm -f $img_file
rm -f $log_file

# Create a safe temporary file.
test -n "$mklog" && log_file=3D`$mklog`

# Sync to prevent GRUB from not finding stage files (notably, on XFS)
sync

xfs_magic

# Now perform the installation.
$grub_shell --batch $no_floppy --device-map=3D$device_map <<EOF >$log_file
root $root_drive
setup $force_lba --stage2=3D$grubdir/stage2 --prefix=3D$grub_prefix $instal=
l_drive
quit
EOF

xfs_magic

if grep "Error [0-9]*: " $log_file >/dev/null || test $debug =3D yes; then
    cat $log_file 1>&2
    exit 1
fi

rm -f $log_file

# Prompt the user to check if the device map is correct.
echo "Installation finished. No error reported."
echo "This is the contents of the device map $device_map."
echo "Check if this is correct or not. If any of the lines is incorrect,"
echo "fix it and re-run the script \`grub-install'."
echo

cat $device_map

# Bye.
exit 0

--xo44VMWPx7vlQ2+2--

--wLAMOaPNJ0fu1fTG
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFCbSGqZCfddIUnua8RAjrIAKCOADfC6ckwjPTUKlI1RzeR6nsFWACfUboG
SfLynkFzY0zhtJHau9yXd6A=
=7sKo
-----END PGP SIGNATURE-----

--wLAMOaPNJ0fu1fTG--