Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Jun 2018 18:52:54 +0200
From:      Polytropon <freebsd@edvax.de>
To:        "Steve O'Hara-Smith" <steve@sohara.org>
Cc:        Manish Jain <jude.obscure@yandex.com>, FreeBSD Questions <freebsd-questions@freebsd.org>, jungle Boogie <jungleboogie0@gmail.com>
Subject:   Re: How to detect single user mode in FreeBSD ?
Message-ID:  <20180614185254.6099d295.freebsd@edvax.de>
In-Reply-To: <20180614084752.bd90d2277cdcaf099c501bdc@sohara.org>
References:  <e9731c0f-1269-8919-836a-29b9a2f6b0dc@yandex.com> <CAKE2PDsJqX5g61wYGhKxXv6CMNbdFVLTCvfhG5FY06o6cqw10Q@mail.gmail.com> <89bc6774-aa0d-6704-71a1-6b8eea8ae3b5@yandex.com> <20180614084752.bd90d2277cdcaf099c501bdc@sohara.org>

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 <jude.obscure@yandex.com> 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:

#!/bin/sh

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

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

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



> > 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.





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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20180614185254.6099d295.freebsd>