Discussion:
How to detect single user mode in FreeBSD ?
(too old to reply)
Manish Jain
2018-06-12 15:08:08 UTC
Permalink
Hi,


I am writing an fsck wrapper which first needs to check if the system is
in single user mode.

Under Linux, there appears to be a sysctl that shows whether the system
is in single user

mode:  sysctl -n kern.singleuser. Is there some similar facility under
FreeBSD too ?


Thanks for any tips.

Manish Jain
Shane Ambler
2018-06-13 10:25:29 UTC
Permalink
Post by Manish Jain
Hi,
I am writing an fsck wrapper which first needs to check if the system is
in single user mode.
Under Linux, there appears to be a sysctl that shows whether the system
is in single user
mode:  sysctl -n kern.singleuser. Is there some similar facility under
FreeBSD too ?
I don't think there is a definite way to know. Single user mode is only
meant to be for interactive recovery.

When in single user mode PID 1 should be "/sbin/init -s" which becomes
"/sbin/init --" in multi user mode.

The next closet to knowing would be looking at mount, in single user
mode you will only have / mounted read only and /dev. It is rare for any
system to be past single user mode with only that mount setup but the
user can mount the filesystems before starting your script and still be
in single user mode.

You could maybe take a hint from the firstboot option. By default a file
called /firstboot is created and if it exists it means the system has
not been started since it was installed, then a series of setup steps
can be carried out.

You should note that you do not have to be in single user mode to run
fsck, it is in fact designed to run in the background even after a user
has logged in and started various tasks.

From man fsck -

If background checking is available, fsck is invoked twice. It is first
invoked at the traditional time, before the file systems are mounted,
with the -F flag to do checking on all the file systems that cannot do
background checking. It is then invoked a second time, after the system
has completed going multi-user, with the -B flag to do checking on all
the file systems that can do background checking.
--
FreeBSD - the place to B...Software Developing

Shane Ambler
Manish Jain
2018-06-13 12:34:08 UTC
Permalink
Post by Shane Ambler
When in single user mode PID 1 should be "/sbin/init -s" which becomes
"/sbin/init --" in multi user mode.
The next closet to knowing would be looking at mount, in single user
mode you will only have / mounted read only and /dev. It is rare for any
system to be past single user mode with only that mount setup but the
user can mount the filesystems before starting your script and still be
in single user mode.
Hi Shane,

Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ? It makes things
easily documentible, just as when we can use sysctl to find out if the
OS is virtualized (I think kern.vm_guest).

Manish Jain
Steve O'Hara-Smith
2018-06-13 13:12:17 UTC
Permalink
On Wed, 13 Jun 2018 18:04:08 +0530
Post by Manish Jain
Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ? It makes things
The runlevel concept comes from sysV init, it isn't a BSD thing at
all. There isn't a real difference at all between single user and multi
user. Single user mode is really just one of two states - before enabling
ttys and running the rc scripts with start or after disabling the ttys and
running the rc.shutdown script. There's nothing special about it.

If you feel a need to test for single user then you should probably
be testing for something else - such as whether a filesystem is mounted
before running fsck on it which is what really matters and is completely
independent of whether or not the system is running multi-user.
--
Steve O'Hara-Smith <***@sohara.org>
Valeri Galtsev
2018-06-13 14:46:47 UTC
Permalink
Post by Steve O'Hara-Smith
On Wed, 13 Jun 2018 18:04:08 +0530
Post by Manish Jain
Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ? It makes things
The runlevel concept comes from sysV init, it isn't a BSD thing at
all. There isn't a real difference at all between single user and multi
user. Single user mode is really just one of two states - before enabling
ttys and running the rc scripts with start or after disabling the ttys and
running the rc.shutdown script. There's nothing special about it.
If you feel a need to test for single user then you should probably
be testing for something else - such as whether a filesystem is mounted
before running fsck on it which is what really matters and is completely
independent of whether or not the system is running multi-user.
Thanks, Steve, for great explanation!

Being Linux refugee myself I know the gang from inside: we fall into
wide spread of the spectrum, from learning hard (or re-learning and
expanding knowledge in case of those of us who had a journey from UNIX
to Linus and back) to actively demanding introduction into FreeBSD of
familiar things: Linuxisms. Explanations like yours always are
ultimately teach Linux refugees what FreeBSD is, and hopefully makes
them (us ;-) stop demanding introduction of Linuxisms into FreeBSD.

