From d2a5e65862872ed75dcae7a7d34370569ddf823c Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 1 Aug 2016 18:08:54 +0200 Subject: [PATCH] Support for checking file systems from /etc/fstab This patch adds support for running fsck on all file systems listed in /etc/fstab with a passno > 0. The fsck runs sequentially, starting with the root fs (passno 1). Signed-off-by: Joachim Nilsson --- README.md | 2 +- contrib/alpine/finit.conf | 3 --- contrib/debian/finit.conf | 3 --- doc/config.md | 3 ++- finit.c | 43 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ed3a4f36..90eab7cc 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ networking, [/etc/finit.d/](doc/config.md#etcfinitd) and the familiar # Fallback if /etc/hostname is missing host myhostname - # Devices to fsck at boot + # Devices to fsck at boot, only needed if not in /etc/fstab check /dev/vda1 # Runlevel to start after bootstrap, runlevel 'S' diff --git a/contrib/alpine/finit.conf b/contrib/alpine/finit.conf index 02c87b3c..1bc7585c 100644 --- a/contrib/alpine/finit.conf +++ b/contrib/alpine/finit.conf @@ -6,9 +6,6 @@ module button module evdev -# Devices to fsck at boot -check /dev/vda1 - # Runlevel to start after bootstrap, runlevel 'S' runlevel 2 diff --git a/contrib/debian/finit.conf b/contrib/debian/finit.conf index b4d7abd1..a6362ed1 100644 --- a/contrib/debian/finit.conf +++ b/contrib/debian/finit.conf @@ -8,9 +8,6 @@ host jessie # with a different user, using the @username syntax user root -# Devices to fsck at boot, /etc/fstab is not used -check /dev/vda1 - # Linux kernel modules to load, /etc/modprobe is not used module button module evdev diff --git a/doc/config.md b/doc/config.md index 6a8faf24..e7df4b33 100644 --- a/doc/config.md +++ b/doc/config.md @@ -27,7 +27,8 @@ like what a SysV init start script usually does. Syntax: * `check ` - Run fsck on a file system before mounting it + Run fsck on a file system. Only needed if not listed in `/etc/fstab`, + Finit 3.0 and later check all file systems automatically at boot. * `module ` Load a kernel module, with optional arguments diff --git a/finit.c b/finit.c index be2b6f5f..ad6f6f20 100644 --- a/finit.c +++ b/finit.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include /* umask(), mkdir() */ @@ -71,6 +72,40 @@ static int banner(void) return 0; } +/* + * Check all filesystems in /etc/fstab with a fs_passno > 0 + */ +static int fsck(int pass) +{ + struct fstab *fs; + + if (!setfsent()) { + _pe("Failed opening fstab"); + return 1; + } + + while ((fs = getfsent())) { + char cmd[80]; + struct stat st; + + if (fs->fs_passno < pass) + continue; + + errno = 0; + if (stat(fs->fs_spec, &st) || !S_ISBLK(st.st_mode)) { + _d("Cannot fsck %s, not a block device: %s", fs->fs_spec, strerror(errno)); + continue; + } + + snprintf(cmd, sizeof(cmd), "/sbin/fsck -C -a %s", fs->fs_spec); + run_interactive(cmd, "Checking file system %s", fs->fs_spec); + } + + endfsent(); + + return 0; +} + static void networking(void) { size_t i; @@ -231,6 +266,14 @@ int main(int argc, char* argv[]) #endif run_interactive(SETUP_DEVFS, "Populating device tree"); + /* + * Check file filesystems in /etc/fstab + */ + for (int pass = 1; pass < 10; pass++) { + if (fsck(pass)) + break; + } + /* * Load plugins first, finit.conf may contain references to * features implemented by plugins.