updated on Thu Jan 19 12:17:07 UTC 2012
[aur-mirror.git] / grub-gfx / install-grub
blob3eb7ce5938a195ffb103b30d83fbd3cd9a9d4a85
1 #!/bin/bash
4 # This is a little helper script that tries to convert linux-style device
5 # names to grub-style. It's not very smart, so it
6 # probably won't work for more complicated setups.
8 # If it doesn't work for you, try installing grub manually:
10 # # mkdir -p /boot/grub
11 # # cp /usr/lib/grub/i386-pc/* /boot/grub/
13 # Then start up the 'grub' shell and run something like the following:
15 # grub> root(hd0,0)
16 # grub> setup(hd0)
18 # The "root" line should point to the partition your kernel is located on,
19 # /boot if you have a separate boot partition, otherwise your root (/).
21 # The "setup" line tells grub which disc/partition to install the
22 # bootloader to. In the example above, it will install to the MBR of the
23 # primary master hard drive.
26 usage() {
27 echo "usage: install-grub <install_device> [boot_device]"
28 echo
29 echo "where <install_device> is the device where Grub will be installed"
30 echo "and [boot_device] is the partition that contains the /boot"
31 echo "directory (auto-detected if omitted)"
32 echo
33 echo "examples: install-grub /dev/hda"
34 echo " install-grub /dev/hda /dev/hda1"
35 echo
36 exit 0
39 ## new install-grub, code was taken from setup script
40 ROOTDEV=$1
41 PART_ROOT=$2
42 VMLINUZ=vmlinuz26
44 if [ "$ROOTDEV" = "" ]; then
45 usage
47 if [ "$PART_ROOT" = "" ]; then
48 PART_ROOT=$(mount | grep "on /boot type" | cut -d' ' -f 1)
50 if [ "$PART_ROOT" = "" ]; then
51 PART_ROOT=$(mount | grep "on / type" | cut -d' ' -f 1)
53 if [ "$PART_ROOT" = "" ]; then
54 echo "error: could not determine BOOT_DEVICE, please specify manually" >&2
55 exit 1
59 get_grub_map() {
60 [ -e /tmp/dev.map ] && rm /tmp/dev.map
61 /sbin/grub --no-floppy --device-map /tmp/dev.map >/tmp/grub.log 2>&1 <<EOF
62 quit
63 EOF
66 mapdev() {
67 partition_flag=0
68 device_found=0
69 devs=$(cat /tmp/dev.map | grep -v fd | sed 's/ *\t/ /' | sed ':a;$!N;$!ba;s/\n/ /g')
70 linuxdevice=$(echo $1 | cut -b1-8)
71 if [ "$(echo $1 | egrep '[0-9]$')" ]; then
72 # /dev/hdXY
73 pnum=$(echo $1 | cut -b9-)
74 pnum=$(($pnum-1))
75 partition_flag=1
77 for dev in $devs
79 if [ "(" = $(echo $dev | cut -b1) ]; then
80 grubdevice="$dev"
81 else
82 if [ "$dev" = "$linuxdevice" ]; then
83 device_found=1
84 break
87 done
88 if [ "$device_found" = "1" ]; then
89 if [ "$partition_flag" = "0" ]; then
90 echo "$grubdevice"
91 else
92 grubdevice_stringlen=${#grubdevice}
93 let grubdevice_stringlen--
94 grubdevice=$(echo $grubdevice | cut -b1-$grubdevice_stringlen)
95 echo "$grubdevice,$pnum)"
97 else
98 echo " DEVICE NOT FOUND"
102 dogrub() {
103 get_grub_map
104 if [ ! -f /boot/grub/menu.lst ]; then
105 echo "Error: Couldn't find /boot/grub/menu.lst. Is GRUB installed?"
106 exit 1
108 # try to auto-configure GRUB...
109 if [ "$PART_ROOT" != "" -a "$S_GRUB" != "1" ]; then
110 grubdev=$(mapdev $PART_ROOT)
111 # look for a separately-mounted /boot partition
112 bootdev=$(mount | grep /boot | cut -d' ' -f 1)
113 if [ "$grubdev" != "" -o "$bootdev" != "" ]; then
114 cp /boot/grub/menu.lst /tmp/.menu.lst
115 # remove the default entries by truncating the file at our little tag (#-*)
116 head -n $(cat /tmp/.menu.lst | grep -n '#-\*' | cut -d: -f 1) /tmp/.menu.lst >/boot/grub/menu.lst
117 rm -f /tmp/.menu.lst
118 echo "" >>/boot/grub/menu.lst
119 echo "# (0) Arch Linux" >>/boot/grub/menu.lst
120 echo "title Arch Linux" >>/boot/grub/menu.lst
121 subdir=
122 if [ "$bootdev" != "" ]; then
123 grubdev=$(mapdev $bootdev)
124 else
125 subdir="/boot"
127 echo "root $grubdev" >>/boot/grub/menu.lst
128 echo "kernel $subdir/$VMLINUZ root=$PART_ROOT ro" >>/boot/grub/menu.lst
129 if [ "$VMLINUZ" = "vmlinuz26" ]; then
130 echo "initrd $subdir/kernel26.img" >>/boot/grub/menu.lst
132 echo "" >>/boot/grub/menu.lst
133 # adding fallback/full image
134 echo "# (1) Arch Linux" >>/boot/grub/menu.lst
135 echo "title Arch Linux Fallback" >>/boot/grub/menu.lst
136 echo "root $grubdev" >>/boot/grub/menu.lst
137 echo "kernel $subdir/$VMLINUZ root=$PART_ROOT ro" >>/boot/grub/menu.lst
138 if [ "$VMLINUZ" = "vmlinuz26" ]; then
139 echo "initrd $subdir/kernel26-fallback.img" >>/boot/grub/menu.lst
141 echo "" >>/boot/grub/menu.lst
145 echo "Installing the GRUB bootloader..."
146 cp -a /usr/lib/grub/i386-pc/* /boot/grub/
147 sync
148 # freeze xfs filesystems to enable grub installation on xfs filesystems
149 if [ -x /usr/sbin/xfs_freeze ]; then
150 /usr/sbin/xfs_freeze -f /boot > /dev/null 2>&1
151 /usr/sbin/xfs_freeze -f / > /dev/null 2>&1
153 # look for a separately-mounted /boot partition
154 bootpart=$(mount | grep /boot | cut -d' ' -f 1)
155 if [ "$bootpart" = "" ]; then
156 bootpart=$PART_ROOT
158 bootpart=$(mapdev $bootpart)
159 bootdev=$(mapdev $ROOTDEV)
160 if [ "$bootpart" = "" ]; then
161 echo "Error: Missing/Invalid root device: $bootpart"
162 exit 1
164 /sbin/grub --no-floppy --batch >/tmp/grub.log 2>&1 <<EOF
165 root $bootpart
166 setup $bootdev
167 quit
169 cat /tmp/grub.log
170 # unfreeze xfs filesystems
171 if [ -x /usr/sbin/xfs_freeze ]; then
172 /usr/sbin/xfs_freeze -u /boot > /dev/null 2>&1
173 /usr/sbin/xfs_freeze -u / > /dev/null 2>&1
176 if grep "Error [0-9]*: " /tmp/grub.log >/dev/null; then
177 echo "Error installing GRUB. (see /tmp/grub.log for output)"
178 exit 1
180 echo "GRUB was successfully installed."
182 rm -f /tmp/grub.log
184 exit 0
187 dogrub