|
| 1 | +// SPDX-FileCopyrightText: © 2026 Phala Network <dstack@phala.network> |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::error::Error; |
| 5 | +use std::str::FromStr; |
| 6 | + |
| 7 | +use qemu_acpi::{build, MachineConfig, QemuVersion}; |
| 8 | + |
| 9 | +fn optional<T: FromStr>(args: &mut impl Iterator<Item = String>, default: T) -> Result<T, String> { |
| 10 | + match args.next() { |
| 11 | + Some(value) => value.parse().map_err(|_| format!("invalid value: {value}")), |
| 12 | + None => Ok(default), |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +fn main() -> Result<(), Box<dyn Error>> { |
| 17 | + let mut args = std::env::args().skip(1); |
| 18 | + let nics: u32 = optional(&mut args, 1)?; |
| 19 | + let cpus: u32 = optional(&mut args, 1)?; |
| 20 | + let version = QemuVersion::from_str(&optional::<String>(&mut args, "11.1.0".into())?) |
| 21 | + .map_err(|error| format!("invalid QEMU version: {error}"))?; |
| 22 | + let gpus = optional(&mut args, 0)?; |
| 23 | + let nvswitches = optional(&mut args, 0)?; |
| 24 | + let hugepages = optional::<u8>(&mut args, 0)? == 1; |
| 25 | + let root_verity = optional::<u8>(&mut args, 1)? != 0; |
| 26 | + let hotplug_off = optional::<u8>(&mut args, 0)? == 1; |
| 27 | + let smm = optional::<u8>(&mut args, 0)? == 1; |
| 28 | + let hole = optional::<u64>(&mut args, 0)?; |
| 29 | + let memory_size = optional(&mut args, 2u64 << 30)?; |
| 30 | + let blobs = build(&MachineConfig { |
| 31 | + qemu_version: version, |
| 32 | + cpu_count: cpus, |
| 33 | + memory_size, |
| 34 | + pic: false, |
| 35 | + smm, |
| 36 | + hugepages, |
| 37 | + num_gpus: gpus, |
| 38 | + num_nvswitches: nvswitches, |
| 39 | + num_nics: nics, |
| 40 | + num_verity_volumes: 0, |
| 41 | + hotplug_off, |
| 42 | + root_verity, |
| 43 | + pci_hole64_size: (hole != 0).then_some(hole), |
| 44 | + })?; |
| 45 | + std::fs::write("/tmp/rust.bin", blobs.tables)?; |
| 46 | + Ok(()) |
| 47 | +} |
0 commit comments