ksud: fix mount and remount order

This commit is contained in:
tiann
2023-02-26 11:44:08 +08:00
parent 794b725928
commit ffa5a93c75
2 changed files with 27 additions and 16 deletions

View File

@@ -25,8 +25,8 @@ fn mount_partition(partition: &str, lowerdir: &mut Vec<String>) -> Result<()> {
let result = stock_mount.umount(); let result = stock_mount.umount();
if result.is_err() { if result.is_err() {
let remount_result = stock_mount.remount(); let remount_result = stock_mount.remount();
if remount_result.is_err() { if let Err(e) = remount_result {
log::error!("remount stock mount of failed: {:?}", remount_result); log::error!("remount stock failed: {:?}", e);
} }
bail!("umount stock mount of failed: {:?}", result); bail!("umount stock mount of failed: {:?}", result);
} }
@@ -40,11 +40,13 @@ fn mount_partition(partition: &str, lowerdir: &mut Vec<String>) -> Result<()> {
let result = mount::mount_overlay(&lowerdir, &lowest_dir); let result = mount::mount_overlay(&lowerdir, &lowest_dir);
if result.is_ok() && stock_mount.remount().is_err() { if let Err(e) = stock_mount.remount() {
// if mount overlay ok but stock remount failed, we should umount overlay if result.is_ok() {
warn!("remount stock mount of failed, umount overlay {lowest_dir} now"); // if mount overlay ok but stock remount failed, we should umount overlay
if mount::umount_dir(&lowest_dir).is_err() { warn!("remount stock failed: {:?}, umount overlay {lowest_dir}", e);
warn!("umount overlay {lowest_dir} failed"); if mount::umount_dir(&lowest_dir).is_err() {
warn!("umount overlay {lowest_dir} failed");
}
} }
} }

View File

@@ -275,16 +275,18 @@ impl StockMount {
} }
fn get_target_mounts(&self) -> Vec<&proc_mounts::MountInfo> { fn get_target_mounts(&self) -> Vec<&proc_mounts::MountInfo> {
let mounts = self let mut mounts = self
.mountlist .mountlist
.destination_starts_with(std::path::Path::new(&self.mnt)) .destination_starts_with(std::path::Path::new(&self.mnt))
.filter(|m| m.fstype != "overlay" && m.fstype != "rootfs"); .filter(|m| m.fstype != "overlay" && m.fstype != "rootfs")
mounts.collect() .collect::<Vec<_>>();
mounts.sort_by(|a, b| b.dest.cmp(&a.dest)); // inverse order
mounts
} }
pub fn umount(&self) -> Result<()> { pub fn umount(&self) -> Result<()> {
let mounts = self.get_target_mounts(); let mounts = self.get_target_mounts();
log::info!("stock mount for {} : {:?}", self.mnt, mounts); log::info!("umount stock for {} : {:?}", self.mnt, mounts);
for m in mounts { for m in mounts {
let dst = m let dst = m
.dest .dest
@@ -293,11 +295,14 @@ impl StockMount {
umount_dir(dst)?; umount_dir(dst)?;
log::info!("umount: {:?}", m); log::info!("umount: {:?}", m);
} }
log::info!("umount stock succeed!");
Ok(()) Ok(())
} }
pub fn remount(&self) -> Result<()> { pub fn remount(&self) -> Result<()> {
let mounts = self.get_target_mounts(); let mut mounts = self.get_target_mounts();
mounts.reverse(); // remount it in order
log::info!("remount stock for {} : {:?}", self.mnt, mounts);
for m in mounts { for m in mounts {
let src = std::fs::canonicalize(&m.source)?; let src = std::fs::canonicalize(&m.source)?;
@@ -310,16 +315,20 @@ impl StockMount {
let fstype = m.fstype.as_str(); let fstype = m.fstype.as_str();
let options = m.options.join(","); let options = m.options.join(",");
log::info!("mount: {:?}", m); log::info!("begin remount: {src} -> {dst}");
std::process::Command::new("mount") let result = std::process::Command::new("mount")
.arg("-t") .arg("-t")
.arg(fstype) .arg(fstype)
.arg("-o") .arg("-o")
.arg(options) .arg(options)
.arg(src) .arg(src)
.arg(dst) .arg(dst)
.status() .status();
.with_context(|| format!("Failed to mount {:?}", m))?; if let Err(e) = result {
log::error!("remount failed: {}", e);
} else {
log::info!("remount {src} -> {dst} succeed!");
}
} }
Ok(()) Ok(())
} }