no real benefit vs go for now

This commit is contained in:
Mikaël Cluseau 2024-05-15 09:59:57 +11:00
parent 7c85855281
commit 57064a39b6
15 changed files with 366 additions and 215 deletions

View File

@ -6,3 +6,4 @@ go.work.sum
modd.*conf
test-initrd*
test-initrd/**/*
target

284
Cargo.lock generated
View File

@ -12,97 +12,100 @@ dependencies = [
]
[[package]]
name = "bindgen"
version = "0.69.4"
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"bitflags",
"cexpr",
"clang-sys",
"itertools",
"lazy_static",
"lazycell",
"proc-macro2",
"quote",
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a64c907d4e79225ac72e2a354c9ce84d50ebb4586dee56c82b3ee73004f537f5"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
"regex",
"rustc-hash",
"shlex",
"syn",
]
[[package]]
name = "bitflags"
version = "2.5.0"
name = "env_logger"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "cc"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b"
[[package]]
name = "cexpr"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
dependencies = [
"nom",
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clang-sys"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67523a3b4be3ce1989d607a828d036249522dd9c1c8de7f4dd2dae43a37369d1"
dependencies = [
"glob",
"libc",
"libloading",
]
[[package]]
name = "either"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47c1c47d2f5964e29c61246e81db715514cd532db6b5116a25ea3c03d6780a2"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "getrandom"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "glob"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
[[package]]
name = "hashbrown"
version = "0.14.3"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "indexmap"
@ -118,19 +121,18 @@ dependencies = [
name = "init"
version = "0.1.0"
dependencies = [
"libcryptsetup-rs",
"env_logger",
"log",
"serde",
"serde_json",
"serde_yaml",
]
[[package]]
name = "itertools"
version = "0.12.1"
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
dependencies = [
"either",
]
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]]
name = "itoa"
@ -138,64 +140,6 @@ version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "libcryptsetup-rs"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99a61d3782d841dca88244f582cfd95d96da9d175fb06616d50a480058647e39"
dependencies = [
"bitflags",
"either",
"lazy_static",
"libc",
"libcryptsetup-rs-sys",
"log",
"pkg-config",
"semver",
"serde_json",
"uuid",
]
[[package]]
name = "libcryptsetup-rs-sys"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c78b397341cb9aa5ddc8d11118754ed0eab4aeb9cee96ee7cbe83a7d2867b8d2"
dependencies = [
"bindgen",
"cc",
"pkg-config",
"semver",
]
[[package]]
name = "libloading"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c2a198fb6b0eada2a8df47933734e6d35d350665a33a3593d7164fa52c75c19"
dependencies = [
"cfg-if",
"windows-targets",
]
[[package]]
name = "log"
version = "0.4.21"
@ -208,28 +152,6 @@ version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "proc-macro2"
version = "1.0.81"
@ -277,38 +199,26 @@ version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56"
[[package]]
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "semver"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d43fe69e652f3df9bdc2b85b2854a0825b86e4fb76bc44d945137d053639ca"
[[package]]
name = "serde"
version = "1.0.198"
version = "1.0.199"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9846a40c979031340571da2545a4e5b7c4163bdae79b301d5f86d03979451fcc"
checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.198"
version = "1.0.199"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e88edab869b01783ba905e7d0153f9fc1a6505a96e4ad3018011eedb838566d9"
checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc"
dependencies = [
"proc-macro2",
"quote",
@ -339,12 +249,6 @@ dependencies = [
"unsafe-libyaml",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.60"
@ -369,19 +273,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "uuid"
version = "1.8.0"
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
dependencies = [
"getrandom",
]
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"

View File

@ -6,6 +6,8 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libcryptsetup-rs = "0.9.3"
env_logger = "0.11.3"
log = "0.4.21"
serde = { version = "1.0.198", features = ["derive"] }
serde_json = "1.0.116"
serde_yaml = "0.9.34"

View File

@ -1,3 +1,15 @@
from rust:1.77.1-alpine as rust
run apk add --no-cache musl-dev # pkgconfig cryptsetup-dev lvm2-dev clang-dev clang-static
workdir /src
copy Cargo.* .
copy src src
run --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,sharing=private,target=/src/target \
cargo build --release \
&& cp target/release/init /init-rs
from golang:1.21.6-alpine3.19 as build
workdir /src
@ -22,7 +34,8 @@ run . /etc/os-release \
run apk add --no-cache -p . musl lvm2 lvm2-extra lvm2-dmeventd udev cryptsetup e2fsprogs btrfs-progs lsblk
run rm -rf usr/share/apk var/cache/apk
copy --from=build /go/bin/init .
#copy --from=build /go/bin/init .
copy --from=rust /init-rs init
# check viability
run chroot /layer /init hello

5
Dockerfile.build Normal file
View File

@ -0,0 +1,5 @@
from rust:1.77.1-alpine as rust
run apk add --no-cache musl-dev
run apk add --no-cache musl lvm2 lvm2-extra lvm2-dmeventd udev cryptsetup e2fsprogs btrfs-progs lsblk

2
Dockerfile.test Normal file
View File

@ -0,0 +1,2 @@
from alpine:3.19.0
run apk add --no-cache musl lvm2 lvm2-extra lvm2-dmeventd udev cryptsetup e2fsprogs btrfs-progs lsblk

29
build-init Executable file
View File

@ -0,0 +1,29 @@
set -ex
which podman &>/dev/null && docker=podman || docker=docker
mkdir -p empty
$docker build -t nv-rs-build --network=host -f Dockerfile.build empty
case $1 in
release)
opts=--release
bindir=target/release
;;
"")
bindir=target/debug
;;
*)
echo >&2 "invalid arg: $1"
exit 1
;;
esac
$docker run --rm -i --net=host --user=$UID \
-v $HOME/.cargo-alpine/registry:/usr/local/cargo/registry \
-v $PWD:/src -w /src \
nv-rs-build \
cargo build $opts
mkdir -p dist
cp $bindir/init dist/

