diff options
author | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-05-06 12:59:29 +0200 |
---|---|---|
committer | HimbeerserverDE <himbeerserverde@gmail.com> | 2023-05-06 12:59:29 +0200 |
commit | 3e1a515bf12c3d42b9403d7791cd42766e6ff1fd (patch) | |
tree | 2e1e1075c69656ff8914e9f25459ed2e8d278a45 | |
parent | 62a5a67b9c7e0fc9658ca0236a9d64b6ab007d9e (diff) |
read the instance file generated by rustkrazy_packer for hardware info
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/main.rs | 37 |
3 files changed, 26 insertions, 15 deletions
@@ -1722,6 +1722,8 @@ dependencies = [ "fatfs", "fscommon", "reqwest", + "serde", + "serde_json", "squashfs-ng", "tempfile", ] @@ -12,5 +12,7 @@ clap = { version = "4.2.7", features = ["derive"] } fatfs = "0.3.6" fscommon = "0.1.1" reqwest = { version = "0.11.17", features = ["blocking"] } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" squashfs-ng = "0.1.2" tempfile = "3.5.0" diff --git a/src/main.rs b/src/main.rs index c724d30..a395004 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use clap::Parser; use fatfs::{FatType, FormatVolumeOptions}; use fscommon::StreamSlice; use reqwest::{blocking::Client, header::CONTENT_TYPE, Url}; +use serde::{Deserialize, Serialize}; use squashfs_ng::write::{ Source as SqsSource, SourceData as SqsSourceData, SourceFile as SqsSourceFile, TreeProcessor as SqsTreeProcessor, @@ -36,12 +37,9 @@ struct Args { /// Base URL of the instance, e.g. https://rustkrazy:1234@198.51.100.1 #[arg(short = 'u', long = "update")] update: String, - /// Size of image in bytes. - #[arg(short = 'n', long = "size")] - size: u64, - /// Architecture of the device running the image. Supported: x86_64 rpi. - #[arg(short = 'a', long = "architecture")] - arch: String, + /// Location of the instance file generated by rustkrazy_packer. + #[arg(short = 'm', long = "instance")] + instance: String, /// Crates to install into the image. #[arg(short = 'c', long = "crates")] crates: Vec<String>, @@ -53,6 +51,12 @@ struct Args { init: String, } +#[derive(Clone, Debug, Serialize, Deserialize)] +struct Instance { + size: u64, + arch: String, +} + fn write_mbr_partition_table( mbr: &mut StreamSlice<NamedTempFile>, dev_size: u64, @@ -106,6 +110,15 @@ fn write_mbr_partition_table( } fn update_instance(args: Args) -> anyhow::Result<()> { + let mut file = File::open(args.instance)?; + let info: Instance = serde_json::from_reader(&mut file)?; + + match info.arch.as_str() { + "x86_64" => {} + "rpi" => {} + _ => bail!("invalid architecture (supported: x86_64 rpi)"), + } + let mut mbr_buf = Vec::new(); let mut boot_buf = Vec::new(); let mut root_buf = Vec::new(); @@ -122,11 +135,11 @@ fn update_instance(args: Args) -> anyhow::Result<()> { let mut boot = StreamSlice::new(boot_file, 0, (256 * MiB).into())?; let mut root = StreamSlice::new(root_file, 0, (256 * MiB).into())?; - write_mbr_partition_table(&mut mbr, args.size)?; + write_mbr_partition_table(&mut mbr, info.size)?; - let buf = write_boot(&mut boot, &args.arch)?; + let buf = write_boot(&mut boot, &info.arch)?; write_mbr(&mut mbr, &mut boot, &buf["vmlinuz"], &buf["cmdline.txt"])?; - write_root(&mut root, &args.arch, &args.crates, &args.git, &args.init)?; + write_root(&mut root, &info.arch, &args.crates, &args.git, &args.init)?; mbr.rewind()?; boot.rewind()?; @@ -628,12 +641,6 @@ fn write_root( fn main() -> anyhow::Result<()> { let args = Args::parse(); - match args.arch.as_str() { - "x86_64" => {} - "rpi" => {} - _ => bail!("invalid architecture (supported: x86_64 rpi)"), - } - let init_in_crates = args.crates.iter().any(|pkg| *pkg == args.init); let init_in_git = args.git.iter().any(|location| { let mut split = location.split('%'); |