Valeri
--
++++++++++++++++++++++++++++++++++++++++
Valeri Galtsev
Sr System Administrator
Department of Astronomy and Astrophysics
Kavli Institute for Cosmological Physics
University of Chicago
Phone: 773-702-4247
++++++++++++++++++++++++++++++++++++++++
blubee blubeeme
2018-06-13 23:20:17 UTC
Permalink
Post by Valeri Galtsev
Post by Steve O'Hara-Smith
On Wed, 13 Jun 2018 18:04:08 +0530
Post by Manish Jain
Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ? It makes things
The runlevel concept comes from sysV init, it isn't a BSD thing at
all. There isn't a real difference at all between single user and multi
user. Single user mode is really just one of two states - before enabling
ttys and running the rc scripts with start or after disabling the ttys
and
Post by Steve O'Hara-Smith
running the rc.shutdown script. There's nothing special about it.
If you feel a need to test for single user then you should probably
be testing for something else - such as whether a filesystem is mounted
before running fsck on it which is what really matters and is completely
independent of whether or not the system is running multi-user.
Thanks, Steve, for great explanation!
Being Linux refugee myself I know the gang from inside: we fall into
wide spread of the spectrum, from learning hard (or re-learning and
expanding knowledge in case of those of us who had a journey from UNIX
to Linus and back) to actively demanding introduction into FreeBSD of
familiar things: Linuxisms. Explanations like yours always are
ultimately teach Linux refugees what FreeBSD is, and hopefully makes
them (us ;-) stop demanding introduction of Linuxisms into FreeBSD.
I can't agree with you more. FreeBSD isn't a Linux distribution.
Post by Valeri Galtsev
Valeri
--
++++++++++++++++++++++++++++++++++++++++
Valeri Galtsev
Sr System Administrator
Department of Astronomy and Astrophysics
Kavli Institute for Cosmological Physics
University of Chicago
Phone: 773-702-4247
++++++++++++++++++++++++++++++++++++++++
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "
JD
2018-06-13 23:30:29 UTC
Permalink
Post by blubee blubeeme
Post by Valeri Galtsev
Post by Steve O'Hara-Smith
On Wed, 13 Jun 2018 18:04:08 +0530
Post by Manish Jain
Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ? It makes things
The runlevel concept comes from sysV init, it isn't a BSD thing at
all. There isn't a real difference at all between single user and multi
user. Single user mode is really just one of two states - before enabling
ttys and running the rc scripts with start or after disabling the ttys
and
Post by Steve O'Hara-Smith
running the rc.shutdown script. There's nothing special about it.
If you feel a need to test for single user then you should probably
be testing for something else - such as whether a filesystem is mounted
before running fsck on it which is what really matters and is completely
independent of whether or not the system is running multi-user.
Thanks, Steve, for great explanation!
Being Linux refugee myself I know the gang from inside: we fall into
wide spread of the spectrum, from learning hard (or re-learning and
expanding knowledge in case of those of us who had a journey from UNIX
to Linus and back) to actively demanding introduction into FreeBSD of
familiar things: Linuxisms. Explanations like yours always are
ultimately teach Linux refugees what FreeBSD is, and hopefully makes
them (us ;-) stop demanding introduction of Linuxisms into FreeBSD.
I can't agree with you more. FreeBSD isn't a Linux distribution.
To the original poster of this question:
Does the command
who -r
still work in FBSD?
Manish Jain
2018-06-14 00:18:43 UTC
Permalink
Post by JD
Post by blubee blubeeme
Post by Valeri Galtsev
Post by Steve O'Hara-Smith
On Wed, 13 Jun 2018 18:04:08 +0530
Post by Manish Jain
Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ? It makes things
       The runlevel concept comes from sysV init, it isn't a BSD
thing at
all. There isn't a real difference at all between single user and multi
user. Single user mode is really just one of two states - before enabling
ttys and running the rc scripts with start or after disabling the ttys
and
Post by Steve O'Hara-Smith
running the rc.shutdown script. There's nothing special about it.
       If you feel a need to test for single user then you should
