How to detect single user mode in FreeBSD ?

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|

How to detect single user mode in FreeBSD ?

Manish Jain-2
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


_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Shane Ambler-5
On 13/06/2018 00:38, Manish Jain wrote:

> 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

_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Manish Jain-2
On 06/13/18 15:55, Shane Ambler wrote:
> 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
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Steve O'Hara-Smith
On Wed, 13 Jun 2018 18:04:08 +0530
Manish Jain <[hidden email]> wrote:

> 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 <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Polytropon
In reply to this post by Manish Jain-2
On Wed, 13 Jun 2018 18:04:08 +0530, Manish Jain wrote:

> On 06/13/18 15:55, Shane Ambler wrote:
> > 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).



> 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, ...
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Valeri Galtsev
In reply to this post by Steve O'Hara-Smith


On 06/13/18 08:12, Steve O'Hara-Smith wrote:

> On Wed, 13 Jun 2018 18:04:08 +0530
> Manish Jain <[hidden email]> wrote:
>
>> 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
++++++++++++++++++++++++++++++++++++++++
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

blubee blubeeme
On Wed, Jun 13, 2018, 22:49 Valeri Galtsev <[hidden email]>
wrote:

>
>
> On 06/13/18 08:12, Steve O'Hara-Smith wrote:
> > On Wed, 13 Jun 2018 18:04:08 +0530
> > Manish Jain <[hidden email]> wrote:
> >
> >> 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.
>

I can't agree with you more. FreeBSD isn't a Linux distribution.

>
> Valeri
> --
> ++++++++++++++++++++++++++++++++++++++++
> Valeri Galtsev
> Sr System Administrator
> Department of Astronomy and Astrophysics
> Kavli Institute for Cosmological Physics
> University of Chicago
> Phone: 773-702-4247
> ++++++++++++++++++++++++++++++++++++++++
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "
> [hidden email]"
>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

jd1008


On 06/13/2018 05:20 PM, blubee blubeeme wrote:

> On Wed, Jun 13, 2018, 22:49 Valeri Galtsev <[hidden email]>
> wrote:
>
>>
>> On 06/13/18 08:12, Steve O'Hara-Smith wrote:
>>> On Wed, 13 Jun 2018 18:04:08 +0530
>>> Manish Jain <[hidden email]> wrote:
>>>
>>>> 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.
>>
> 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?

_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Jungle Boogie
In reply to this post by Manish Jain-2
On 8:10AM, Tue, Jun 12, 2018 Manish Jain <[hidden email]> wrote:
>
> 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.
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Manish Jain-2
In reply to this post by jd1008
On 06/14/18 05:00, JD wrote:

>
>
> On 06/13/2018 05:20 PM, blubee blubeeme wrote:
>> On Wed, Jun 13, 2018, 22:49 Valeri Galtsev <[hidden email]>
>> wrote:
>>
>>>
>>> On 06/13/18 08:12, Steve O'Hara-Smith wrote:
>>>> On Wed, 13 Jun 2018 18:04:08 +0530
>>>> Manish Jain <[hidden email]> wrote:
>>>>
>>>>> 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.
>>>
>> 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?
>
> _______________________________________________
> [hidden email] mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to
> "[hidden email]"

who -r returns error

_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Manish Jain-2
In reply to this post by Jungle Boogie
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 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
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

jd1008


On 06/13/2018 06:29 PM, 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 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.



_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Jungle Boogie
On 5:49PM, Wed, Jun 13, 2018 JD <[hidden email]> wrote:

>
>
>
> On 06/13/2018 06:29 PM, 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 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?
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Steve O'Hara-Smith
In reply to this post by jd1008
On Wed, 13 Jun 2018 17:30:29 -0600
JD <[hidden email]> wrote:

> To the original poster of this question:
> Does the command
> who -r
> still work in FBSD?

        It never did, and still doesn't.

--
Steve O'Hara-Smith <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Steve O'Hara-Smith
In reply to this post by Manish Jain-2
On Thu, 14 Jun 2018 05:59:16 +0530
Manish Jain <[hidden email]> 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 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.

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

> 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 <[hidden email]>
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Polytropon
In reply to this post by Steve O'Hara-Smith
On Thu, 14 Jun 2018 08:38:40 +0100, Steve O'Hara-Smith wrote:
> On Wed, 13 Jun 2018 17:30:29 -0600
> JD <[hidden email]> wrote:
>
> > To the original poster of this question:
> > 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, ...
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Polytropon
In reply to this post by Steve O'Hara-Smith
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 <[hidden email]> 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, ...
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Valeri Galtsev
In reply to this post by Polytropon


On 06/14/18 11:43, Polytropon wrote:

> On Thu, 14 Jun 2018 08:38:40 +0100, Steve O'Hara-Smith wrote:
>> On Wed, 13 Jun 2018 17:30:29 -0600
>> JD <[hidden email]> wrote:
>>
>>> To the original poster of this question:
>>> 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
++++++++++++++++++++++++++++++++++++++++
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Steve O'Hara-Smith
In reply to this post by Polytropon
On Thu, 14 Jun 2018 18:43:29 +0200
Polytropon <[hidden email]> wrote:

> 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/
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
Reply | Threaded
Open this post in threaded view
|

Re: How to detect single user mode in FreeBSD ?

Polytropon
On Fri, 15 Jun 2018 20:00:51 +0100, Steve O'Hara-Smith wrote:
> On Thu, 14 Jun 2018 18:43:29 +0200
> Polytropon <[hidden email]> wrote:
>
> > 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, ...
_______________________________________________
[hidden email] mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "[hidden email]"
12