7

I want to test the pivot_root command which moves the root file system of the current process to the directory put_old and makes new_root the new root file system.

But I always get the following error:

pivot_root: failed to change root from .' toold-root/': Invalid argument

I use fedora as base root, I have a Archlinux in my home folder

[root@localhost arch-root]# ls
bin boot dev etc home lib lib64 mnt old-root 
opt proc root run sbin srv sys tmp usr var
[root@localhost arch-root]# pivot_root . old-root/
pivot_root: failed to change root from .' toold-root/': Invalid argument

I also try to call linux function pivot_root("/chroot_test", "/chroot_test/old-root"); Got same error.

Any ideas about this ?

Update #1

I also try to test pivot_root in Docker. I mount this arch-root in to Docker container. But get the following error: Operation not permitted

root@00d871ce892b:/# cd test_root/
root@00d871ce892b:/test_root# ls
bin  boot  dev  etc  home  lib  lib64  mnt  old-root  opt  proc  root  run  sbin  srv     sys  test_pivot_root  test_pivot_root.c   tmp  usr  var
root@00d871ce892b:/test_root# pivot_root . tmp/
pivot_root: Operation not permitted

Solution

I have found the solution:

Run docker with --privileged=true

So we can test pivot_root in the docker container.

4

Are you really sure that arch-root is on a separate filesystem that can be mounted and umounted?

pivot_root, as well as the more current switch_root, works by juggling information about mounted filesystems in the kernel.

The new root file system must be "the root" of a file system, you cannot pass "." as the new root unless "." is the root directory of a mounted filesystem.

I believe the easiest way if you want to try pivot_root from your current setup (assuming arch-root is a subdirectory and not a root directory) is to create a tmpfs filesystem to switch to, and copy the required stuff there.

Something along these lines might get you started: (adjust 500M to fit du -sh arch-root)

mkdir /ramroot
mount -n -t tmpfs -o size=500M none /ramroot
cd arch-root # (containing the root filesystem contents)
find . -depth -xdev -print | cpio -pd --quiet /ramroot
cd /ramroot
mkdir oldroot
pivot_root . oldroot
exec chroot . bin/sh
  • Thank you very much, For now I am testing it in a docker container. it works. I will try your method later – vvilp Sep 18 '14 at 3:17
  • I think there is a much more minimal way to satisfy this requirement, mount --bind /chroot-test /chroot-test && cd /chroot-test. (The second part makes sure that . refers to the new /chroot-test, not the old /chroot-test). This is according to man7.org/linux/man-pages/man8/switch_root.8.html#NOTES – sourcejedi Jun 17 '18 at 9:53
  • You might need to do an unshare -m before the pivot_root. bugzilla.redhat.com/show_bug.cgi?id=1361043 – koalo Jul 18 '18 at 11:11
2

From the man page, I believe this is your issue:

The following restrictions apply to new_root and put_old:

- They must be directories.

- new_root and put_old must not be on the same file system as the current
root.

- put_old must be underneath new_root, that is, adding a nonzero number of
/.. to the string pointed to by put_old must yield the same directory as
new_root.

- No other file system may be mounted on put_old.

According to the above the same filesystem cannot be used for both the put_old and the new_root.

References

  • Thank you for your reply. I update the question. I also try to test in Docker. But Operation not permitted – vvilp Sep 16 '14 at 3:11

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.