probably
be testing for something else - such as whether a filesystem is mounted
before running fsck on it which is what really matters and is completely
independent of whether or not the system is running multi-user.
Thanks, Steve, for great explanation!
Being Linux refugee myself I know the gang from inside: we fall into
wide spread of the spectrum, from learning hard (or re-learning and
expanding knowledge in case of those of us who had a journey from UNIX
to Linus and back) to actively demanding introduction into FreeBSD of
familiar things: Linuxisms. Explanations like yours always are
ultimately teach Linux refugees what FreeBSD is, and hopefully makes
them (us ;-) stop demanding introduction of Linuxisms into FreeBSD.
I can't agree with you more. FreeBSD isn't a Linux distribution.
Does the command
who -r
still work in FBSD?
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to
who -r returns error
Steve O'Hara-Smith
2018-06-14 07:38:40 UTC
Permalink
On Wed, 13 Jun 2018 17:30:29 -0600
Post by JD
Does the command
who -r
still work in FBSD?
It never did, and still doesn't.
--
Steve O'Hara-Smith <***@sohara.org>
Polytropon
2018-06-14 16:43:29 UTC
Permalink
Post by Steve O'Hara-Smith
On Wed, 13 Jun 2018 17:30:29 -0600
Post by JD
Does the command
who -r
still work in FBSD?
It never did, and still doesn't.
As FreeBSD doesn't have runlevels (SysV-specific thing),
no command (!) will be able to print one. Maybe this will
change when FreeBSD starts using systemd... ;-)
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Valeri Galtsev
2018-06-14 17:07:05 UTC
Permalink
Post by Polytropon
Post by Steve O'Hara-Smith
On Wed, 13 Jun 2018 17:30:29 -0600
Post by JD
Does the command
who -r
still work in FBSD?
It never did, and still doesn't.
As FreeBSD doesn't have runlevels (SysV-specific thing),
no command (!) will be able to print one. Maybe this will
change when FreeBSD starts using systemd... ;-)
Which I hope it will not. I already fled away from the mess in one
place, would be sad to flee the same mess again. (Of course, I migrated
server from Linux to FreeBSD long before systemd and friends era ;-)

Valeri
--
++++++++++++++++++++++++++++++++++++++++
Valeri Galtsev
Sr System Administrator
Department of Astronomy and Astrophysics
Kavli Institute for Cosmological Physics
University of Chicago
Phone: 773-702-4247
++++++++++++++++++++++++++++++++++++++++
Steve O'Hara-Smith
2018-06-15 19:00:51 UTC
Permalink
On Thu, 14 Jun 2018 18:43:29 +0200
Post by Polytropon
change when FreeBSD starts using systemd... ;-)
<shudder> I'll probably decamp to DragonFly or NetBSD then.
--
Steve O'Hara-Smith | Directable Mirror Arrays
C:\>WIN | A better way to focus the sun
The computer obeys and wins. | licences available see
You lose and Bill collects. | http://www.sohara.org/
Polytropon
2018-06-15 21:21:41 UTC
Permalink
Post by Steve O'Hara-Smith
On Thu, 14 Jun 2018 18:43:29 +0200
Post by Polytropon
change when FreeBSD starts using systemd... ;-)
<shudder> I'll probably decamp to DragonFly or NetBSD then.
Luckily, systemd is highly incompatible to FreeBSD's principle
designs, and it maintains a tight coupling to many Linux kernel
facilities. We hopefully don't have to worry. :-)
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
Antonio Olivares
2018-06-16 12:41:25 UTC
Permalink
Post by Polytropon
Post by Steve O'Hara-Smith
On Thu, 14 Jun 2018 18:43:29 +0200
Post by Polytropon
change when FreeBSD starts using systemd... ;-)
<shudder> I'll probably decamp to DragonFly or NetBSD then.
Luckily, systemd is highly incompatible to FreeBSD's principle
designs, and it maintains a tight coupling to many Linux kernel
facilities. We hopefully don't have to worry. :-)
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "
freebsd-questions-***@freebsd.org"
Resistance is futile. A BSD* variant known as systembsd is being worked on
as we argue about this.

https://www.reddit.com/r/linux/comments/2fsh5m/systembsd_a_systemd_compatibility_layer_for_bsd/

Then there would be no escape! And many system administrators would have to
create a FreeBSDuan like the Debian ones who created Devuan to not have
systemd on their FreeBSD.

Best Regards,


