mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-30 04:34:27 +00:00
agent/rustjail: implement functions to pivot_root
Use conditional compilation (#[cfg]) to change pivot_root behaviour at compilation time. For example, such function will just return `Ok(())` when the unit tests are being compiled, otherwise real pivot_root operation is performed. Signed-off-by: Julio Montes <julio.montes@intel.com>
This commit is contained in:
parent
672da4d08c
commit
7cf0fd95f1
@ -352,6 +352,16 @@ fn mount_cgroups(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn pivot_root<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
|
||||||
|
new_root: &P1,
|
||||||
|
put_old: &P2,
|
||||||
|
) -> anyhow::Result<(), nix::Error> {
|
||||||
|
#[cfg(not(test))]
|
||||||
|
return unistd::pivot_root(new_root, put_old);
|
||||||
|
#[cfg(test)]
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn pivot_rootfs<P: ?Sized + NixPath + std::fmt::Debug>(path: &P) -> Result<()> {
|
pub fn pivot_rootfs<P: ?Sized + NixPath + std::fmt::Debug>(path: &P) -> Result<()> {
|
||||||
let oldroot = fcntl::open("/", OFlag::O_DIRECTORY | OFlag::O_RDONLY, Mode::empty())?;
|
let oldroot = fcntl::open("/", OFlag::O_DIRECTORY | OFlag::O_RDONLY, Mode::empty())?;
|
||||||
defer!(unistd::close(oldroot).unwrap());
|
defer!(unistd::close(oldroot).unwrap());
|
||||||
@ -360,7 +370,7 @@ pub fn pivot_rootfs<P: ?Sized + NixPath + std::fmt::Debug>(path: &P) -> Result<(
|
|||||||
|
|
||||||
// Change to the new root so that the pivot_root actually acts on it.
|
// Change to the new root so that the pivot_root actually acts on it.
|
||||||
unistd::fchdir(newroot)?;
|
unistd::fchdir(newroot)?;
|
||||||
unistd::pivot_root(".", ".").context(format!("failed to pivot_root on {:?}", path))?;
|
pivot_root(".", ".").context(format!("failed to pivot_root on {:?}", path))?;
|
||||||
|
|
||||||
// Currently our "." is oldroot (according to the current kernel code).
|
// Currently our "." is oldroot (according to the current kernel code).
|
||||||
// However, purely for safety, we will fchdir(oldroot) since there isn't
|
// However, purely for safety, we will fchdir(oldroot) since there isn't
|
||||||
|
Loading…
Reference in New Issue
Block a user