Skip to content

Commit d57d386

Browse files
committed
boot_service: split volumes auto open into a separate systemd service
Signed-off-by: Kun Lai <[email protected]>
1 parent 7c7e176 commit d57d386

File tree

9 files changed

+72
-50
lines changed

9 files changed

+72
-50
lines changed

cryptpilot.spec

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ install -d -p %{buildroot}%{dracut_dst}
7171
install -p -m 755 dist/dracut/modules.d/91cryptpilot/module-setup.sh %{buildroot}%{dracut_dst}
7272
install -p -m 755 dist/dracut/modules.d/91cryptpilot/initrd-trigger-network-online.sh %{buildroot}%{dracut_dst}
7373
install -p -m 755 dist/dracut/modules.d/91cryptpilot/initrd-wait-network-online.sh %{buildroot}%{dracut_dst}
74-
install -p -m 644 dist/dracut/modules.d/91cryptpilot/cryptpilot-before-sysroot.service %{buildroot}%{dracut_dst}
75-
install -p -m 644 dist/dracut/modules.d/91cryptpilot/cryptpilot-after-sysroot.service %{buildroot}%{dracut_dst}
74+
install -p -m 644 dist/dracut/modules.d/91cryptpilot/cryptpilot-fde-before-sysroot.service %{buildroot}%{dracut_dst}
75+
install -p -m 644 dist/dracut/modules.d/91cryptpilot/cryptpilot-fde-after-sysroot.service %{buildroot}%{dracut_dst}
76+
install -p -m 644 dist/dracut/modules.d/91cryptpilot/cryptpilot-auto-open.service %{buildroot}%{dracut_dst}
7677
install -p -m 644 dist/dracut/modules.d/91cryptpilot/initrd-wait-network-online.service %{buildroot}%{dracut_dst}
7778
install -d -p %{buildroot}%{_prefix}/lib/systemd/system
7879
install -d -p %{buildroot}/etc/cryptpilot
@@ -114,8 +115,9 @@ rm -rf %{buildroot}
114115
%{dracut_dst}module-setup.sh
115116
%{dracut_dst}initrd-trigger-network-online.sh
116117
%{dracut_dst}initrd-wait-network-online.sh
117-
%{dracut_dst}cryptpilot-before-sysroot.service
118-
%{dracut_dst}cryptpilot-after-sysroot.service
118+
%{dracut_dst}cryptpilot-fde-before-sysroot.service
119+
%{dracut_dst}cryptpilot-fde-after-sysroot.service
120+
%{dracut_dst}cryptpilot-auto-open.service
119121
%{dracut_dst}initrd-wait-network-online.service
120122

121123
%changelog

dist/dracut/modules.d/91cryptpilot/cryptpilot-before-sysroot.service renamed to dist/dracut/modules.d/91cryptpilot/cryptpilot-auto-open.service

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
[Unit]
2-
Description=Cryptpilot Service (before /sysroot mount)
2+
Description=Cryptpilot Volumes Auto Open Service
33
DefaultDependencies=no
44
ConditionPathExists=/etc/initrd-release
55
Requires=network-online.target
66
After=network-online.target
7+
After=cryptpilot-fde-before-sysroot.service
78
Before=initrd-root-device.target
89
Wants=attestation-agent.service
910
After=attestation-agent.service
1011

1112
[Service]
1213
Type=oneshot
1314
RemainAfterExit=true
14-
ExecStart=/usr/bin/cryptpilot boot-service --stage initrd-before-sysroot
15+
ExecStart=/usr/bin/cryptpilot boot-service --stage initrd-volumes-auto-open
1516
StandardOutput=journal+console
1617
StandardError=journal+console
1718

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
[Unit]
2-
Description=Cryptpilot Service (after /sysroot mount)
2+
Description=Cryptpilot FDE Service (after /sysroot mount)
33
DefaultDependencies=no
44
ConditionPathExists=/etc/initrd-release
5-
After=cryptpilot-before-sysroot.service
6-
Requisite=cryptpilot-before-sysroot.service
5+
After=cryptpilot-fde-before-sysroot.service
6+
Requisite=cryptpilot-fde-before-sysroot.service
77
After=dracut-initqueue.service
88
After=sysroot.mount
99
Before=initrd-root-fs.target
@@ -12,9 +12,9 @@ Requires=sysroot.mount
1212
[Service]
1313
Type=oneshot
1414
RemainAfterExit=true
15-
ExecStart=/usr/bin/cryptpilot boot-service --stage initrd-after-sysroot
15+
ExecStart=/usr/bin/cryptpilot boot-service --stage initrd-fde-after-sysroot
1616
StandardOutput=journal+console
1717
StandardError=journal+console
1818

