Two questions on entering namespace of systemd
by zzzhhh from LinuxQuestions.org on (#6HTXS)
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
fi3) 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) 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
fi3) 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:
- 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.
- 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.