I just installed Apache for the first time on Gentoo. When I chroot into Gentoo from another OS (in this case Microknoppix) and try to start Apache (with /etc/init.d/apache2 start) I get the error WARNING: apache2 is already starting. If I try to stop it I get ERROR: apache2 stopped by something else. Apache functions normally when I run it under Gentoo without the chrooting.

I could probably get away with running Apache from within Gentoo itself and skip the chrooting entirely but I'm curious. Why is this happening and can I fix it?

I talked to someone earlier who thought it might be:

  1. a problem with the with multiple programs trying to use the same pid file
  2. or
  3. a problem with the way Gentoo handles chrooting.

But I don't know enough about either to discuss the subjects coherently myself.

Edit: Here are the commands I've been using to chroot into Gentoo, cribbed from the install instructions in the Gentoo handbook:

sudo mount --bind /etc/resolv.conf /mnt/gentoo/etc/resolv.conf sudo mount -t proc proc /mnt/gentoo/proc sudo mount --rbind /sys /mnt/gentoo/sys sudo mount --rbind /dev /mnt/gentoo/dev sudo chroot /mnt/gentoo /bin/bash env-update && source /etc/profile export PS1="(chroot) $PS1"

  • are you using the right environment variables? I remmeber that chrooting in gentoo involved a step of source /etc/profile and env-update or something like this. The relevant documentation is here gentoo.org/doc/en/handbook/handbook-x86.xml?part=2&chap=5 Are you calling the system from the right linux? – vfbsilva Oct 29 '14 at 3:31
  • @vfbsilva, I've added the commands I've been using to my original post so they can be critiqued, just in case there's a better way to do it. – Robert Larkins Oct 31 '14 at 9:28

Gentoo init scripts don't work well when Gentoo is running in a chroot. Gentoo usually has inter-dependencies where apache won't start until 'net' has been started, and so on, and since none of these ran at startup (because Gentoo isn't the host system) things will generally be broken. In the worst case, Gentoo might mess up your network settings or run fsck on your harddrive thinking that it isn't mounted yet.

Also, init scripts need to run as root. If you aren't running it as root, it won't have permissions to modify the files that track a service's state. If you are still running as root, beware that root can escape a chroot pretty easily.

I would suggest skipping the init script, and just pass apache a custom config file as you start it in the chroot. Something like:

chroot /path/to/gentoo /usr/sbin/apache2 -f /etc/apache2/httpd.conf

I also recommend running this command in the outer system with a supervisor program, like perp or runit or s6. See http://skarnet.org/software/s6/why.html

It might also be that you need to have /proc mounted in the chroot. Beware that if proc is mounted in the chroot, things in the chroot will be able to get a process list from the host system, which might be bad. I assume you're doing this because you don't trust a script that you will be serving from apache?

  • I'm afraid you're giving me too much credit. The main reason I've been chrooting into Gentoo is because I got into the habit of doing it when I was setting it up and I thought being able to access my Gentoo install while working in another OS was a handy thing to do. Eventually Hearing you enumerate the risks has persuaded me that this might not be the smartest move. – Robert Larkins Oct 31 '14 at 9:33
  • I've also read that using chroot jails is a good security move and useful when installing software from source but I'll have to do more research before I use chroot jails, and for now I'll run Gentoo normally, outside of chroot. – Robert Larkins Oct 31 '14 at 10:02
  • I hesitate to criticize anyone's usage patterns, but it does sound like you might be doing it for the wrong reasons ;-) I have found Gentoo chroots to be useful for two purposes: building custom linuxes by installing gentoo in a chroot and then using "mksquashfs" to build an image of only the necessary bits, and for running software that needs a completely different set of system libraries than the rest of my system. – M Conrad Nov 3 '14 at 21:45
  • The second case lets you do things like testing software with uClibc or musl-libc, and building static-linked binaries. One time I also used a Gentoo chroot to get a graphical desktop on a console-only system. This is how I learned that the Gentoo init scripts weren't likely to work, or would have unintended consequences. In the end, running the chrooted binaries directly from scripts in the host system worked the best for me. – M Conrad Nov 3 '14 at 21:46
  • Oh, or if you want to run really new software on a really old system. You can build things like the latest java or latest mysql (or apache) in a chroot, and then run them from the chroot, instead of the sometimes-impossible task of getting them installed in the host. – M Conrad Nov 3 '14 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.