Antonio
Valeri Galtsev
2018-06-16 16:44:12 UTC
Permalink
Post by Antonio Olivares
Post by Polytropon
Post by Steve O'Hara-Smith
On Thu, 14 Jun 2018 18:43:29 +0200
Post by Polytropon
change when FreeBSD starts using systemd... ;-)
<shudder> I'll probably decamp to DragonFly or NetBSD then.
Luckily, systemd is highly incompatible to FreeBSD's principle
designs, and it maintains a tight coupling to many Linux kernel
facilities. We hopefully don't have to worry. :-)
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "
Resistance is futile. A BSD* variant known as systembsd is being worked on
as we argue about this.
https://www.reddit.com/r/linux/comments/2fsh5m/systembsd_a_systemd_compatibility_layer_for_bsd/
Then there would be no escape! And many system administrators would have to
create a FreeBSDuan like the Debian ones who created Devuan to not have
systemd on their FreeBSD.
When Linux went systemd (and yes, it is Linux, not any particular
distribution, as: once in kernel, the darn code IS in kernel, and it will
be tremendous effort to strip the crap out of kernel all the time, every
release to maintain systemd-free distro like devuan... My thought then
was: well, it pretty much looks like the deed of Linux rivals. If you can
not defeat your enemy, get inside, and make them become piece of crap from
inside. In every thing happening one can look at the reasons from the
viewpoint: who benefits from it. Linux was "stealing" a lot of potential
customers from proprietary system vendor monster[s]. We will not name the
name even though one obvious is Microsoft. And after all it was Microsoft
who "donated" to RedHat a big chunk on money. And do not look for time
correlation, which may not appear to exist, better look at: who stuffed
systemd down the throats of Linux folks. Yes, it was RedHat employee,
right? And it was RedHat who was paid by MS to push Linux towards quite MS
Windows looking system administration wise. And the same sickening system
administration wise. Note that the REASON I alleged is there is my own
opinion, and as everyone has the same and even better brain than mine, go
ahead and come up with the most reasonable for you explanation.

That thing done made a big wave of refugees from Linux (I fled servers
from Linux to FreeBSD even earlier due to a bit different reason: I just
got sick of the need to reboot Linux servers every 45 days on average due
to either kernel or glibc security updates).

Now this leaves everybody with: most active and opinionated about how not
to and how do to administer the system sysadmins fleeing away from Linux.
And more inert (sorry, folks, I may be called that myself too) and "home
users" stay and adjust to new brew MS Windows like and progressing that
way Linux system. It will then allow more or less unified "Linux
Administration certification" similar to MS one, and same based on
learning by heart where to click. And if click does not work, it is up to
the support call to the vendor to resolve the issue. Which is the hiring
dream for IT illiterate IT managers one can come across almost everywhere
these days. Who are hired by people who don't and shouldn't have any IT
knowledge on the basis of larger Certificate collection.

Which leaves big boys with almost only one rival (with multiple faces):
*BSD. This is why I am scared as hell to see MS donate to any of *BSDs.
FreeBSD and OpenBSD got donations already, if my memory still serves me.
My worst nightmare is to think what the donor will ultimately get as
return for their investment. Yes, in the commercial world there is no such
thing as gift or donation. Any money or favor is an investment with the
goal of getting return on investment which is larger (often much larger)
than investment itself.

This all is dark picture, alas, but such is the reality. Everything
flowerful in the end turns out to be our wishful thinking.

Valeri

PS I was praising RedHat for their excellent compliance to GPL, and
variety of other great things, like buying out the creator of cygwin
Cygnus Solutions, and keeping cywgin open source freely available, and
being developed, not buried. Now though one big bad thing changes my
opinion.
Post by Antonio Olivares
Best Regards,
Antonio
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to
++++++++++++++++++++++++++++++++++++++++
Valeri Galtsev
Sr System Administrator
Department of Astronomy and Astrophysics
Kavli Institute for Cosmological Physics
University of Chicago
Phone: 773-702-4247
++++++++++++++++++++++++++++++++++++++++

Polytropon
2018-06-13 13:46:49 UTC
Permalink
Post by Manish Jain
Post by Shane Ambler
When in single user mode PID 1 should be "/sbin/init -s" which becomes
"/sbin/init --" in multi user mode.
The next closet to knowing would be looking at mount, in single user
mode you will only have / mounted read only and /dev. It is rare for any
system to be past single user mode with only that mount setup but the
user can mount the filesystems before starting your script and still be
in single user mode.
Hi Shane,
Tx for replying. But don't you think there should ideally be a sysctl to
be detect the runlevel, particularly single user mode ?
Currently, there is no such sysctl availablr (or another
utility that prints the current runlevel - see runlevel-
related comment later on).
Post by Manish Jain
It makes things
easily documentible, just as when we can use sysctl to find out if the
OS is virtualized (I think kern.vm_guest).
You can probably work with the information from "man 8 init", such as

If the system security level (see security(7)) is initially nonzero, then
init leaves it unchanged. Otherwise, init raises the level to 1 before
going multi-user for the first time. Since the level cannot be reduced,
it will be at least 1 for subsequent operation, even on return to single-
user. If a level higher than 1 is desired while running multi-user, it
can be set before going multi-user, e.g., by the startup script rc(8),
using sysctl(8) to set the kern.securelevel variable to the required
security level.

[...]

If run as a user process as shown in the second synopsis line, init will
emulate AT&T System V UNIX behavior, i.e., super-user can specify the
desired run-level on a command line, and init will signal the original
(PID 1) init as follows:

Run-level Signal Action
0 SIGUSR2 Halt and turn the power off
1 SIGTERM Go to single-user mode
6 SIGINT Reboot the machine
c SIGTSTP Block further logins
q SIGHUP Rescan the ttys(5) file

See "man 8 init" for further details.



For details about the run-levels (which aren't that common to
the FreeBSD boot & startup mechanism, which is BSD-style init,
whereas runlevels appear in System V sysvinit-like mechanisms),
check:

https://docs.freebsd.org/doc/7.4-RELEASE/usr/share/doc/en_US.ISO8859-1/articles/linux-users/startup.html

https://www.freebsd.org/doc/en_US.ISO8859-1/articles/linux-users/startup.html

You could summarize it as "FreeBSD doesn't have runlevels". :-)



