How to install Grub2 under several common scenarios

13,392

General Background on Grub2 Related Commands

  • grub-install

    1. sudo grub-install /device
      The /device refers to the physical drive that Grub will write the main grub executables to the MBR or Partition Boot Record of. Devices of the form /dev/sd[a-z] will have the MBR written into and devices of the form /dev/sd[a-z][0-9] will have the PBR written into. The primary difference being that the hardware always executes what is in the MBR and it then it is up to the code there as to whether or not the PBR is executed. There are setups where one might install something other than Grub2 to the MBR (say grub legacy) and have that load Grub2 which is installed in the PBR of one of the partitions. In general however, it is recommended to install Grub2 to the MBR using /dev/sd[a-z]. This option is required for all forms of the command.
    2. sudo grub-install {--no-floppy|--allow-floppy} /device
      These two flags can be used with any other variation of the grub-install command line. The first option --no-floppy was used to tell grub not to look for floppy disks which reduced the overall execution time. More recent copies of Grub now have the --allow-floppy option to specifically enable this feature since the default is now to not search for floppies.
    3. sudo grub-install [--force] /device
      Adding the --force option simply allows the installation of Grub into a Partition Boot Record instead of the Master Boot Record. Normally this behavior is not allowed. Reasons for why one might do this may be detailed in one of the scenarios below.
    4. sudo grub-install [--recheck] /device
      This regenerates the file /boot/grub/device.map which is used [FILL THIS IN]
    5. sudo grub-install [--target=] /device This option is used if you want to specify the specific firmware executed on boot. There are variants for BIOS vs UEFI and for x86 vs x64. If you are using BIOS and are installing Grub2 on the same machine you plan on booting then this is completely optional and it is recommended to let Grub2 detect this on its own.
  • grub-mkconfig

    1. sudo grub-mkconfig -o /boot/grub/grub.cfg
      This is the standard form for this command. This will cause each of the scripts in /etc/grub.d to run, in order, and append the output from all of those to the file specified by the -o file option. The location of the file depends on the type of install or repair being done. See the entry for grub/grub.cfg
  • grub-update

    1. sudo grub-update [*]
      This is the only form in which grub-update can be called. It is merely a wrapper for grup-mkconfig -o /boot/grub/grub.cfg which means that /boot must be mounted for it to work. All arguments are passed directly to grub-mkconfig

General Background on Grub2 Related Files

  • /boot/grub/device.map (Optional)
    [FILL THIS IN]

  • /boot/grub/core.img (Required)
    This is grub2's main executable. [FILL IN MORE]

  • grub/grub.cfg (Mostly Required)
    This file is the file where all the menu entries for grub are read from. It is generated with a list of scripts in /etc/grub.d of the operating system from which grub was installed (this may or may not be the one that grub will eventually boot). See the entries for grub-mkconfig. This file will be overwritten in the event that (Grub has an update AND /boot is mounted) OR (grub-update is run) OR (grub-mkconfig -o /boot/grub/grub.cfg is run). This file is listed with a relative path because where exactly it resides depends on if you are installing or repairing. If repairing, the partition which contains /boot will need to be mounted somewhere, say /mnt/boot. In this case the path will be /mnt/boot/grub/grub.cfg. If installing on an already-booted linux system the path will be /boot/grub/grub.cfg. In the event this file is missing, it is still possible to boot via the grub command shell. This is outside the scope of this document (hence the mostly required).

Scenario 1: No Boot Partition

  1. Boot a Live CD, USB, or other Linux environment

Scenario 2: Boot Partition

  1. Boot a Live CD, USB, or other Linux environment

Scenario 3: Thumb Drive

  1. Attach USB drive to existing Linux environment
  2. Create mount point for /dev/sdb1 & mount it
  3. Install grub into the MBR of the /dev/sdb
  4. ???

Commands:

