switch_root: zap the last directory for the mount point of new-root

Kang-Che Sung explorer09 at gmail.com
Fri Aug 2 14:40:01 UTC 2019

On Friday, August 2, 2019, 阿保 純一 <abo_junghichi at yahoo.co.jp> wrote:
>> Speaking of, since we are now overmounting the root before zapping the
>> initramfs, I wonder if we can remove one check about whether the new root
>> is a mount point (this saves code size; mount() would fail with EINVAL in
>> that case).
> At least, I must check other mount points.
> For example, below is the init-script I'm using in initramfs.
> #!/bin/sh
> mount -t vfat /dev/sda2 /disk
> mount -o loop,ro -t squashfs /disk/squash.img /base
> mount -o loop -t xfs /disk/xfs.img /vary
> mount -o lowerdir=/base,upperdir=/vary/rootfs,workdir=/vary/work \
>         -t overlay overlay /root
> mount --move /disk /root/initfs/disk
> mount --move /base /root/initfs/base
> mount --move /vary /root/initfs/vary
> my_standalone_switch_root -c /dev/tty1 /root /sbin/init
> I think switch_root in busybox is written already so dense, so every it's
check-codes treats not only the new-root but also other storages.

What I suggested is to remove these lines:

if (st.st_dev == rootdev) {
// Show usage, it says new root must be a mountpoint

Because mount(..., MS_MOVE) will do the same check before moving the mount
point, and the check would be done before deleting anything if your patch
is applied. (And there is no point to show the program usage when it's
already PID 1. You would rather see error message of what specifically goes
wrong. The kernel will panic when PID 1 dies, leaving you no option but

The delete_contents() function in switch_root.c already prevents itself
from deleting files of other filesystems.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20190802/5b75295f/attachment.html>

More information about the busybox mailing list