Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Jun 2018 18:52:54 +0200
From:      Polytropon <>
To:        "Steve O'Hara-Smith" <>
Cc:        Manish Jain <>, FreeBSD Questions <>, jungle Boogie <>
Subject:   Re: How to detect single user mode in FreeBSD ?
Message-ID:  <>
In-Reply-To: <>
References:  <> <> <> <>

Next in thread | Previous in thread | Raw E-Mail | Index | Archive | Help
On Thu, 14 Jun 2018 08:47:52 +0100, Steve O'Hara-Smith wrote:
> On Thu, 14 Jun 2018 05:59:16 +0530
> Manish Jain <> wrote:
> > On 06/14/18 05:09, jungle Boogie wrote:
> > > Describe the problem you want to solve, not how.
> > 
> > The problem is this:
> > 
> > I am writing a shell script which can run fsck on all UFS / ext2 /ext4 
> > hard disk partitions listed in /etc/fstab.
> 	The one mounted on / is going to be a problem in Linux.
> 	I'm not sure I see the point in such a script though, the boot time
> checks have always been adequate in my experience but hey you want it so
> fine.

The core idea of FreeBSD's automatic boot is that fsck will
be run automatically anyway if needed. If serious problems
are being encountered, the SUM "emergency" shell will be
invoked, inviting the user to re-run fsck with the desired
option. The choice _which_ file systems to check, with the
filesystem specification needed, is obtained from /etc/fstab.

So in worst case, you could do

	# fsck -yf

manually (no script needed), if you _know_ what you're doing
with this command. In worst case, don't use -y right away.
Of course this implies that the needed information is already
present in /etc/fstab (the FreeBSD and Linux partitions need
to have a correct entry each).

> > The script should be portable and be able to run no matter whether the 
> > OS running is FreeBSD or Linux. The only thing that matters is that the 
> > commands fsck_ufs / fsck.ext2 and fsck.ext4 are available.
> 	Fine.

Doesn't Linux have a similar built-in checking mechanism?

> > Ideally, the script should run only under single user mode, or else bail 
> > out immediately.
> 	Why ? It would be far more sensible for the script to check each
> filesystem to see if it is mounted and run fsck if it is not. That way it
> will run correctly at any time, including in single user with filesystems
> mounted which is a perfectly feasible condition.

It's possible to check file systems in multi-user mode as
well, but as you correctly mentioned, they need to be unmounted
to allow modifications (if needed).

I have such a script (even though for a different purpose),
it checks like this:


PARTITIONS="/usr /home /export /whatever"

# ... some command-line flag checking here ...

	MOUNTED=`mount | grep "on ${PARTITION}"`
	if [ "${MOUNTED}"  != "" ]; then
		if [ `id -un` = "root" ]; then
			# ...
			# do "privileged" stuff here
			# ...
		echo "${PARTITION} ${STATUS}"
		echo "${PARTITION} currently not operational"

> > Linux has a clean way to find out whether the system is in single user 
> > mode. I would think, no matter what others on this list have said, 
> > sysctl under FreeBSD too should have a variable for indicating single 
> > user mode. But there currently is not any.
> 	I can't think of any time when single user mode is the correct
> thing to be checking for before doing something.

It is typical do resort to single-user mode in case of severe
problems (or at least when expecting them); using the kernel
debugger to "surprisingly" reboot the machine with unclean
filesystems and then going "boot -s", followed by a problem
analysis in single user mode, with the correct steps taken
(file recovery, file system repair, etc.). This is convenient
as it makes perfectly sure no other programs are being run
that _might_ change filesystem content, which in case of an
upcoming recovery is the last thing you want to happen.

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

Want to link to this message? Use this URL: <>