Why pivot_root is obsolete (was Re: Two roots mounted)

Rob Landley rob at landley.net
Wed Oct 12 18:50:14 UTC 2005


On Wednesday 12 October 2005 02:52, Dmytro Bablinyuk wrote:
> I couldn't find place where actually rootfs comes from.

From the kernel.  It's the ramfs that initramfs populates (and you've got to 
delete the files out of that to free up space if you use initramfs and want 
to mount a real root over it.)

And here's a big "caution: severe tire damage" that is not properly documented 
for newbies to initramfs:


The obvious thing to do at the end of initramfs is pivot_root to the new root 
and then umount rootfs.  This does not work.  In current kernels it's 
ignored, and in older kernels it hung the kernel hard!  (The "can't unmount 
rootfs" code only checked when you were trying to umount /, and could be 
bypassed if you pivot_rooted it to a subdirectory first.  They fixed that, 
but still didn't document the real issue.)

The problem is, the rootfs entry is used as a search terminator in the 
kernel's internal doubly linked list of active mounts, and if it's not there 
there kernel will lock hard in an endless loop going through that list...

The correct way to pivot_root away from initramfs is a bit of a song and 
dance: recursively delete everything from initramfs (including the executable 
you're running right now), overmount rootfs with the new root partition 
(using the now empty / as your mount point), and exec new init.  Of course 
how do you mount when you have no node for the block device?  To be honest, i 
don't remember, there's some black magic in there somewhere.

So the kernel developers buried a program in klibc to do all this.  Since 
klibc itself is still pretty obscure 
(http://www.kernel.org/pub/linux/libs/klibc/) and the file you need 
(utils/run_init.c) blends in pretty well, it's a bit of a pain to find even 
if you remember it's there.

By the way, Red Hat decided to integrade this into nash as the "switch_root" 
command, and I think that's what the Debian guys are calling it too.  I think 
busybox should also grow a switch_root based on this.

Rob



More information about the busybox mailing list