home *** CD-ROM | disk | FTP | other *** search
/ tusportal.tus.k12.pa.us / tusportal.tus.k12.pa.us.tar / tusportal.tus.k12.pa.us / Wyse / latest-image.raw / 0.img / usr / sbin / grub-install.unsupported < prev    next >
Text File  |  2010-05-11  |  14KB  |  503 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=suse
  30. pkglibdir=${libdir}/${PACKAGE}/
  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. The grub source distribution carries this script to ease the
  81. installation of grub as a boot loader. In Suse Linux however, grub
  82. installation is much better handled by Yast; use of this script is
  83. generally discouraged and therefore unsupported. Consequently, the
  84. script has been renamed to grub-install.unsupported.
  85.  
  86. In order to (re-)install grub for the currently running system, simply
  87. use Yast. For an installation aiming at a differing system, the
  88. crafting of an appropriate device.map and grub shell script is
  89. strongly advised, e.g.
  90. "grub --batch --device-map=target-device.map < target-grub.conf"
  91.  
  92. EOF
  93. }
  94.  
  95. # Usage: convert os_device
  96. # Convert an OS device to the corresponding GRUB drive.
  97. # This part is OS-specific.
  98. convert () {
  99.     # First, check if the device file exists.
  100.     if test -e "$1"; then
  101.     :
  102.     else
  103.     echo "$1: Not found or not a block device." 1>&2
  104.     exit 1
  105.     fi
  106.  
  107.     # Break the device name into the disk part and the partition part.
  108.     case "$host_os" in
  109.     linux*)
  110.     tmp_disk=`echo "$1" | sed -e 's%\([sh]d[a-z]\)[0-9]*$%\1%' \
  111.                   -e 's%\(d[0-9]*\)p[0-9]*$%\1%' \
  112.                   -e 's%\(fd[0-9]*\)$%\1%' \
  113.                   -e 's%/part[0-9]*$%/disc%' \
  114.                   -e 's%\(c[0-7]d[0-9]*\).*$%\1%'`
  115.     tmp_part=`echo "$1" | sed -e 's%.*/[sh]d[a-z]\([0-9]*\)$%\1%' \
  116.                   -e 's%.*d[0-9]*p%%' \
  117.                   -e 's%.*/fd[0-9]*$%%' \
  118.                   -e 's%.*/floppy/[0-9]*$%%' \
  119.                   -e 's%.*/\(disc\|part\([0-9]*\)\)$%\2%' \
  120.                   -e 's%.*c[0-7]d[0-9]*p%%'`
  121.     ;;
  122.     gnu*)
  123.     tmp_disk=`echo "$1" | sed 's%\([sh]d[0-9]*\).*%\1%'`
  124.     tmp_part=`echo "$1" | sed "s%$tmp_disk%%"` ;;
  125.     freebsd* | kfreebsd*-gnu)
  126.     tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([saw]d[0-9]*\).*$%r\1%' \
  127.                 | sed 's%r\{0,1\}\(da[0-9]*\).*$%r\1%'`
  128.     tmp_part=`echo "$1" \
  129.         | sed "s%.*/r\{0,1\}[saw]d[0-9]\(s[0-9]*[a-h]\)%\1%" \
  130.                | sed "s%.*/r\{0,1\}da[0-9]\(s[0-9]*[a-h]\)%\1%"`
  131.     ;;
  132.     netbsd* | knetbsd*-gnu)
  133.     tmp_disk=`echo "$1" | sed 's%r\{0,1\}\([sw]d[0-9]*\).*$%r\1d%' \
  134.         | sed 's%r\{0,1\}\(fd[0-9]*\).*$%r\1a%'`
  135.     tmp_part=`echo "$1" \
  136.         | sed "s%.*/r\{0,1\}[sw]d[0-9]\([abe-p]\)%\1%"`
  137.     ;;
  138.     *)
  139.     echo "grub-install does not support your OS yet." 1>&2
  140.     exit 1 ;;
  141.     esac
  142.  
  143.     # Get the drive name.
  144.     tmp_drive=`grep -v '^#' $device_map | grep "$tmp_disk *$" \
  145.     | sed 's%.*\(([hf]d[0-9][a-g0-9,]*)\).*%\1%'`
  146.  
  147.     # If not found, print an error message and exit.
  148.     if test "x$tmp_drive" = x; then
  149.     echo "$1 does not have any corresponding BIOS drive." 1>&2
  150.     exit 1
  151.     fi
  152.  
  153.     if test "x$tmp_part" != x; then
  154.     # If a partition is specified, we need to translate it into the
  155.     # GRUB's syntax.
  156.     case "$host_os" in
  157.     linux*)
  158.         echo "$tmp_drive" | sed "s%)$%,`expr $tmp_part - 1`)%" ;;
  159.     gnu*)
  160.         if echo $tmp_part | grep "^s" >/dev/null; then
  161.         tmp_pc_slice=`echo $tmp_part \
  162.             | sed "s%s\([0-9]*\)[a-g]*$%\1%"`
  163.         tmp_drive=`echo "$tmp_drive" \
  164.             | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
  165.         fi
  166.         if echo $tmp_part | grep "[a-g]$" >/dev/null; then
  167.         tmp_bsd_partition=`echo "$tmp_part" \
  168.             | sed "s%[^a-g]*\([a-g]\)$%\1%"`
  169.         tmp_drive=`echo "$tmp_drive" \
  170.             | sed "s%)%,$tmp_bsd_partition)%"`
  171.         fi
  172.         echo "$tmp_drive" ;;
  173.     freebsd* | kfreebsd*-gnu)
  174.         if echo $tmp_part | grep "^s" >/dev/null; then
  175.         tmp_pc_slice=`echo $tmp_part \
  176.             | sed "s%s\([0-9]*\)[a-h]*$%\1%"`
  177.         tmp_drive=`echo "$tmp_drive" \
  178.             | sed "s%)%,\`expr "$tmp_pc_slice" - 1\`)%"`
  179.         fi
  180.         if echo $tmp_part | grep "[a-h]$" >/dev/null; then
  181.         tmp_bsd_partition=`echo "$tmp_part" \
  182.             | sed "s%s\{0,1\}[0-9]*\([a-h]\)$%\1%"`
  183.         tmp_drive=`echo "$tmp_drive" \
  184.             | sed "s%)%,$tmp_bsd_partition)%"`
  185.         fi
  186.         echo "$tmp_drive" ;;
  187.     netbsd* | knetbsd*-gnu)
  188.         if echo $tmp_part | grep "^[abe-p]$" >/dev/null; then
  189.         tmp_bsd_partition=`echo "$tmp_part" \
  190.             | sed "s%\([a-p]\)$%\1%"`
  191.         tmp_drive=`echo "$tmp_drive" \
  192.             | sed "s%)%,$tmp_bsd_partition)%"`
  193.         fi
  194.         echo "$tmp_drive" ;;
  195.     esac
  196.     else
  197.     # If no partition is specified, just print the drive name.
  198.     echo "$tmp_drive"
  199.     fi
  200. }
  201.  
  202. # Usage: resolve_symlink file
  203. # Find the real file/device that file points at
  204. resolve_symlink () {
  205.     tmp_fname=$1
  206.     # Resolve symlinks
  207.     while test -L $tmp_fname; do
  208.         tmp_new_fname=`ls -al $tmp_fname | sed -n 's%.*-> \(.*\)%\1%p'`
  209.         if test -z "$tmp_new_fname"; then
  210.             echo "Unrecognized ls output" 2>&1
  211.             exit 1
  212.         fi
  213.  
  214.         # Convert relative symlinks
  215.         case $tmp_new_fname in
  216.             /*) tmp_fname="$tmp_new_fname"
  217.             ;;
  218.             *) tmp_fname="`echo $tmp_fname | sed 's%/[^/]*$%%'`/$tmp_new_fname"
  219.             ;;
  220.         esac
  221.     done
  222.     echo "$tmp_fname"
  223. }
  224.  
  225. # Usage: find_device file
  226. # Find block device on which the file resides.
  227. find_device () {
  228.     # For now, this uses the program `df' to get the device name, but is
  229.     # this really portable?
  230.     tmp_fname=`df $1/ | awk '/\/dev\/|LABEL=|UUID=/{ print $1 }'`
  231.  
  232.     if grep -q LABEL= <<<$tmp_fname; then
  233.     label=`sed -e s/.*=// <<<$tmp_fname`
  234.     tmp_fname=`readlink -f /dev/disk/by-label/$label`
  235.     fi    
  236.     if grep -q UUID= <<<$tmp_fname; then
  237.     uuid=`sed -e s/.*=// <<<$tmp_fname`
  238.     tmp_fname=`readlink -f /dev/disk/by-uuid/$uuid`
  239.     fi
  240.  
  241.     if test -z "$tmp_fname"; then
  242.     echo "Could not find device for $1" 2>&1
  243.     exit 1
  244.     fi
  245.  
  246.     tmp_fname=`resolve_symlink $tmp_fname`
  247.  
  248.     echo "$tmp_fname"
  249. }
  250.  
  251. # Check the arguments.
  252. for option in "$@"; do
  253.     case "$option" in
  254.     -h | --help)
  255.     usage
  256.     exit 0 ;;
  257.     -v | --version)
  258.     echo "grub-install (GNU GRUB ${VERSION})"
  259.     exit 0 ;;
  260.     --root-directory=*)
  261.     rootdir=`echo "$option" | sed 's/--root-directory=//'` ;;
  262.     --grub-shell=*)
  263.     grub_shell=`echo "$option" | sed 's/--grub-shell=//'` ;;
  264.     --no-floppy)
  265.     no_floppy="--no-floppy" ;;
  266.     --force-lba)
  267.     force_lba="--force-lba" ;;
  268.     --recheck)
  269.     recheck=yes ;;
  270.     # This is an undocumented feature...
  271.     --debug)
  272.     debug=yes ;;
  273.     -*)
  274.     echo "Unrecognized option \`$option'" 1>&2
  275.     usage
  276.     exit 1
  277.     ;;
  278.     *)
  279.     if test "x$install_device" != x; then
  280.         echo "More than one install_devices?" 1>&2
  281.         usage
  282.         exit 1
  283.     fi
  284.     install_device="${option}" ;;
  285.     esac
  286. done
  287.  
  288. if test "x$install_device" = x; then
  289.     echo "install_device not specified." 1>&2
  290.     usage
  291.     exit 1
  292. fi
  293.  
  294. # If the debugging feature is enabled, print commands.
  295. if test $debug = yes; then
  296.     set -x
  297. fi
  298.  
  299. # Initialize these directories here, since ROOTDIR was initialized.
  300. case "$host_os" in
  301. netbsd* | openbsd*)
  302.     # Because /boot is used for the boot block in NetBSD and OpenBSD, use /grub
  303.     # instead of /boot/grub.
  304.     grub_prefix=/grub
  305.     bootdir=${rootdir}
  306.     ;;
  307. *)
  308.     # Use /boot/grub by default.
  309.     bootdir=${rootdir}/boot
  310.     ;;
  311. esac
  312.  
  313. grubdir=${bootdir}/grub
  314. device_map=${grubdir}/device.map
  315.  
  316. # Check if GRUB is installed.
  317. # This is necessary, because the user can specify "grub --read-only".
  318. set $grub_shell dummy
  319. if test -f "$1"; then
  320.     :
  321. else
  322.     echo "$1: Not found." 1>&2
  323.     exit 1
  324. fi
  325.  
  326. if test -f "$pkglibdir/stage1"; then
  327.     :
  328. else
  329.     echo "${pkglibdir}/stage1: Not found." 1>&2
  330.     exit 1
  331. fi
  332.  
  333. if test -f "$pkglibdir/stage2"; then
  334.     :
  335. else
  336.     echo "${pkglibdir}/stage2: Not found." 1>&2
  337.     exit 1
  338. fi
  339.  
  340. # Don't check for *stage1_5, because it is not fatal even if any
  341. # Stage 1.5 does not exist.
  342.  
  343. # Create the GRUB directory if it is not present.
  344. test -d "$bootdir" || mkdir "$bootdir" || exit 1
  345. test -d "$grubdir" || mkdir "$grubdir" || exit 1
  346.  
  347. # If --recheck is specified, remove the device map, if present.
  348. if test $recheck = yes; then
  349.     rm -f $device_map
  350. fi
  351.  
  352. # Create the device map file if it is not present.
  353. if test -f "$device_map"; then
  354.     :
  355. else
  356.     # Create a safe temporary file.
  357.     test -n "$mklog" && log_file=`$mklog`
  358.  
  359.     $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
  360. quit
  361. EOF
  362.     if grep "Error [0-9]*: " $log_file >/dev/null; then
  363.     cat $log_file 1>&2
  364.     exit 1
  365.     fi
  366.  
  367.     rm -f $log_file
  368. fi
  369.  
  370. # Make sure that there is no duplicated entry.
  371. tmp=`sed -n '/^([fh]d[0-9]*)/s/\(^(.*)\).*/\1/p' $device_map \
  372.     | sort | uniq -d | sed -n 1p`
  373. if test -n "$tmp"; then
  374.     echo "The drive $tmp is defined multiple times in the device map $device_map" 1>&2
  375.     exit 1
  376. fi
  377.  
  378. # Check for INSTALL_DEVICE.
  379. case "$install_device" in
  380. /dev/*)
  381.     install_device=`resolve_symlink "$install_device"`
  382.     install_drive=`convert "$install_device"`
  383.     # I don't know why, but some shells wouldn't die if exit is
  384.     # called in a function.
  385.     if test "x$install_drive" = x; then
  386.     exit 1
  387.     fi ;;
  388. \([hf]d[0-9]*\))
  389.     install_drive="$install_device" ;;
  390. [hf]d[0-9]*)
  391.     # The GRUB format with no parenthesis.
  392.     install_drive="($install_device)" ;;
  393. *)
  394.     echo "Format of install_device not recognized." 1>&2
  395.     usage
  396.     exit 1 ;;
  397. esac
  398.  
  399. # Get the root drive.
  400. root_device=`find_device ${rootdir}`
  401. bootdir_device=`find_device ${bootdir}`
  402.  
  403. # Check if the boot directory is in the same device as the root directory.
  404. if test "x$root_device" != "x$bootdir_device"; then
  405.     # Perhaps the user has a separate boot partition.
  406.     root_device=$bootdir_device
  407.     grub_prefix="/grub"
  408. fi
  409.  
  410. # Convert the root device to a GRUB drive.
  411. root_drive=`convert "$root_device"`
  412. if test "x$root_drive" = x; then
  413.     exit 1
  414. fi
  415.  
  416. # Check if the root directory exists in the same device as the grub
  417. # directory.
  418. grubdir_device=`find_device ${grubdir}`
  419.  
  420. if test "x$grubdir_device" != "x$root_device"; then
  421.     # For now, cannot deal with this situation.
  422.     cat <<EOF 1>&2
  423. You must set the root directory by the option --root-directory, because
  424. $grubdir does not exist in the root device $root_device.
  425. EOF
  426.     exit 1
  427. fi
  428.  
  429. # FHS says that /usr/share is used for architecture independent data,
  430. # so all stage-files are directly installed to /usr/lib/grub.
  431. # Therefor this part is no longer needed.
  432. # <--cut_here--> 
  433. ## Copy the GRUB images to the GRUB directory.
  434. #for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
  435. #    rm -f $file || exit 1
  436. #done
  437. #for file in \
  438. #    ${pkglibdir}/stage1 ${pkglibdir}/stage2 ${pkglibdir}/*stage1_5; do
  439. #    cp -f $file ${grubdir} || exit 1
  440. #done
  441. # <--uncut-->
  442.  
  443. # Make a default file.
  444. ${grub_set_default} --root-directory=${rootdir} default
  445.  
  446. # Make sure that GRUB reads the same images as the host OS.
  447. test -n "$mkimg" && img_file=`$mkimg`
  448. test -n "$mklog" && log_file=`$mklog`
  449.  
  450. for file in ${grubdir}/stage1 ${grubdir}/stage2 ${grubdir}/*stage1_5; do
  451.     count=5
  452.     tmp=`echo $file | sed "s|^${grubdir}|${grub_prefix}|"`
  453.     while test $count -gt 0; do
  454.     $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
  455. dump ${root_drive}${tmp} ${img_file}
  456. quit
  457. EOF
  458.     if grep "Error [0-9]*: " $log_file >/dev/null; then
  459.         :
  460.     elif cmp $file $img_file >/dev/null; then
  461.         break
  462.     fi
  463.     sleep 1
  464.     count=`expr $count - 1`    
  465.     done
  466.     if test $count -eq 0; then
  467.     echo "The file $file not read correctly." 1>&2
  468.     exit 1
  469.     fi
  470. done
  471.  
  472. rm -f $img_file
  473. rm -f $log_file
  474.  
  475. # Create a safe temporary file.
  476. test -n "$mklog" && log_file=`$mklog`
  477.  
  478. # Now perform the installation.
  479. $grub_shell --batch $no_floppy --device-map=$device_map <<EOF >$log_file
  480. root $root_drive
  481. setup $force_lba --stage2=$grubdir/stage2 --prefix=$grub_prefix $install_drive
  482. quit
  483. EOF
  484.  
  485. if grep "Error [0-9]*: " $log_file >/dev/null || test $debug = yes; then
  486.     cat $log_file 1>&2
  487.     exit 1
  488. fi
  489.  
  490. rm -f $log_file
  491.  
  492. # Prompt the user to check if the device map is correct.
  493. echo "Installation finished. No error reported."
  494. echo "This is the contents of the device map $device_map."
  495. echo "Check if this is correct or not. If any of the lines is incorrect,"
  496. echo "fix it and re-run the script \`grub-install'."
  497. echo
  498.  
  499. cat $device_map
  500.  
  501. # Bye.
  502. exit 0
  503.