Skip to content

Commit 67c158a

Browse files
committed
feat: Add TPM arguments to cryptsetup generation
1 parent e8664d3 commit 67c158a

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/backend/repart_output.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct CryptData {
1919

2020
/// Extra cmdline options for the kernel
2121
pub cmdline_opts: Vec<String>,
22+
pub tpm: bool,
2223
}
2324

2425
fn cryptsetup_luks_uuid(node: &str) -> Result<String, color_eyre::eyre::Error> {
@@ -119,20 +120,48 @@ impl RepartOutput {
119120

120121
let luks_partitions = self.partitions.iter().filter(|part| is_luks(&part.node));
121122

123+
let mut is_tpm = false;
124+
122125
let has_luks = luks_partitions.clone().count() > 0;
123126
for part in luks_partitions {
124127
let uuid = cryptsetup_luks_uuid(&part.node).expect("Failed to get LUKS UUID");
125128
let label = &part.label;
126129

127-
writeln!(&mut crypttab, "{label}\tUUID={uuid}\tnone\tluks,discard")?;
130+
let mut extra_opts = String::new();
131+
132+
let part_uses_tpm: bool = {
133+
let file_config = std::fs::read_to_string(&part.file)?;
134+
let config: RepartConfig = serde_systemd_unit::from_str(&file_config)?;
135+
136+
match config.partition.encrypt {
137+
super::repartcfg::EncryptOption::KeyFileTpm2 => true,
138+
super::repartcfg::EncryptOption::Tpm2 => true,
139+
_ => false,
140+
}
141+
};
142+
143+
if part_uses_tpm {
144+
is_tpm = true;
145+
extra_opts.push_str("tpm2-device=auto,");
146+
}
147+
148+
writeln!(
149+
&mut crypttab,
150+
"{label}\tUUID={uuid}\tnone\t{extra_opts}luks,discard"
151+
)?;
128152

129153
cmdline_opts.push(format!("rd.luks.name={uuid}={label}"));
130154
}
155+
156+
if is_tpm {
157+
cmdline_opts.push("rd.luks.options=tpm2-device=auto".to_string());
158+
}
131159

132160
match has_luks {
133161
true => Ok(Some(CryptData {
134162
crypttab,
135163
cmdline_opts,
164+
tpm: is_tpm,
136165
})),
137166
false => Ok(None),
138167
}

0 commit comments

Comments
 (0)