1
init-rs Symbolic link
View File

@ -0,0 +1 @@
target/debug/init

View File

@ -9,11 +9,15 @@ go.??? **/*.go {
**/*.rs {
prep: cargo test
prep: cargo build
#prep: cargo build
#prep: (cd test-initrd && ../target/debug/init)
prep: bash ./build-init
prep: docker exec -i -w /src/test-initrd nv-initrd-test ./init
prep: ./build-init release
}
dist/init Dockerfile {
prep: docker build -t novit-initrd-gen .
prep: docker run novit-initrd-gen |base64 -d >dist/initrd.new
prep: mv dist/initrd.new dist/initrd
}
#dist/init Dockerfile {
# prep: docker build --network host -t novit-initrd-gen .
# prep: docker run --net=host --rm novit-initrd-gen |base64 -d >dist/initrd.new
# prep: mv dist/initrd.new dist/initrd
#}

2
run-docker Executable file
View File

@ -0,0 +1,2 @@
docker build -t nv-initrd-test -f Dockerfile.test empty
docker run -d --name nv-initrd-test -it --privileged -v $PWD:/src --workdir /src/test-initrd nv-initrd-test

View File

@ -2,13 +2,14 @@
pub struct Config {
anti_phishing_code: String,
keymap: String,
keymap: Option<String>,
modules: String,
auths: Vec<Auth>,
networks: Vec<Network>,
#[serde(default)]
ssh: SSHServer,
#[serde(default)]
@ -48,6 +49,14 @@ pub struct SSHServer {
listen: String,
keys: SSHKeys,
}
impl Default for SSHServer {
fn default() -> Self {
Self {
listen: "[::]:22".to_string(),
keys: SSHKeys::default(),
}
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct SSHKeys {
@ -56,30 +65,58 @@ pub struct SSHKeys {
ecdsa: String,
ed25519: String,
}
impl Default for SSHKeys {
fn default() -> Self {
Self {
dsa: "id_dsa".to_string(),
rsa: "id_rsa".to_string(),
ecdsa: "id_ecdsa".to_string(),
ed25519: "id_ed25519".to_string(),
}
}
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct LvmVG {
vg: String,
pvs: LvmPV,
defaults: Option<LvmLVDefaults>,
#[serde(default)]
defaults: LvmLVDefaults,
lvs: Vec<LvmLV>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct LvmLVDefaults {
#[serde(default = "default_fs")]
fs: String,
raid: Option<Raid>,
}
impl Default for LvmLVDefaults {
fn default() -> Self {
Self {
fs: default_fs(),
raid: None,
}
}
}
fn default_fs() -> String {
"ext4".to_string()
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct LvmLV {
name: String,
fs: String,
#[serde(skip_serializing_if = "Option::is_none")]
fs: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
raid: Option<Raid>,
size: String,
extents: String,
#[serde(skip_serializing_if = "Option::is_none")]
size: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
extents: Option<String>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
@ -90,15 +127,15 @@ pub struct LvmPV {
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct CryptDev {
dev: String,
prefix: String,
name: String,
dev: Option<String>,
prefix: Option<String>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Raid {
mirrors: u8,
stripes: u8,
mirrors: Option<u8>,
stripes: Option<u8>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]

27
src/lsblk.rs Normal file
View File

@ -0,0 +1,27 @@
use std::io;
use std::process::Command;
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Report {
pub blockdevices: Vec<BlockDev>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct BlockDev {
pub name: String,
#[serde(rename = "maj:min")]
pub maj_min: String,
pub rm: bool,
pub size: String,
pub ro: bool,
#[serde(rename = "type")]
pub dev_type: String,
pub mountpoints: Vec<Option<String>>,
#[serde(default)]
pub children: Vec<BlockDev>,
}
pub fn report() -> io::Result<Report> {
let output = Command::new("lsblk").arg("--json").output()?;
Ok(serde_json::from_slice(output.stdout.as_slice()).unwrap())
}

84
src/lvm.rs Normal file
View File

@ -0,0 +1,84 @@
use std::io;
use std::process::Command;
#[derive(Debug, serde::Deserialize, serde::Serialize)]
struct Report {
report: Vec<ReportObj>,
}
#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[serde(untagged)]
enum ReportObj {
PV { pv: Vec<PV> },
VG { vg: Vec<VG> },
LV { lv: Vec<LV> },
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct PV {
pub pv_name: String,
pub vg_name: String,
pub pv_fmt: String,
pub pv_attr: String,
pub pv_size: String,
pub pv_free: String,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct VG {
pub vg_name: String,
pub pv_count: String,
pub lv_count: String,
pub snap_count: String,
pub vg_attr: String,
pub vg_size: String,
pub vg_free: String,
}
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct LV {
lv_name: String,
vg_name: String,
lv_attr: String,
lv_size: String,
pool_lv: String,
origin: String,
data_percent: String,
metadata_percent: String,
move_pv: String,
mirror_log: String,
copy_percent: String,
convert_lv: String,
}
pub fn pvs() -> io::Result<Vec<PV>> {
report_cmd("pvs", |o| match o {
ReportObj::PV { pv } => Some(pv),
_ => None,
})
}
pub fn vgs() -> io::Result<Vec<VG>> {
report_cmd("vgs", |o| match o {
ReportObj::VG { vg } => Some(vg),
_ => None,
})
}
pub fn lvs() -> io::Result<Vec<LV>> {
report_cmd("lvs", |o| match o {
ReportObj::LV { lv } => Some(lv),
_ => None,
})
}
fn report_cmd<T>(cmd: &str, find: fn(ReportObj) -> Option<Vec<T>>) -> io::Result<Vec<T>> {
let output = Command::new(cmd).arg("--reportformat=json").output()?;
let report: Report = serde_json::from_slice(output.stdout.as_slice()).unwrap();
Ok(report
.report
.into_iter()
.filter_map(find)
.flatten()
.collect())
}

View File

@ -1,16 +1,55 @@
mod bootstrap;
mod lsblk;
mod lvm;
use std::fs;
use std::io;
use log::error;
use std::io::{self, Read, Write};
use std::process::exit;
use std::{env, fs};
fn main() {
println!("Hello, world!");
if let None = env::var_os("RUST_LOG") {
env::set_var("RUST_LOG", "info");
}
env_logger::init();
let cfg_in = fs::OpenOptions::new()
.read(true)
.open("config.yaml")
.unwrap();
let cfg: bootstrap::config::Config = serde_yaml::from_reader(cfg_in).unwrap();
if let Some(arg) = env::args().into_iter().nth(1) {
match arg.as_str() {
"hello" => println!("hello"),
_ => {
error!("invalid arg: {arg}");
exit(1);
}
};
exit(0);
}
let Ok(cfg) =
fs::File::open("config.yaml").inspect_err(|e| error!("failed to read config: {e}"))
else {
exit(1);
};
let Ok(cfg) = serde_yaml::from_reader::<_, bootstrap::config::Config>(cfg)
.inspect_err(|e| error!("failed to parse config: {e}"))
else {
exit(1);
};
serde_yaml::to_writer(io::stdout(), &cfg).unwrap();
for pv in lvm::pvs().unwrap() {
println!("pv: {pv:?}");
}
for vg in lvm::vgs().unwrap() {
println!("vg: {vg:?}");
}
for lv in lvm::lvs().unwrap() {
println!("lv: {lv:?}");
}
println!("lsblk: {:?}", lsblk::report());
// TODO
}

1
test-initrd/init Symbolic link
View File

@ -0,0 +1 @@
../dist/init