16

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
Roman Riabenko
  • 2,145
  • 3
  • 15
  • 39
vvilp
  • 371
  • 2
  • 4
  • 9
  • From the sound of this post it sounds like a bug to me: https://github.com/lxc/lxc/issues/61. This thread too sounds related: https://lists.linuxcontainers.org/pipermail/lxc-users/2014-January/006124.html – slm Sep 16 '14 at 02:16
  • Yea I am trying to test the function of Docker and lxc feature which use pivot_root – vvilp Sep 16 '14 at 03:11

3 Answers3

6

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
MattBianco
  • 3,676
  • 6
  • 27
  • 43
  • 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 03: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 http://man7.org/linux/man-pages/man8/switch_root.8.html#NOTES – sourcejedi Jun 17 '18 at 09:53
  • 1
    You might need to do an `unshare -m` before the pivot_root. https://bugzilla.redhat.com/show_bug.cgi?id=1361043 – koalo Jul 18 '18 at 11:11
4

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 neither put_old or new_root filesystems should reside on the same filesystem as current_root.

References

slm
  • 363,520
  • 117
  • 767
  • 871
  • 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 03:11
  • 2
    Your summary is wrong. `new_root` and `put_old` *can* be on the same FS, it's just that neither should reside on the same FS *as the current root*. – iBug Mar 17 '20 at 09:24
  • @iBug - ty fixed. – slm Mar 18 '20 at 11:00
1

The author of the question provided this solution:

Solution

I have found the solution:

Run docker with --privileged=true

So we can test pivot_root in the docker container.

Roman Riabenko
  • 2,145
  • 3
  • 15
  • 39