Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for systemd #2474

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions host-bin/enter-chroot
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TARGET=''
USERNAME='1000'
TMPXMETHOD=''
NOLOGIN=''
SYSTEMD=''
SETUPSCRIPT='/prepare.sh'

USAGE="$APPLICATION [options] [command [args...]]
Expand All @@ -37,7 +38,8 @@ Options:
-X XMETHOD Override the auto-detected XMETHOD for this session.
-x Does not log in, but directly executes the command instead.
Note that the environment will be empty (sans TERM).
Specify -x a second time to run the $SETUPSCRIPT script."
Specify -x a second time to run the $SETUPSCRIPT script.
-s Start systemd inside the chroot. Requires a distribution with systemd support."

# Common functions
. "$BINDIR/../installer/functions"
Expand All @@ -59,7 +61,7 @@ chrootcmd() {

# Process arguments
prevoptind=1
while getopts 'bc:k:ln:t:u:X:x' f; do
while getopts 'bc:k:ln:t:u:X:x:s' f; do
# Disallow empty string as option argument
if [ "$((OPTIND-prevoptind))" = 2 -a -z "$OPTARG" ]; then
error 2 "$USAGE"
Expand All @@ -76,6 +78,7 @@ while getopts 'bc:k:ln:t:u:X:x' f; do
X) TMPXMETHOD="$OPTARG";;
x) NOLOGIN="$((NOLOGIN+1))"
[ "$NOLOGIN" -gt 2 ] && NOLOGIN=2;;
s) SYSTEMD='y';;
\?) error 2 "$USAGE";;
esac
done
Expand Down Expand Up @@ -348,8 +351,10 @@ fi
bindmount /dev
bindmount /dev/pts
bindmount /dev/shm
bindmount /tmp /tmp exec
bindmount /proc
if [ -z "$SYSTEMD" ]; then
bindmount /tmp /tmp exec
bindmount /proc
fi
tmpfsmount /var/run 'noexec,nosuid,mode=0755,size=10%'
tmpfsmount /var/run/lock 'noexec,nosuid,nodev,size=5120k'
bindmount /var/run/dbus /var/host/dbus
Expand Down Expand Up @@ -610,8 +615,8 @@ fi

ret=0

# Launch the system dbus unless we are entering a basic shell.
if [ ! "$NOLOGIN" = 1 ] && grep -q '^root:' "$passwd" 2>/dev/null; then
# Launch the system dbus unless we are entering a basic shell or systemd.
if [ ! "$NOLOGIN" = 1 ] && [ -z "$SYSTEMD" ] && grep -q '^root:' "$passwd" 2>/dev/null; then
# Try to detect the dbus user by parsing its configuration file
# If it fails, or if the user does not exist, `id -un '$dbususer'`
# will fail, and we fallback on a default user name ("messagebus")
Expand Down Expand Up @@ -677,6 +682,21 @@ if [ -n "$NOLOGIN" ]; then
error "$ret" 'Failed to complete chroot setup.'
fi
fi
elif [ -n "$SYSTEMD" ]; then
[ -e "/run/crouton/$NAME.systemd.pid" ] && \
read -r SYSTEMD_PID < "/run/crouton/$NAME.systemd.pid"
if [ -z "SYSTEMD_PID" ] || ! pwdx $SYSTEMD_PID >/dev/null 2>&1; then
echo "Starting systemd..."
env -i container=1 /sbin/minijail0 -v -C "$CHROOT" -f "/run/crouton/$NAME.systemd.pid" -i -I /bin/bash -c "exec /lib/systemd/systemd"
sleep 1
read -r SYSTEMD_PID < "/run/crouton/$NAME.systemd.pid"
fi
if [ -n "$SYSTEMD_PID" ]; then
echo "Entering systemd PID $SYSTEMD_PID..."
env -i TERM="$TERM" nsenter -m -t $SYSTEMD_PID -p -r -w -u -- su - "$USERNAME"
else
echo "Could not start systemd" >&2
fi
else
# Check and run rc.local
if [ -n "$firstrun" -a -x "$CHROOT/etc/rc.local" ]; then
Expand Down