From 3ca9537d8a2b1933df612c61eb1aac1382c57181 Mon Sep 17 00:00:00 2001 From: tiann Date: Tue, 24 Jan 2023 13:13:46 +0800 Subject: [PATCH] ksud: retry for mount --- userspace/ksud/Cargo.lock | 59 ++++++++++++++++++++++++++++++++++++- userspace/ksud/Cargo.toml | 1 + userspace/ksud/src/utils.rs | 13 ++++++-- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/userspace/ksud/Cargo.lock b/userspace/ksud/Cargo.lock index 05b59f93..03a64017 100644 --- a/userspace/ksud/Cargo.lock +++ b/userspace/ksud/Cargo.lock @@ -336,6 +336,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.11.0+wasi-snapshot-preview1", +] + [[package]] name = "heck" version = "0.4.0" @@ -426,6 +437,7 @@ dependencies = [ "java-properties", "log", "regex", + "retry", "serde", "serde_json", "subprocess", @@ -522,6 +534,12 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -564,11 +582,35 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + [[package]] name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] [[package]] name = "regex" @@ -587,6 +629,15 @@ version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" +[[package]] +name = "retry" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9166d72162de3575f950507683fac47e30f6f2c3836b71b7fbc61aa517c9c5f4" +dependencies = [ + "rand", +] + [[package]] name = "rustix" version = "0.36.6" @@ -715,7 +766,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" dependencies = [ "libc", - "wasi", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi", ] @@ -776,6 +827,12 @@ version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + [[package]] name = "winapi" version = "0.3.9" diff --git a/userspace/ksud/Cargo.toml b/userspace/ksud/Cargo.toml index ce675673..013d4194 100644 --- a/userspace/ksud/Cargo.toml +++ b/userspace/ksud/Cargo.toml @@ -20,6 +20,7 @@ serde = { version = "1.0" } serde_json = "1.0" regex = "1.5.4" encoding = "0.2.33" +retry = "2.0.0" [profile.release] strip = true diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index 05094b76..383dd614 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -4,11 +4,20 @@ use std::{ }; use anyhow::{ensure, Result}; +use retry::delay::NoDelay; use subprocess::Exec; -pub fn mount_image(src: &str, target: &str) -> Result<()> { +fn do_mount_image(src: &str, target: &str) -> Result<()> { let result = Exec::shell(format!("mount -t ext4 {} {}", src, target)).join()?; - ensure!(result.success(), "mount: {} -> {} failed.", src, target); + ensure!(result.success(), "do mount: {} -> {} failed.", src, target); + Ok(()) +} + +pub fn mount_image(src: &str, target: &str) -> Result<()> { + // umount target first. + let _ = umount_dir(target); + let a = retry::retry(NoDelay.take(3), || do_mount_image(src, target)); + ensure!(a.is_ok(), "mount: {} -> {} failed.", src, target); Ok(()) }