Installing GRUB on a USB drive

Prerequires

A USB drive with at least 1GB capacity.

Installing gdisk

I’m going to use UEFI in this tutorial. But if you are going to use MBR you need fdisk. You can find both gdisk and fdisk installation commands for your distros package manager.

DistroCommand (gdisk)Command (fdisk)
Debian/Ubuntusudo apt install gdisksudo apt install fdisk
Archsudo pacman -S extra/gptfdisksudo pacman -S util-linux
Fedorasudo dnf install gdisksudo dnf install util-linux
Gentoosudo emerge –ask sys-apps/
gptfdisk
sudo emerge --ask sys-apps/util-linux
openSUSEsudo zypper install gdisksudo zypper install util-linux
Alpinesudo apk add gdisk sudo apk add fdisk
Voidsudo xbps-install -S gdisksudo xbps-install -S util-linux

Finding your USB Drive

Next up we are going to find our USB drive to install Grub. We can do this by typing lsblk into any terminal, it will list all available disks.

Example output :

NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda           8:0    0   1.8T  0 disk 
└─sda1        8:1    0   1.8T  0 part 
sdb           8:16   0 931.5G  0 disk 
└─sdb1        8:17   0 931.5G  0 part 
sdc           8:32   1  29.8G  0 disk 
├─sdc1        8:33   1  29.8G  0 part /run/media/mal1kore1ss/FBB0-9D0B
└─sdc2        8:34   1     6M  0 part 
zram0       253:0    0     4G  0 disk [SWAP]
nvme0n1     259:0    0 232.9G  0 disk 
├─nvme0n1p1 259:1    0     1G  0 part /boot
└─nvme0n1p2 259:2    0 231.9G  0 part /

In my case the USB drive is the one called sdc. Your usb drive would show up as sdX. Don’t forget that the X is different in your case.

Partitioning

We found our USB drive, now we can partition it. You can do the steps below to partition your USB drive using both gdisk and fdisk.

Partitioning using gdisk

If you are going to use gdisk simply just type sudo gdisk /dev/sdX to your terminal. (Don’t forget to change the X letter)

After that we are inside gdisk, now we can start partitioning. Partitioning is done within the n command.

But if there are some partitions, we maybe need to delete those. Deleting partitions are done within the d command. You can just go and delete the partition numbers.

After that we can finally start to create new partitions, simply just type n onto your terminal, you can change the partition number.

Don’t touch the first sector, we need to leave it empty.

Set the last sector to 1G, for the type make it EF00.

If you done these you can write these changes to your USB drive using the w command.

Partitioning using fdisk

If you are going to use gdisk simply just type sudo fdisk /dev/sdX to your terminal.
(Don’t forget to change the X letter)

After that we are inside fdisk, now we can start partitioning. Partitioning is done within the n command.

But if there are some partitions, we maybe need to delete those. Deleting partitions are done within the d command. You can just go and delete the partition numbers.

After that we can finally start to create new partitions, simply just type n onto your terminal, you can change the partition number.

Don’t touch the first sector, we need to leave it empty.

Set the last sector to 1G, for the type make it 1.

If you done these you can write these changes to your USB drive using the w command.

Formatting the ESP

Now we are ready to format the ESP.

Simply just type sudo mkfs.fat -F 32 /dev/sdX1
(Don’t forget to change the X letter)

Mounting the USB drive and installing Grub

We are going to mount our USB drives ESP. Then we are going to install Grub onto it.

We can run the command sudo mount /dev/sdX1 /mnt to mount our USB drive to /mnt, however you can change the mount point to wherever you want. But if you are going to use another mount point you need to edit the mount points for the next commands.
(Don’t forget to change the X letter)

Installing Grub to the Usb drive

Now we can finally install Grub onto our Usb drive. You can simply just run the command i give for both MBR and UEFI.

For MBR :

sudo grub-install --target=i386-pc --boot-directory=/mnt/usb/boot /dev/sdX

(For MBR don’t use the partition number, use the whole device such as /dev/sdc)

For UEFI :

sudo grub-install --target=x86_64-efi --efi-directory=/mnt --bootloader-id=GRUB --removable

Generating the Grub Configuration

This is the most important step, we need a grub.cfg file otherwise we can’t boot into our OS. First of all we need to create the grub directory.

For MBR :

sudo mkdir -p /mnt/usb/boot/grub

For UEFI :

sudo mkdir -p /mnt/EFI/grub

Now we are ready to generate our Grub configuration. You can run the command below to generate the configuration.

For MBR :

sudo grub-mkconfig -o /mnt/usb/boot/grub/grub.cfg

For UEFI :

sudo grub-mkconfig -o /mnt/EFI/grub/grub.cfg

If you done these correctly, you successfully installed Grub into a USB drive.
ou can check if it’s correct with the tree command if you have the package or just using ls.
Example Outputs for UEFI :

tree

/mnt/
└── EFI
    ├── BOOT
       └── BOOTX64.EFI
    └── grub
        └── grub.cfg

ls -r

/mnt/:
EFI

/mnt/EFI:
BOOT  grub

/mnt/EFI/BOOT:
BOOTX64.EFI

/mnt/EFI/grub:
grub.cfg

Best regards, Mal1koRe1ss.

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *