9b1deefa9ab4b7c21e091e5d3628a7c2bf02112c
[genkernel.git] / gen_bootloader.sh
1 # $Id$
2
3 set_bootloader() {
4         case "${BOOTLOADER}" in
5                 grub)
6                         set_bootloader_grub
7                         ;;
8                 *)
9                         print_warning "Bootloader ${BOOTLOADER} is not currently supported"
10                         ;;
11         esac
12 }
13
14 set_bootloader_read_fstab() {
15         local ROOTFS=$(awk 'BEGIN{RS="((#[^\n]*)?\n)"}( $2 == "/" ) { print $1; exit }' /etc/fstab)
16         local BOOTFS=$(awk 'BEGIN{RS="((#[^\n]*)?\n)"}( $2 == "'${BOOTDIR}'") { print $1; exit }' /etc/fstab)
17
18         # If ${BOOTDIR} is not defined in /etc/fstab, it must be the same as /
19         [ -z "${BOOTFS}" ] && BOOTFS=${ROOTFS}
20
21         echo "${ROOTFS} ${BOOTFS}"
22 }
23
24 set_bootloader_grub_read_device_map() {
25         # Read GRUB device map
26         [ ! -d ${TEMP} ] && mkdir ${TEMP}
27         echo "quit" | grub --batch --device-map=${TEMP}/grub.map &>/dev/null
28         echo "${TEMP}/grub.map"
29 }
30
31 set_bootloader_grub() {
32         local GRUB_CONF="${BOOTDIR}/grub/grub.conf"
33
34         print_info 1 "Adding kernel to ${GRUB_CONF}..."
35
36         if [ ! -e ${GRUB_CONF} ]
37         then
38                 local GRUB_BOOTFS
39                 if [ -n "${BOOTFS}" ]
40                 then
41                         GRUB_BOOTFS=$BOOTFS
42                 else
43                         GRUB_BOOTFS=$(set_bootloader_read_fstab | cut -d' ' -f2)
44                 fi
45
46                 # Get the GRUB mapping for our device
47                 local GRUB_BOOT_DISK1=$(echo ${GRUB_BOOTFS} | sed -e 's#\(/dev/.\+\)[[:digit:]]\+#\1#')
48                 local GRUB_BOOT_DISK=$(awk '{if ($2 == "'${GRUB_BOOT_DISK1}'") {gsub(/(\(|\))/, "", $1); print $1;}}' ${TEMP}/grub.map)
49                 local GRUB_BOOT_PARTITION=$(($(echo ${GRUB_BOOTFS} | sed -e 's#/dev/.\+\([[:digit:]]?*\)#\1#') - 1))
50
51                 if [ -n "${GRUB_BOOT_DISK}" -a -n "${GRUB_BOOT_PARTITION}" ]
52                 then
53
54                         # Create grub configuration directory and file if it doesn't exist.
55                         [ ! -d `dirname ${GRUB_CONF}` ] && mkdir -p `dirname ${GRUB_CONF}`
56
57                         touch ${GRUB_CONF}
58                         echo 'default 0' >> ${GRUB_CONF}
59                         echo 'timeout 5' >> ${GRUB_CONF}
60                         echo "root (${GRUB_BOOT_DISK},${GRUB_BOOT_PARTITION})" >> ${GRUB_CONF}
61                         echo >> ${GRUB_CONF}
62
63                         # Add grub configuration to grub.conf   
64                         echo "# Genkernel generated entry, see GRUB documentation for details" >> ${GRUB_CONF}
65                         echo "title=Gentoo Linux ($KV)" >> ${GRUB_CONF}
66                         if [ "${BUILD_INITRD}" -eq '0' ]
67                         then
68                                 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=${GRUB_ROOTFS}" >> ${GRUB_CONF}
69                         else
70                                 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=/dev/ram0 init=/linuxrc real_root=${GRUB_ROOTFS}" >> ${GRUB_CONF}
71                                 if [ "${PAT}" -gt '4' ]
72                                 then
73                                     echo -e "\tinitrd /initramfs-${KNAME}-${ARCH}-${KV}" >> ${GRUB_CONF}
74                                 fi
75                         fi
76                         echo >> ${GRUB_CONF}
77                 else
78                         print_error 1 "Error! ${BOOTDIR}/grub/grub.conf does not exist and the correct settings can not be automatically detected."
79                         print_error 1 "Please manually create your ${BOOTDIR}/grub/grub.conf file."
80                 fi
81
82         else
83                 # The grub.conf already exists, so let's try to duplicate the default entry
84                 if set_bootloader_grub_check_for_existing_entry "${GRUB_CONF}"; then
85                         print_warning 1 "An entry was already found for a kernel/initramfs with this name...skipping update"
86                         return 0
87                 fi
88
89                 set_bootloader_grub_duplicate_default "${GRUB_CONF}"
90         fi
91
92 }
93
94 set_bootloader_grub_duplicate_default_replace_kernel_initrd() {
95         sed -r -e "/^[[:space:]]*kernel/s/kernel-[[:alnum:][:punct:]]+/kernel-${KNAME}-${ARCH}-${KV}/" - |
96         sed -r -e "/^[[:space:]]*initrd/s/init(rd|ramfs)-[[:alnum:][:punct:]]+/init\1-${KNAME}-${ARCH}-${KV}/"
97 }
98
99 set_bootloader_grub_check_for_existing_entry() {
100         local GRUB_CONF=$1
101         if grep -q "^[[:space:]]*kernel[[:space:]=]*.*/kernel-${KNAME}-${ARCH}-${KV}\([[:space:]]\|$\)" "${GRUB_CONF}" &&
102                 grep -q "^[[:space:]]*initrd[[:space:]=]*.*/initramfs-${KNAME}-${ARCH}-${KV}\([[:space:]]\|$\)" "${GRUB_CONF}"
103         then
104                 return 0
105         fi
106         return 1
107 }
108
109 set_bootloader_grub_duplicate_default() {
110         local GRUB_CONF=$1
111         local GRUB_CONF_TMP="${GRUB_CONF}.tmp"
112         
113         line_count=$(wc -l < "${GRUB_CONF}")
114         line_nums="$(grep -n "^title" "${GRUB_CONF}" | cut -d: -f1)"
115         if [ -z "${line_nums}" ]; then
116                 print_error 1 "No current 'title' entries found in your grub.conf...skipping update"
117                 return 0
118         fi
119         line_nums="${line_nums} $((${line_count}+1))"
120
121         # Find default entry
122         default=$(sed -rn '/^[[:space:]]*default[[:space:]=]/s/^.*default[[:space:]=]+([[:alnum:]]+).*$/\1/p' "${GRUB_CONF}")
123     if [ -z "${default}" ]; then
124                 print_warning 1 "No default entry found...assuming 0"
125                 default=0
126         fi
127         if ! echo ${default} | grep -q '^[0-9]\+$'; then
128                 print_error 1 "We don't support non-numeric (such as 'saved') default values...skipping update"
129                 return 0
130         fi
131
132         # Grub defaults are 0 based, cut is 1 based
133         # Figure out where the default entry lives
134         startstop=$(echo ${line_nums} | cut -d" " -f$((${default}+1))-$((${default}+2)))
135         startline=$(echo ${startstop} | cut -d" " -f1)
136         stopline=$(echo ${startstop} | cut -d" " -f2)
137
138         # Write out the bits before the default entry
139         sed -n 1,$((${startline}-1))p "${GRUB_CONF}" > "${GRUB_CONF_TMP}"
140
141         # Put in our title
142         echo "title=Gentoo Linux (${KV})" >> "${GRUB_CONF_TMP}"
143
144         # Pass the default entry (minus the title) through to the replacement function and pipe the output to GRUB_CONF_TMP
145         sed -n $((${startline}+1)),$((${stopline}-1))p "${GRUB_CONF}" | set_bootloader_grub_duplicate_default_replace_kernel_initrd >> "${GRUB_CONF_TMP}"
146
147         # Finish off with everything including the previous default entry
148         sed -n ${startline},${line_count}p "${GRUB_CONF}" >> "${GRUB_CONF_TMP}"
149
150         cp "${GRUB_CONF}" "${GRUB_CONF}.bak"
151         cp "${GRUB_CONF_TMP}" "${GRUB_CONF}"
152         rm "${GRUB_CONF_TMP}"
153 }