Linux – How to properly use Gentoo Linux as a supplement to an existing distribution

.rcchrootinit-scriptlinuxsystem-installation

While I originally wanted to entirely replace the Linux distribution my NAS uses, I meanwhile came to the conclusion that it would be better to leave the existing system as unmodified as possible and merely supplement it via Gentoo (or Arch) Linux – which is basically what the answer to the previous question suggested. So currently the only modification to the original system consist of a /gentoo directory that I chroot into via the following script:

#!/bin/bash
set -e

cp -L /etc/resolv.conf etc/ # for internet access
cp -P /etc/localtime etc/   # to keep the timezones consistent
cp -d /etc/mtab etc/        # to check mounted systems
# cp /etc/{mdadm.conf,hosts,fstab} etc  # Maybe?
mount --rbind /mnt mnt      # use host's mounts
mkdir host; mount --bind / host
mount --bind /var/log var/log # or run own syslogd?

mount --bind /dev dev
mount -t devpts devpts dev/pts
mount --bind /proc proc  # or mount -t procfs proc proc?
mount --bind /sys sys    # or mount -t sysfs sysfs sys?
chroot . /usr/sbin/sshd -p 22222
# chroot . /bin/env -i TERM=$TERM /bin/bash

Now I can simply ssh into the host via port 22222 and end up in the chroot environment, which basically behaves like Gentoo Linux, and this script can be run from the host's /etc/init.d/rcS.

But of course I don't want to end up starting every Gentoo service I'd like to use manually that way – that's what OpenRC (or systemd, if preferred) is good for after all. So my major question is

What Gentoo command should chroot execute in order to properly "boot" the Gentoo Linux on top of the host Linux with as little interference as possible?

With as little interference as possible I mean it should not attempt to remount the filesystem (but at the same time it would be nice if Gentoo's mount would work correctly) – so simply running init is probably not correct, some modifactions to the OpenRC configuration will probably be necessary, but which ones?

Additionally, there's the question of host daemons – should I use them or have Gentoo run it's own instances of e.g. crond and syslogd (and how would they need to be set up in order to not interfere with the host instances?), or should I go even further and entirely virtualize Gentoo? As mentioned in another question it would be neat to have the Gentoo instance have its own IP and more or less behave like an independent system, but on the other hand I'd like to have as little overhead as possible due to the system's limited resources. The host system is running these daemons, plus my thoughts so far:

Daemon           | Use Gentoo's own?
-----------------+---------------------------------------------------------------
udevd            | N bind-mount /dev
klogd, k*        | N using host kernel (although UML might be interesting...)
dhcpd, inetd     | ? depends on using own IP or not
syslogd          | ? bind-mount /var/log or use Gentoo's more versatile settings?
mdadm --monitor  | ? should Gentoo bother with the RAID configuration?
smbd, nmbd       | ? disable host's samba in favour of Gentoo's one? maybe with a
                 |   maintenance-only share on the host
crond            | Y to minimize interference with host's maintenance scripts
sshd             | Y to directly SSH into the chrooted Gentoo system
daemonwatch      | ? maybe use host instance to watch Gentoo instance?
logchkd, errormon| ? unknown

Finally, I'd like to know what I should consider on shutdown/reboot – can I simply have the host's shutdown script run chroot /gentoo /bin/init shutdown before its own sequence, or could that cause a power off by Gentoo before the host's actual shutdown sequence?

Best Answer

If you want to run services in a chroot: that's not really what a chroot is built for.

You'd be better off to isolate your Gentoo system in a Docker container.

You can easily create a Docker image from your chroot by creating a new baseimage from it:

tar --numeric-owner -cf- /gentoo | docker import - gentoo:base

Then build a proper Docker imageon top of it using this Dockerfile:

FROM gentoo:base
EXPOSE 22 # make SSH accessible, repeat for any port you're running a service on in this container
ENTRYPOINT ["/usr/lib/systemd/systemd"]

Use this command to build a proper container based on the Dockerfile (the Dockerfile needs to be in the same directory from which you're running this command and it needs to be named Dockerfile):

docker build -t gentoo:latest .

Now you should be able to start this container using

GENTOO_CONTAINER=$(docker run -d gentoo:base)

Using docker inspect ${GENTOO_CONTAINER} you can see now all the details of this container (IP, which ports are used to expose the services of running inside your container to the outside, etc.).

Using docker ps you can see the containers which are currently running.

Using docker ps -a you can see all containers which have ever been run, including the currently running ones.

Also make sure to do the Docker tutorial which is really helpful in understanding Docker basics.

Related Question