Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions mgmtd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ generate_structs! {
#[serde(skip)]
init: bool = false,

/// Optionally specifies the FsUuid when initializing the database.
///
/// If not provided, a new FsUuid will be generated.
#[arg(long)]
#[arg(hide = true)]
#[arg(value_name = "UUID")]
#[serde(skip)]
fs_uuid: Option<String> = None,

/// Upgrades an outdated management database to the current version, then exits.
///
/// Automatically creates a backup of the existing database file in the same directory.
Expand Down
17 changes: 15 additions & 2 deletions mgmtd/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,21 @@ pub const MIGRATIONS: &[sqlite::Migration] = include!(concat!(env!("OUT_DIR"), "

/// Inserts initial entries into a new database. Remember to commit the transaction after calling
/// this function.
pub fn initial_entries(tx: &Transaction) -> Result<()> {
config::set(tx, Config::FsUuid, Uuid::new_v4().to_string())?;
///
/// If `fs_uuid` is provided, it will be used. Otherwise, a new FsUUID will be generated.
pub fn initial_entries(tx: &Transaction, fs_uuid: Option<String>) -> Result<()> {
let uuid = match fs_uuid {
Some(ref s) => {
let parsed =
Uuid::parse_str(s).map_err(|_| anyhow!("Provided fs_uuid is not a valid UUID"))?;
if parsed.get_version_num() != 4 {
bail!("Provided fs_uuid is not a valid v4 UUID");
}
s.clone()
}
None => Uuid::new_v4().to_string(),
};
config::set(tx, Config::FsUuid, uuid)?;
config::set(
tx,
Config::FsInitDateSecs,
Expand Down
11 changes: 8 additions & 3 deletions mgmtd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ fn inner_main() -> Result<()> {
}

if user_config.init || user_config.import_from_v7.is_some() {
init_db(&user_config.db_file, user_config.import_from_v7.as_deref())?;
init_db(
&user_config.db_file,
user_config.import_from_v7.as_deref(),
user_config.fs_uuid.clone(),
)?;
return Ok(());
}

Expand Down Expand Up @@ -150,9 +154,10 @@ doc.beegfs.io.",
}

/// Create and initialize a new database. Optionally import v7 data from the given path.
/// Optionally the FsUUID can be specified otherwise it will be autogenerated.
///
/// The database file is only written to disk if the initialization succeeds.
fn init_db(db_file: &Path, v7_path: Option<&Path>) -> Result<()> {
fn init_db(db_file: &Path, v7_path: Option<&Path>, fs_uuid: Option<String>) -> Result<()> {
if db_file.try_exists()? {
bail!("Database file {db_file:?} already exists");
}
Expand All @@ -165,7 +170,7 @@ fn init_db(db_file: &Path, v7_path: Option<&Path>) -> Result<()> {

let version =
sqlite::migrate_schema(&tx, db::MIGRATIONS).context("Creating schema failed")?;
db::initial_entries(&tx).context("Creating initial entries failed")?;
db::initial_entries(&tx, fs_uuid).context("Creating initial entries failed")?;

if let Some(v7_path) = v7_path {
db::import_v7(&tx, v7_path).context("v7 management data import failed")?;
Expand Down
Loading