chroot into Ubuntu from Live USB (and fix boot issues)

chroot into Ubuntu from Live USB (and fix boot issues)
Photo by Gabriel Heinzer / Unsplash

One of my worst nightmares came to life a couple of days ago. I turned off my laptop, plugged out the docking station and put the laptop into my backpack.
Then I went away for the weekend and forgot about my laptop all along, since I didn't need it.

When I got back from the weekend and turned it on I found that it's not booting any OS at all.
The boot entries are all there but I'm stuck in the endless loop of GRUB.

So I started digging, tried some things and figured out I needed to chroot somehow into my Ubuntu install. So here's a guide on how to do this


First you need to find the correct paritions for your system by running:

sudo blkid

The output will look something like:

/dev/nvme0n1p2: UUID="5c0832f6-9112-4935-9637-cf918ac6b976" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="9526345c-be1a-4545-987a-f0ac22645dd5"
/dev/nvme0n1p5: BLOCK_SIZE="512" UUID="103E0A1E3E09FD8A" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="312bc580-d86d-4c33-9d4d-fc7659f6143c"
/dev/nvme0n1p3: UUID="70662faa-eb76-46e8-8c22-372e8b933bba" TYPE="swap" PARTUUID="732940fa-cec2-44d0-9a07-a04fec96dde8"
/dev/nvme0n1p1: UUID="F5F8-0986" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="276793a1-c77e-4d30-a180-f8869df956db"
/dev/nvme0n1p6: BLOCK_SIZE="512" UUID="1E4A6A3A4A6A0EB9" TYPE="ntfs" PARTUUID="6513345f-2d5b-4c01-9cad-97b829138f8c"
/dev/nvme0n1p4: PARTLABEL="Microsoft reserved partition" PARTUUID="6c22ac6b-fb72-4729-bead-895e78fc2f89"

The relevant partitions here are:
- /dev/nvme0n1p1 : This is the EFI boot partition as you can see from the vfat filesystem
- /dev/nvme0n1p2 : Is the root partition, denoted by the ext4 filesystem type.

Now we need to mount the necessary partitions.
And we first start with the root partition using

sudo mount /dev/nvme0n1p2 /mnt

Now the root partition is mounted in /mnt

Then the boot partition needs to be mounted into the system. This is done by running

sudo mount /dev/nvme0n1p1 /mnt/boot
⚠️
If you are running on EFI, you will need to mount it to /mnt/boot/efi using sudo mount /dev/nvme0n1p1 /mnt/boot/efi

Then you need to mount the /proc filesystem to provide information about processes and kernel data to the chrooted system. Many commands (e.g. GRUB scripts, etc.) rely on information found in /proc

sudo mount -t proc none /mnt/proc

You will also need to the /dev directory because it contains the device nodes (files that represent hardware and virtual devices). Binding the host's /dev into the chroot allows the chrooted environment to interact with hardware devices, which is essential for tasks like mounting filesystems or accessing disks.

sudo mount -o bind /dev /mnt/dev

The /run directory is holding runtime data, such as PID files and system state information that many services require to operate correctly.

sudo mount -o bind /run /mnt/run

The /sys filesystem is a virtual filesystem that provides detailed information about the hardware and system configuration. It's needed for many low-level commands and for the operation of device management utilities within the chroot.

sudo mount -o bind /sys /mnt/sys

And finally, if you are on an EFI system and want to, for example repair, update or reinstall GRUB, you will also need the EFI variables mounted so that they can be read and written. They contain the UEFI settings, including boot entries in NVRAM.

sudo mount -o bind /sys/firmware/efi/efivars /mnt/sys/firmware/efi/efivars

Now you are all set and can chroot into the system

sudo chroot /mnt

And you can run any command, like updating the system, modifying files, etc.

📚
To fix my bootloader I needed to reinstall grub with grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB and update the GRUB configuration with update-grub.

When you are done with the chroot you can just do

exit

and you are back on the host's system.

Now you can also unmount all the filesystems

sudo umount /mnt/boot/{efi}
sudo umount /mnt/dev
sudo umount /mnt/proc
sudo umount /mnt/run
sudo umount /mnt/sys/firmware/efi/efivars
sudo umount /mnt/sys
sudo umount /mnt