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