10f66f451Sopenharmony_ci/* pivot_root.c - edit system mount tree 20f66f451Sopenharmony_ci * 30f66f451Sopenharmony_ci * Copyright 2012 Rob Landley <rob@landley.net> 40f66f451Sopenharmony_ci 50f66f451Sopenharmony_ciUSE_PIVOT_ROOT(NEWTOY(pivot_root, "<2>2", TOYFLAG_SBIN)) 60f66f451Sopenharmony_ci 70f66f451Sopenharmony_ciconfig PIVOT_ROOT 80f66f451Sopenharmony_ci bool "pivot_root" 90f66f451Sopenharmony_ci default y 100f66f451Sopenharmony_ci help 110f66f451Sopenharmony_ci usage: pivot_root OLD NEW 120f66f451Sopenharmony_ci 130f66f451Sopenharmony_ci Swap OLD and NEW filesystems (as if by simultaneous mount --move), and 140f66f451Sopenharmony_ci move all processes with chdir or chroot under OLD into NEW (including 150f66f451Sopenharmony_ci kernel threads) so OLD may be unmounted. 160f66f451Sopenharmony_ci 170f66f451Sopenharmony_ci The directory NEW must exist under OLD. This doesn't work on initramfs, 180f66f451Sopenharmony_ci which can't be moved (about the same way PID 1 can't be killed; see 190f66f451Sopenharmony_ci switch_root instead). 200f66f451Sopenharmony_ci*/ 210f66f451Sopenharmony_ci 220f66f451Sopenharmony_ci#define FOR_pivot_root 230f66f451Sopenharmony_ci#include "toys.h" 240f66f451Sopenharmony_ci 250f66f451Sopenharmony_ci#include <sys/syscall.h> 260f66f451Sopenharmony_ci#include <unistd.h> 270f66f451Sopenharmony_ci 280f66f451Sopenharmony_civoid pivot_root_main(void) 290f66f451Sopenharmony_ci{ 300f66f451Sopenharmony_ci if (syscall(__NR_pivot_root, toys.optargs[0], toys.optargs[1])) 310f66f451Sopenharmony_ci perror_exit("'%s' -> '%s'", toys.optargs[0], toys.optargs[1]); 320f66f451Sopenharmony_ci} 33