Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xtask: Add create-test-disk action #10

Merged
merged 1 commit into from
Jun 3, 2024
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
125 changes: 125 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ edition = "2021"
publish = false

[dependencies]
clap = { version = "4.5.0", default-features = false, features = ["derive", "help", "std"] }
nix = { version = "0.29.0", features = ["user"] }
68 changes: 67 additions & 1 deletion xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,72 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use clap::{Parser, Subcommand};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;

struct DiskParams {
path: PathBuf,
size_in_kilobytes: u32,
}

impl DiskParams {
fn create(&self) {
let uid = nix::unistd::getuid();
let gid = nix::unistd::getgid();

let status = Command::new("mkfs.ext4")
// Set the ownership of the root directory in the filesystem
// to the current uid/gid instead of root. That allows the
// mounted filesystem to be edited without root permissions,
// although the mount operation itself still requires root.
.args(["-E", &format!("root_owner={uid}:{gid}")])
.arg(&self.path)
.arg(format!("{}k", self.size_in_kilobytes))
.status()
.unwrap();
assert!(status.success());
}
}

fn create_test_data() {
let dir = Path::new("test_data");
if !dir.exists() {
fs::create_dir(dir).unwrap();
}

let path = dir.join("test_disk1.bin");
if !path.exists() {
let disk = DiskParams {
path: path.to_owned(),
size_in_kilobytes: 1024 * 64,
};
disk.create();
// TODO(nicholasbishop): mount the filesystem and fill it with
// test data.
}
}

#[derive(Parser)]
struct Opt {
#[command(subcommand)]
action: Action,
}

#[derive(Subcommand)]
enum Action {
/// Create files for tests.
///
/// The test files will be committed via git-lfs, so developers
/// working on the repo do not typically need to run this command.
CreateTestData,
}

fn main() {
todo!()
let opt = Opt::parse();

match &opt.action {
Action::CreateTestData => create_test_data(),
}
}