From 8e341d6005deabbf25a807c4fa99031072388ebc Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Sun, 6 Jan 2019 18:15:44 -0500 Subject: [PATCH] Mount tmpfs overlay over rootfs if citadel.overlay is set --- citadel-mount/src/main.rs | 34 ++++++++++++++++++++++++++++++++-- libcitadel/src/cmdline.rs | 2 ++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/citadel-mount/src/main.rs b/citadel-mount/src/main.rs index 07f4780..ca46698 100644 --- a/citadel-mount/src/main.rs +++ b/citadel-mount/src/main.rs @@ -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(()) +} diff --git a/libcitadel/src/cmdline.rs b/libcitadel/src/cmdline.rs index df610fe..fe3d786 100644 --- a/libcitadel/src/cmdline.rs +++ b/libcitadel/src/cmdline.rs @@ -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") }