1919
[Install]
20-
WantedBy=initrd-root-fs.target
20+
RequiredBy=initrd-root-fs.target
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[Unit]
2+
Description=Cryptpilot FDE Service (before /sysroot mount)
3+
DefaultDependencies=no
4+
ConditionPathExists=/etc/initrd-release
5+
Requires=network-online.target
6+
After=network-online.target
7+
Before=initrd-root-device.target
8+
Wants=attestation-agent.service
9+
After=attestation-agent.service
10+
11+
[Service]
12+
Type=oneshot
13+
RemainAfterExit=true
14+
ExecStart=/usr/bin/cryptpilot boot-service --stage initrd-fde-before-sysroot
15+
StandardOutput=journal+console
16+
StandardError=journal+console
17+
18+
[Install]
19+
RequiredBy=initrd-root-device.target

dist/dracut/modules.d/91cryptpilot/module-setup.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ install() {
3535
inst_simple $moddir/initrd-wait-network-online.service /usr/lib/systemd/system/initrd-wait-network-online.service
3636
systemctl --root "$initdir" enable initrd-wait-network-online.service
3737
fi
38-
inst_simple $moddir/cryptpilot-before-sysroot.service /usr/lib/systemd/system/cryptpilot-before-sysroot.service
39-
inst_simple $moddir/cryptpilot-after-sysroot.service /usr/lib/systemd/system/cryptpilot-after-sysroot.service
40-
systemctl --root "$initdir" enable cryptpilot-after-sysroot.service
41-
systemctl --root "$initdir" enable cryptpilot-before-sysroot.service
38+
inst_simple $moddir/cryptpilot-fde-before-sysroot.service /usr/lib/systemd/system/cryptpilot-fde-before-sysroot.service
39+
inst_simple $moddir/cryptpilot-fde-after-sysroot.service /usr/lib/systemd/system/cryptpilot-fde-after-sysroot.service
40+
inst_simple $moddir/cryptpilot-auto-open.service /usr/lib/systemd/system/cryptpilot-auto-open.service
41+
systemctl --root "$initdir" enable cryptpilot-fde-after-sysroot.service
42+
systemctl --root "$initdir" enable cryptpilot-fde-before-sysroot.service
43+
systemctl --root "$initdir" enable cryptpilot-auto-open.service
4244

4345
set +u
4446
set +e

src/cli.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,22 @@ pub struct BootServiceOptions {
8282

8383
#[derive(ValueEnum, Clone, Debug)]
8484
pub enum BootStage {
85-
#[clap(name = "initrd-before-sysroot")]
86-
InitrdBeforeSysroot,
85+
#[clap(name = "initrd-fde-before-sysroot")]
86+
InitrdFdeBeforeSysroot,
8787

88-
#[clap(name = "initrd-after-sysroot")]
89-
InitrdAfterSysroot,
88+
#[clap(name = "initrd-fde-after-sysroot")]
89+
InitrdFdeAfterSysroot,
90+
91+
#[clap(name = "initrd-volumes-auto-open")]
92+
InitrdVolumesAutoOpen,
9093
}
9194

9295
impl Display for BootStage {
9396
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
9497
match self {
95-
BootStage::InitrdBeforeSysroot => write!(f, "initrd-before-sysroot"),
96-
BootStage::InitrdAfterSysroot => write!(f, "initrd-after-sysroot"),
98+
BootStage::InitrdFdeBeforeSysroot => write!(f, "initrd-fde-before-sysroot"),
99+
BootStage::InitrdFdeAfterSysroot => write!(f, "initrd-fde-after-sysroot"),
100+
BootStage::InitrdVolumesAutoOpen => write!(f, "initrd-volumes-auto-open"),
97101
}
98102
}
99103
}

src/cmd/boot_service/copy_config.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use super::{detect_boot_part, initrd_state::InitrdStateConfigSource};
1818

1919
const CRYPTPILOT_CONFIG_DIR_INITRD_UNTRUSTED: &'static str = "cryptpilot/config";
2020

21-
pub async fn copy_config_to_initrd_state_if_not_exist() -> Result<()> {
21+
pub async fn copy_config_to_initrd_state_if_not_exist(extend_measurement: bool) -> Result<()> {
2222
if InitrdStateConfigSource::exist() {
2323
return Ok(());
2424
}
@@ -30,15 +30,18 @@ pub async fn copy_config_to_initrd_state_if_not_exist() -> Result<()> {
3030
let initrd_state = InitrdState { config };
3131
serialize_initrd_state(&initrd_state).await?;
3232

33-
// Extend config hash to runtime measurement
34-
let measure = AutoDetectMeasure::new().await;
35-
if let Err(e) = measure
36-
.extend_measurement_hash(OPERATION_NAME_LOAD_CONFIG.into(), config_str)
37-
.await
38-
.context("Failed to extend cryptpilot config hash to runtime measurement")
39-
{
40-
warn!("{e:?}")
33+
if extend_measurement {
34+
// Extend config hash to runtime measurement
35+
let measure = AutoDetectMeasure::new().await;
36+
if let Err(e) = measure
37+
.extend_measurement_hash(OPERATION_NAME_LOAD_CONFIG.into(), config_str)
38+
.await
39+
.context("Failed to extend cryptpilot config hash to runtime measurement")
40+
{
41+
warn!("{e:?}")
42+
}
4143
}
44+
4245
Ok(())
4346
}
4447

src/cmd/boot_service/mod.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ pub struct BootServiceCommand {
8282
impl super::Command for BootServiceCommand {
8383
async fn run(&self) -> Result<()> {
8484
match &self.boot_service_options.stage {
85-
BootStage::InitrdBeforeSysroot => {
85+
BootStage::InitrdFdeBeforeSysroot => {
8686
setup_volumes_required_by_fde()
8787
.await
8888
.context("Failed to setup volumes required by FDE")?;
89+
}
90+
BootStage::InitrdVolumesAutoOpen => {
8991
setup_user_provided_volumes(&self.boot_service_options)
9092
.await
9193
.context("Failed to setup volumes user provided automatically")?;
9294
}
93-
BootStage::InitrdAfterSysroot => {
95+
BootStage::InitrdFdeAfterSysroot => {
9496
let measure = AutoDetectMeasure::new().await;
9597
if let Err(e) = measure
9698
.extend_measurement(OPERATION_NAME_INITRD_SWITCH_ROOT.into(), "{}".into()) // empty json object
@@ -575,7 +577,7 @@ async fn setup_user_provided_volumes(boot_service_options: &BootServiceOptions)
575577
info!("Opening volumes according to volume configs");
576578
for volume_config in &volume_configs {
577579
match boot_service_options.stage {
578-
BootStage::InitrdBeforeSysroot
580+
BootStage::InitrdFdeBeforeSysroot
579581
if volume_config.extra_config.auto_open != Some(true) =>
580582
{
581583
info!(
@@ -584,8 +586,8 @@ async fn setup_user_provided_volumes(boot_service_options: &BootServiceOptions)
584586
);
585587
continue;
586588
}
587-
BootStage::InitrdAfterSysroot => {
588-
unreachable!("This should never happen in initrd-after-sysroot stage")
589+
BootStage::InitrdFdeAfterSysroot => {
590+
unreachable!("This should never happen in initrd-fde-after-sysroot stage")
589591
}
590592
_ => { /* Accept */ }
591593
};

src/lib.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::path::Path;
1010

1111
use anyhow::{bail, Context, Result};
1212
use clap::Parser as _;
13-
use cli::{BootServiceOptions, BootStage};
1413
use cmd::{
1514
boot_service::{
1615
copy_config::copy_config_to_initrd_state_if_not_exist,
@@ -53,19 +52,9 @@ pub async fn run() -> Result<()> {
5352

5453
// Handle config dir
5554
match args.command {
56-
cli::Command::BootService(BootServiceOptions {
57-
stage: BootStage::InitrdBeforeSysroot,
58-
}) => {
55+
cli::Command::BootService(_) => {
5956
// We should load the configs from unsafe space and save them to initrd state for using later.
60-
copy_config_to_initrd_state_if_not_exist().await?;
61-
config::source::set_config_source(CachedConfigSource::new(
62-
InitrdStateConfigSource::new(),
63-
))
64-
.await;
65-
}
66-
cli::Command::BootService(BootServiceOptions {
67-
stage: BootStage::InitrdAfterSysroot,
68-
}) => {
57+
copy_config_to_initrd_state_if_not_exist(true).await?;
6958
config::source::set_config_source(CachedConfigSource::new(
7059
InitrdStateConfigSource::new(),
7160
))
@@ -82,8 +71,8 @@ pub async fn run() -> Result<()> {
8271
))
8372
.await;
8473
} else if Path::new("/etc/initrd-release").exists() {
85-
// If we are in initrd, copy config to initrd state and load it from there.
86-
copy_config_to_initrd_state_if_not_exist().await?;
74+
// If we are in initrd, copy config to initrd state and load it from there, so we can run cryptpilot commands manually in initrd in case we need to operate in emergency shell.
75+
copy_config_to_initrd_state_if_not_exist(false).await?;
8776
config::source::set_config_source(CachedConfigSource::new(
8877
InitrdStateConfigSource::new(),
8978
))

0 commit comments

Comments
 (0)