Skip to content

Commit

Permalink
init: better handle dependency detection, better support OpenRC for i…
Browse files Browse the repository at this point in the history
…nitful containers, better document in man pages how to create initful containers

Signed-off-by: Luca Di Maio <[email protected]>
  • Loading branch information
89luca89 committed Aug 26, 2023
1 parent ab764d6 commit 0c4615b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
53 changes: 47 additions & 6 deletions distrobox-init
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,37 @@ if [ "${shell_pkg}" = "ash" ]; then
shell_pkg="bash"
fi

# Check dependencies in a list, and install all if one is missing
missing_packages=0
dependencies="
bc
chpasswd
curl
diff
find
findmnt
less
lsof
mount
passwd
pinentry
ps
script
sudo
time
umount
useradd
wc
wget
${shell_pkg}
"
for dep in ${dependencies}; do
! command -v "${dep}" && missing_packages=1 && break
done

# Check if dependencies are met for the script to run.
if [ "${upgrade}" -ne 0 ] || ! command -v find || ! command -v mount || ! command -v passwd ||
! command -v sudo || ! command -v useradd || ! command -v diff ||
! command -v pinentry || ! command -v wget || ! command -v curl ||
! command -v less || ! command -v bc || ! command -v time || ! command -v lsof ||
! command -v "${shell_pkg}" ||
if [ "${upgrade}" -ne 0 ] ||
[ "${missing_packages}" -ne 0 ] ||
{ [ -n "${container_additional_packages}" ] && [ ! -e /.containersetupdone ]; }; then

# Detect the available package manager
Expand Down Expand Up @@ -1835,6 +1860,23 @@ if [ -e /dev/console ]; then
fi
fi

if [ -e /etc/inittab ]; then
# Cleanup openrc to not interfere with the host
sed -i 's/^\(tty\d\:\:\)/#\1/g' /etc/inittab
sed -i \
-e 's/#rc_env_allow=".*"/rc_env_allow="\*"/g' \
-e 's/#rc_crashed_stop=.*/rc_crashed_stop=NO/g' \
-e 's/#rc_crashed_start=.*/rc_crashed_start=YES/g' \
-e 's/#rc_provide=".*"/rc_provide="loopback net"/g' \
/etc/rc.conf
rm -f /etc/init.d/hwdrivers \
/etc/init.d/hwclock \
/etc/init.d/hwdrivers \
/etc/init.d/modules \
/etc/init.d/modules-load \
/etc/init.d/modloop
fi

if command -v systemctl 2> /dev/null; then
# Cleanup systemd to not interfere with the host
UNIT_TARGETS="
Expand Down Expand Up @@ -1887,7 +1929,6 @@ ExecStart=sh -c "ln -sf /run/host/run/user/\$(id -ru)/keyring/ /run/user/\$(id -
ln -sf /run/host/run/user/\$(id -ru)/.mutter* /run/user/\$(id -ru)/"
Slice=user-%i.slice
EOF

fi

# Now we can launch init
Expand Down
9 changes: 9 additions & 0 deletions docs/usage/distrobox-create.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ Note that enabling `--init` **will disable host's process integration**.
From within the container you will not be able to see and manage host's processes.
This is needed because `/sbin/init` must be pid 1.

If you want to use a non-pre-create image, you'll need to add the additional package:

distrobox create -i alpine:latest --init --additional-packages "openrc" -n test
distrobox create -i debian:stable --init --additional-packages "systemd libpam-systemd" -n test
distrobox create -i ubuntu:22.04 --init --additional-packages "systemd libpam-systemd" -n test
distrobox create -i archlinux:latest --init --additional-packages "systemd" -n test
distrobox create -i registry.opensuse.org/opensuse/tumbleweed:latest --init --additional-packages "systemd" -n test
distrobox create -i registry.fedoraproject.org/fedora:38 --init --additional-packages "systemd" -n test

The `--home` flag let's you specify a custom HOME for the container.
Note that this will NOT prevent the mount of the host's home directory,
but will ensure that configs and dotfiles will not litter it.
Expand Down
11 changes: 7 additions & 4 deletions docs/useful_tips.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ Simply use:

## Export to the host

Distrobox supports exporting to the host either binaries, applications or systemd
services. [Head over the usage page to have an explanation and examples.](usage/distrobox-export.md)
Distrobox supports exporting to the host either binaries or applications.
[Head over the usage page to have an explanation and examples.](usage/distrobox-export.md)

## Execute commands on the host

Expand Down Expand Up @@ -248,8 +248,11 @@ You can use such feature using:
If you want to use a non-pre-create image, you'll need to add the additional package:

`distrobox create -i debian:unstable --init --additional-packages "systemd" -n test`
`distrobox create -i ubuntu:18.04 --init --additional-packages "systemd" -n test`
`distrobox create -i alpine:latest --init --additional-packages "openrc" -n test`
`distrobox create -i debian:stable --init --additional-packages "systemd libpam-systemd" -n test`
`distrobox create -i ubuntu:22.04 --init --additional-packages "systemd libpam-systemd" -n test`
`distrobox create -i archlinux:latest --init --additional-packages "systemd" -n test`
`distrobox create -i registry.opensuse.org/opensuse/tumbleweed:latest --init --additional-packages "systemd" -n test`
`distrobox create -i registry.fedoraproject.org/fedora:38 --init --additional-packages "systemd" -n test`

Note however that in this mode, you'll not be able to access host's processes
Expand Down

0 comments on commit 0c4615b

Please sign in to comment.