ksud: fallback to system mount when rust lib mount failed

This commit is contained in:
tiann
2023-02-02 17:12:41 +08:00
parent 1727ec41c4
commit 86998a032e
2 changed files with 28 additions and 30 deletions

View File

@@ -505,35 +505,29 @@ fn do_install_module(zip: String) -> Result<()> {
// mount the modules_update.img to mountpoint // mount the modules_update.img to mountpoint
println!("- Mounting image"); println!("- Mounting image");
{ mount::AutoMountExt4::try_new(tmp_module_img, module_update_tmp_dir)?;
// we need auto drop, so we use a block here
mount::mount_ext4(tmp_module_img, module_update_tmp_dir, true)?;
setsyscon(module_update_tmp_dir)?; setsyscon(module_update_tmp_dir)?;
let module_dir = format!("{module_update_tmp_dir}/{module_id}"); let module_dir = format!("{module_update_tmp_dir}/{module_id}");
ensure_clean_dir(&module_dir)?; ensure_clean_dir(&module_dir)?;
info!("module dir: {}", module_dir); info!("module dir: {}", module_dir);
// unzip the image and move it to modules_update/<id> dir // unzip the image and move it to modules_update/<id> dir
let file = File::open(&zip)?; let file = File::open(&zip)?;
let mut archive = zip::ZipArchive::new(file)?; let mut archive = zip::ZipArchive::new(file)?;
archive.extract(&module_dir)?; archive.extract(&module_dir)?;
// set selinux for module/system dir // set selinux for module/system dir
let mut module_system_dir = PathBuf::from(module_dir); let mut module_system_dir = PathBuf::from(module_dir);
module_system_dir.push("system"); module_system_dir.push("system");
let module_system_dir = module_system_dir.as_path(); let module_system_dir = module_system_dir.as_path();
if module_system_dir.exists() { if module_system_dir.exists() {
let path = module_system_dir.to_str().unwrap(); let path = module_system_dir.to_str().unwrap();
restore_syscon(path)?; restore_syscon(path)?;
}
exec_install_script(&zip)?;
} }
// remove modules_update dir, ignore the error exec_install_script(&zip)?;
remove_dir_all(module_update_tmp_dir)?;
// all done, rename the tmp image to modules_update.img // all done, rename the tmp image to modules_update.img
if std::fs::rename(tmp_module_img, defs::MODULE_UPDATE_IMG).is_err() { if std::fs::rename(tmp_module_img, defs::MODULE_UPDATE_IMG).is_err() {
@@ -551,7 +545,6 @@ pub fn install_module(zip: String) -> Result<()> {
// error happened, do some cleanup! // error happened, do some cleanup!
let _ = std::fs::remove_file(defs::MODULE_UPDATE_TMP_IMG); let _ = std::fs::remove_file(defs::MODULE_UPDATE_TMP_IMG);
let _ = mount::umount_dir(defs::MODULE_UPDATE_TMP_DIR); let _ = mount::umount_dir(defs::MODULE_UPDATE_TMP_DIR);
let _ = std::fs::remove_dir_all(defs::MODULE_UPDATE_TMP_DIR);
println!("- Error: {e}"); println!("- Error: {e}");
} }
result result
@@ -588,13 +581,11 @@ where
ensure_clean_dir(update_dir)?; ensure_clean_dir(update_dir)?;
// mount the modules_update img // mount the modules_update img
mount::mount_ext4(defs::MODULE_UPDATE_TMP_IMG, update_dir, true)?; mount::AutoMountExt4::try_new(defs::MODULE_UPDATE_TMP_IMG, update_dir)?;
// call the operation func // call the operation func
let result = func(id, update_dir); let result = func(id, update_dir);
// umount modules_update.img
let _ = mount::umount_dir(update_dir);
let _ = remove_dir_all(update_dir); let _ = remove_dir_all(update_dir);
std::fs::rename(modules_update_tmp_img, defs::MODULE_UPDATE_IMG)?; std::fs::rename(modules_update_tmp_img, defs::MODULE_UPDATE_IMG)?;

View File

@@ -7,15 +7,16 @@ use retry::delay::NoDelay;
#[cfg(target_os = "android")] #[cfg(target_os = "android")]
use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags}; use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags};
#[cfg(target_os = "android")] #[allow(dead_code)]
struct AutoMountExt4 { pub struct AutoMountExt4 {
mnt: String, mnt: String,
#[cfg(target_os = "android")]
mount: Option<Mount>, mount: Option<Mount>,
} }
#[cfg(target_os = "android")]
#[allow(dead_code)] #[allow(dead_code)]
impl AutoMountExt4 { impl AutoMountExt4 {
#[cfg(target_os = "android")]
pub fn try_new(src: &str, mnt: &str) -> Result<Self> { pub fn try_new(src: &str, mnt: &str) -> Result<Self> {
let result = Mount::builder() let result = Mount::builder()
.fstype(FilesystemType::from("ext4")) .fstype(FilesystemType::from("ext4"))
@@ -50,6 +51,12 @@ impl AutoMountExt4 {
} }
} }
#[cfg(not(target_os = "android"))]
pub fn try_new(_src: &str, _mnt: &str) -> Result<Self> {
unimplemented!()
}
#[cfg(target_os = "android")]
pub fn umount(&self) -> Result<()> { pub fn umount(&self) -> Result<()> {
match self.mount { match self.mount {
Some(ref mount) => mount Some(ref mount) => mount