Results 1 to 1 of 1

Thread: Add an option to grub regeneration script (by Jimysbil)

  1. #1
    Join Date
    2016-Aug
    Posts
    1

    Cool How to add a menuentry to 'Advanced options' of grub regeneration script

    I have seen a lot of people asking questions about grub and they are wondering if they would be able to do some specific things with it.Grub is the most popular bootloader and it's being used in a lot of distributions (Ubuntu , Mint , Debian e.t.c.).
    After a lot google search about adding an option to the 'Advanced options' submenu of grub regeneration script, I wasn't be able to find anything to help me out.So I desided to make some changes by myself in order to make it work.

    ** This guide is tested on Ubuntu and Kali linux but you shouldn't have problem to use it on other distributions. **
    * This tutorial is made for grub 2.02~beta2-36 scripts *


    STEP 1

    Open a terminal and type the command:
    Code:
    sudo su
    (type the password when it's being asked)
    * It will give you root permissions.Everything you are doing from now on is up to you *

    STEP 2

    * After you have succesfully get root permissions you should better take a backup of your scripts before you edit them, just in case you mess it up and you don't remember how to revert the changes by yourself. *

    You have to backup /usr/sbin/grub-mkconfig /etc/default/grub files and /etc/grub.d/ folder because we are going to edit their contents.(You should probably make a backup of your /boot/grub/grub.cfg file just in case your system stays unbootable after the procedure.)

    So type those commands to the terminal:
    Code:
    cp /usr/sbin/grub-mkconfig /usr/sbin/grub-mkconfig.backup
    cp /etc/default/grub /etc/default/grub.backup
    cp -r /etc/grub.d /etc/grub.d.backup
    cp /boot/grub/grub.cfg /boot/grub/grub.cfg.backup
    If you get an error message don't proceed unless you know what you are doing.

    STEP 3

    As long as you have your backup you are ready to start the editing proccess.

    Give the command:
    Code:
    gedit /usr/sbin/grub-mkconfig /etc/default/grub /etc/grub.d/10_linux
    Now you should be able to see the three scripts you need to edit on 'gedit' text editor.

    STEP 4

    So the first thing you have to do is to manually patch the grub-mkconfig script by adding some new variables.

    Go to grub-mkconfig tab and search the script untill you find:
    Code:
    # These are optional, user-defined variables.
    Add a line for eash of these new variables:
    Code:
    GRUB_CMDLINE_LINUX_OPTION1 \
    GRUB_DISABLE_OPTION1 \
    GRUB_OPTION1_TITLE \
    Example:
    Code:
    # These are optional, user-defined variables.
    export GRUB_DEFAULT \
      GRUB_HIDDEN_TIMEOUT \
      GRUB_HIDDEN_TIMEOUT_QUIET \
      GRUB_TIMEOUT \
      GRUB_TIMEOUT_STYLE \
      GRUB_DEFAULT_BUTTON \
      GRUB_HIDDEN_TIMEOUT_BUTTON \
      GRUB_TIMEOUT_BUTTON \
      GRUB_TIMEOUT_STYLE_BUTTON \
      GRUB_BUTTON_CMOS_ADDRESS \
      GRUB_BUTTON_CMOS_CLEAN \
      GRUB_DISTRIBUTOR \
      GRUB_CMDLINE_LINUX \
      GRUB_CMDLINE_LINUX_DEFAULT \
      GRUB_CMDLINE_LINUX_OPTION1 \
      GRUB_CMDLINE_XEN \
      GRUB_CMDLINE_XEN_DEFAULT \
      GRUB_CMDLINE_LINUX_XEN_REPLACE \
      GRUB_CMDLINE_LINUX_XEN_REPLACE_DEFAULT \
      GRUB_CMDLINE_NETBSD \
      GRUB_CMDLINE_NETBSD_DEFAULT \
      GRUB_CMDLINE_GNUMACH \
      GRUB_TERMINAL_INPUT \
      GRUB_TERMINAL_OUTPUT \
      GRUB_SERIAL_COMMAND \
      GRUB_DISABLE_LINUX_UUID \
      GRUB_DISABLE_RECOVERY \
      GRUB_DISABLE_OPTION1 \
      GRUB_VIDEO_BACKEND \
      GRUB_GFXMODE \
      GRUB_BACKGROUND \
      GRUB_THEME \
      GRUB_GFXPAYLOAD_LINUX \
      GRUB_DISABLE_OS_PROBER \
      GRUB_INIT_TUNE \
      GRUB_SAVEDEFAULT \
      GRUB_ENABLE_CRYPTODISK \
      GRUB_BADRAM \
      GRUB_OS_PROBER_SKIP_LIST \
      GRUB_DISABLE_SUBMENU \
      GRUB_RECORDFAIL_TIMEOUT \
      GRUB_RECOVERY_TITLE \
      GRUB_OPTION1_TITLE
    ** The last line should not have a (\)

    Somewhere inside the script you should see:
    Code:
    if [ "x${GRUB_RECOVERY_TITLE}" = "x" ]; then
      GRUB_RECOVERY_TITLE="recovery mode"
    fi
    Let a blank line and add:
    Code:
    if [ "x${GRUB_OPTION1_TITLE}" = "x" ]; then
      GRUB_OPTION1_TITLE="my special mode"
    fi
    As long as you make the changes save them.

    STEP 5

    Go to 10_linux tab.You should be able to find somewhere:
    Code:
    if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
        linux_entry "${OS}" "${version}" recovery \
                    "${GRUB_CMDLINE_LINUX_RECOVERY} ${GRUB_CMDLINE_LINUX}"
      fi
    Let a blank line and add:
    Code:
    if [ "x${GRUB_DISABLE_OPTION1}" != "xtrue" ]; then
        linux_entry "${OS}" "${version}" option1 \
                    "${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_OPTION1}"
      fi
    You should also be able to find:
    Code:
    recovery)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")" ;;
    press enter at the end of this line and add:
    Code:
    option1)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_OPTION1_TITLE}")")" ;;
    It should look like this:
    Code:
    if [ x$type != xsimple ] ; then
          case $type in
          recovery)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_RECOVERY_TITLE}")")" ;;
          vesa)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "$(gettext "${GRUB_OPTION1_TITLE}")")" ;;
          init-*)
              title="$(gettext_printf "%s, with Linux %s (%s)" "${os}" "${version}" "${type#init-}")" ;;
          *)
              title="$(gettext_printf "%s, with Linux %s" "${os}" "${version}")" ;;
    As long as you make the changes save them too.

    STEP 6

    Now you can move to grub tab and set up everything you like.
    If you want to add some specific feature to your new option you can add a new line like this:
    Code:
    GRUB_CMDLINE_LINUX_OPTION1=""
    in order to add those options to the kernel(s) line every time the system or you execute the command 'update-grub'

    For example if you want to force vesa driver on startup add:
    Code:
    GRUB_CMDLINE_LINUX_OPTION1="nomodeset xforcevesa"
    Those options should disable nouveau driver and force vesa driver to load on your OS startup.

    You could also add:
    Code:
    GRUB_DISABLE_OPTION1="true"
    in case you want to disable this (new) feature you just added to your script.

    As long as you have make all the changes you like save them and close the application.

    STEP 7
    So the only thing you have to do now is to execute the command:
    Code:
    update-grub
    This command will regenerate the grub.cfg (configuration) file for grub (bootloader) and it should have now the extra choice of yours for every kernel of your linux OS.

    # If you face some error on the execution of the command 'update-grub' you have probably mess up with your scripts or your scripts version do not cοoperate well with the injected code.DON't panic!!!Just run the commands:

    Code:
    cp -f /usr/sbin/grub-mkconfig.backup /usr/sbin/grub-mkconfig
    cp -f /etc/default/grub.backup /etc/default/grub
    cp -rf /etc/grub.d.backup /etc/grub.d
    * Those commands will restore your backups

    Now execute the command:

    Code:
    update-grub
    This time if all have gone well with the restoration of your backups, your grub configuration file should be regenerated without errors.

    STEP 8

    If you have some experience and you want to test your regenerated configuration file before you reboot your system give the command:

    Code:
    gedit /boot/grub/grub.cfg
    STEP 9

    In case you what to reboot your system immediately type:
    Code:
    reboot
    In order to exit the root cell type:
    Code:
    exit
    STEP 10

    If you rebooted your system and everything is OK you can remove your backups safely typing:
    Code:
    sudo su
    rm -f /usr/sbin/grub-mkconfig.backup
    rm -f /etc/default/grub.backup
    rm -rf /etc/grub.d.backup
    rm -f /boot/grub/grub.cfg.backup
    *** You have to be realy careful when you are using rm command as it could delete important files and folders.

    STEP 11

    In case your system is unbootable because you accidentally made something wrong with your modifications but your grub.cfg file was created without errors DON'T panic.You can always revert your untached configurations back in place.
    Boot from a live CD/USB e.t.c. and open a terminal.
    Type the command:
    Code:
    sudo su
    in order to take root permissions.
    Open a file explorer (nautilus for example) and mount the partition with your OS.
    Go to your terminal, type 'cd' and press space.
    Go to your file explorer and drag & drop the path of your mounted partition inside your terminal.

    Example:
    Code:
    cd '/media/root/sda1'
    * Your path will be different.This is just an example.
    After that execute the commands:
    Code:
    cp -f usr/sbin/grub-mkconfig.backup usr/sbin/grub-mkconfig
    cp -f etc/default/grub.backup etc/default/grub
    cp -rf etc/grub.d.backup etc/grub.d
    cp -f boot/grub/grub.cfg.backup boot/grub/grub.cfg
    Those commands should restore your system in bootable condition.But in order to make sure your scripts are working well you should reboot into your OS.
    Open a terminal and run the commands:
    Code:
    sudo su
    update-grub
    If everything was well, your OS should be bootable again on the next startup.

    ## When you make sure your system is bootable you can execute STEP 10.

    Congratulations!If you made all of those steps correctly you should have a working script.
    If you face some problem or you need some help with it, I would be glad to help you.
    Last edited by Jimysbil; 2016-09-18 at 20:03. Reason: Added more specific title

Similar Threads

  1. Kali Linux Grub repair using live CD option does not work
    By isurumadusanka in forum Installing Archive
    Replies: 1
    Last Post: 2017-12-12, 16:43
  2. Windows 8.1 not booting from grub menu option after updating grub
    By rahzex in forum TroubleShooting Archive
    Replies: 0
    Last Post: 2016-07-11, 14:27
  3. grub not showing windows option
    By rushic24 in forum TroubleShooting Archive
    Replies: 7
    Last Post: 2016-03-12, 15:44
  4. Replies: 0
    Last Post: 2014-08-17, 03:08

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •