1 of 14

rootfs 淺談

Carl Su

2 of 14

Linux 組成四大要素

  • Toolchain
  • Bootloader
  • Linux kernel
  • rootfs

3 of 14

rootfs 概觀

  • rootfs = Root File System
  • Linux kernel 以唯讀模式掛載 rootfs 到根目錄 /
  • Linux kernel 執行 /sbin/init/bin/sh 初始化系統
  • init 根據 /etc/fstab 重新掛載 rootfs 和其餘檔案系統
  • 幾大主流 Linux distro 使用 systemd,/sbin/init 由 systemd 接管

4 of 14

x86_64 開機流程

  1. BIOS/UEFI
  2. MBR/GPT
  3. Linux kernel
  4. /sbin/init

5 of 14

ARM 開機流程

  • ROM code
  • SPL (Secondary Program Loader)
  • TPL (Tertiary Program Loader)
  • Linux kernel
  • /sbin/init

6 of 14

檔案系統階層標準 (FHS)

7 of 14

rootfs 最小結構

  • /dev/
  • /proc/
  • /bin/
    • Utilities: sh, ls, cp, mv, etc.
  • /etc/
    • fstab, group, passwd, profile
  • /sbin/
    • init
  • /lib/
    • Runtime libs: glibc, musl, etc.
  • /mnt/
  • /usr/
  • /tmp/

8 of 14

建立 rootfs 的方式

  • 手動建立
  • 使用現成系統
    • archlinux-bootstrap-2020.07.01-x86_64.tar.gz
  • rootfs 製作工具
    • debootstrap, cdebootstrap
    • arch-install-scripts
    • Buildroot
    • Project Yocto

9 of 14

使用 Arch Linux bootstrap image

  • sudo tar xf archlinux-bootstrap-2020.07.01-x86_64.tar.gz

10 of 14

使用 Ubuntu bootstrap image

  • mkdir root.focal64

  • debootstrap --arch=amd64 focal ./root.focal64 http://free.nchc.org.tw/ubuntu/

11 of 14

Chroot 歷史

  • 1979: chroot 系統呼叫首次問世。
  • 1982: Bill Joy 移植 chroot 到 BSD。
  • 1991: William Cheswick 開發出第一個蜜罐程式。
  • 2000: FreeBSD 團隊開發 jail 指令。
  • 2004: 駭客開發破解 chroot 的方法,出現了 jailbreak 術語。
  • 2005: 昇陽電腦開發 Solaris Containers,稱它是吃了類固醇的 chroot。
  • 2006: Linux 團隊開發 cgroups。
  • 2008: Linux 團隊基於 cgroups 開發 LXC。
  • 2013: Kernel 3.8 內建 LXC,同時 Docker 首次問世。

12 of 14

進入 Arch Linux rootfs

  • mount --bind root.x86_64 root.x86_64
  • sudo root.x86_64/bin/arch-chroot root.x86_64
  • 編輯 root.x86_64/etc/pacman.d/mirrorlist
  • pacman-key --init
  • pacman-key --populate archlinux
  • pacman -Syy
  • pacman -S base --noconfirm

13 of 14

進入 Ubuntu 20.04 rootfs

  • sudo chroot root.focal64 /bin/bash

14 of 14

更多資源

  • FHS Spec:
  • Linux From Scratch (LFS)
  • 建立基於 BusyBox 的 rootfs