How to install Grub2 under several common scenarios
General Background on Grub2 Related Commands
-
grub-install
-
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. -
sudo grub-install {--no-floppy|--allow-floppy} /device
These two flags can be used with any other variation of thegrub-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. -
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. -
sudo grub-install [--recheck] /device
This regenerates the file/boot/grub/device.map
which is used [FILL THIS IN] -
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
-
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 forgrub/grub.cfg
-
-
grub-update
-
sudo grub-update [*]
This is the only form in whichgrub-update
can be called. It is merely a wrapper forgrup-mkconfig -o /boot/grub/grub.cfg
which means that/boot
must be mounted for it to work. All arguments are passed directly togrub-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 forgrub-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
- Boot a Live CD, USB, or other Linux environment
Scenario 2: Boot Partition
- Boot a Live CD, USB, or other Linux environment
Scenario 3: Thumb Drive
- Attach USB drive to existing Linux environment
- Create mount point for
/dev/sdb1
& mount it - Install grub into the MBR of the
/dev/sdb
- ???
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
- Attach USB drive, from Scenario #3, to existing Linux environment
Really Informative Sites
- [Subject] : Link
Related videos on Youtube
Huckle
Updated on September 18, 2022Comments
-
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 educationalScenario 1: Non-booting Linux OS, No boot partition, Fix from Live CD
Setup:
/dev/sda1
is formattedext*
/dev/sda2
is formattedlinux_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
anddd 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 formattedfat
/dev/sda2
is formattedext*
/dev/sda3
is formattedlinux_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 thefstab
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 formattedfat
/dev/sdb2
is formattedext*
/dev/sdb3
is formattedfat
- 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 formattedfat
/dev/sdb1
contains/iso/live.iso
/dev/sdb2
is formattedext*
/dev/sdb3
is formattedfat
- 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 over 11 yearsSince 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 over 11 yearsI was considering the same and was just looking up what exactly that entailed.
-
nanofarad over 11 yearsJust 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 over 11 yearsYou're missing some steps for the first two scenarios.
-
Huckle over 11 yearsIt's not done yet. It's midterm week so this is a work in progress.