# Step 2
sudo mkdir /mnt/usb-boot-part
sudo mount /dev/sdb1 /mnt/usb-boot-part
# Step 3
sudo grub-install --boot-directory=/mnt/usb-boot-part /dev/sdb

Scenario 4: Loopback Devices

  1. Attach USB drive, from Scenario #3, to existing Linux environment

Really Informative Sites

  1. [Subject] : Link
Share:
13,392

Related videos on Youtube

Huckle
Author by

Huckle

Updated on September 18, 2022

Comments

  • Huckle
    Huckle over 1 year

    I feel the community has long needed a clean guide on how to install Grub2 under a a few extremely common scenarios. I will accept answer as solved when it has one section per scenario and assumes nothing other than what is specified. Please add to the existing answer, wiki style, keeping to the original assumptions.

    Rules:
    1. You cannot, at any point in the answer, invoke Ubiquity (the Ubuntu installer).
    2. I strongly recommend not using any automatic boor-repair tools as they're not very educational

    Scenario 1: Non-booting Linux OS, No boot partition, Fix from Live CD

    Setup:

    • /dev/sda1 is formatted ext*
    • /dev/sda2 is formatted linux_swap
    • /dev/sda1 doesn't boot because MBR is scrambled and /boot/grub/* was erased
    • Would be the equivalent of running rm -rf /boot/grub and dd if=/dev/zero of=/dev/sda bs=440 count=1

    Explain:

    • How to boot to a Live CD / USB and restore Grub2 to the MBR and /boot of /dev/sda1

    Scenario 2: Non-booting Linux OS, Boot partition, Fix from Live CD

    Setup:

    • /dev/sda1 is formatted fat
    • /dev/sda2 is formatted ext*
    • /dev/sda3 is formatted linux_swap
    • /dev/sda2 doesn't boot because the MBR is scrambled and /dev/sda1 was formatted

    Explain:

    • How to boot to a Live CD / USB and restore Grub2 to the MBR and /dev/sda1 and then update the fstab on /dev/sda2

    Scenario 3: Install on to thumb drive, Booting various OSes, From Linux OS

    Setup:

    • /dev/sdb is removable media
    • /dev/sdb1 is formatted fat
    • /dev/sdb2 is formatted ext*
    • /dev/sdb3 is formatted fat
    • The MBR of /dev/sdb is otherwise not initialized
    • You are executing from a Linux based OS installed on /dev/sda

    Explain:

    • How to install Grub2 on to /dev/sdb1, mark /dev/sdb1 active, be able to chose between /dev/sdb2 and /dev/sdb3 on boot.

    Scenario 4: (Bonus) Install on to thumb drive, Booting ISO, From Linux OS

    Setup:

    • /dev/sdb is removable media
    • /dev/sdb1 is formatted fat
    • /dev/sdb1 contains /iso/live.iso
    • /dev/sdb2 is formatted ext*
    • /dev/sdb3 is formatted fat
    • The MBR of /dev/sdb is otherwise not initialized
    • You are executing from a Linux based OS installed on /dev/sda

    Explain:

    • How to install Grub2 on to /dev/sdb1, mark /dev/sdb1 active, be able to chose between /dev/sdb2, /dev/sdb3, and /iso/live.iso on boot.
    • nanofarad
      nanofarad over 11 years
      Since one user must work very hard to make a good answer and update it for changes in GRUB behavior, I recommend making one community-wiki answer.
    • Huckle
      Huckle over 11 years
      I was considering the same and was just looking up what exactly that entailed.
    • nanofarad
      nanofarad over 11 years
      Just make a CW answer with the right sections, fill it with [todo]'s and publicise it, while also working on it yourself. If you do a lot of work, I may give a bounty to you, being the owner of the CW post.
  • psusi
    psusi over 11 years
    You're missing some steps for the first two scenarios.
  • Huckle
    Huckle over 11 years
    It's not done yet. It's midterm week so this is a work in progress.