fsck 在哪里运行?

sys*_*ake 6 kernel fsck systemd init sysvinit

在此处输入图片说明我正在阅读启动期间运行的所有内容,并看到在安装 rootfs/sbin/fsck.ext4之后,运行和 systemd 运行之后。我想知道在哪里或如何fsck运行,因为我在内核源代码中搜索它,但找不到它,它不是 init 脚本的一部分。那么运行fsck什么?我使用的发行版是 mint。

编辑:在此图像中显示在挂载根文件系统后运行 fsck

Tim*_*Tim 5

编辑 2:检查来源

我找到了 ubuntu initramfs-tools 来源。在这里你可以清楚地看到,Begin: "Mounting root file system"消息是先打印的,但在mount_root函数中fsck是在实际挂载之前运行的。我省略了一些不相关的代码,只是为了表明顺序。(如果您要检查链接的源,您还会从屏幕截图中找到其他报告的脚本)。

/init 第 256 行

log_begin_msg "Mounting root file system"
# Always load local and nfs (since these might be needed for /etc or
# /usr, irrespective of the boot script used to mount the rootfs).
. /scripts/local
. /scripts/nfs
. /scripts/${BOOT}
parse_numeric ${ROOT}
maybe_break mountroot
mount_top
mount_premount
mountroot
log_end_msg
Run Code Online (Sandbox Code Playgroud)

/scripts/local @第 244 行

mountroot()
{
    local_mount_root
}
Run Code Online (Sandbox Code Playgroud)

/scripts/local @第 131 行

local_mount_root()
{
# Some code ommited
    # FIXME This has no error checking
    [ -n "${FSTYPE}" ] && modprobe ${FSTYPE}

    checkfs ${ROOT} root "${FSTYPE}"

    # FIXME This has no error checking
    # Mount root
    mount ${roflag} ${FSTYPE:+-t ${FSTYPE} }${ROOTFLAGS} ${ROOT} ${rootmnt}
    mountroot_status="$?"
    if [ "$LOOP" ]; then
        if [ "$mountroot_status" != 0 ]; then
            if [ ${FSTYPE} = ntfs ] || [ ${FSTYPE} = vfat ]; then
                panic "<Error message ommited>"
            fi
        fi

        mkdir -p /host
        mount -o move ${rootmnt} /host
# Some code ommitted
}
Run Code Online (Sandbox Code Playgroud)

原答案,因历史原因保留

两种选择:

  1. Root 在引导期间以只读方式挂载,并且init实现正在运行fsckSystemd是mint 上的init实现,因为你已经检查过它是否存在,所以这个选项不适用。
  2. /sbin/fsck.ext4“早期用户空间”中运行,由initramfs设置。在您的系统中很可能就是这种情况。

系统化

即使您注意到/sbin/fsck.ext4之前运行过systemd,我也想详细说明一下。Systemd完全有能力fsck在只读挂载的文件系统上自行运行。请参阅systemd-fsck@.service文档。很可能这个服务在 mint 中默认没有启用,因为它对于早期的用户空间服务来说是多余的。

初始文件

我不知道 initramfs mint 的哪个实现正在运行,但我将dracut用作示例。(在 Debian、openSuse 等中使用)它在其挂载准备文档中说明了以下内容:

当根文件系统最终变得可见时:

  • 任何无法在已挂载的根文件系统上运行的维护任务都已完成。
  • 根文件系统以只读方式挂载。
  • 任何必须继续运行的进程(例如 rd.splash 屏幕助手及其命令 FIFO)都被提升到新安装的根文件系统中。

维护任务包括fsck. 进一步的证据,在dracut cmdline 选项中有可能关闭fsck

跳过文件

对于 rootfs 和 /usr 跳过 fsck。如果您以只读方式挂载 /usr 并且 init 系统在重新挂载之前执行 fsck,您可能需要使用此选项来避免重复

initramfs 的实现

可以使用systemd 基础设施实现动态(基于 udev)和灵活的 initramfs 。Dracut 就是这样一个实现,可能有发行版想要编写自己的发行版。

另一种选择是基于脚本的 initramfs。在这种情况下,busybox ash它被用作脚本外壳,甚至可能替换udevmdev,或者可能只是完全静态的。我发现有些人busybox由于某些 fsck 错误 int mint被丢弃到shell,所以这个实现可以适用于 mint。

如果您真的想确定,请尝试解压缩 initramfs 文件/boot并查看其中的内容。也可能会看到它安装在/initramfs.