Removing bootsplash support since it hasn't been in a kernel we've supported for...
[genkernel.git] / gen_bootloader.sh
1 #!/bin/bash
2
3 set_bootloader() {
4         if [ "x${BOOTLOADER}" == 'xgrub' ]
5         then
6                 set_grub_bootloader
7         else
8                 return 0
9         fi
10 }
11
12 set_grub_bootloader() {
13         local GRUB_CONF="${BOOTDIR}/grub/grub.conf"
14
15         print_info 1 ''
16         print_info 1 "Adding kernel to ${GRUB_CONF}..."
17         if [ "${BOOTFS}" != '' ]
18         then
19                 GRUB_BOOTFS=${BOOTFS}
20         else    
21                 # Extract block device information from /etc/fstab
22                 GRUB_ROOTFS=$(awk 'BEGIN{RS="((#[^\n]*)?\n)"}( $2 == "/" ) { print $1; exit }' /etc/fstab)
23                 GRUB_BOOTFS=$(awk 'BEGIN{RS="((#[^\n]*)?\n)"}( $2 == "'${BOOTDIR}'") { print $1; exit }' /etc/fstab)
24
25                 # If ${BOOTDIR} is not defined in /etc/fstab, it must be the same as /
26                 [ "x${GRUB_BOOTFS}" == 'x' ] && GRUB_BOOTFS=${GRUB_ROOTFS}
27         fi
28
29         # Read GRUB device map
30         [ ! -d ${TEMP} ] && mkdir ${TEMP}
31         grub --batch --device-map=${TEMP}/grub.map <<EOF >/dev/null 2>&1
32 quit
33 EOF
34         # Get the GRUB mapping for our device
35         local GRUB_BOOT_DISK1=$(echo ${GRUB_BOOTFS} | sed -e 's#\(/dev/.\+\)[[:digit:]]\+#\1#')
36         local GRUB_BOOT_DISK=$(awk '{if ($2 == "'${GRUB_BOOT_DISK1}'") {gsub(/(\(|\))/, "", $1); print $1;}}' ${TEMP}/grub.map)
37
38         local GRUB_BOOT_PARTITION=$(echo ${GRUB_BOOTFS} | sed -e 's#/dev/.\+\([[:digit:]]?*\)#\1#')
39         [ ! -d ${TEMP} ] && rm -r ${TEMP}
40         
41         # Create grub configuration directory and file if it doesn't exist.
42         [ ! -e `dirname ${GRUB_CONF}` ] && mkdir -p `dirname ${GRUB_CONF}`
43
44         if [ ! -e ${GRUB_CONF} ]
45         then
46                 if [ "${GRUB_BOOT_DISK}" != '' -a "${GRUB_BOOT_PARTITION}" != '' ]
47                 then
48                         GRUB_BOOT_PARTITION=`expr ${GRUB_BOOT_PARTITION} - 1`
49                         # grub.conf doesn't exist - create it with standard defaults
50                         touch ${GRUB_CONF}
51                         echo 'default 0' >> ${GRUB_CONF}
52                         echo 'timeout 5' >> ${GRUB_CONF}
53                         echo >> ${GRUB_CONF}
54
55                         # Add grub configuration to grub.conf   
56                         echo "# Genkernel generated entry, see GRUB documentation for details" >> ${GRUB_CONF}
57                         echo "title=Gentoo Linux ($KV)" >> ${GRUB_CONF}
58                         echo -e "\troot (${GRUB_BOOT_DISK},${GRUB_BOOT_PARTITION})" >> ${GRUB_CONF}
59                         if [ "${BUILD_INITRD}" -eq '0' ]
60                         then
61                                 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=${GRUB_ROOTFS}" >> ${GRUB_CONF}
62                         else
63                                 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=/dev/ram0 init=/linuxrc real_root=${GRUB_ROOTFS}" >> ${GRUB_CONF}
64                                 if [ "${PAT}" -gt '4' ]
65                                 then
66                                     echo -e "\tinitrd /initramfs-${KNAME}-${ARCH}-${KV}" >> ${GRUB_CONF}
67                                 fi
68                         fi
69                         echo >> ${GRUB_CONF}
70                 else
71                         print_error 1 "Error! ${BOOTDIR}/grub/grub.conf does not exist and the correct settings can not be automatically detected."
72                         print_error 1 "Please manually create your ${BOOTDIR}/grub/grub.conf file."
73                 fi
74         else
75                 # grub.conf already exists; so...
76                 # ... Clone the first boot definition and change the version.
77                 local TYPE
78                 [ "${KERN_24}" -eq '1' ] && TYPE='rd' || TYPE='ramfs'
79
80                 cp -f ${GRUB_CONF} ${GRUB_CONF}.bak
81                 awk 'BEGIN { RS="\n"; }
82                      {
83                         if(match($0, "kernel-" KNAME "-" ARCH "-" KV))
84                         { have_k="1" }
85                         if(match($0, "init" TYPE "-" KNAME "-" ARCH "-" KV))
86                         { have_i="1" }
87                         if(have_k == "1" && have_i == "1")
88                         { exit 1; }
89                      }' KNAME=${KNAME} ARCH=${ARCH} KV=${KV} TYPE=${TYPE} ${GRUB_CONF}.bak
90                 if [ "$?" -eq '0' ]
91                 then
92                         local LIMIT=$(wc -l ${GRUB_CONF}.bak)
93                         awk -f ${GK_BIN}/gen_bootloader_grub.awk LIMIT=${LIMIT/ */} KNAME=${KNAME} ARCH=${ARCH} KV=${KV} TYPE=${TYPE} ${GRUB_CONF}.bak > ${GRUB_CONF}
94                 else
95                         print_info 1 "GRUB: Definition found, not duplicating."
96                 fi
97         fi
98 }