4 case "${BOOTLOADER}" in
12 print_warning "Bootloader ${BOOTLOADER} is not currently supported"
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)
21 # If ${BOOTDIR} is not defined in /etc/fstab, it must be the same as /
22 [ -z "${BOOTFS}" ] && BOOTFS=${ROOTFS}
24 echo "${ROOTFS} ${BOOTFS}"
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"
34 set_bootloader_grub2() {
37 "${BOOTDIR}/grub2/grub.cfg" \
38 "${BOOTDIR}/grub/grub.cfg" \
40 if [[ -e "${candidate}" ]]; then
41 GRUB_CONF=${candidate}
46 if [[ -z "${GRUB_CONF}" ]]; then
47 print_error 1 "Error! Grub2 configuration file does not exist, please ensure grub2 is correctly setup first."
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}"
56 set_bootloader_grub() {
57 local GRUB_CONF="${BOOTDIR}/grub/grub.conf"
59 print_info 1 "Adding kernel to ${GRUB_CONF}..."
61 if [ ! -e ${GRUB_CONF} ]
68 GRUB_BOOTFS=$(set_bootloader_read_fstab | cut -d' ' -f2)
71 # Get the GRUB mapping for our device
72 local GRUB_BOOT_DISK1=$(echo ${GRUB_BOOTFS} | sed -e 's#\(/dev/.\+\)[[:digit:]]\+#\1#')
73 local GRUB_BOOT_DISK=$(awk '{if ($2 == "'${GRUB_BOOT_DISK1}'") {gsub(/(\(|\))/, "", $1); print $1;}}' ${TEMP}/grub.map)
74 local GRUB_BOOT_PARTITION=$(($(echo ${GRUB_BOOTFS} | sed -e 's#/dev/.\+\([[:digit:]]?*\)#\1#') - 1))
76 if [ -n "${GRUB_BOOT_DISK}" -a -n "${GRUB_BOOT_PARTITION}" ]
79 # Create grub configuration directory and file if it doesn't exist.
80 [ ! -d `dirname ${GRUB_CONF}` ] && mkdir -p `dirname ${GRUB_CONF}`
83 echo 'default 0' >> ${GRUB_CONF}
84 echo 'timeout 5' >> ${GRUB_CONF}
85 echo "root (${GRUB_BOOT_DISK},${GRUB_BOOT_PARTITION})" >> ${GRUB_CONF}
88 # Add grub configuration to grub.conf
89 echo "# Genkernel generated entry, see GRUB documentation for details" >> ${GRUB_CONF}
90 echo "title=Gentoo Linux ($KV)" >> ${GRUB_CONF}
91 if [ "${BUILD_INITRD}" = '0' ]
93 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=${GRUB_ROOTFS}" >> ${GRUB_CONF}
95 echo -e "\tkernel /kernel-${KNAME}-${ARCH}-${KV} root=/dev/ram0 init=/linuxrc real_root=${GRUB_ROOTFS}" >> ${GRUB_CONF}
96 if [ "${PAT}" -gt '4' ]
98 echo -e "\tinitrd /initramfs-${KNAME}-${ARCH}-${KV}" >> ${GRUB_CONF}
103 print_error 1 "Error! ${BOOTDIR}/grub/grub.conf does not exist and the correct settings can not be automatically detected."
104 print_error 1 "Please manually create your ${BOOTDIR}/grub/grub.conf file."
108 # The grub.conf already exists, so let's try to duplicate the default entry
109 if set_bootloader_grub_check_for_existing_entry "${GRUB_CONF}"; then
110 print_warning 1 "An entry was already found for a kernel/initramfs with this name...skipping update"
114 set_bootloader_grub_duplicate_default "${GRUB_CONF}"
119 set_bootloader_grub_duplicate_default_replace_kernel_initrd() {
120 sed -r -e "/^[[:space:]]*kernel/s/kernel-[[:alnum:][:punct:]]+/kernel-${KNAME}-${ARCH}-${KV}/" - |
121 sed -r -e "/^[[:space:]]*initrd/s/init(rd|ramfs)-[[:alnum:][:punct:]]+/init\1-${KNAME}-${ARCH}-${KV}/"
124 set_bootloader_grub_check_for_existing_entry() {
126 if grep -q "^[[:space:]]*kernel[[:space:]=]*.*/kernel-${KNAME}-${ARCH}-${KV}\([[:space:]]\|$\)" "${GRUB_CONF}" &&
127 grep -q "^[[:space:]]*initrd[[:space:]=]*.*/initramfs-${KNAME}-${ARCH}-${KV}\([[:space:]]\|$\)" "${GRUB_CONF}"
134 set_bootloader_grub_duplicate_default() {
136 local GRUB_CONF_TMP="${GRUB_CONF}.tmp"
138 line_count=$(wc -l < "${GRUB_CONF}")
139 line_nums="$(grep -n "^title" "${GRUB_CONF}" | cut -d: -f1)"
140 if [ -z "${line_nums}" ]; then
141 print_error 1 "No current 'title' entries found in your grub.conf...skipping update"
144 line_nums="${line_nums} $((${line_count}+1))"
147 default=$(sed -rn '/^[[:space:]]*default[[:space:]=]/s/^.*default[[:space:]=]+([[:alnum:]]+).*$/\1/p' "${GRUB_CONF}")
148 if [ -z "${default}" ]; then
149 print_warning 1 "No default entry found...assuming 0"
152 if ! echo ${default} | grep -q '^[0-9]\+$'; then
153 print_error 1 "We don't support non-numeric (such as 'saved') default values...skipping update"
157 # Grub defaults are 0 based, cut is 1 based
158 # Figure out where the default entry lives
159 startstop=$(echo ${line_nums} | cut -d" " -f$((${default}+1))-$((${default}+2)))
160 startline=$(echo ${startstop} | cut -d" " -f1)
161 stopline=$(echo ${startstop} | cut -d" " -f2)
163 # Write out the bits before the default entry
164 sed -n 1,$((${startline}-1))p "${GRUB_CONF}" > "${GRUB_CONF_TMP}"
167 echo "title=Gentoo Linux (${KV})" >> "${GRUB_CONF_TMP}"
169 # Pass the default entry (minus the title) through to the replacement function and pipe the output to GRUB_CONF_TMP
170 sed -n $((${startline}+1)),$((${stopline}-1))p "${GRUB_CONF}" | set_bootloader_grub_duplicate_default_replace_kernel_initrd >> "${GRUB_CONF_TMP}"
172 # Finish off with everything including the previous default entry
173 sed -n ${startline},${line_count}p "${GRUB_CONF}" >> "${GRUB_CONF_TMP}"
175 cp "${GRUB_CONF}" "${GRUB_CONF}.bak"
176 cp "${GRUB_CONF_TMP}" "${GRUB_CONF}"
177 rm "${GRUB_CONF_TMP}"