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
println!("- Mounting image");
{
// we need auto drop, so we use a block here
mount::mount_ext4(tmp_module_img, module_update_tmp_dir, true)?;
mount::AutoMountExt4::try_new(tmp_module_img, module_update_tmp_dir)?;
setsyscon(module_update_tmp_dir)?;
setsyscon(module_update_tmp_dir)?;
let module_dir = format!("{module_update_tmp_dir}/{module_id}");
ensure_clean_dir(&module_dir)?;
info!("module dir: {}", module_dir);
let module_dir = format!("{module_update_tmp_dir}/{module_id}");
ensure_clean_dir(&module_dir)?;
info!("module dir: {}", module_dir);
// unzip the image and move it to modules_update/<id> dir
let file = File::open(&zip)?;
let mut archive = zip::ZipArchive::new(file)?;
archive.extract(&module_dir)?;
// unzip the image and move it to modules_update/<id> dir
let file = File::open(&zip)?;
let mut archive = zip::ZipArchive::new(file)?;
archive.extract(&module_dir)?;
// set selinux for module/system dir
let mut module_system_dir = PathBuf::from(module_dir);
module_system_dir.push("system");
let module_system_dir = module_system_dir.as_path();
if module_system_dir.exists() {
let path = module_system_dir.to_str().unwrap();
restore_syscon(path)?;
}
exec_install_script(&zip)?;
// set selinux for module/system dir
let mut module_system_dir = PathBuf::from(module_dir);
module_system_dir.push("system");
let module_system_dir = module_system_dir.as_path();
if module_system_dir.exists() {
let path = module_system_dir.to_str().unwrap();
restore_syscon(path)?;
}
// remove modules_update dir, ignore the error
remove_dir_all(module_update_tmp_dir)?;
exec_install_script(&zip)?;
// all done, rename the tmp image to modules_update.img
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!
let _ = std::fs::remove_file(defs::MODULE_UPDATE_TMP_IMG);
let _ = mount::umount_dir(defs::MODULE_UPDATE_TMP_DIR);
let _ = std::fs::remove_dir_all(defs::MODULE_UPDATE_TMP_DIR);
println!("- Error: {e}");
}
result
@@ -588,13 +581,11 @@ where
ensure_clean_dir(update_dir)?;
// 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
let result = func(id, update_dir);
// umount modules_update.img
let _ = mount::umount_dir(update_dir);
let _ = remove_dir_all(update_dir);
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")]
use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags};
#[cfg(target_os = "android")]
struct AutoMountExt4 {
#[allow(dead_code)]
pub struct AutoMountExt4 {
mnt: String,
#[cfg(target_os = "android")]
mount: Option<Mount>,
}
#[cfg(target_os = "android")]
#[allow(dead_code)]
impl AutoMountExt4 {
#[cfg(target_os = "android")]
pub fn try_new(src: &str, mnt: &str) -> Result<Self> {
let result = Mount::builder()
.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<()> {
match self.mount {
Some(ref mount) => mount