From: Andrew Gaffney Date: Mon, 15 Dec 2008 01:51:01 +0000 (-0600) Subject: Adding code that duplicates default grub.conf entry to replace old awk script. Based... X-Git-Tag: v3.4.10.903~21 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a86ba695a91b38da5b952434ac5db2d4dd15625f;p=genkernel.git Adding code that duplicates default grub.conf entry to replace old awk script. Based on code written by Mike Auty --- diff --git a/ChangeLog b/ChangeLog index 16c7d19..c580344 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,10 @@ # Copyright 1999-2008 Gentoo Foundation; 2008 Chris Gianelloni, Andrew Gaffney # Distributed under the GPL v2 + 15 Dec 2008; Andrew Gaffney gen_bootloader.sh: + Adding code that duplicates default grub.conf entry to replace old awk + script. Based on code written by Mike Auty + 15 Dec 2008; Andrew Gaffney +gen_bootloader.sh, gen_cmdline.sh, gen_determineargs.sh, genkernel: Initial commit for re-adding support for --bootloader=grub diff --git a/gen_bootloader.sh b/gen_bootloader.sh index d39b6bd..5f0bb51 100644 --- a/gen_bootloader.sh +++ b/gen_bootloader.sh @@ -79,6 +79,54 @@ set_bootloader_grub() { else # The grub.conf already exists, so let's try to duplicate the default entry + set_bootloader_grub_duplicate_default "${GRUB_CONF}" fi } + +set_bootloader_grub_duplicate_default_replace_kernel_initrd() { + sed -r -e "/^[[:space:]]*kernel/s/kernel-[[:alnum:][:punct:]]+/kernel-${KNAME}-${ARCH}-${KV}/" - | + sed -r -e "/^[[:space:]]*initrd/s/init(rd|ramfs)-[[:alnum:][:punct:]]+/init\1-${KNAME}-${ARCH}-${KV}/" +} + +set_bootloader_grub_duplicate_default() { + local GRUB_CONF=$1 + local GRUB_CONF_TMP="${GRUB_CONF}.tmp" + + line_count=$(wc -l < "${GRUB_CONF}") + line_nums="$(grep -n "^title" "${GRUB_CONF}" | cut -d: -f1)" + if [ -z "${line_nums}" ]; then + print_error 1 "No current 'title' entries found in your grub.conf...skipping update" + return 0 + fi + line_nums="${line_nums} $((${line_count}+1))" + + # Find default entry + default=$(sed -rn '/^[[:space:]]*default[[:space:]=]/s/^.*default[[:space:]=]+([[:alnum:]]+).*$/\1/p' "${GRUB_CONF}") + if ! echo ${default} | grep -q '^[0-9]\+$'; then + print_error 1 "We don't support non-numeric (such as 'saved') default values...skipping update" + return 0 + fi + + # Grub defaults are 0 based, cut is 1 based + # Figure out where the default entry lives + startstop=$(echo ${line_nums} | cut -d" " -f$((${default}+1))-$((${default}+2))) + startline=$(echo ${startstop} | cut -d" " -f1) + stopline=$(echo ${startstop} | cut -d" " -f2) + + # Write out the bits before the default entry + sed -n 1,$((${startline}-1))p "${GRUB_CONF}" > "${GRUB_CONF_TMP}" + + # Put in our title + echo "title=Gentoo Linux (${KV})" >> "${GRUB_CONF_TMP}" + + # Pass the default entry (minus the title) through to the replacement function and pipe the output to GRUB_CONF_TMP + sed -n $((${startline}+1)),$((${stopline}-1))p "${GRUB_CONF}" | set_bootloader_grub_duplicate_default_replace_kernel_initrd >> "${GRUB_CONF_TMP}" + + # Finish off with everything including the previous default entry + sed -n ${startline},${line_count}p "${GRUB_CONF}" >> "${GRUB_CONF_TMP}" + + cp "${GRUB_CONF}" "${GRUB_CONF}.bak" + cp "${GRUB_CONF_TMP}" "${GRUB_CONF}" + rm "${GRUB_CONF_TMP}" +}