4

I discovered that is not possible to run fsck on a loopback device at boot by the fsck flag inside the fstab file, nor is it possible to accomplish this by manually running fsck when the loop device is mounted.

Is there an alternative to check the device at boot time?

4
  • doing it in a script? Dec 31, 2017 at 15:27
  • Or as a service. Dec 31, 2017 at 16:35
  • Fsck must runs before the loopback device mount. So putting it in a script (in /etc/rc.local for example) unfortunately is not suitable. Dec 31, 2017 at 21:22
  • Perhaps you could expand with details; maybe showing the /etc/fstab you thought would work. It should work if the mount line includes the file system type, and the fsck pass is greater than the file system containing the loop-backed partition Jan 1, 2018 at 2:07

2 Answers 2

4

The key phrase there is "when the loop device is mounted". You can run fsck on an unmounted loopback device.

In other words, you can run losetup to create the loopback block device and attach it the the relevant file, then run fsck on it, then mount it.

This, of course, can be scripted. You could even do it in /etc/rc.local or similar if the loopback mounts were flagged noauto in /etc/fstab - then do the losetup/fsck/mount for each loopback fs in /etc/fstab.

Note that is kind of an abuse of the noauto flag because the loopback filesystem(s) will end up being automatically mounted. Comment out any loopback filesystems that you don't want auto-mounted.

Alternatively, you could modify whatever code runs mount -a on your system (systemd, sysvinit shell script, or whatever) so that it runs mount -a -tnoloop instead, so that loopback filesystems are never automounted like other filesystems. The script that does the losetup/fsck/mount could then be written to honour the noauto flag if present.

2
  • alternative is not possible for systemd, because that does not use mount -a.
    – sourcejedi
    Jan 1, 2018 at 12:40
  • Thank you for your proposal, but I would give you other information about my setup. I have setup my loopback device inside fstab because I need (for some reasons) to override some system paths (/home, /var and /u01) before any service start. If I setup my loop device by the rc.local script it will be mounted after services startup loosing so integrity of my overridden file system. Jan 2, 2018 at 7:38
0

I found an elegant and reliable solution.

I have writteng a script for then"/etc/initramfs-tools/scripts/local-premount/" boot phase in order to process my loop disk just before the file system mounting.

Below the details:

  1. Create the script into /etc/initramfs-tools/scripts/local-premount/.
  2. Update the initrd.img by the command update-initramfs -u.

Here is the script source:

#!/bin/sh

# Antonio Petricca <[email protected]> - 03/01/2018

PREREQ=""

# Output pre-requisites
prereqs()
{
        echo "$PREREQ"
}

case "$1" in
    prereqs)
        prereqs
        exit 0
        ;;
esac

. /scripts/functions

log_begin_msg "Running file system check on loop device(s)"

DEV=/dev/sdb5
MNT=/tmp/mnt
LOOP=$MNT/.linux-loops/242eef08-32d6-42c2-93eb-afdc2111a13e.ext4

mkdir $MNT && \
mount -t ntfs $DEV $MNT && \
fsck.ext4 -p -v $LOOP && \
umount $MNT

# Uncomment next line to hold messages for debugging
# sleep 10

log_end_msg "Done"

# Continue boot anyway

exit 0

Regards!

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .