From 355b55a01d677ab1afefdf68e04b18320ed1e72e Mon Sep 17 00:00:00 2001 From: weishu Date: Thu, 22 Feb 2024 16:23:20 +0800 Subject: [PATCH] ksud: use bind mount to serve module webui --- userspace/ksud/src/cli.rs | 12 ++++++++++++ userspace/ksud/src/module.rs | 21 +++++++++++++++++++++ userspace/ksud/src/mount.rs | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index dac6f599..438f2d0d 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -192,6 +192,15 @@ enum Module { #[arg(default_value = "8080")] port: u16, }, + /// Link modules for manager + LinkManager { + /// module id + mid: String, + /// Manager's pid + pid: i32, + /// Manager's package name + pkg: String, + }, } #[derive(clap::Subcommand, Debug)] @@ -273,6 +282,9 @@ pub fn run() -> Result<()> { Module::List => module::list_modules(), Module::Shrink => module::shrink_ksu_images(), Module::Serve { id, port } => server::serve_module(&id, port), + Module::LinkManager { mid, pid, pkg } => { + module::link_module_for_manager(pid, &pkg, &mid) + } } } Commands::Install => event::install(), diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index bad40d7a..6f65e454 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -703,3 +703,24 @@ pub fn shrink_ksu_images() -> Result<()> { } Ok(()) } + +#[cfg(any(target_os = "linux", target_os = "android"))] +pub fn link_module_for_manager(pid: i32, pkg: &str, mid: &str) -> Result<()> { + // switch to manager's mnt ns + utils::switch_mnt_ns(pid)?; + let target = PathBuf::from("/data/data").join(pkg).join("webroot"); + + // umount previous mount + let _ = mount::umount_dir(&target); + + let src = PathBuf::from(defs::MODULE_DIR) + .join(mid) + .join(defs::MODULE_WEB_DIR); + mount::bind_mount(src, &target)?; + Ok(()) +} + +#[cfg(not(any(target_os = "linux", target_os = "android")))] +pub fn link_module_for_manager(_pid: i32, _pkg: &str, _mid: &str) -> Result<()> { + unimplemented!() +} diff --git a/userspace/ksud/src/mount.rs b/userspace/ksud/src/mount.rs index af358648..25026ad4 100644 --- a/userspace/ksud/src/mount.rs +++ b/userspace/ksud/src/mount.rs @@ -174,7 +174,7 @@ pub fn mount_tmpfs(dest: impl AsRef) -> Result<()> { } #[cfg(any(target_os = "linux", target_os = "android"))] -fn bind_mount(from: impl AsRef, to: impl AsRef) -> Result<()> { +pub fn bind_mount(from: impl AsRef, to: impl AsRef) -> Result<()> { info!( "bind mount {} -> {}", from.as_ref().display(),