From 9ee211eacad51bce6858ca9b9755b6c9eb253f2e Mon Sep 17 00:00:00 2001 From: TruongSinh Tran-Nguyen Date: Wed, 26 Apr 2023 10:15:12 -0700 Subject: [PATCH] feat(cmd_mount): Add support for magic keyword `OLD_BLKID_UUID` This commit enhances mount sub command by allowing it to accept `OLD_BLKID_UUID=xxx` as the device, enabling whole fs mounting without relying on colon-separated devices. Using colon-separated devices in fstab is discouraged due to potential device reference issues after reboot. Additionally, using `UUID=xxx` in fstab or with `mount -t bcachefs` is currently infeasible as it depends on blkid, which prior to v2.39, does not recognize bcachefs. This update enables mounting via fstab using `OLD_BLKID_UUID=xxx` as obtained from `bcachefs show-super` "External UUID". Signed-off-by: TruongSinh Tran-Nguyen --- rust-src/src/cmd_mount.rs | 44 +++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/rust-src/src/cmd_mount.rs b/rust-src/src/cmd_mount.rs index af370ef6..0150ffd5 100644 --- a/rust-src/src/cmd_mount.rs +++ b/rust-src/src/cmd_mount.rs @@ -1,9 +1,8 @@ use atty::Stream; use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle}; -use log::{info, warn, debug, error, trace, LevelFilter}; +use log::{info, debug, error, LevelFilter}; use clap::Parser; use uuid::Uuid; -use std::convert::TryInto; use std::path::PathBuf; use crate::key; use crate::key::KeyLoc; @@ -161,17 +160,31 @@ struct Cli { verbose: u8, } +fn devs_str_sbs_from_uuid(uuid: String) -> anyhow::Result<(String, Vec)> { + debug!("enumerating devices with UUID {}", uuid); + + let devs_sbs = Uuid::parse_str(&uuid) + .map(|uuid| get_devices_by_uuid(uuid))??; + + let devs_str = devs_sbs + .iter() + .map(|(dev, _)| dev.to_str().unwrap()) + .collect::>() + .join(":"); + + let sbs: Vec = devs_sbs.iter().map(|(_, sb)| *sb).collect(); + + Ok((devs_str, sbs)) + +} + fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> { let (devs, sbs) = if opt.dev.starts_with("UUID=") { let uuid = opt.dev.replacen("UUID=", "", 1); - let uuid = Uuid::parse_str(&uuid)?; - let devs_sbs = get_devices_by_uuid(uuid)?; - - let devs_strs: Vec<_> = devs_sbs.iter().map(|(dev, _)| dev.clone().into_os_string().into_string().unwrap()).collect(); - let devs_str = devs_strs.join(":"); - let sbs = devs_sbs.iter().map(|(_, sb)| *sb).collect(); - - (devs_str, sbs) + devs_str_sbs_from_uuid(uuid)? + } else if opt.dev.starts_with("OLD_BLKID_UUID=") { + let uuid = opt.dev.replacen("OLD_BLKID_UUID=", "", 1); + devs_str_sbs_from_uuid(uuid)? } else { let mut sbs = Vec::new(); @@ -183,7 +196,9 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> { (opt.dev, sbs) }; - if unsafe { bcachefs::bch2_sb_is_encrypted(sbs[0].sb) } { + if sbs.len() == 0 { + Err(anyhow::anyhow!("No device found from specified parameters"))?; + } else if unsafe { bcachefs::bch2_sb_is_encrypted(sbs[0].sb) } { let key = opt .key_location .0 @@ -192,6 +207,13 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> { key::prepare_key(&sbs[0], key)?; } + info!( + "mounting with params: device: {}, target: {}, options: {}", + devs, + &opt.mountpoint.to_string_lossy(), + &opt.options + ); + mount(devs, &opt.mountpoint, &opt.options)?; Ok(()) }