Skip to content

Commit 59db80a

Browse files
feat: add optional fs-uuid parameter for database initialization (#14)
1 parent 293cbf3 commit 59db80a

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

mgmtd/src/config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fmt::Debug;
1111
use std::ops::RangeInclusive;
1212
use std::path::PathBuf;
1313
use std::time::Duration;
14+
use uuid::Uuid;
1415

1516
/// Generates `Config` to be filled by the functions below and exported to be used by the program.
1617
///
@@ -107,6 +108,16 @@ generate_structs! {
107108
#[serde(skip)]
108109
init: bool = false,
109110

111+
/// Optionally specifies the FsUuid when initializing the database.
112+
///
113+
/// If not provided, a new FsUuid will be generated.
114+
#[arg(long)]
115+
#[arg(num_args = 1)]
116+
#[arg(hide = true)]
117+
#[arg(value_name = "UUID")]
118+
#[serde(skip)]
119+
fs_uuid: Option<Uuid> = None,
120+
110121
/// Upgrades an outdated management database to the current version, then exits.
111122
///
112123
/// Automatically creates a backup of the existing database file in the same directory.
@@ -393,6 +404,12 @@ generate_structs! {
393404

394405
impl Config {
395406
pub fn check_validity(&self) -> Result<()> {
407+
if let Some(ref uuid) = self.fs_uuid {
408+
if uuid.get_version_num() != 4 {
409+
bail!("Provided file system UUID is not a valid v4 UUID");
410+
}
411+
}
412+
396413
if self.quota_enforce && !self.quota_enable {
397414
bail!("Quota enforcement requires quota being enabled");
398415
}

mgmtd/src/db.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ pub const MIGRATIONS: &[sqlite::Migration] = include!(concat!(env!("OUT_DIR"), "
3737

3838
/// Inserts initial entries into a new database. Remember to commit the transaction after calling
3939
/// this function.
40-
pub fn initial_entries(tx: &Transaction) -> Result<()> {
41-
config::set(tx, Config::FsUuid, Uuid::new_v4().to_string())?;
40+
///
41+
/// If `fs_uuid` is provided, it will be used. Otherwise, a new FsUUID will be generated.
42+
pub fn initial_entries(tx: &Transaction, fs_uuid: Option<Uuid>) -> Result<()> {
43+
config::set(tx, Config::FsUuid, fs_uuid.unwrap_or_else(Uuid::new_v4))?;
4244
config::set(
4345
tx,
4446
Config::FsInitDateSecs,

mgmtd/src/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::fmt::Write;
1111
use std::path::Path;
1212
use std::{fs, panic};
1313
use tokio::signal::ctrl_c;
14+
use uuid::Uuid;
1415

1516
fn main() -> Result<(), i32> {
1617
inner_main().map_err(|err| {
@@ -62,7 +63,11 @@ fn inner_main() -> Result<()> {
6263
}
6364

6465
if user_config.init || user_config.import_from_v7.is_some() {
65-
init_db(&user_config.db_file, user_config.import_from_v7.as_deref())?;
66+
init_db(
67+
&user_config.db_file,
68+
user_config.import_from_v7.as_deref(),
69+
user_config.fs_uuid,
70+
)?;
6671
return Ok(());
6772
}
6873

@@ -149,10 +154,11 @@ doc.beegfs.io.",
149154
})
150155
}
151156

152-
/// Create and initialize a new database. Optionally import v7 data from the given path.
157+
/// Create and initialize a new database.
153158
///
154-
/// The database file is only written to disk if the initialization succeeds.
155-
fn init_db(db_file: &Path, v7_path: Option<&Path>) -> Result<()> {
159+
/// Optionally import v7 data from the given path. Optionally the FsUUID can be specified otherwise
160+
/// it will be autogenerated. The database file is only written to disk if initialization succeeds.
161+
fn init_db(db_file: &Path, v7_path: Option<&Path>, fs_uuid: Option<Uuid>) -> Result<()> {
156162
if db_file.try_exists()? {
157163
bail!("Database file {db_file:?} already exists");
158164
}
@@ -165,7 +171,7 @@ fn init_db(db_file: &Path, v7_path: Option<&Path>) -> Result<()> {
165171

166172
let version =
167173
sqlite::migrate_schema(&tx, db::MIGRATIONS).context("Creating schema failed")?;
168-
db::initial_entries(&tx).context("Creating initial entries failed")?;
174+
db::initial_entries(&tx, fs_uuid).context("Creating initial entries failed")?;
169175

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

0 commit comments

Comments
 (0)