Use new mount api

This commit is contained in:
LoveSy
2024-02-01 00:22:25 +08:00
parent c1a2cbf1e4
commit 4d4bd4793f
4 changed files with 62 additions and 148 deletions

View File

@@ -853,6 +853,7 @@ dependencies = [
"jwalk",
"libc",
"log",
"loopdev",
"nom",
"procfs",
"regex",
@@ -862,7 +863,6 @@ dependencies = [
"serde",
"serde_json",
"sha256",
"sys-mount",
"tempdir",
"which",
"zip 0.6.4",
@@ -1463,17 +1463,6 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "smart-default"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "133659a15339456eeeb07572eb02a91c91e9815e9cbc89566944d2c8d3efdbf6"
dependencies = [
"proc-macro2",
"quote",
"syn 1.0.107",
]
[[package]]
name = "strsim"
version = "0.10.0"
@@ -1508,19 +1497,6 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "sys-mount"
version = "2.0.2"
source = "git+https://github.com/tiann/sys-mount?branch=loopfix#c7c4048e4a4ffdf8b108a85956363a75f2c554f0"
dependencies = [
"bitflags 1.3.2",
"libc",
"loopdev",
"smart-default",
"thiserror",
"tracing",
]
[[package]]
name = "tempdir"
version = "0.3.7"
@@ -1599,37 +1575,6 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "tracing"
version = "0.1.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef"
dependencies = [
"pin-project-lite",
"tracing-attributes",
"tracing-core",
]
[[package]]
name = "tracing-attributes"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.48",
]
[[package]]
name = "tracing-core"
version = "0.1.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54"
dependencies = [
"once_cell",
]
[[package]]
name = "typenum"
version = "1.16.0"

View File

@@ -39,11 +39,11 @@ chrono = "0.4"
hole-punch = { git = "https://github.com/tiann/hole-punch" }
[target.'cfg(any(target_os = "android", target_os = "linux"))'.dependencies]
sys-mount = { git = "https://github.com/tiann/sys-mount", branch = "loopfix" }
rustix = { version = "0.38", features = ["all-apis"] }
# some android specific dependencies which compiles under unix are also listed here for convenience of coding
android-properties = { version = "0.2.2", features = ["bionic-deprecated"] }
procfs = "0.16"
loopdev = { git = "https://github.com/tiann/loopdev", branch = "loopfix" }
[target.'cfg(target_os = "android")'.dependencies]
android_logger = "0.13"

View File

@@ -1,5 +1,7 @@
use anyhow::{bail, Context, Result};
use log::{info, warn};
#[cfg(target_os = "android")]
use std::path::PathBuf;
use std::{collections::HashMap, path::Path};
use crate::module::prune_modules;

View File

@@ -1,11 +1,11 @@
use anyhow::{bail, Ok, Result};
use anyhow::{anyhow, bail, Ok, Result};
#[cfg(any(target_os = "linux", target_os = "android"))]
use anyhow::Context;
#[cfg(any(target_os = "linux", target_os = "android"))]
use retry::delay::NoDelay;
#[cfg(any(target_os = "linux", target_os = "android"))]
use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags};
use rustix::{fd::AsFd, fs::CWD, mount::*};
use crate::defs::KSU_OVERLAY_SOURCE;
use log::{info, warn};
@@ -14,49 +14,32 @@ use procfs::process::Process;
use std::path::Path;
pub struct AutoMountExt4 {
mnt: String,
#[cfg(any(target_os = "linux", target_os = "android"))]
mount: Option<Mount>,
target: String,
auto_umount: bool,
}
impl AutoMountExt4 {
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn try_new(src: &str, mnt: &str, auto_umount: bool) -> Result<Self> {
let result = Mount::builder()
.fstype(FilesystemType::from("ext4"))
.flags(MountFlags::empty())
.create_loop(true)
.mount(src, mnt)
.map(|mount| {
pub fn try_new(source: &str, target: &str, auto_umount: bool) -> Result<Self> {
let new_loopback = loopdev::LoopControl::open()?.next_free()?;
new_loopback.with().attach(source)?;
let lo = new_loopback.path().ok_or(anyhow!("no loop"))?;
let fs = fsopen("ext4", FsOpenFlags::FSOPEN_CLOEXEC)?;
let fs = fs.as_fd();
fsconfig_set_string(fs, "source", lo)?;
fsconfig_create(fs)?;
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
move_mount(
mount.as_fd(),
"",
CWD,
target,
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
)?;
Ok(Self {
mnt: mnt.to_string(),
mount: Some(mount),
target: target.to_string(),
auto_umount,
})
});
if let Err(e) = result {
println!("- Mount failed: {e}, retry with system mount");
let result = std::process::Command::new("mount")
.arg("-t")
.arg("ext4")
.arg(src)
.arg(mnt)
.status();
if let Err(e) = result {
Err(anyhow::anyhow!(
"mount partition: {src} -> {mnt} failed: {e}"
))
} else {
Ok(Self {
mnt: mnt.to_string(),
mount: None,
auto_umount,
})
}
} else {
result.unwrap()
}
}
#[cfg(not(any(target_os = "linux", target_os = "android")))]
@@ -66,27 +49,17 @@ impl AutoMountExt4 {
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn umount(&self) -> Result<()> {
if let Some(ref mount) = self.mount {
mount
.unmount(UnmountFlags::empty())
.map_err(|e| anyhow::anyhow!(e))
} else {
let result = std::process::Command::new("umount").arg(&self.mnt).status();
if let Err(e) = result {
Err(anyhow::anyhow!("umount: {} failed: {e}", self.mnt))
} else {
unmount(self.target.as_str(), UnmountFlags::DETACH)?;
Ok(())
}
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
impl Drop for AutoMountExt4 {
fn drop(&mut self) {
log::info!(
"AutoMountExt4 drop: {}, auto_umount: {}",
self.mnt,
self.target,
self.auto_umount
);
if self.auto_umount {
@@ -98,18 +71,7 @@ impl Drop for AutoMountExt4 {
#[allow(dead_code)]
#[cfg(any(target_os = "linux", target_os = "android"))]
fn mount_image(src: &str, target: &str, autodrop: bool) -> Result<()> {
if autodrop {
Mount::builder()
.fstype(FilesystemType::from("ext4"))
.create_loop(true)
.mount_autodrop(src, target, UnmountFlags::empty())
.with_context(|| format!("Failed to do mount: {src} -> {target}"))?;
} else {
Mount::builder()
.fstype(FilesystemType::from("ext4"))
.mount(src, target)
.with_context(|| format!("Failed to do mount: {src} -> {target}"))?;
}
AutoMountExt4::try_new(src, target, autodrop)?;
Ok(())
}
@@ -146,28 +108,37 @@ fn mount_overlayfs(
dest.as_ref().display(),
options
);
Mount::builder()
.fstype(FilesystemType::from("overlay"))
.data(&options)
.flags(MountFlags::RDONLY)
.mount(KSU_OVERLAY_SOURCE, dest.as_ref())
.with_context(|| {
format!(
"mount overlayfs on {} options {} failed",
dest.as_ref().display(),
options
)
})?;
let fs = fsopen("overlay", FsOpenFlags::FSOPEN_CLOEXEC)?;
let fs = fs.as_fd();
fsconfig_set_string(fs, "lowerdir", lower_dirs.join(":"))?;
fsconfig_set_string(fs, "source", KSU_OVERLAY_SOURCE)?;
fsconfig_create(fs)?;
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
move_mount(
mount.as_fd(),
"",
CWD,
dest.as_ref(),
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
)?;
Ok(())
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn mount_tmpfs(dest: impl AsRef<Path>) -> Result<()> {
info!("mount tmpfs on {}", dest.as_ref().display());
Mount::builder()
.fstype(FilesystemType::from("tmpfs"))
.mount(KSU_OVERLAY_SOURCE, dest.as_ref())
.with_context(|| format!("mount tmpfs on {} failed", dest.as_ref().display()))?;
let fs = fsopen("tmpfs", FsOpenFlags::FSOPEN_CLOEXEC)?;
let fs = fs.as_fd();
fsconfig_set_string(fs, "source", KSU_OVERLAY_SOURCE)?;
fsconfig_create(fs)?;
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
move_mount(
mount.as_fd(),
"",
CWD,
dest.as_ref(),
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
)?;
Ok(())
}
@@ -178,16 +149,12 @@ fn bind_mount(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
from.as_ref().display(),
to.as_ref().display()
);
Mount::builder()
.flags(MountFlags::BIND)
.mount(from.as_ref(), to.as_ref())
.with_context(|| {
format!(
"bind mount failed: {} -> {}",
from.as_ref().display(),
to.as_ref().display()
)
})?;
let tree = open_tree(
CWD,
from.as_ref(),
OpenTreeFlags::OPEN_TREE_CLOEXEC | OpenTreeFlags::OPEN_TREE_CLONE,
)?;
move_mount(tree.as_fd(), "", CWD, to.as_ref(), MoveMountFlags::empty())?;
Ok(())
}