Mount tmpfs overlay over rootfs if citadel.overlay is set

This commit is contained in:
Bruce Leidl 2019-01-06 18:15:44 -05:00
parent 96429ed4c7
commit 8e341d6005
2 changed files with 34 additions and 2 deletions

View File

@ -5,8 +5,9 @@ extern crate libc;
use std::process::exit;
use std::env;
use std::fs;
use libcitadel::{Result,CommandLine,set_verbose,format_error,ResourceImage};
use libcitadel::{Result,CommandLine,set_verbose,format_error,ResourceImage,util};
mod boot_select;
@ -20,7 +21,7 @@ use rootfs::Rootfs;
/// citadel-mount rootfs
/// citadel-mount kernel
/// citadel-mount extra
/// citadel-mount copy-artifacts
/// citadel-mount overlay
///
/// 'rootfs' creates the /dev/mapper/rootfs device which will be mounted as root filesystem
///
@ -41,6 +42,7 @@ fn main() {
Some(ref s) if s == "rootfs" => mount_rootfs(),
Some(ref s) if s == "kernel" => mount_kernel(),
Some(ref s) if s == "extra" => mount_extra(),
Some(ref s) if s == "overlay" => mount_overlay(),
_ => Err(format_err!("Bad or missing argument")),
};
@ -69,3 +71,31 @@ fn mount_extra() -> Result<()> {
image.mount()?;
Ok(())
}
fn mount_overlay() -> Result<()> {
if !CommandLine::overlay() {
info!("Not mounting rootfs overlay because citadel.overlay is not enabled");
return Ok(())
}
info!("Creating rootfs overlay");
info!("Moving /sysroot mount to /rootfs.ro");
fs::create_dir_all("/rootfs.ro")?;
util::exec_cmdline("/usr/bin/mount", "--make-private /")?;
util::exec_cmdline("/usr/bin/mount", "--move /sysroot /rootfs.ro")?;
info!("Mounting tmpfs on /rootfs.rw");
fs::create_dir_all("/rootfs.rw")?;
util::exec_cmdline("/usr/bin/mount", "-t tmpfs -orw,noatime,mode=755 rootfs.rw /rootfs.rw")?;
info!("Creating /rootfs.rw/work /rootfs.rw/upperdir");
fs::create_dir_all("/rootfs.rw/upperdir")?;
fs::create_dir_all("/rootfs.rw/work")?;
info!("Mounting overlay on /sysroot");
util::exec_cmdline("/usr/bin/mount", "-t overlay overlay -olowerdir=/rootfs.ro,upperdir=/rootfs.rw/upperdir,workdir=/rootfs.rw/work /sysroot")?;
info!("Moving /rootfs.ro and /rootfs.rw to new root");
fs::create_dir_all("/sysroot/rootfs.ro")?;
fs::create_dir_all("/sysroot/rootfs.rw")?;
util::exec_cmdline("/usr/bin/mount", "--move /rootfs.ro /sysroot/rootfs.ro")?;
util::exec_cmdline("/usr/bin/mount", "--move /rootfs.rw /sysroot/rootfs.rw")?;
Ok(())
}

View File

@ -60,6 +60,8 @@ impl CommandLine {
CommandLine::var_exists("citadel.recovery")
}
pub fn overlay() -> bool { CommandLine::var_exists("citadel.overlay") }
pub fn channel() -> Option<&'static str> {
CommandLine::get_value("citadel.channel")
}