From 71c2790f084280b0dddb6acd50525e25cec9a815 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 30 Jan 2024 12:20:36 +0800 Subject: [PATCH 1/6] ksud: remove unnecessary image check --- userspace/ksud/src/module.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index 3a500af53c91..626ac492b59d 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -361,6 +361,7 @@ fn _install_module(zip: &str) -> Result<()> { "Failed to format ext4 image: {}", String::from_utf8(result.stderr).unwrap() ); + check_image(tmp_module_img)?; } else if modules_update_img_exist { // modules_update.img exists, we should use it as tmp img info!("Using existing modules_update.img as tmp image"); @@ -399,7 +400,6 @@ fn _install_module(zip: &str) -> Result<()> { .status()?; } } - check_image(tmp_module_img)?; // ensure modules_update exists ensure_dir_exists(module_update_tmp_dir)?; From be70a91f161fdf39ea6340a35934e980865f83f8 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 30 Jan 2024 12:22:12 +0800 Subject: [PATCH 2/6] ksud: resize the journal size of image --- userspace/ksud/src/module.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index 626ac492b59d..72f2173e7d1d 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -340,6 +340,7 @@ fn _install_module(zip: &str) -> Result<()> { ); let sparse_image_size = 256 * (1 << 30); // 256G + let jounnel_size = 8; // 8M 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 // create a tmp module img and mount it to modules_update @@ -353,6 +354,8 @@ fn _install_module(zip: &str) -> Result<()> { let result = Command::new("mkfs.ext4") .arg("-b") .arg("1024") + .arg("-J") + .arg(format!("size={jounnel_size}")) .arg(tmp_module_img) .stdout(Stdio::piped()) .output()?; From b6ecce431783d2bf5de4297f26eec47bf62411f3 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 30 Jan 2024 12:30:21 +0800 Subject: [PATCH 3/6] ksud: use default block size to reduce image size --- userspace/ksud/src/module.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index 72f2173e7d1d..8c0378bb8de8 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -352,8 +352,6 @@ fn _install_module(zip: &str) -> Result<()> { // format the img to ext4 filesystem let result = Command::new("mkfs.ext4") - .arg("-b") - .arg("1024") .arg("-J") .arg(format!("size={jounnel_size}")) .arg(tmp_module_img) From 946fb6f99984fc7bf78ec956a2ea4572c31b1514 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 30 Jan 2024 12:42:05 +0800 Subject: [PATCH 4/6] ksud: default 1T for sparse file --- userspace/ksud/src/module.rs | 2 +- website/docs/guide/faq.md | 4 ++-- website/docs/zh_CN/guide/faq.md | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index 8c0378bb8de8..baae59439452 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -339,7 +339,7 @@ fn _install_module(zip: &str) -> Result<()> { humansize::format_size(zip_uncompressed_size, humansize::DECIMAL) ); - let sparse_image_size = 256 * (1 << 30); // 256G + let sparse_image_size = 1 << 40; // 1T let jounnel_size = 8; // 8M 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 diff --git a/website/docs/guide/faq.md b/website/docs/guide/faq.md index e3b78bd4ed4a..b17c55ea1b9e 100644 --- a/website/docs/guide/faq.md +++ b/website/docs/guide/faq.md @@ -67,8 +67,8 @@ We do not recommend you to modify the system partition directly. You should use Of course. But KernelSU doesn't have builtin hosts support, you can install [systemless-hosts](https://github.com/symbuzzer/systemless-hosts-KernelSU-module) to do it. -## Why is there a huge 256G file? +## Why is there a huge 1T file? -The 256G size `modules.img` is a disk image file, **don't worry about its size**, it's a special type of file known as a [sparse file](https://en.wikipedia.org/wiki/Sparse_file), its actual size is only the size of the module you use, and it will dynamically shrink after you delete the module; it does not actually occupy 256G of disk space (actually your mobile phone may not have that much space) . +The 1T size `modules.img` is a disk image file, **don't worry about its size**, it's a special type of file known as a [sparse file](https://en.wikipedia.org/wiki/Sparse_file), its actual size is only the size of the module you use, and it will dynamically shrink after you delete the module; it does not actually occupy 1T of disk space (actually your mobile phone may not have that much space) . If you're really unhappy with the size of this file, you can use the `resize2fs -M` command to make it the actual size; but the module may not work properly at this time, and we won't provide any support for this. diff --git a/website/docs/zh_CN/guide/faq.md b/website/docs/zh_CN/guide/faq.md index b44bbd0df730..dac9ae670ce7 100644 --- a/website/docs/zh_CN/guide/faq.md +++ b/website/docs/zh_CN/guide/faq.md @@ -67,8 +67,8 @@ GKI1 跟 GKI2 完全是两个东西,所以你需要自行编译内核。 当然可以。但这个功能 KernelSU 没有内置,你可以安装这个 [systemless-hosts](https://github.com/symbuzzer/systemless-hosts-KernelSU-module) -## 为什么有个 256G 的超大文件? +## 为什么有个 1T 的超大文件? -256G 大小的 `modules.img` 是一个磁盘镜像文件,**不要担心它的大小**,它是一种被称之为[稀疏文件](https://en.wikipedia.org/wiki/Sparse_file)的文件格式,它的实际大小只有你使用的模块的大小,并且你在删除模块后它会动态缩小;它并不实际占用 256G 大小的磁盘空间(实际上你手机可能并没有这么多空间)。 +1T 大小的 `modules.img` 是一个磁盘镜像文件,**不要担心它的大小**,它是一种被称之为[稀疏文件](https://en.wikipedia.org/wiki/Sparse_file)的文件格式,它的实际大小只有你使用的模块的大小,并且你在删除模块后它会动态缩小;它并不实际占用 1T 大小的磁盘空间(实际上你手机可能并没有这么多空间)。 如果你真的对这个文件的大小感到不爽,你可以使用 `resize2fs -M` 命令让它变成实际大小;但此时模块可能无法正常工作,我们也不会为此提供任何支持。 From cce423a2f698fffcf03b9d5559a9bf36c6fbf7b5 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 30 Jan 2024 12:49:24 +0800 Subject: [PATCH 5/6] ksud: Add cli for fast copy sparse file --- userspace/ksud/src/cli.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/userspace/ksud/src/cli.rs b/userspace/ksud/src/cli.rs index 5e941522dabf..7848c66e806b 100644 --- a/userspace/ksud/src/cli.rs +++ b/userspace/ksud/src/cli.rs @@ -112,6 +112,13 @@ enum Debug { Mount, + /// Copy sparse file + Xcp { + /// source file + src: String, + /// destination file + dst: String, + }, /// For testing Test, } @@ -277,6 +284,10 @@ pub fn run() -> Result<()> { } Debug::Su => crate::ksu::grant_root(), Debug::Mount => event::mount_systemlessly(defs::MODULE_DIR), + Debug::Xcp { src, dst } => { + utils::copy_sparse_file(&src, &dst)?; + Ok(()) + } Debug::Test => todo!(), }, From ca960a2a8f0c7ad435e55d474202267e3a1b6c10 Mon Sep 17 00:00:00 2001 From: weishu Date: Tue, 30 Jan 2024 12:56:42 +0800 Subject: [PATCH 6/6] ksud: shrink image before resize --- userspace/ksud/src/module.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/userspace/ksud/src/module.rs b/userspace/ksud/src/module.rs index baae59439452..e3c45b431bad 100644 --- a/userspace/ksud/src/module.rs +++ b/userspace/ksud/src/module.rs @@ -387,6 +387,16 @@ fn _install_module(zip: &str) -> Result<()> { // legacy image, truncate it to new size. if std::fs::metadata(modules_img)?.len() < sparse_image_size { println!("- Truncate legacy image to new size"); + + // shrink it to minimum size + check_image(tmp_module_img)?; + Command::new("resize2fs") + .arg("-M") + .arg(tmp_module_img) + .stdout(Stdio::piped()) + .status()?; + + // truncate the file to new size OpenOptions::new() .write(true) .open(tmp_module_img) @@ -394,6 +404,7 @@ fn _install_module(zip: &str) -> Result<()> { .set_len(sparse_image_size) .context("Failed to truncate ext4 image")?; + // resize the image to new size check_image(tmp_module_img)?; Command::new("resize2fs") .arg(tmp_module_img)