GRUB configuration is the process of creating menu entries, setting boot parameters, and customizing the bootloader behavior for your container-ready Linux distribution. Proper configuration ensures reliable booting with optimal container performance and security features.
Primary Configuration Files:
/boot/grub/grub.cfg: Main configuration (auto-generated)/etc/default/grub: Default settings and variables/etc/grub.d/: Configuration snippets directory00_header: GRUB environment and setup05_debian_theme: Visual theme (if applicable)10_linux: Linux kernel entries20_memtest86+: Memory testing utilities30_os-prober: Other OS detection40_custom: User customizations41_custom: Additional customizations
# Generate complete GRUB configuration
grub-mkconfig -o /boot/grub/grub.cfg
# Preview configuration without writing
grub-mkconfig | head -50
# Validate configuration syntax
grub-script-check /boot/grub/grub.cfgBasic Settings:
# Boot menu timeout (seconds)
GRUB_TIMEOUT=5
# Default menu entry (0 = first entry)
GRUB_DEFAULT=0
# Hide menu unless Shift is held
GRUB_TIMEOUT_STYLE=hidden
# Disable submenu display
GRUB_DISABLE_SUBMENU=yDisplay Settings:
# Resolution settings
GRUB_GFXMODE=1920x1080
GRUB_GFXPAYLOAD_LINUX=keep
# Theme settings
GRUB_THEME=/boot/grub/themes/starfield/theme.txt
# Terminal output
GRUB_TERMINAL=consoleContainer-Specific Settings:
# Kernel parameters for container support
GRUB_CMDLINE_LINUX_DEFAULT="quiet cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1"
# Additional parameters
GRUB_CMDLINE_LINUX="apparmor=1 security=apparmor selinux=1"Linux Kernel Detection:
# GRUB automatically detects kernels in /boot/
# Generated entries include:
# - Current kernel with latest initramfs
# - Previous kernel versions (if available)
# - Recovery mode entriesGenerated Menu Entry Example:
menuentry 'LFS GNU/Linux' --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-6.1.11-advanced-agnostic' {
load_video
insmod gzio
if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi
insmod part_gpt
insmod ext2
search --no-floppy --fs-uuid --set=root 12345678-1234-1234-1234-123456789012
echo 'Loading Linux 6.1.11 ...'
linux /boot/vmlinuz-6.1.11 root=UUID=12345678-1234-1234-1234-123456789012 ro quiet cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1
echo 'Loading initial ramdisk ...'
initrd /boot/initramfs-6.1.11.img
}Basic Manual Entry:
# Add to /etc/grub.d/40_custom
menuentry 'LFS Container System' --class gnu-linux {
insmod gzio
insmod part_msdos
search --set=root --fs-uuid 12345678-1234-1234-1234-123456789012
linux /boot/vmlinuz-6.1.11 root=UUID=12345678-1234-1234-1234-123456789012 ro quiet
initrd /boot/initramfs-6.1.11.img
}Advanced Manual Entry with Container Features:
menuentry 'LFS Container System (Advanced)' --class gnu-linux {
insmod gzio
insmod part_gpt
insmod ext2
search --no-floppy --fs-uuid --set=root 12345678-1234-1234-1234-123456789012
echo 'Loading LFS with container support...'
linux /boot/vmlinuz-6.1.11 root=UUID=12345678-1234-1234-1234-123456789012 ro quiet \
cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1 \
apparmor=1 security=apparmor selinux=1 \
threadirqs rcu_nocbs=0-7
echo 'Loading container-aware initramfs...'
initrd /boot/initramfs-6.1.11.img
}Cgroup Configuration:
# Enable cgroup v2 only
cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1
# Legacy cgroup v1 support (if needed)
cgroup_enable=memory swapaccount=1
# Cgroup delegation
systemd.unified_cgroup_hierarchy=1Namespace Support:
# User namespace support
namespace.unpriv_enable=1
# Network namespace isolation
netns=1Security Modules:
# AppArmor
apparmor=1 security=apparmor
# SELinux
selinux=1 security=selinux
# LSM stacking
security=apparmor,selinuxContainer Runtime Optimization:
# Memory management
transparent_hugepage=always
# I/O scheduling
elevator=noop
# Network optimization
net.core.somaxconn=1024Resource Limits:
# Increase limits for containers
kernel.pid_max=65536
vm.max_map_count=262144Container Debugging:
# Enable debugging
debug ignore_loglevel
# Verbose container logging
cgroup_debug
# Namespace debugging
namespace_debug=1Superuser Setup:
# Generate password hash
HASH=$(grub-mkpasswd-pbkdf2 | grep "PBKDF2 hash" | cut -d' ' -f7)
# Add to /etc/grub.d/40_custom
cat >> /etc/grub.d/40_custom << EOF
set superusers="root"
password_pbkdf2 root $HASH
EOFMenu Entry Protection:
# Protect specific entries
menuentry 'LFS Admin' --users root {
linux /boot/vmlinuz root=/dev/sda1
initrd /boot/initramfs.img
}UEFI Secure Boot:
# Check Secure Boot status
mokutil --sb-state
# Configure for Secure Boot
GRUB_ENABLE_CRYPTODISK=n # Disable if causing issuesKernel Module Signing:
# Ensure modules are signed
find /lib/modules -name "*.ko" | head -5 | xargs modinfo | grep -i sign# Add to /etc/grub.d/40_custom
menuentry 'LFS Kernel 6.1.11 (Stable)' {
linux /boot/vmlinuz-6.1.11-stable root=/dev/sda1 ro quiet
initrd /boot/initramfs-6.1.11-stable.img
}
menuentry 'LFS Kernel 6.1.11 (Experimental)' {
linux /boot/vmlinuz-6.1.11-exp root=/dev/sda1 ro quiet
initrd /boot/initramfs-6.1.11-exp.img
}Windows Dual Boot:
menuentry 'Windows 10' {
insmod part_gpt
insmod fat
insmod search_fs_uuid
insmod chain
search --fs-uuid --set=root $hints_string $fs_uuid
chainloader /EFI/Microsoft/Boot/bootmgfw.efi
}Other Linux Distributions:
menuentry 'Ubuntu' {
insmod part_gpt
insmod ext2
search --fs-uuid --set=root 87654321-4321-4321-4321-210987654321
linux /boot/vmlinuz-5.15.0-50-generic root=UUID=87654321-4321-4321-4321-210987654321
initrd /boot/initrd.img-5.15.0-50-generic
}Custom Theme Setup:
# Create theme directory
mkdir -p /boot/grub/themes/lfs
# Download or create theme files
# theme.txt, background.png, etc.
# Configure theme
echo 'GRUB_THEME="/boot/grub/themes/lfs/theme.txt"' >> /etc/default/grubSerial Boot Menu:
# Enable serial console
echo 'GRUB_TERMINAL="serial console"' >> /etc/default/grub
echo 'GRUB_SERIAL_COMMAND="serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1"' >> /etc/default/grubPXE Configuration:
# Enable network modules
echo 'GRUB_MODULES="net tftp http"' >> /etc/default/grub
# Configure network boot
menuentry 'LFS Network Boot' {
insmod net
insmod efinet
insmod tftp
net_bootp
linux (tftp)/vmlinuz root=/dev/nfs nfsroot=192.168.1.100:/srv/nfs/lfs
initrd (tftp)/initramfs.img
}# Check configuration syntax
grub-script-check /boot/grub/grub.cfg
# Validate specific entries
grub-script-check /etc/grub.d/10_linux
# Test configuration loading
grub-mkconfig | grub-script-checkConfiguration Testing:
# Test menu entry without rebooting
grub-mkconfig -o /tmp/grub.cfg.test
grub-script-check /tmp/grub.cfg.test
# Simulate boot process
qemu-system-x86_64 -kernel /boot/vmlinuz -initrd /boot/initramfs.img -append "root=/dev/sda1"# Check current kernel parameters
cat /proc/cmdline
# Verify container parameters are active
grep cgroup /proc/cmdline
grep apparmor /proc/cmdline
# Check security modules
cat /sys/kernel/security/lsmMenu Not Appearing:
# Check timeout settings
grep TIMEOUT /etc/default/grub
# Verify GRUB_TIMEOUT_STYLE
# Set to 'menu' for always visible menuKernel Not Found:
# Check kernel location
ls -la /boot/vmlinuz-*
# Verify GRUB can read filesystem
grub-probe --target=fs /boot
# Check device mapping
grub-mkdevicemapInitramfs Issues:
# Verify initramfs exists
ls -la /boot/initramfs-*.img
# Check initramfs contents
lsinitrd /boot/initramfs-6.1.11.img | head -20
# Regenerate initramfs
dracut --regenerate-all --forceParameter Not Applied:
# Check parameter syntax
# Ensure parameters are properly quoted
GRUB_CMDLINE_LINUX_DEFAULT="param1 param2=value"
# Regenerate configuration
grub-mkconfig -o /boot/grub/grub.cfgVerbose Boot:
# Enable verbose output
echo 'GRUB_CMDLINE_LINUX_DEFAULT="debug ignore_loglevel"' >> /etc/default/grub
# Check boot logs
dmesg | grep -i grub
journalctl -b | grep -i grubConfiguration Debug:
# Enable GRUB debug
echo 'GRUB_CMDLINE_LINUX_DEFAULT="grub.debug=all"' >> /etc/default/grub
# Test configuration generation
grub-mkconfig --debugReduce Timeout:
echo 'GRUB_TIMEOUT=1' >> /etc/default/grub
echo 'GRUB_TIMEOUT_STYLE=hidden' >> /etc/default/grubMinimize Modules:
# Load only essential modules
echo 'GRUB_PRELOAD_MODULES="part_gpt ext2"' >> /etc/default/grubFast Boot Parameters:
echo 'GRUB_CMDLINE_LINUX_DEFAULT="quiet fastboot"' >> /etc/default/grubMinimal Configuration:
# Reduce GRUB memory usage
echo 'GRUB_BADRAM="0x01234567,0x89abcdef"' >> /etc/default/grub # If bad RAM exists# Backup current configuration
cp /etc/default/grub /etc/default/grub.backup
cp /boot/grub/grub.cfg /boot/grub/grub.cfg.backup
# Backup custom entries
cp /etc/grub.d/40_custom /etc/grub.d/40_custom.backupRestore Configuration:
# Restore from backup
cp /etc/default/grub.backup /etc/default/grub
cp /boot/grub/grub.cfg.backup /boot/grub/grub.cfg
# Regenerate if needed
grub-mkconfig -o /boot/grub/grub.cfgEmergency Boot:
# Boot from GRUB command line
grub> linux /boot/vmlinuz root=/dev/sda1 ro
grub> initrd /boot/initramfs.img
grub> bootgraph TD
A[Edit /etc/default/grub] --> B[Edit /etc/grub.d/ files]
B --> C[Add Custom Entries]
C --> D[Run grub-mkconfig]
D --> E[Validate Configuration]
E --> F[Test Boot]
F -->|Success| G[Configuration Complete]
F -->|Failure| H[Debug Issues]
H --> I[Fix Configuration]
I --> D
- Examine
/etc/default/gruband understand each setting - Generate GRUB configuration:
grub-mkconfig -o /boot/grub/grub.cfg - Examine generated
/boot/grub/grub.cfgand identify key sections - Change boot timeout to 10 seconds and regenerate config
- Verify changes take effect on reboot
Expected Outcome: Understanding of GRUB configuration files and generation process
- Add container support parameters to
/etc/default/grub:GRUB_CMDLINE_LINUX_DEFAULT="quiet cgroup_no_v1=all systemd.unified_cgroup_hierarchy=1" - Add security parameters:
GRUB_CMDLINE_LINUX="apparmor=1 security=apparmor selinux=1" - Regenerate configuration:
grub-mkconfig -o /boot/grub/grub.cfg - Verify parameters in generated config:
grep cgroup /boot/grub/grub.cfg - Reboot and check active parameters:
cat /proc/cmdline
Expected Outcome: GRUB configured with container and security parameters
- Create a custom menu entry in
/etc/grub.d/40_custom:menuentry 'LFS Container System' --class gnu-linux { linux /boot/vmlinuz root=/dev/sda1 ro quiet initrd /boot/initramfs.img } - Regenerate configuration
- Verify entry appears in GRUB menu on boot
- Test booting with the custom entry
- Add additional parameters for container testing
Expected Outcome: Custom GRUB menu entry for container system
- Generate password hash:
grub-mkpasswd-pbkdf2 - Add superuser configuration to
/etc/grub.d/40_custom - Protect the main menu entry with password
- Regenerate configuration
- Test password protection on boot
- Document security configuration
Expected Outcome: Password-protected GRUB with secure configuration
- Create multiple kernel entries (simulate different versions)
- Add entries for "stable" and "testing" kernels
- Configure different parameters for each entry
- Test booting into each configuration
- Verify different parameters are applied
Expected Outcome: Multi-kernel GRUB configuration with different boot options
- Intentionally create syntax error in GRUB config
- Attempt to generate configuration:
grub-mkconfig -o /boot/grub/grub.cfg - Use
grub-script-checkto identify the error - Fix the error and regenerate configuration
- Test that configuration loads correctly
Expected Outcome: Ability to troubleshoot and fix GRUB configuration errors
- Measure current boot time:
systemd-analyze time - Optimize GRUB timeout and parameters for faster boot
- Regenerate configuration with performance settings
- Reboot and measure improvement:
systemd-analyze time - Document performance optimizations applied
Expected Outcome: Optimized GRUB configuration for faster boot times
With GRUB properly configured for container support, proceed to Chapter 5.4 for initramfs creation. The initramfs will include the necessary modules for container runtimes and filesystem overlays.
- GRUB Configuration Manual: https://www.gnu.org/software/grub/manual/grub/html_node/Configuration.html
- Kernel Parameters Documentation: https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
- systemd Boot Parameters: https://www.freedesktop.org/software/systemd/man/systemd.html
- AppArmor Documentation: https://apparmor.net/
- SELinux Documentation: https://selinuxproject.org/page/Main_Page