Skip to content

Instantly share code, notes, and snippets.

@stringang
Last active July 2, 2024 01:26
Show Gist options
  • Save stringang/67a09fe62556aa67af830c5ca531f23a to your computer and use it in GitHub Desktop.
Save stringang/67a09fe62556aa67af830c5ca531f23a to your computer and use it in GitHub Desktop.
Linux From Scratch (LFS)

Linux From Scratch

caveats

  • 包编译完成删除原代码目录

host requirements

apt install -y binutils bison gcc g++ make texinfo

ls -snf /usr/bin/gawk /usr/bin/awk
ls -snf /usr/bin/bash /usr/bin/sh

3. download source

export LFS=/mnt/lfs
mkdir -v $LFS/sources
chmod -v a+wt $LFS/sources
wget https://www.linuxfromscratch.org/lfs/view/stable/wget-list-sysv
wget --input-file=wget-list-sysv --continue --directory-prefix=$LFS/sources
# from mirror
wget -m -nH --cut-dirs=3 -np --reject 'index.*' --directory-prefix=$LFS/sources -e robots=off https://mirrors.ustc.edu.cn/lfs/lfs-packages/12.1/

chown root:root $LFS/sources/*

4. build preparing

diretory layout

mkdir -pv $LFS/{etc,var} $LFS/usr/{bin,lib,sbin}

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 -pv $LFS/tools

lfs user

groupadd lfs
useradd -s /bin/bash -g lfs -m -k /dev/null lfs

chown -v lfs $LFS/{usr{,/*},lib,var,etc,bin,sbin,tools}
case $(uname -m) in
  x86_64) chown -v lfs $LFS/lib64 ;;
esac

su - lfs

build enviroment

cat > ~/.bash_profile << "EOF"
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
EOF

cat > ~/.bashrc << "EOF"
set +h
umask 022
LFS=/mnt/lfs
LC_ALL=POSIX
LFS_TGT=$(uname -m)-lfs-linux-gnu
PATH=/usr/bin
if [ ! -L /bin ]; then PATH=/bin:$PATH; fi
PATH=$LFS/tools/bin:$PATH
CONFIG_SITE=$LFS/usr/share/config.site
export LFS LC_ALL LFS_TGT PATH CONFIG_SITE
EOF

cat >> ~/.bashrc << "EOF"
export MAKEFLAGS=-j$(nproc)
EOF

source ~/.bash_profile

5. cross compiling toolchain

  • binutils
  • gcc
    • mpfr
    • gmp
    • mpc
  • linux(Linux API Headers)
  • glibc
  • libstdc++(standard C++ library)
tar -xf 

6. compiling tools

  • M4
  • Ncurses
  • Bash
  • Coreutils
  • Diffutils
  • File
  • Findutils
  • Gawk
  • Grep
  • Gzip
  • Make
  • Patch
  • Sed
  • Tar
  • Xz
  • Binutils
  • GCC
ln -sv libncursesw.so $LFS/usr/lib/libncurses.so

7. chroot environment

  • virtual kernel file system
mkdir -pv $LFS/{dev,proc,sys,run}

mount -vt devpts devpts -o gid=5,mode=0620 $LFS/dev/pts
mount -vt proc proc $LFS/proc
mount -vt sysfs sysfs $LFS/sys
mount -vt tmpfs tmpfs $LFS/run

reference

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