ksud: add context for module install failure.

This commit is contained in:
tiann
2023-01-05 13:41:29 +08:00
parent ed2fb62034
commit 9e1ee1c513

View File

@@ -96,14 +96,13 @@ fn get_minimal_image_size(img: &str) -> Result<u64> {
} }
fn check_image(img: &str) -> Result<()> { fn check_image(img: &str) -> Result<()> {
// trim image
let result = Command::new("e2fsck") let result = Command::new("e2fsck")
.args(["-yf", img]) .args(["-yf", img])
.stdout(Stdio::null()) .stdout(Stdio::null())
.stderr(Stdio::null()) .stderr(Stdio::null())
.status() .status()
.with_context(|| format!("Failed exec e2fsck {}", img))?; .with_context(|| format!("Failed exec e2fsck {}", img))?;
ensure!(result.success(), "f2fsck exec failed."); ensure!(result.success(), "check image f2sck exec failed.");
Ok(()) Ok(())
} }
@@ -127,6 +126,35 @@ fn grow_image_size(img: &str, extra_size: u64) -> Result<()> {
Ok(()) Ok(())
} }
/// execute every modules' post-fs-data.sh
pub fn exec_post_fs_data() -> Result<()> {
let modules_dir = Path::new(defs::MODULE_DIR);
let dir = std::fs::read_dir(modules_dir)?;
for entry in dir.flatten() {
let path = entry.path();
let disabled = path.join(defs::DISABLE_FILE_NAME);
if disabled.exists() {
println!("{} is disabled, skip", path.display());
continue;
}
let post_fs_data = path.join("post-fs-data.sh");
if !post_fs_data.exists() {
continue;
}
println!("exec {} post-fs-data.sh", path.display());
Command::new("/system/bin/sh")
.arg(&post_fs_data)
.current_dir(path)
.env("KSU", "1")
.status()
.with_context(|| format!("Failed to exec {}", post_fs_data.display()))?;
}
Ok(())
}
pub fn install_module(zip: String) -> Result<()> { pub fn install_module(zip: String) -> Result<()> {
ensure_boot_completed()?; ensure_boot_completed()?;
@@ -161,7 +189,6 @@ pub fn install_module(zip: String) -> Result<()> {
info!("module id: {}", module_id); info!("module id: {}", module_id);
let modules_img = Path::new(defs::MODULE_IMG); let modules_img = Path::new(defs::MODULE_IMG);
let modules_dir = Path::new(defs::MODULE_DIR);
let modules_update_img = Path::new(defs::MODULE_UPDATE_IMG); let modules_update_img = Path::new(defs::MODULE_UPDATE_IMG);
let module_update_tmp_dir = defs::MODULE_UPDATE_TMP_DIR; let module_update_tmp_dir = defs::MODULE_UPDATE_TMP_DIR;
@@ -172,8 +199,8 @@ pub fn install_module(zip: String) -> Result<()> {
let img_size_per_m = current_img_size / 1024 / 1024 + 256; let img_size_per_m = current_img_size / 1024 / 1024 + 256;
let modules_exist = modules_dir.exists(); let modules_img_exist = modules_img.exists();
let modules_update_exist = modules_update_img.exists(); let modules_update_img_exist = modules_update_img.exists();
// prepare the tmp module img // prepare the tmp module img
let tmp_module_img = defs::MODULE_UPDATE_TMP_IMG; let tmp_module_img = defs::MODULE_UPDATE_TMP_IMG;
@@ -186,7 +213,7 @@ pub fn install_module(zip: String) -> Result<()> {
println!("- Preparing image"); println!("- Preparing image");
if !modules_exist && !modules_update_exist { if !modules_img_exist && !modules_update_img_exist {
// if no modules and modules_update, it is brand new installation, we should create a new img // if no modules and modules_update, it is brand new installation, we should create a new img
// create a tmp module img and mount it to modules_update // create a tmp module img and mount it to modules_update
let result = Exec::shell(format!( let result = Exec::shell(format!(
@@ -206,14 +233,26 @@ pub fn install_module(zip: String) -> Result<()> {
ensure!(result.success(), "format ext4 image failed!"); ensure!(result.success(), "format ext4 image failed!");
check_image(tmp_module_img)?; check_image(tmp_module_img)?;
} else if modules_update_exist { } else if modules_update_img_exist {
// modules_update.img exists, we should use it as tmp img // modules_update.img exists, we should use it as tmp img
std::fs::copy(modules_update_img, tmp_module_img)?; std::fs::copy(modules_update_img, tmp_module_img).with_context(|| {
format!(
"Failed to copy {} to {}",
modules_update_img.display(),
tmp_module_img
)
})?;
// grow size of the tmp image // grow size of the tmp image
grow_image_size(tmp_module_img, default_grow_size)?; grow_image_size(tmp_module_img, default_grow_size)?;
} else { } else {
// modules.img exists, we should use it as tmp img // modules.img exists, we should use it as tmp img
std::fs::copy(modules_img, tmp_module_img)?; std::fs::copy(modules_img, tmp_module_img).with_context(|| {
format!(
"Failed to copy {} to {}",
modules_img.display(),
tmp_module_img
)
})?;
// grow size of the tmp image // grow size of the tmp image
grow_image_size(tmp_module_img, default_grow_size)?; grow_image_size(tmp_module_img, default_grow_size)?;
} }
@@ -222,6 +261,8 @@ pub fn install_module(zip: String) -> Result<()> {
ensure_clean_dir(module_update_tmp_dir)?; ensure_clean_dir(module_update_tmp_dir)?;
// mount the modules_update.img to mountpoint // mount the modules_update.img to mountpoint
println!("- Mounting image");
mount_image(tmp_module_img, module_update_tmp_dir)?; mount_image(tmp_module_img, module_update_tmp_dir)?;
let result = { let result = {
@@ -253,10 +294,12 @@ pub fn install_module(zip: String) -> Result<()> {
let _ = remove_dir_all(module_update_tmp_dir); let _ = remove_dir_all(module_update_tmp_dir);
// return if exec script failed // return if exec script failed
result?; result.with_context(|| format!("Failed to execute install script for {}", module_id))?;
// all done, rename the tmp image to modules_update.img // all done, rename the tmp image to modules_update.img
std::fs::rename(tmp_module_img, defs::MODULE_UPDATE_IMG)?; if std::fs::rename(tmp_module_img, defs::MODULE_UPDATE_IMG).is_err() {
let _ = std::fs::remove_file(tmp_module_img);
}
mark_update()?; mark_update()?;