home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / grub-install < prev    next >
Encoding:
Text File  |  2007-04-10  |  15.0 KB  |  557 lines

  1. #! /bin/sh
  2.  
  3. # Install GRUB on your drive.
  4. #   Copyright (C) 1999,2000,2001,2002,2003,2004 Free Software Foundation, Inc.
  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. # Initialize some variables.
  21. prefix=/usr
  22. exec_prefix=${prefix}
  23. sbindir=${exec_prefix}/sbin
  24. libdir=${exec_prefix}/lib
  25. PACKAGE=grub
  26. VERSION=0.97
  27. host_cpu=i386
  28. host_os=linux-gnu
  29. host_vendor=pc
  30. pkglibdir=${libdir}/${PACKAGE}/${host_cpu}-${host_vendor}
  31.  
  32. grub_shell=${sbindir}/grub
  33. grub_set_default=${sbindir}/grub-set-default
  34. log_file=/tmp/grub-install.log.$$
  35. img_file=/tmp/grub-install.img.$$
  36. rootdir=
  37. grub_prefix=/boot/grub
  38.  
  39. install_device=
  40. no_floppy=
  41. force_lba=
  42. recheck=no
  43. debug=no
  44.  
  45. # look for secure tempfile creation wrappers on this platform
  46. if test -x /bin/tempfile; then
  47.     mklog="/bin/tempfile --prefix=grub"
  48.     mkimg="/bin/tempfile --prefix=grub"
  49. elif test -x /bin/mktemp; then
  50.     mklog="/bin/mktemp /tmp/grub-install.log.XXXXXX"
  51.     mkimg="/bin/mktemp /tmp/grub-install.img.XXXXXX"
  52. else
  53.     mklog=""
  54.     mkimg=""
  55. fi
  56.  
  57. # Usage: usage
  58. # Print the usage.
  59. usage () {
  60.     cat <<EOF
  61. Usage: grub-install [OPTION] install_device
  62. Install GRUB on your drive.
  63.  
  64.   -h, --help              print this message and exit
  65.   -v, --version           print the version information and exit
  66.   --root-directory=DIR    install GRUB images under the directory DIR
  67.                           instead of the root directory
  68.   --grub-shell=FILE       use FILE as the grub shell
  69.   --no-floppy             do not probe any floppy drive
  70.   --force-lba             force GRUB to use LBA mode even for a buggy
  71.                           BIOS
  72.   --recheck               probe a device map even if it already exists
  73.  
  74. INSTALL_DEVICE can be a GRUB device name or a system device filename.
  75.  
  76. grub-install copies GRUB images into the DIR/boot directory specfied by
  77. --root-directory, and uses the grub shell to install grub into the boot
  78. sector.
  79.  
  80. Report bugs to <bug-grub@gnu.org>.
  81. EOF
  82. }
  83.  
  84. # Usage: getraid_mdadm mddevice
  85. # Routine to find a physical device from an md device
  86. # If found, the first grub BIOS device (from device.map) is returned 
  87. # If no BIOS drives match the RAID devices, the first device returned
  88. # from mdadm -D is returned
  89. getraid_mdadm() {
  90.     device=$1
  91.     mdadm=$(mdadm -D "$device") || {
  92.         echo "$PROG: mdadm -D $device failed" >&2
  93.         exit 1
  94.     }
  95.     eval "$(
  96.         echo "$mdadm" | awk '
  97.             $1 == "Number" && $2 == "Major" { start = 1; next }
  98.             $1 == "UUID" { print "uuid=" $3; start = 0; next }
  99.             !start { next }
  100.             $2 == 0 && $3 == 0 { next }
  101.             { devices = devices "\n" $NF }
  102.             END { print "devices='\''" devices "'\''" }
  103.         '
  104.     )"
  105.  
  106.     # Convert RAID devices list into a list of disks
  107.     tmp_disks=`echo "$devices" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
  108.                      -e 's%\(d[0-9]*\)p[0-9]*$%\1%' \
  109.                      -e 's%\(fd[0-9]*\)$%\1%' \
  110.                      -e 's%/part[0-9]*$%/disc%' \
  111.                      -e 's%\(c[0-7]d[0-9]*\).*$%\1%' \
  112.                      -e '/^$/d' |
  113.                      sed -n '1h;2,$H;${g;s/\n/|/g;p}'`
  114.  
  115.     # Find first BIOS disk that's a member of the RAID array
  116.     # Default to first RAID member if no tmp_disks are BIOS devices
  117.     set -- `egrep $tmp_disks $device_map | \
  118.         sort | \
  119.         sed -n 1p `
  120.     device=${2:-${tmp_disks%%|*}}
  121.  
  122.     # Return first partition on BIOS disk that's part of the RAID
  123.     echo "$devices" | \
  124.         sed -n "\:${device}:p" | \
  125.         sed -n 1p
  126. }
  127.  
  128. # Usage: convert os_device
  129. # Convert an OS device to the corresponding GRUB drive.
  130. # This part is OS-specific.
  131. convert () {
  132.     # First, check if the device file exists.
  133.     if test -e "$1"; then
  134.     :
  135.     else
  136.     echo "$1: Not found or not a block device." 1>&2
  137.     exit 1
  138.     fi
  139.  
  140.     # Break the device name into the disk part and the partition part.
  141.     case "$host_os" in
  142.     linux*)
  143.     # Find an actual physical device if we're passed a RAID device
  144.     case $1 in
  145.         /dev/md*)  set -- `getraid_mdadm $1`
  146.     esac
  147.     tmp_disk=`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
  148.                   -e 's%\(d[0-9]*\)p[0-9]*$%\1%' \
  149.                   -e 's%\(fd[0-9]*\)$%\1%' \
  150.                   -e 's%/part[0-9]*$%/disc%' \
  151.                   -e 's%\(c[0-7]d[0-9]*\).*$%\1%' \
  152.                   -e 's%\(e[0-9]\.[0-9]*\).*$%\1%'`
  153.     tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
  154.                   -e 's%.*d[0-9]*p%%' \
  155.                   -e 's%.*/fd[0-9]*$%%' \
  156.                   -e 's%.*/floppy/[0-9]*$%%' \
  157.                   -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
  158.                   -e 's%.*c[0-7]d[0-9]*p%%' \
  159.                   -e 's%.*e[0-9]\.[0-9]*p%%' \
  160.                   -e 's%.*e[0-9]\.[0-9]*\$%%'`
  161.     ;;
  162.     gnu*)
  163.     tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
  164.     tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
  165.     freebsd* | kfreebsd*-gnu)
  166.     tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%\1%' \
  167.                 | sed 's%r\{0,1\}\(da[0-9]*\).*$%\1%'`
  168.     tmp_part=`echo "$1" \
  169.         | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
  170.                | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
  171.     ;;
  172.     netbsd* | knetbsd*-gnu)
  173.     tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
  174.         | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
  175.     tmp_part=`echo "$1" \
  176.         | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
  177.     ;;
  178.     *)
  179.     echo "grub-install does not support your OS yet." 1>&2
  180.     exit 1 ;;
  181.     esac
  182.  
  183.     # Get the drive name.
  184.     tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
  185.     | sed 's%.*\(([hf]d[0-9][a-z0-9,]*)\).*%\1%'`
  186.  
  187.     # If not found, print an error message and exit.
  188.     if test "x$tmp_drive" = x; then
  189.     echo "$1 does not have any corresponding BIOS drive." 1>&2
  190.     exit 1
  191.     fi
  192.  
  193.     if test "x$tmp_part" != x; then
  194.     # If a partition is specified, we need to translate it into the
  195.     # GRUB's syntax.
  196.     case "$host_os" in
  197.     linux*)
  198.         echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
  199.     gnu*)
  200.         if echo $tmp_part | grep "^s" >/dev/null; then
  201.         tmp_pc_slice=`echo $tmp_part \
  202.             | sed "s%s\([0-9]*\)[a-z]*$%\1%"`
  203.         tmp_drive=`echo "$tmp_drive" \
  204.             | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
  205.         fi
  206.         if echo $tmp_part | grep "[a-z]$" >/dev/null; then
  207.         tmp_bsd_partition=`echo "$tmp_part" \
  208.             | sed "s%[^a-z]*\([a-z]\)$%\1%"`
  209.         tmp_drive=`echo "$tmp_drive" \
  210.             | sed "s%)%,$tmp_bsd_partition)%"`
  211.         fi
  212.         echo "$tmp_drive" ;;
  213.     freebsd* | kfreebsd*-gnu)
  214.         if echo $tmp_part | grep "^s" >/dev/null; then
  215.         tmp_pc_slice=`echo $tmp_part \
  216.             | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
  217.         tmp_drive=`echo "$tmp_drive" \
  218.             | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
  219.         fi
  220.         if echo $tmp_part | grep "[a-h]$" >/dev/null; then
  221.         tmp_bsd_partition=`echo "$tmp_part" \
  222.             | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
  223.         tmp_drive=`echo "$tmp_drive" \
  224.             | sed "s%)%,$tmp_bsd_partition)%"`
  225.         fi
  226.         echo "$tmp_drive" ;;
  227.     netbsd* | knetbsd*-gnu)
  228.         if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
  229.         tmp_bsd_partition=`echo "$tmp_part" \
  230.             | sed "s%\([a-p]\)$%\1%"`
  231.         tmp_drive=`echo "$tmp_drive" \
  232.             | sed "s%)%,$tmp_bsd_partition)%"`
  233.         fi
  234.         echo "$tmp_drive" ;;
  235.     esac
  236.     else
  237.     # If no partition is specified, just print the drive name.
  238.     echo "$tmp_drive"
  239.     fi
  240. }
  241.  
  242. # Usage: resolve_symlink file
  243. # Find the real file/device that file points at
  244. resolve_symlink () {
  245.     tmp_fname=$1
  246.     # Resolve symlinks
  247.     while test -L $tmp_fname; do
  248.         tmp_new_fname=`ls -al $tmp_fname | sed -n 's%.*-> \(.*\)%\1%p'`
  249.         if test -z "$tmp_new_fname"; then
  250.             echo "Unrecognized ls output" 2>&1
  251.             exit 1
  252.         fi
  253.  
  254.         # Convert relative symlinks
  255.         case $tmp_new_fname in
  256.             /*) tmp_fname="$tmp_new_fname"
  257.             ;;
  258.             *) tmp_fname="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname"
  259.             ;;
  260.         esac
  261.     done
  262.     echo "$tmp_fname"
  263. }
  264.  
  265. # Usage: find_device file
  266. # Find block device on which the file resides.
  267. find_device () {
  268.     # For now, this uses the program `df' to get the device name, but is
  269.     # this really portable?
  270.     tmp_fname=`df $1/ | sed -n 's%.*\(/dev/[^     ]*\).*%\1%p'`
  271.  
  272.     if test -z "$tmp_fname"; then
  273.     echo "Could not find device for $1" 2>&1
  274.     exit 1
  275.     fi
  276.  
  277.     tmp_fname=`resolve_symlink $tmp_fname`
  278.  
  279.     echo "$tmp_fname"
  280. }
  281.  
  282. # Check the arguments.
  283. for option in "$@"; do
  284.     case "$option" in
  285.     -h | --help)
  286.     usage
  287.     exit 0 ;;
  288.     -v | --version)
  289.     echo "grub-install (GNU GRUB ${VERSION})"
  290.     exit 0 ;;
  291.     --root-directory=*)
  292.     rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
  293.     --grub-shell=*)
  294.     grub_shell=`echo "$option" | sed 's/--grub-shell=//'` ;;
  295.     --no-floppy)
  296.     no_floppy="--no-floppy" ;;
  297.     --force-lba)
  298.     force_lba="--force-lba" ;;
  299.     --recheck)
  300.     recheck=yes ;;
  301.     # This is an undocumented feature...
  302.     --debug)
  303.     debug=yes ;;
  304.     -*)
  305.     echo "Unrecognized option \`$option'" 1>&2
  306.     usage
  307.     exit 1
  308.     ;;
  309.     *)
  310.     if test "x$install_device" != x; then
  311.         echo "More than one install_devices?" 1>&2
  312.         usage
  313.         exit 1
  314.     fi
  315.     install_device="${option}" ;;
  316.     esac
  317. done
  318.  
  319. if test "x$install_device" = x; then
  320.     echo "install_device not specified." 1>&2
  321.     usage
  322.     exit 1
  323. fi
  324.  
  325. # If the debugging feature is enabled, print commands.
  326. if test $debug = yes; then
  327.     set -x
  328. fi
  329.  
  330. # Initialize these directories here, since ROOTDIR was initialized.
  331. case "$host_os" in
  332. netbsd* | openbsd*)
  333.     # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
  334.     # instead of /boot/grub.
  335.     grub_prefix=/grub
  336.     bootdir=${rootdir}
  337.     ;;
  338. *)
  339.     # Use /boot/grub by default.
  340.     bootdir=${rootdir}/boot
  341.     ;;
  342. esac
  343.  
  344. grubdir=${bootdir}/grub
  345. device_map=${grubdir}/device.map
  346.  
  347. # Check if GRUB is installed.
  348. # This is necessary, because the user can specify "grub --read-only".
  349. set $grub_shell dummy
  350. if test -f "$1"; then
  351.     :
  352. else
  353.     echo "$1: Not found." 1>&2
  354.     exit 1
  355. fi
  356.  
  357. if test -f "$pkglibdir/stage1"; then
  358.     :
  359. else
  360.     echo "${pkglibdir}/stage1: Not found." 1>&2
  361.     exit 1
  362. fi
  363.  
  364. if test -f "$pkglibdir/stage2"; then
  365.     :
  366. else
  367.     echo "${pkglibdir}/stage2: Not found." 1>&2
  368.     exit 1
  369. fi
  370.  
  371. # Don't check for *stage1_5, because it is not fatal even if any
  372. # Stage 1.5 does not exist.
  373.  
  374. # Create the GRUB directory if it is not present.
  375. test -d "$bootdir" || mkdir "$bootdir" || exit 1
  376. test -d "$grubdir" || mkdir "$grubdir" || exit 1
  377.  
  378. # If --recheck is specified, remove the device map, if present.
  379. if test $recheck = yes; then
  380.     rm -f $device_map
  381. fi
  382.  
  383. # Create the device map file if it is not present.
  384. if test -f "$device_map"; then
  385.     :
  386. else
  387.     # Create a safe temporary file.
  388.     test -n "$mklog" && log_file=`$mklog`
  389.  
  390.     # Before all invocations of the grub shell, call sync to make sure
  391.     # the raw device is in sync with any bufferring in filesystems.
  392.     sync
  393.  
  394.     $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
  395. quit
  396. EOF
  397.     if grep "Error [0-9]*: " $log_file >/dev/null; then
  398.     cat $log_file 1>&2
  399.     exit 1
  400.     fi
  401.  
  402.     rm -f $log_file
  403. fi
  404.  
  405. # Make sure that there is no duplicated entry.
  406. tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
  407.     | sort | uniq -d | sed -n 1p`
  408. if test -n "$tmp"; then
  409.     echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
  410.     exit 1
  411. fi
  412.  
  413. # Check for INSTALL_DEVICE.
  414. case "$install_device" in
  415. /dev/*)
  416.     install_device=`resolve_symlink "$install_device"`
  417.     install_drive=`convert "$install_device"`
  418.     # I don't know why, but some shells wouldn't die if exit is
  419.     # called in a function.
  420.     if test "x$install_drive" = x; then
  421.     exit 1
  422.     fi ;;
  423. \([hf]d[0-9]*\))
  424.     install_drive="$install_device" ;;
  425. [hf]d[0-9]*)
  426.     # The GRUB format with no parenthesis.
  427.     install_drive="($install_device)" ;;
  428. *)
  429.     echo "Format of install_device not recognized." 1>&2
  430.     usage
  431.     exit 1 ;;
  432. esac
  433.  
  434. # Get the root drive.
  435. root_device=`find_device ${rootdir}`
  436. bootdir_device=`find_device ${bootdir}`
  437.  
  438. # Check if the boot directory is in the same device as the root directory.
  439. if test "x$root_device" != "x$bootdir_device"; then
  440.     # Perhaps the user has a separate boot partition.
  441.     root_device=$bootdir_device
  442.     grub_prefix="/grub"
  443. fi
  444.  
  445. # Convert the root device to a GRUB drive.
  446. root_drive=`convert "$root_device"`
  447. if test "x$root_drive" = x; then
  448.     exit 1
  449. fi
  450.  
  451. # Check if the root directory exists in the same device as the grub
  452. # directory.
  453. grubdir_device=`find_device ${grubdir}`
  454.  
  455. if test "x$grubdir_device" != "x$root_device"; then
  456.     # For now, cannot deal with this situation.
  457.     cat <<EOF 1>&2
  458. You must set the root directory by the option --root-directory, because
  459. $grubdir does not exist in the root device $root_device.
  460. EOF
  461.     exit 1
  462. fi
  463.  
  464. # Copy the GRUB images to the GRUB directory.
  465. for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
  466.     rm -f $file || exit 1
  467. done
  468. for file in \
  469.     ${pkglibdir}/stage1 ${pkglibdir}/stage2 ${pkglibdir}/*stage1_5; do
  470.     cp -f $file ${grubdir} || exit 1
  471. done
  472.  
  473. # Make a default file.
  474. ${grub_set_default} --root-directory=${rootdir} default
  475.  
  476. # Make sure that GRUB reads the same images as the host OS.
  477. test -n "$mkimg" && img_file=`$mkimg`
  478. test -n "$mklog" && log_file=`$mklog`
  479.  
  480. for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
  481.     count=5
  482.     tmp=`echo $file | sed "s|^${grubdir}|${grub_prefix}|"`
  483.     while test $count -gt 0; do
  484.     $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
  485. dump ${root_drive}${tmp} ${img_file}
  486. quit
  487. EOF
  488.     if grep "Error [0-9]*: " $log_file >/dev/null; then
  489.         :
  490.     elif cmp $file $img_file >/dev/null; then
  491.         break
  492.     fi
  493.     sleep 1
  494.     count=`expr $count - 1`    
  495.     done
  496.     if test $count -eq 0; then
  497.     echo "The file $file not read correctly." 1>&2
  498.     exit 1
  499.     fi
  500. done
  501.  
  502. rm -f $img_file
  503. rm -f $log_file
  504.  
  505. # Create a safe temporary file.
  506. test -n "$mklog" && log_file=`$mklog`
  507.  
  508. # Sync to prevent GRUB from not finding stage files (notably, on XFS)
  509. sync
  510.  
  511. # XFS needs special magic
  512. xfs_frozen=false
  513. if which xfs_freeze > /dev/null ; then
  514.   cat << EOF
  515. Due to a bug in xfs_freeze, the following command might produce a segmentation
  516. fault when ${grubdir} is not in an XFS filesystem. This error is harmless and
  517. can be ignored.
  518. EOF
  519.   if xfs_freeze -f ${grubdir} ; then xfs_frozen=true ; fi
  520. fi
  521.  
  522. # Before all invocations of the grub shell, call sync to make sure
  523. # the raw device is in sync with any bufferring in filesystems.
  524. sync
  525.  
  526. # Now perform the installation.
  527. $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
  528. root $root_drive
  529. setup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive
  530. quit
  531. EOF
  532.  
  533. if ${xfs_frozen} ; then
  534.   xfs_freeze -u ${grubdir}
  535. fi
  536.  
  537. if grep "Error [0-9]*: " $log_file >/dev/null || test $debug = yes; then
  538.     cat $log_file 1>&2
  539.     exit 1
  540. fi
  541.  
  542. dpkg-query -W -f '${Version}\n' grub > ${grubdir}/installed-version
  543.  
  544. rm -f $log_file
  545.  
  546. # Prompt the user to check if the device map is correct.
  547. echo "Installation finished. No error reported."
  548. echo "This is the contents of the device map $device_map."
  549. echo "Check if this is correct or not. If any of the lines is incorrect,"
  550. echo "fix it and re-run the script \`grub-install'."
  551. echo
  552.  
  553. cat $device_map
  554.  
  555. # Bye.
  556. exit 0
  557.