Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions mgmtd/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::Debug;
use std::ops::RangeInclusive;
use std::path::PathBuf;
use std::time::Duration;
use uuid::Uuid;

/// Generates `Config` to be filled by the functions below and exported to be used by the program.
///
Expand Down Expand Up @@ -107,6 +108,16 @@ 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(num_args = 1)]
#[arg(hide = true)]
#[arg(value_name = "UUID")]
#[serde(skip)]
fs_uuid: Option<Uuid> = 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 Expand Up @@ -393,6 +404,12 @@ generate_structs! {

impl Config {
pub fn check_validity(&self) -> Result<()> {
if let Some(ref uuid) = self.fs_uuid {
if uuid.get_version_num() != 4 {
bail!("Provided file system UUID is not a valid v4 UUID");
}
}

if self.quota_enforce && !self.quota_enable {
bail!("Quota enforcement requires quota being enabled");
}
Expand Down
6 changes: 4 additions & 2 deletions mgmtd/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ 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<Uuid>) -> Result<()> {
config::set(tx, Config::FsUuid, fs_uuid.unwrap_or_else(Uuid::new_v4))?;
config::set(
tx,
Config::FsInitDateSecs,
Expand Down
16 changes: 11 additions & 5 deletions mgmtd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt::Write;
use std::path::Path;
use std::{fs, panic};
use tokio::signal::ctrl_c;
use uuid::Uuid;

fn main() -> Result<(), i32> {
inner_main().map_err(|err| {
Expand Down Expand Up @@ -62,7 +63,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,
)?;
return Ok(());
}

Expand Down Expand Up @@ -149,10 +154,11 @@ doc.beegfs.io.",
})
}

/// Create and initialize a new database. Optionally import v7 data from the given path.
/// Create and initialize a new database.
///
/// The database file is only written to disk if the initialization succeeds.
fn init_db(db_file: &Path, v7_path: Option<&Path>) -> Result<()> {
/// 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 initialization succeeds.
fn init_db(db_file: &Path, v7_path: Option<&Path>, fs_uuid: Option<Uuid>) -> Result<()> {
if db_file.try_exists()? {
bail!("Database file {db_file:?} already exists");
}
Expand All @@ -165,7 +171,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