However, you can still check for a process that is typucally
run in multi-user mode. If it's _not_ running - well, you are
in single-user mode.

Such a test could look like this:

if ! ps -auxww | grep -v "grep" | grep "adjkerntz" > /dev/null; then
echo "Single user";
else
echo "Multi-user";
fi

In this example, a running instance of adjkerntz is being
process-grepped for.

Suggestion found here:

https://superuser.com/questions/457111/how-to-get-run-level-in-freebsd
--
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...
jungle Boogie
2018-06-13 23:39:21 UTC
Permalink
Post by Manish Jain
Hi,
I am writing an fsck wrapper which first needs to check if the system is
in single user mode.
Describe the problem you want to solve, not how.
Manish Jain
2018-06-14 00:29:16 UTC
Permalink
Post by jungle Boogie
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 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.

Ideally, the script should run only under single user mode, or else bail
out immediately.

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.

Tx and Regards
Manish Jain
JD
2018-06-14 00:48:19 UTC
Permalink
Post by Manish Jain
Post by jungle Boogie
Describe the problem you want to solve, not how.
I am writing a shell script which can run fsck on all UFS / ext2 /ext4
hard disk partitions listed in /etc/fstab.
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.
Ideally, the script should run only under single user mode, or else
bail out immediately.
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.
Tx and Regards
Manish Jain
You need to realize that to run fsck on a filesystem, it has to be
UNMOUNTED, unless
you provide the -n option to fsck (which means no write - so the mounted
fs will not be
corrupted).
Lynux will not allow the unmounting of / and remounting as read only.
So, you will not be able to fsck the device which provides the root fs,
at least in lynux.
It used to be that FBSD could run in single user mode with / mounted as
ro (read only),
but I have not booted my FBSD machine for quite some time, so I am not
sure id
FBSD 10.x will let me do that.
jungle Boogie
2018-06-14 01:26:26 UTC
Permalink
Post by JD
Post by Manish Jain
Post by jungle Boogie
Describe the problem you want to solve, not how.
I am writing a shell script which can run fsck on all UFS / ext2 /ext4
hard disk partitions listed in /etc/fstab.
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.
Ideally, the script should run only under single user mode, or else
bail out immediately.
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.
Tx and Regards
Manish Jain
You need to realize that to run fsck on a filesystem, it has to be
UNMOUNTED, unless
you provide the -n option to fsck (which means no write - so the mounted
fs will not be
corrupted).
Lynux will not allow the unmounting of / and remounting as read only.
So, you will not be able to fsck the device which provides the root fs,
at least in lynux.
It used to be that FBSD could run in single user mode with / mounted as
ro (read only),
but I have not booted my FBSD machine for quite some time, so I am not
sure id
FBSD 10.x will let me do that.
So given what JD has said, couldn't you run fsck and see what the return
message is and act on that?
Steve O'Hara-Smith
2018-06-14 07:47:52 UTC
Permalink
On Thu, 14 Jun 2018 05:59:16 +0530
Post by Manish Jain
Post by jungle Boogie
Describe the problem you want to solve, not how.
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.
Post by Manish Jain
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.
Post by Manish Jain
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.
Post by Manish Jain
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.
--
Steve O'Hara-Smith <***@sohara.org>
Polytropon
2018-06-14 16:52:54 UTC
Permalink
Post by Steve O'Hara-Smith
On Thu, 14 Jun 2018 05:59:16 +0530
Post by Manish Jain
Post by jungle Boogie
Describe the problem you want to solve, not how.
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).
Post by Steve O'Hara-Smith
Post by Manish Jain
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?
Post by Steve O'Hara-Smith
Post by Manish Jain
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
Post by Steve O'Hara-Smith
Post by Manish Jain
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, ...
Loading...