Skip to content

Instantly share code, notes, and snippets.

@Postrediori
Last active March 30, 2025 10:31
Show Gist options
  • Save Postrediori/aa6e082b43da7caab4659ce39aa19200 to your computer and use it in GitHub Desktop.
Save Postrediori/aa6e082b43da7caab4659ce39aa19200 to your computer and use it in GitHub Desktop.
Rebuild Grub and initramfs of Linux with chroot

Introduction

This guide describes process of re-generating initramfs of Linux installation using chroot (for example, using a Live USB). Such manpilations may be useful, for example, in case the system fails to boot after changing of disks, cryptsetup partitions, re-created partitions with changed UUIDs.

Tesed on:

  • Fedora 41 (Legacy BIOS & UEFI)
  • Ubuntu 24.04, 24.10 (UEFI)

Pre-requisites

Assume some rescue Linux system is running and has access to the system disk.

Lets ssume the layout of the systm is the following:

/dev/sda1 - EFI partition
/dev/sda2 - /boot (unencrypted)
/dev/sda3 - / encrypted with LUKS
/dev/sda4 - /home encrypted with LUKS

Prepare fs for chroot

Unencrypt LUKS partitions with cryptsetup. Names of luks partitions should be the same as in /etc/crypttab file.

sudo cryptsetup open /dev/sda3 luks-01234567-89ab-cdef-0123-456789abcdef
sudo cryptsetup open /dev/sda4 luks-fedcba98-7654-3210-fedc-ba9876543210

Create folder for mounting the root and mount sub-folders

mkdir /mnt/target
sudo mount /dev/mapper/luks-01234567-89ab-cdef-0123-456789abcdef /mnt/target
sudo mount /dev/mapper/luks-fedcba98-7654-3210-fedc-ba9876543210 /mnt/target/home
sudo mount /dev/sda2 /mnt/target/boot
sudo mount /dev/sda1 /mnt/target/efi # Not needed in case of Legacy BIOS setup

Important. Mount /proc, /sys and /dev from the main system so that it can be safelly unmounted afterwards (if Live USB is based on systemd there will be a lot of implicit mounts in the subfolders).

sudo mount -t proc /proc /mnt/target/proc
sudo mount --rbind /dev /mnt/target/dev
sudo mount --make-rslave /mnt/target/dev
sudo mount --rbind /sys /mnt/target/sys
sudo mount --make-rslave /mnt/target/sys

Chroot to the mounted system

sudo chroot /mnt/target

Re-generate Grub and initramfs

For Ubuntu:

update-grub
update-initramfs -k all -c

For Fedora (UEFI):

dracut --regenerate-all --force
grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

For Fedora (legacy BIOS):

dracut --regenerate-all --force
grub2-mkconfig -o /boot/grub2/grub.cfg

Unmount chroot

Exit chroot system and unmount the fs tree

sudo umount --recursive /mnt/target/sys
sudo umount --recursive /mnt/target/dev
sudo umount /mnt/target/proc
sudo umount /mnt/target/home
sudo umount /mnt/target/boot
sudo umount /mnt/target

Re-lock LUKS partitions

sudo cryptsetup close luks-01234567-89ab-cdef-0123-456789abcdef
sudo cryptsetup close luks-fedcba98-7654-3210-fedc-ba9876543210

Restart the system.

Not described in this guide

  • Mounting LVM

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment