updated on Fri Jan 20 00:01:56 UTC 2012
[aur-mirror.git] / grub-ext / install-grub
blob70b06600b20233605fef6f047da814ccc29a6b4d
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}
43 if [ "${ROOTDEV}" = "" ]; then
44 usage
46 if [ "${PART_ROOT}" = "" ]; then
47 PART_ROOT=$(mount | grep "on /boot type" | cut -d' ' -f 1)
49 if [ "$PART_ROOT" = "" ]; then
50 PART_ROOT=$(mount | grep "on / type" | cut -d' ' -f 1)
52 if [ "${PART_ROOT}" = "" ]; then
53 echo "error: could not determine BOOT_DEVICE, please specify manually" >&2
54 exit 1
58 get_grub_map() {
59 [ -e /tmp/dev.map ] && rm /tmp/dev.map
60 /sbin/grub --no-floppy --device-map /tmp/dev.map >/tmp/grub.log 2>&1 <<EOF
61 quit
62 EOF
65 mapdev() {
66 partition_flag=0
67 device_found=0
68 devs=$(cat /tmp/dev.map | grep -v fd | sed 's/ *\t/ /' | sed ':a;$!N;$!ba;s/\n/ /g')
69 linuxdevice=$(echo $1 | cut -b1-8)
70 if [ "$(echo ${1} | egrep '[0-9]$')" ]; then
71 # /dev/hdXY
72 pnum=$(echo ${1} | cut -b9-)
73 pnum=$((${pnum}-1))
74 partition_flag=1
76 for dev in ${devs}; do
77 if [ "(" = $(echo ${dev} | cut -b1) ]; then
78 grubdevice="${dev}"
79 else
80 if [ "${dev}" = "${linuxdevice}" ]; then
81 device_found=1
82 break
85 done
86 if [ "${device_found}" = "1" ]; then
87 if [ "${partition_flag}" = "0" ]; then
88 echo "${grubdevice}"
89 else
90 grubdevice_stringlen=${#grubdevice}
91 let grubdevice_stringlen--
92 grubdevice=$(echo $grubdevice | cut -b1-$grubdevice_stringlen)
93 echo "${grubdevice},${pnum})"
95 else
96 echo " DEVICE NOT FOUND"
100 dogrub() {
101 get_grub_map
102 if [ ! -f /boot/grub/menu.lst ]; then
103 echo "Error: Couldn't find /boot/grub/menu.lst. Is GRUB installed?"
104 exit 1
106 # try to auto-configure GRUB...
107 if [ "${PART_ROOT}" != "" -a "$S_GRUB" != "1" ]; then
108 grubdev=$(mapdev ${PART_ROOT})
109 # look for a separately-mounted /boot partition
110 bootdev=$(mount | grep /boot | cut -d' ' -f 1)
111 if [ "${grubdev}" != "" -o "${bootdev}" != "" ]; then
112 cp /boot/grub/menu.lst /tmp/.menu.lst
113 # remove the default entries by truncating the file at our little tag (#-*)
114 head -n $(cat /tmp/.menu.lst | grep -n '#-\*' | cut -d: -f 1) /tmp/.menu.lst >/boot/grub/menu.lst
115 rm -f /tmp/.menu.lst
117 for kernel in /boot/vmlinuz-linux* /boot/vmlinuz26-*; do
118 if [ ${kernel} == "/boot/vmlinuz-linux*" ] || [ ${kernel} == "/boot/vmlinuz26-*" ] ; then
119 echo > /dev/null
120 else
121 VMLINUZ=$( echo ${kernel} | cut -c 7- )
123 if [ "$( echo ${VMLINUZ} | cut -c -13 )" = "vmlinuz-linux" ]; then # new naming scheme for linux > 3.0
124 extension=$( echo ${VMLINUZ} | cut -c 14- )
125 INITRAMFS_BASENAME=initramfs-linux${extension}
126 else # old naming scheme for lts kernel
127 extension=$( echo ${VMLINUZ} | cut -c 10- )
128 INITRAMFS_BASENAME=kernel26${extension}
131 echo "" >>/boot/grub/menu.lst
132 echo "# (0) Arch Linux" >>/boot/grub/menu.lst
133 echo "title Arch Linux - ${VMLINUZ}" >>/boot/grub/menu.lst
134 subdir=
135 if [ "${bootdev}" != "" ]; then
136 grubdev=$(mapdev ${bootdev})
137 else
138 subdir="/boot"
140 echo "root ${grubdev}" >>/boot/grub/menu.lst
141 echo "kernel ${subdir}/${VMLINUZ} root=${PART_ROOT} ro" >>/boot/grub/menu.lst
142 echo "initrd ${subdir}/${INITRAMFS_BASENAME}.img" >>/boot/grub/menu.lst
143 echo "" >>/boot/grub/menu.lst
145 # adding fallback/full image
146 echo "# (1) Arch Linux" >>/boot/grub/menu.lst
147 echo "title Arch Linux Fallback - ${VMLINUZ}" >>/boot/grub/menu.lst
148 echo "root ${grubdev}" >>/boot/grub/menu.lst
149 echo "kernel ${subdir}/${VMLINUZ} root=${PART_ROOT} ro" >>/boot/grub/menu.lst
150 echo "initrd ${subdir}/${INITRAMFS_BASENAME}-fallback.img" >>/boot/grub/menu.lst
151 echo "" >>/boot/grub/menu.lst
153 done
157 echo "Installing the GRUB bootloader..."
158 cp -a /usr/lib/grub/i386-pc/* /boot/grub/
159 sync
161 # freeze xfs filesystems to enable grub installation on xfs filesystems
162 if [ -x /usr/sbin/xfs_freeze ]; then
163 [ "$(stat -fLc %T /boot)" == "xfs" ] && /usr/sbin/xfs_freeze -f /boot > /dev/null 2>&1
164 [ "$(stat -fLc %T /)" == "xfs" ] && /usr/sbin/xfs_freeze -f / > /dev/null 2>&1
167 # look for a separately-mounted /boot partition
168 bootpart=$(mount | grep /boot | cut -d' ' -f 1)
169 if [ "${bootpart}" = "" ]; then
170 bootpart=${PART_ROOT}
172 bootpart=$(mapdev ${bootpart})
173 bootdev=$(mapdev ${ROOTDEV})
174 if [ "${bootpart}" = "" ]; then
175 echo "Error: Missing/Invalid root device: ${bootpart}"
176 exit 1
179 echo ${bootpart}
180 echo ${bootdev}
181 /sbin/grub --no-floppy --batch >/tmp/grub.log 2>&1 <<EOF
182 root ${bootpart}
183 setup ${bootdev}
184 quit
186 cat /tmp/grub.log
188 # unfreeze xfs filesystems
189 if [ -x /usr/sbin/xfs_freeze ]; then
190 [ "$(stat -fLc %T /boot)" == "xfs" ] && /usr/sbin/xfs_freeze -u /boot > /dev/null 2>&1
191 [ "$(stat -fLc %T /)" == "xfs" ] && /usr/sbin/xfs_freeze -u / > /dev/null 2>&1
193 if grep "Error [0-9]*: " /tmp/grub.log >/dev/null; then
194 echo "Error installing GRUB. (see /tmp/grub.log for output)"
195 exit 1
197 echo "GRUB was successfully installed."
199 rm -f /tmp/grub.log
201 exit 0
204 dogrub