应用程序的世界

应用程序的世界

2024 南京大学《操作系统:设计与实现》
应用程序的世界

Initramfs: 并不是我们实际看到的 Linux

启动的初级阶段

  • 加载剩余必要的驱动程序,例如磁盘/网卡
  • 挂载必要的文件系统
  • 将根文件系统和控制权移交给另一个程序,例如 systemd

启动的第二级阶段

  • 看一看系统里的 /sbin/init 是什么?
  • 计算机系统没有魔法 (一切都有合适的解释)
    • pstree 埋下的伏笔得到解答
2024 南京大学《操作系统:设计与实现》
应用程序的世界

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

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)
2024 南京大学《操作系统:设计与实现》
应用程序的世界

/etc/fstab

systemd 通过 mount 系统调用配置文件系统

  • systemd: “system and service manager”, “provides a dependency system between units”

File System Table: 每行一个文件系统

  • 设备:/dev/sda1, UUID=XXXX
  • 挂载点 (mount point): /, /home, ...
  • 文件系统类型: ext4, vfat, ...
  • 挂载选项: ro, rw, ...
  • Dump/pass: 备份/检查标记
2024 南京大学《操作系统:设计与实现》
应用程序的世界

我们可以自己接管一切!

创建一个磁盘镜像

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

Initramfs 会被释放

  • 功成身退
2024 南京大学《操作系统:设计与实现》
应用程序的世界

例子

使用 sh-xv6

  • 在 Linux 系统调用上,完全可以全部使用自己的应用程序
  • 使用 Linux 系统调用的程序甚至不需要跑在 Linux 上

内核模块加载

  • 网卡驱动 e1000.ko

HTTP Server (busybox)

  • 监听 8080 端口,送出 CGI 脚本生成的动态内容
  • 在 2000 年,不用多少代码就可以挣大钱!(现在呢?)
2024 南京大学《操作系统:设计与实现》