Today, it's China's 75th Birthday. Let me join the
celebration. And, today, I'm going to build up a customized Linux operating system by
following Linux From
Scratch, with the MOST up-to-date kernel
6.11.1.
1. Preparation
1.1 Create a Disk Image File
1 2 3 4
➜ repo dd if=/dev/zero of=./lfs.img bs=1M count=65536 65536+0 records in 65536+0 records out 68719476736 bytes (69 GB, 64 GiB) copied, 114.926 s, 598 MB/s
FINISHED --2024-08-27 03:51:25-- Total wall clock time: 2m 20s Downloaded: 94 files, 519M in 1m 32s (5.67 MB/s) You have new mail in /var/mail/root root@lvision-MS-7C60:/mnt/docker/repo#
1.7
Creating a Limited Directory Layout in the LFS Filesystem
for i in bin lib sbin; do ln -sv usr/$i $LFS/$i done
case $(uname -m) in x86_64) mkdir -pv $LFS/lib64 ;; esac mkdir: created directory '/mnt/lfs/etc' mkdir: created directory '/mnt/lfs/var' mkdir: created directory '/mnt/lfs/usr' mkdir: created directory '/mnt/lfs/usr/bin' mkdir: created directory '/mnt/lfs/usr/lib' mkdir: created directory '/mnt/lfs/usr/sbin' '/mnt/lfs/bin' -> 'usr/bin' '/mnt/lfs/lib' -> 'usr/lib' '/mnt/lfs/sbin' -> 'usr/sbin' mkdir: created directory '/mnt/lfs/lib64' root@lvision-MS-7C60:/mnt/lfs# mkdir -pv $LFS/tools mkdir: created directory '/mnt/lfs/tools' root@lvision-MS-7C60:/mnt/lfs#
1.8 Adding the LFS User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
root@lvision-MS-7C60:/mnt/lfs# groupadd lfs useradd -s /bin/bash -g lfs -m -k /dev/null lfs groupadd: group 'lfs' already exists useradd: user 'lfs' already exists root@lvision-MS-7C60:/mnt/lfs# chown -v lfs $LFS/{usr{,/*},lib,var,etc,bin,sbin,tools} case $(uname -m) in x86_64) chown -v lfs $LFS/lib64 ;; esac changed ownership of '/mnt/lfs/usr' from root to lfs changed ownership of '/mnt/lfs/usr/bin' from root to lfs changed ownership of '/mnt/lfs/usr/lib' from root to lfs changed ownership of '/mnt/lfs/usr/sbin' from root to lfs ownership of '/mnt/lfs/lib' retained as lfs changed ownership of '/mnt/lfs/var' from root to lfs changed ownership of '/mnt/lfs/etc' from root to lfs ownership of '/mnt/lfs/bin' retained as lfs ownership of '/mnt/lfs/sbin' retained as lfs changed ownership of '/mnt/lfs/tools' from root to lfs changed ownership of '/mnt/lfs/lib64' from root to lfs
1 2
root@lvision-MS-7C60:/mnt/lfs# su - lfs lfs@lvision-MS-7C60:~$
2.
Building the LFS Cross Toolchain and Temporary Tools
In file included from grep.c:43: ../lib/getopt.h:84:10: fatal error: getopt-cdefs.h: No such file or directory 84 | #include <getopt-cdefs.h> | ^~~~~~~~~~~~~~~~ compilation terminated.
******************************************************* GRUB2 will be compiled with following components: Platform: x86_64-efi With devmapper support: No (need libdevmapper header) With memory debugging: No With disk cache statistics: No With boot time statistics: No efiemu runtime: No (not available on efi) grub-mkfont: No (need freetype2 library) grub-mount: No (need fuse or fuse3 libraries) starfield theme: No (No build-time grub-mkfont) With libzfs support: No (need zfs library) Build-time grub-mkfont: No (need freetype2 library) Without unifont (no build-time grub-mkfont) With liblzma from -llzma (support for XZ-compressed mips images) With stack smashing protector: No *******************************************************
unset TARGET_CC && make ...... TARGET_OBJ2ELF= sh genmod.sh moddep.lst gcry_whirlpool.module build-grub-module-verifier gcry_whirlpool.mod make[3]: Leaving directory '/sources/grub-2.12/grub-core' make[2]: Leaving directory '/sources/grub-2.12/grub-core' Making all in po make[2]: Entering directory '/sources/grub-2.12/po' make[2]: Nothing to be donefor'all'. make[2]: Leaving directory '/sources/grub-2.12/po' Making all in docs make[2]: Entering directory '/sources/grub-2.12/docs' make[2]: Nothing to be donefor'all'. make[2]: Leaving directory '/sources/grub-2.12/docs' Making all in util/bash-completion.d make[2]: Entering directory '/sources/grub-2.12/util/bash-completion.d' ../../config.status --file=grub:grub-completion.bash.in config.status: creating grub make[2]: Leaving directory '/sources/grub-2.12/util/bash-completion.d' make[1]: Leaving directory '/sources/grub-2.12'
4.3
Generate Configuration File /boot/grub/grub.cfg for GRUB
1 2 3 4 5 6 7 8
(lfs chroot) root:/boot# grub-mkconfig -o /boot/grub/grub.cfg Generating grub configuration file ... Found linux image: /boot/vmlinuz-6.11.1-lfs-12.2 Warning: os-prober will not be executed to detect other bootable partitions. Systems on them will not be added to the GRUB boot configuration. Check GRUB_DISABLE_OS_PROBER documentation entry. Adding boot menu entry for UEFI Firmware Settings ... done
Let's take a look at the generated
/boot/grub/grub.cfg:
(lfs chroot) root:~# cat /boot/grub/grub.cfg # # DO NOT EDIT THIS FILE # # It is automatically generated by grub-mkconfig using templates # from /etc/grub.d and settings from /etc/default/grub #
### BEGIN /etc/grub.d/00_header ### if [ -s $prefix/grubenv ]; then load_env fi if [ "${next_entry}" ] ; then set default="${next_entry}" set next_entry= save_env next_entry set boot_once=true else set default="0" fi
if [ x"${feature_menuentry_id}" = xy ]; then menuentry_id_option="--id" else menuentry_id_option="" fi
export menuentry_id_option
if [ "${prev_saved_entry}" ]; then set saved_entry="${prev_saved_entry}" save_env saved_entry set prev_saved_entry= save_env prev_saved_entry set boot_once=true fi
function savedefault { if [ -z "${boot_once}" ]; then saved_entry="${chosen}" save_env saved_entry fi }
function load_video { if [ x$feature_all_video_module = xy ]; then insmod all_video else insmod efi_gop insmod efi_uga insmod ieee1275_fb insmod vbe insmod vga insmod video_bochs insmod video_cirrus fi }
if [ x$feature_default_font_path = xy ] ; then font=unicode else insmod ext2 search --no-floppy --fs-uuid --set=root 174bc795-5e06-4c0b-979f-7eccfca1ab10 font="/usr/share/grub/unicode.pf2" fi
if loadfont $font ; then set gfxmode=auto load_video insmod gfxterm fi terminal_output gfxterm if [ x$feature_timeout_style = xy ] ; then set timeout_style=menu settimeout=5 # Fallback normal timeout code in case the timeout_style feature is # unavailable. else settimeout=5 fi ### END /etc/grub.d/00_header ###
### BEGIN /etc/grub.d/10_linux ### menuentry 'GNU/Linux' --class gnu-linux --class gnu --class os $menuentry_id_option'gnulinux-simple-174bc795-5e06-4c0b-979f-7eccfca1ab10' { load_video insmod gzio insmod ext2 search --no-floppy --fs-uuid --set=root 174bc795-5e06-4c0b-979f-7eccfca1ab10 echo'Loading Linux 6.11.1-lfs-12.2 ...' linux /boot/vmlinuz-6.11.1-lfs-12.2 root=/dev/loop14 ro } submenu 'Advanced options for GNU/Linux'$menuentry_id_option'gnulinux-advanced-174bc795-5e06-4c0b-979f-7eccfca1ab10' { menuentry 'GNU/Linux, with Linux 6.11.1-lfs-12.2' --class gnu-linux --class gnu --class os $menuentry_id_option'gnulinux-6.11.1-lfs-12.2-advanced-174bc795-5e06-4c0b-979f-7eccfca1ab10' { load_video insmod gzio insmod ext2 search --no-floppy --fs-uuid --set=root 174bc795-5e06-4c0b-979f-7eccfca1ab10 echo'Loading Linux 6.11.1-lfs-12.2 ...' linux /boot/vmlinuz-6.11.1-lfs-12.2 root=/dev/loop14 ro } menuentry 'GNU/Linux, with Linux 6.11.1-lfs-12.2 (recovery mode)' --class gnu-linux --class gnu --class os $menuentry_id_option'gnulinux-6.11.1-lfs-12.2-recovery-174bc795-5e06-4c0b-979f-7eccfca1ab10' { load_video insmod gzio insmod ext2 search --no-floppy --fs-uuid --set=root 174bc795-5e06-4c0b-979f-7eccfca1ab10 echo'Loading Linux 6.11.1-lfs-12.2 ...' linux /boot/vmlinuz-6.11.1-lfs-12.2 root=/dev/loop14 ro single } }
### END /etc/grub.d/10_linux ###
### BEGIN /etc/grub.d/20_linux_xen ### ### END /etc/grub.d/20_linux_xen ###
### BEGIN /etc/grub.d/25_bli ### if [ "$grub_platform" = "efi" ]; then insmod bli fi ### END /etc/grub.d/25_bli ###
### BEGIN /etc/grub.d/30_os-prober ### ### END /etc/grub.d/30_os-prober ###
### BEGIN /etc/grub.d/30_uefi-firmware ### if [ "$grub_platform" = "efi" ]; then fwsetup --is-supported if [ "$?" = 0 ]; then menuentry 'UEFI Firmware Settings'$menuentry_id_option'uefi-firmware' { fwsetup } fi fi ### END /etc/grub.d/30_uefi-firmware ###
### BEGIN /etc/grub.d/40_custom ### # This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above. ### END /etc/grub.d/40_custom ###
### BEGIN /etc/grub.d/41_custom ### if [ -f ${config_directory}/custom.cfg ]; then source${config_directory}/custom.cfg elif [ -z "${config_directory}" -a -f $prefix/custom.cfg ]; then source$prefix/custom.cfg fi ### END /etc/grub.d/41_custom ### (lfs chroot) root:~#
# Create the mount point if it doesn't already exist mkdir -p /boot/efi
# Mount the image to simulate an EFI System Partition mount -o loop /boot/efi.img /boot/efi 10+0 records in 10+0 records out 10485760 bytes (10 MB, 10 MiB) copied, 0.0157083 s, 668 MB/s mkfs.fat 4.2 (2021-01-31) (lfs chroot) root:~#
and then:
1 2 3
(lfs chroot) root:/boot# grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id=GRUB --no-nvram --removable Installing for x86_64-efi platform. Installation finished. No error reported.
Now, you can see the following file:
1 2
(lfs chroot) root:/# ls /boot/efi/EFI/BOOT/BOOTX64.EFI /boot/efi/EFI/BOOT/BOOTX64.EFI```