Skip to content

Instantly share code, notes, and snippets.

@Bugaddr
Last active October 14, 2025 22:30
Show Gist options
  • Select an option

  • Save Bugaddr/313e0b38b62d0f01638548616b1806e8 to your computer and use it in GitHub Desktop.

Select an option

Save Bugaddr/313e0b38b62d0f01638548616b1806e8 to your computer and use it in GitHub Desktop.
Guide to install gentoo linux inside qemu

Gentoo Installation Guide with UKI

This guide provides a step-by-step process for installing Gentoo Linux using a modern systemd profile and a Unified Kernel Image (UKI) with systemd-boot.

1. Initial Setup & Partitioning

# Unmount all partitions if mounted, suppressing errors
umount /dev/vda1 2>/dev/null
umount /dev/vda2 2>/dev/null

# Setup internet connection
# Follow the prompts to configure your network interface (e.g., eth0)
net-setup

# Enable SSH to manage installation remotely (optional)
/etc/init.d/sshd start
passwd # Set a temporary root password for the live environment

# Synchronize system clock
chronyd -q

# Add a reliable DNS server
echo 'nameserver 1.1.1.3' > /etc/resolv.conf

# Partition the disk (e.g., /dev/vda)
# This creates a 1GB EFI System Partition (ESP) and uses the rest for the root partition.
sgdisk --zap-all \
  --set-alignment=2048 \
  --new=1:0:+1G --typecode=1:ef00 --change-name=1:"ESP" \
  --new=2:0:0   --typecode=2:8300 --change-name=2:"ROOT" \
  /dev/vda

# Format the partitions
partprobe /dev/vda
mkfs.vfat -F32 /dev/vda1
mkfs.ext4 /dev/vda2

# Mount the root partition
mkdir -p /mnt/gentoo
mount /dev/vda2 /mnt/gentoo

2. Download & Extract Stage3

# Navigate to the mount point
cd /mnt/gentoo

# Dynamically find and download the latest systemd stage3 tarball
MIRROR="https://distfiles.gentoo.org/releases/amd64/autobuilds"
STAGE3=$(curl -s "${MIRROR}/latest-stage3-amd64-systemd.txt" | grep -v '^#' | grep '\.tar\.xz' | awk '{print $1}')
wget "${MIRROR}/${STAGE3}"

# Extract the stage3 tarball
tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner

Note: It's crucial to use the systemd stage3 for this guide, as the selected profile relies on it.

3. Core System Configuration

# Configure Portage make.conf
# IMPORTANT: Adjust these settings for your specific hardware.
# -jX: Set X to the number of CPU threads you have.
# VIDEO_CARDS: Set to "intel", "amdgpu", "nvidia", etc.
cat > /mnt/gentoo/etc/portage/make.conf << EOF
COMMON_FLAGS="-march=native -O2 -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"
MAKEOPTS="-j8 -l9"
RUSTFLAGS="${RUSTFLAGS} -C target-cpu=native"
ACCEPT_LICENSE="*"
FEATURES="${FEATURES} binpkg-request-signature getbinpkg"
USE="modules-sign dist-kernel"

# NOTE: This stage was built with the bindist USE flag enabled

# This sets the language of build output to English.
# Please keep this setting intact when reporting bugs.
LC_MESSAGES=C.utf8
EOF

# Copy repository configuration from the stage3 default
mkdir -p /mnt/gentoo/etc/portage/repos.conf
cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf

# Copy DNS info into the new environment
cp --dereference /etc/resolv.conf /mnt/gentoo/etc/

4. Mount Filesystems & Chroot

# Mount necessary pseudo-filesystems
mount --types proc /proc /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
mount --make-rslave /mnt/gentoo/dev
mount --bind /run /mnt/gentoo/run
mount --make-slave /mnt/gentoo/run

# Enter the new environment
chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) ${PS1}"

# Mount the EFI partition
mkdir -p /efi
mount /dev/vda1 /efi

5. Select Profile & Update System

# Update mirrors
emerge -av app-portage/mirrorselect
mirrorselect -i -o 2>/dev/null >> /etc/portage/make.conf

# Use binary packages
sudo sed -i 's/priority = 1/priority = 9999/' /etc/portage/binrepos.conf/gentoobinhost.conf

# Sync the Portage tree
emerge-webrsync

# Explicitly set profile (systemd stage3 comes with a systemd profile, but you can switch variants here)
# Example: default systemd profile or systemd + KDE Plasma
eselect profile list
eselect profile set default/linux/amd64/23.0/systemd

# Install the 'getuto' script to configure the binary package host
getuto

# Install freetype before world
USE="-harfbuzz" emerge --oneshot media-libs/freetype

