pivot_root from initramfs to new root – error “Invalid argument”

You can not use pivot_root on an initramfs rootfs, you will get Invalid Argument. You can only pivot real filesystems.

Indeed:

  1. Fedora Linux 28 – this uses the dracut initramfs.
  2. Boot into an initramfs shell, by adding rd.break as an option on the kernel command line.
  3. cd /sysroot
  4. usr/bin/pivot_root . mnt

-> pivot_root fails with “Invalid argument”, corresponding to an errno value of EINVAL.

There is no explanation for this in man 2 pivot_root:

EINVAL put_old is not underneath new_root.

Why does it fail? And as the next commenter replied, “Then how would Linux exit early user space?”

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Unlike the initrd, Linux does not allow to unmount the initramfs. Apparently this helped keep the kernel code simple.

Instead of pivot_root, you can use the switch_root command. It implements the following procedure. Notice that switch_root deletes all the files on the old root, to free the initramfs memory, so you need to be careful where you run this command.

initramfs is rootfs: you can neither pivot_root
rootfs, nor unmount it. Instead delete everything out of rootfs to
free up the space (find -xdev / -exec rm ‘{}’ ‘;’), overmount rootfs
with the new root (cd /newmount; mount –move . /; chroot .), attach
stdin/stdout/stderr to the new /dev/console, and exec the new init.

Note the shell commands suggested are only rough equivalents to the C code. The commands won’t really work unless they are all built in to your shell, because the first command deletes all the programs and other files from the initramfs :-).


Rootfs is a special instance of ramfs (or tmpfs, if that’s enabled), which is always present in 2.6 systems. You can’t unmount rootfs for approximately the same reason you can’t kill the init process; rather than having special code to check for and handle an empty list, it’s smaller and simpler for the kernel to just make sure certain lists can’t become empty.

https://github.com/torvalds/linux/blob/v4.17/Documentation/filesystems/ramfs-rootfs-initramfs.txt

Method 2

From the comment on the pivot_root syscall in fs/namespace.c:

Also, the current root cannot be on the ‘rootfs’ (initial ramfs)
filesystem. See Documentation/filesystems/ramfs-rootfs-initramfs.txt
for alternatives in this situation.

I submitted a patch to the man-pages project about this, so in future releases of it, man 2 pivot_root will mention this case.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x