Skip to content

Commit 3745453

Browse files
committed
Add logs to dstack-mr
1 parent e49e4f2 commit 3745453

9 files changed

Lines changed: 40 additions & 13 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dstack-mr/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ object.workspace = true
1818
hex-literal.workspace = true
1919
fs-err.workspace = true
2020
bon.workspace = true
21+
log.workspace = true

dstack-mr/cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ hex.workspace = true
1515
dstack-types.workspace = true
1616
fs-err.workspace = true
1717
serde_json.workspace = true
18+
tracing-subscriber.workspace = true

dstack-mr/cli/src/main.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ enum Commands {
1818
Measure(MachineConfig),
1919
}
2020

21+
type Bool = bool;
22+
2123
#[derive(Parser)]
2224
struct MachineConfig {
2325
/// Number of CPUs
@@ -33,18 +35,18 @@ struct MachineConfig {
3335

3436
/// Enable two-pass add pages
3537
#[arg(long, default_value = "true")]
36-
two_pass_add_pages: bool,
38+
two_pass_add_pages: Bool,
3739

3840
/// Enable PIC
3941
#[arg(long, default_value = "true")]
40-
pic: bool,
42+
pic: Bool,
4143

4244
/// Enable SMM
4345
#[arg(long, default_value = "false")]
44-
smm: bool,
46+
smm: Bool,
4547

46-
/// PCI hole64 size
47-
#[arg(long)]
48+
/// PCI hole64 size (accepts decimal or hex with 0x prefix)
49+
#[arg(long, value_parser = parse_memory_size)]
4850
pci_hole64_size: Option<u64>,
4951

5052
/// Enable hugepages
@@ -61,18 +63,20 @@ struct MachineConfig {
6163

6264
/// Disable hotplug
6365
#[arg(long, default_value = "false")]
64-
hotplug_off: bool,
66+
hotplug_off: Bool,
6567

6668
/// Enable root verity
6769
#[arg(long, default_value = "true")]
68-
root_verity: bool,
70+
root_verity: Bool,
6971

7072
/// Output JSON
71-
#[arg(long, default_value = "false")]
73+
#[arg(long)]
7274
json: bool,
7375
}
7476

7577
fn main() -> Result<()> {
78+
tracing_subscriber::fmt::init();
79+
7680
let cli = Cli::parse();
7781
match &cli.command {
7882
Commands::Measure(config) => {
@@ -123,20 +127,22 @@ fn main() -> Result<()> {
123127
Ok(())
124128
}
125129

130+
/// Parse a memory size value that can be decimal or hexadecimal (with 0x prefix)
126131
fn parse_memory_size(s: &str) -> Result<u64> {
127132
let s = s.trim();
128133

129-
// Check if the string is empty
130134
if s.is_empty() {
131135
return Err(anyhow!("Empty memory size"));
132136
}
137+
if s.starts_with("0x") || s.starts_with("0X") {
138+
let hex_str = &s[2..];
139+
return u64::from_str_radix(hex_str, 16)
140+
.map_err(|e| anyhow!("Invalid hexadecimal value: {}", e));
141+
}
133142

134-
// Handle bare number (bytes)
135143
if s.chars().all(|c| c.is_ascii_digit()) {
136144
return Ok(s.parse::<u64>()?);
137145
}
138-
139-
// Handle suffixes
140146
let len = s.len();
141147
let (num_part, suffix) = match s.chars().last().unwrap() {
142148
'k' | 'K' => (&s[0..len - 1], 1024u64),

dstack-mr/src/acpi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! translated from an original Go implementation.
33
44
use anyhow::{bail, Context, Result};
5+
use log::debug;
56

67
use crate::Machine;
78

@@ -141,6 +142,8 @@ impl Machine<'_> {
141142
]);
142143
}
143144

145+
debug!("qemu command: {cmd:?}");
146+
144147
// Execute the command and capture output
145148
let output = cmd
146149
.output()

dstack-mr/src/kernel.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{measure_log, measure_sha384, num::read_le, utf16_encode};
1+
use crate::{measure_log, measure_sha384, num::read_le, utf16_encode, util::debug_print_log};
22
use anyhow::{bail, Context, Result};
33
use object::pe;
44
use sha2::{Digest, Sha384};
@@ -213,6 +213,7 @@ pub(crate) fn measure_kernel(
213213
measure_sha384(b"Exit Boot Services Invocation"),
214214
measure_sha384(b"Exit Boot Services Returned with Success"),
215215
];
216+
debug_print_log("RTMR1", &rtmr1_log);
216217
Ok(measure_log(&rtmr1_log))
217218
}
218219

dstack-mr/src/machine.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use crate::tdvf::Tdvf;
2+
use crate::util::debug_print_log;
23
use crate::{kernel, TdxMeasurements};
34
use crate::{measure_log, measure_sha384};
45
use anyhow::{Context, Result};
56
use fs_err as fs;
7+
use log::debug;
68

79
#[derive(Debug, bon::Builder)]
810
pub struct Machine<'a> {
@@ -26,6 +28,7 @@ pub struct Machine<'a> {
2628

2729
impl Machine<'_> {
2830
pub fn measure(&self) -> Result<TdxMeasurements> {
31+
debug!("measuring machine: {self:#?}");
2932
let fw_data = fs::read(self.firmware)?;
3033
let kernel_data = fs::read(self.kernel)?;
3134
let initrd_data = fs::read(self.initrd)?;
@@ -43,6 +46,7 @@ impl Machine<'_> {
4346
kernel::measure_cmdline(self.kernel_cmdline),
4447
measure_sha384(&initrd_data),
4548
];
49+
debug_print_log("RTMR2", &rtmr2_log);
4650
let rtmr2 = measure_log(&rtmr2_log);
4751

4852
Ok(TdxMeasurements {

dstack-mr/src/tdvf.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use hex_literal::hex;
33
use sha2::{Digest, Sha384};
44

55
use crate::num::read_le;
6+
use crate::util::debug_print_log;
67
use crate::{measure_log, measure_sha384, utf16_encode, Machine};
78

89
const PAGE_SIZE: u64 = 0x1000;
@@ -252,6 +253,7 @@ impl<'a> Tdvf<'a> {
252253
measure_sha384(&[0x00, 0x00]), // BootOrder
253254
boot000_hash.to_vec(),
254255
];
256+
debug_print_log("RTMR0", &rtmr0_log);
255257
Ok(measure_log(&rtmr0_log))
256258
}
257259

dstack-mr/src/util.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use log::debug;
12
use sha2::{Digest, Sha384};
23

34
/// Computes a SHA384 hash of the given data.
@@ -12,6 +13,12 @@ pub(crate) fn utf16_encode(input: &str) -> Vec<u8> {
1213
.collect()
1314
}
1415

16+
pub(crate) fn debug_print_log(name: &str, log: &[Vec<u8>]) {
17+
debug!("{name} event log:");
18+
for (i, entry) in log.iter().enumerate() {
19+
debug!("[{i}] digest: {}", hex::encode(entry));
20+
}
21+
}
1522
/// Computes a measurement of the given RTMR event log.
1623
pub(crate) fn measure_log(log: &[Vec<u8>]) -> Vec<u8> {
1724
let mut mr = [0u8; 48]; // SHA384 output size

0 commit comments

Comments
 (0)