LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Two questions on entering namespace of systemd (https://www.linuxquestions.org/questions/linux-server-73/two-questions-on-entering-namespace-of-systemd-4175732735/)

zzzhhh 01-13-2024 07:53 AM

Two questions on entering namespace of systemd
 
I am studying entering namespace of systemd on Ubuntu 22.04.3 LTS desktop. I wrote a script to test entering namespace of systemd. The whole testing are as follows:

1) Run `sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig` to install needed packages

2) Add the following snippet at the beginning of `/etc/bash.bashrc` to invoke my script to enter namespace of systemd.
Code:

SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target"
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
echo "@@@@@@@@@@@@@ SYSTEMD_PID is $SYSTEMD_PID @@@@@@@@@@@@@@@@@@"
if [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != "1" ]; then
    sudo sh /home/zh/enternamespace
fi

3) The script `enternamespace` is stored in my home directory. My understanding of entering namespace of systemd is that it is divided into two steps. The first step is to launch `systemd` as a daemon with an isolated namespace. The second step is have a login session enter the namespace. So my script to enter the namespace of systemd is:
Code:

echo "11111111111111111111111111111111111111111111111111111111111111"
SYSTEMD_EXE="/lib/systemd/systemd --unit=basic.target"
SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
if [ -z "$SYSTEMD_PID" ]; then
    /usr/bin/daemonize /usr/bin/unshare --fork --pid --mount-proc bash -c 'mount -t binfmt_misc binfmt_misc /proc/sys/fs/binfmt_misc; exec '"$SYSTEMD_EXE"
    while [ -z "$SYSTEMD_PID" ]; do
        echo "Sleeping for 1 second to let systemd settle"   
        sleep 1
        SYSTEMD_PID="$(ps -eo pid=,args= | awk '$2" "$3=="'"$SYSTEMD_EXE"'" {print $1}')"
    done
fi
echo "================ SYSTEMD_PID is $SYSTEMD_PID ================="

echo "2222222222222222222222222222222222222222222222222222"
exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a /bin/login -p -f "$SUDO_USER"

4) Reboot. After rebooting to GNOME desktop, open a terminal.

It works sometimes: https://ibb.co/0hPL5td. But there are two problems I don't understand:
  1. There is a `/run/nologin` file. This file is there to prevent ssh login during booting, which is reasonable. When booting is finished, this file should be deleted so that the Ubuntu OS is ready to accept ssh login. It is the case for normal Ubuntu. I don't understand why `/run/nologin` is still present after booting in my test.
  1. If I login in as a normal user, the system would freeze with an error after a while. But if I log in using Ubuntu's rescue root shell, it will never freeze, though file `/run/nologin` is still there.
    This is a screenshot: https://ibb.co/FXWrTGj.

So I was wondering if you can please help me with these two questions. Thank you.

MadeInGermany 01-18-2024 04:51 AM

I think a /run/nologin should never be there.
When ssh.service (sshd) is started it should work immediately.

/etc/nologin or /run/nologin are created by an administrator if a reboot is scheduled, e.g. the shutdown command does it.

Something is wrong on your system. It looks like a shutdown command runs during startup.

zzzhhh 01-19-2024 12:34 AM

Quote:

Originally Posted by MadeInGermany (Post 6477627)
I think a /run/nologin should never be there.
When ssh.service (sshd) is started it should work immediately.

/etc/nologin or /run/nologin are created by an administrator if a reboot is scheduled, e.g. the shutdown command does it.

Something is wrong on your system. It looks like a shutdown command runs during startup.

I gave the whole steps to reproduce the problem, why didn't you follow it to see /run/nologin there instead of just "thinking"?

Nothing is wrong on my system. Maybe something is wrong with your mind simulation an actual Linux system.


All times are GMT -5. The time now is 03:35 PM.