Linux 和应用程序的接口

Linux 和应用程序的接口

2025 南京大学《操作系统原理》
Linux 和应用程序的接口

除了对象和系统调用 API

The last piece: “初始状态”

  • Everything is a state machine
    • “操作系统中的对象” 应该也有一个初始状态

它是什么呢?

  • 观察到 Linux 的系统更新
    • “update-initramfs... (漫长的等待)”
    • 这就是 Linux Kernel 启动后的 “初始状态”
2025 南京大学《操作系统原理》
Linux 和应用程序的接口

initramfs

我们的 initramfs

  • 可以只有一个 init 文件
    • 可以是任意的 binary,包括我们自己实现的
  • (系统启动后,Linux 还会增加 /dev 和 /dev/console)
    • 需要给 stdin/stdout/stderr 一个 “地方”

实际的 initramfs (我们可以解开来看一看)

  • 基本的命令行工具
  • 基础设备驱动程序 (可能从磁盘/网络挂载文件系统)
  • 文件系统工具 (万一磁盘损坏了,还能抢救一下)
2025 南京大学《操作系统原理》
Linux 和应用程序的接口

点亮 Linux 世界

Busybox utilities

[ [[ acpid adjtimex ar arch arp arping ash awk basename bc blkdiscard blockdev
brctl bunzip2 busybox bzcat bzip2 cal cat chgrp chmod chown chpasswd chroot
chvt clear cmp cp cpio crond crontab cttyhack cut date dc dd deallocvt depmod
devmem df diff dirname dmesg dnsdomainname dos2unix dpkg dpkg-deb du dumpkmap
dumpleases echo ed egrep env expand expr factor fallocate false fatattr fdisk
fgrep find fold free freeramdisk fsfreeze fstrim ftpget ftpput getopt getty
grep groups gunzip gzip halt head hexdump hostid hostname httpd hwclock
i2cdetect i2cdump i2cget i2cset id ifconfig ifdown ifup init insmod ionice ip
ipcalc ipneigh kill killall klogd last less link linux32 linux64 linuxrc ln
loadfont loadkmap logger login logname logread losetup ls lsmod lsscsi lzcat
lzma lzop md5sum mdev microcom mkdir mkdosfs mke2fs mkfifo mknod mkpasswd
mkswap mktemp modinfo modprobe more mount mt mv nameif nc netstat nl nologin
nproc nsenter nslookup nuke od openvt partprobe passwd paste patch pidof ping
ping6 pivot_root poweroff printf ps pwd rdate readlink realpath reboot renice
reset resume rev rm rmdir rmmod route rpm rpm2cpio run-init run-parts sed seq
setkeycodes setpriv setsid sh sha1sum sha256sum sha512sum shred shuf sleep sort
ssl_client start-stop-daemon stat static-sh strings stty su sulogin svc svok
swapoff swapon switch_root sync sysctl syslogd tac tail tar taskset tc tee
telnet telnetd test tftp time timeout top touch tr traceroute traceroute6 true
truncate tty tunctl ubirename udhcpc udhcpd uevent umount uname uncompress
unexpand uniq unix2dos unlink unlzma unshare unxz unzip uptime usleep uudecode
uuencode vconfig vi w watch watchdog wc wget which who whoami xargs xxd xz
xzcat yes zcat
2025 南京大学《操作系统原理》
Linux 和应用程序的接口

initramfs: 并不是我们 “看到” 的 Linux 世界

启动的初级阶段

  • 加载剩余必要的驱动程序,例如磁盘/网卡
  • 挂载必要的文件系统
    • Linux 内核有启动选项 (类似环境变量)
      • /proc/cmdline (man 7 bootparam)
    • 读取 root filesystem 的 /etc/fstab
  • 将根文件系统和控制权移交给另一个程序,例如 systemd

启动的第二级阶段

  • 看一看系统里的 /sbin/init 是什么?
  • 计算机系统没有魔法 (一切都有合适的解释)
2025 南京大学《操作系统原理》
Linux 和应用程序的接口

构建 “真正” 应用世界的系统调用

switch_root 命令背后的系统调用

int pivot_root(const char *new_root, const char *put_old);
  • pivot_root() changes the root mount in the mount namespace of the calling process. More precisely, it moves the root mount to the directory put_old and makes new_root the new root mount. The calling process must have the CAP_SYS_ADMIN capability in the user namespace that owns the caller's mount namespace.
  • syscalls(2)
2025 南京大学《操作系统原理》
Linux 和应用程序的接口

我们可以自己接管一切

创建一个磁盘镜像

  • /sbin/init 指向任何一个程序
    • 同样需要加载必要的驱动
    • 例子:pivot_root 之后才加载网卡驱动、配置 IP
    • 例子:tty 字体变化
      • 这些都是 systemd 的工作
  • 例子:NOILinux Lite

Initramfs 会被释放

  • 功成身退
2025 南京大学《操作系统原理》
Linux 和应用程序的接口

That's All: 操作系统 = 对象的集合

初始状态

  • initramfs 中的对象 + /dev/console + 加载的 init

状态迁移

  • 选择一个进程 (对象) 执行一条指令
  • 系统调用指令
    • 进程管理: fork, execve, exit, waitpid, getpid, ...
    • 操作系统对象和访问: open, close, read, write, pipe, mount, mkfifo, mknod, stat, socket, ...
    • 地址空间管理: mmap, sbrk (mmap 的前身)
    • 以及一些其他的机制: pivot_root, chmod, chown, ...
2025 南京大学《操作系统原理》