Skip to content

Commit

Permalink
xtask: Add create-test-disk action
Browse files Browse the repository at this point in the history
This will be used to create test filesystems. The files will be
committed with git-lfs, so everyone will share the same test data, and
developers will not need to run this create-test-disk action except when
creating a new test file.

This initial version just creates an empty filesystem; a later commit
will mount the filesystem to add directories and files.
  • Loading branch information
nicholasbishop committed May 30, 2024
1 parent ec203d8 commit 1a15477
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 3 deletions.
127 changes: 126 additions & 1 deletion Cargo.lock

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

5 changes: 4 additions & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

[package]
name = "xtask"
version = "0.1.0"
version = "0.0.0"
edition = "2021"
publish = false
default-run = "xtask"

[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(),
}
}

0 comments on commit 1a15477

Please sign in to comment.