# Update the @world set to match the new profile, using binary packages where available
# [Use dispatch-conf if config issues arises]
emerge --verbose --update --deep --newuse --getbinpkg @world

# Remove any obsolete packages
emerge --verbose --depclean

6. Localization

# Set timezone (example: Asia/Kolkata)
ln -sf /usr/share/zoneinfo/Asia/Kolkata /etc/localtime

# Configure locale
echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
locale-gen
eselect locale set en_US.utf8

# Reload the environment to apply locale changes
env-update && source /etc/profile && export PS1="(chroot) ${PS1}"

7. Install Kernel & Firmware with UKI Support

# Install firmware and CPU microcode
emerge --verbose sys-kernel/linux-firmware sys-firmware/intel-microcode

# Configure USE flags for UKI support
cat > /etc/portage/package.use/systemd-uki << EOF
sys-apps/systemd boot ukify
sys-kernel/installkernel dracut uki systemd-boot
sys-kernel/dracut uefi
EOF

# Configure dracut to build the UKI
# We use blkid to get the partition UUID for a more robust boot process.
ROOT_UUID=$(blkid -s UUID -o value /dev/vda2)
ESP_UUID=$(blkid -s UUID -o value /dev/vda1)
mkdir -p /etc/dracut.conf.d
cat > /etc/dracut.conf.d/uki.conf << EOF
uefi="yes"
uefi_stub="/usr/lib/systemd/boot/efi/linuxx64.efi.stub"
kernel_cmdline="root=UUID=${ROOT_UUID} rw"
EOF

# Install the kernel (this will automatically trigger dracut to generate the UKI)
emerge --verbose sys-kernel/gentoo-kernel-bin

8. Configure fstab

# Create fstab using UUIDs for reliability
cat > /etc/fstab << EOF
UUID=${ESP_UUID}    /efi        vfat    defaults,noatime    0 2
UUID=${ROOT_UUID}    /           ext4    noatime             0 1
EOF

9. System Configuration

# Set hostname
echo "hostname" > /etc/hostname

# Configure hosts file
cat >> /etc/hosts << EOF
127.0.0.1   localhost
::1         localhost
127.0.1.1   hostname.localdomain hostname
EOF

# Set the root user password for the new system
passwd

# Install a networking client and enable it
emerge --verbose net-misc/dhcpcd
systemctl enable dhcpcd

# Generate the machine-id
systemd-machine-id-setup

# Enable default systemd services
systemctl preset-all --preset-mode=enable-only

10. Install Essential Tools

# Install necessary filesystem tools for the ESP
emerge --verbose sys-fs/dosfstools

# I/O scheduler rules
emerge --verbose sys-block/io-scheduler-udev-rules

# Text editor
emerge --verbose app-editors/nano

11. Install & Configure systemd-boot for UKI

# Install systemd-boot to the ESP
bootctl install

# Configure the loader
cat > /efi/loader/loader.conf << EOF
default @saved
timeout 3
console-mode max
editor no
EOF

# Verify the UKI was created and installed
ls -lh /efi/EFI/Linux/

Note: UKIs are automatically detected by systemd-boot, so no manual boot entries are required.

12. Install KDE Plasma (Optional)

# Emerge the KDE Plasma desktop meta-package
emerge --verbose kde-plasma/plasma-meta

# Enable the SDDM display manager
systemctl enable sddm

13. Create User Account

# Create a new user and add them to important groups
useradd -m -G users,wheel,audio,video,usb -s /bin/bash username
passwd username

# Allow users in the 'wheel' group to use sudo
emerge --verbose app-admin/sudo
echo "%wheel ALL=(ALL:ALL) ALL" > /etc/sudoers

14. Final Steps & Reboot

# Exit the chroot environment
exit

# Unmount all filesystems cleanly
cd
umount -l /mnt/gentoo/dev{/shm,/pts,}
umount -R /mnt/gentoo

# Reboot into your new Gentoo system
reboot

Troubleshooting UKI

If the system doesn't boot:

  1. Boot back into the live environment.
  2. Mount your partitions:
    mount /dev/vda2 /mnt/gentoo
    mount /dev/vda1 /mnt/gentoo/efi
  3. Chroot back into your system using the commands from Step 4.
  4. Inside the chroot, find your installed kernel version:
    # Look for a directory like '6.1.57-gentoo'
    ls /lib/modules
  5. Manually regenerate the UKI, replacing <kernel-version> with the version from the previous step:
    dracut --force --uefi --kver <kernel-version>
  6. Alternatively, simply reinstalling the kernel package will also trigger a rebuild:
    emerge --verbose --oneshot sys-kernel/gentoo-kernel-bin
  7. Verify the UKI file exists and has been updated: ls -lh /efi/EFI/Linux/
  8. Exit the chroot, unmount, and reboot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment