-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathbaseline.rs
More file actions
527 lines (479 loc) · 18.5 KB
/
baseline.rs
File metadata and controls
527 lines (479 loc) · 18.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! # The baseline installer
//!
//! This module handles creation of simple root filesystem setups. At the current time
//! it's very simple - just a direct filesystem (e.g. xfs, ext4, btrfs etc.). It is
//! intended to add opinionated handling of TPM2-bound LUKS too. But that's about it;
//! other more complex flows should set things up externally and use `bootc install to-filesystem`.
use std::borrow::Cow;
use std::fmt::Display;
use std::fmt::Write as _;
use std::io::Write;
use std::process::Command;
use std::process::Stdio;
use anyhow::Ok;
use anyhow::{Context, Result};
use bootc_utils::CommandRunExt;
use camino::Utf8Path;
use camino::Utf8PathBuf;
use cap_std::fs::Dir;
use cap_std_ext::cap_std;
use clap::ValueEnum;
use fn_error_context::context;
use serde::{Deserialize, Serialize};
use super::MountSpec;
use super::RUN_BOOTC;
use super::RW_KARG;
use super::RootSetup;
use super::State;
use super::config::Filesystem;
use crate::task::Task;
use bootc_kernel_cmdline::utf8::Cmdline;
#[cfg(feature = "install-to-disk")]
use bootc_mount::is_mounted_in_pid1_mountns;
/// Check whether DPS auto-discovery is enabled. When `true`,
/// `root=UUID=` is omitted and `systemd-gpt-auto-generator` discovers
/// the root partition via its DPS type GUID instead.
///
/// Defaults to `true` for systemd-boot (which always implements BLI).
/// For GRUB the default is `false` because we cannot know at install
/// time whether the GRUB build includes the `bli` module — the module
/// is baked into the signed EFI binary with no external manifest.
/// Distros shipping a BLI-capable GRUB should set
/// `discoverable-partitions = true` in their install config.
#[cfg(feature = "install-to-disk")]
fn use_discoverable_partitions(state: &State) -> bool {
// Explicit config takes priority
if let Some(ref config) = state.install_config {
if let Some(v) = config.discoverable_partitions {
return v;
}
}
// systemd-boot always supports BLI
matches!(
state.config_opts.bootloader,
Some(crate::spec::Bootloader::Systemd)
)
}
// This ensures we end up under 512 to be small-sized.
pub(crate) const BOOTPN_SIZE_MB: u32 = 510;
pub(crate) const EFIPN_SIZE_MB: u32 = 512;
/// EFI Partition size for composefs installations
/// We need more space than ostree as we have UKIs and UKI addons
/// We might also need to store UKIs for pinned deployments
pub(crate) const CFS_EFIPN_SIZE_MB: u32 = 1024;
#[cfg(feature = "install-to-disk")]
pub(crate) const PREPBOOT_GUID: &str = "9E1A2D38-C612-4316-AA26-8B49521E5A8B";
#[cfg(feature = "install-to-disk")]
pub(crate) const PREPBOOT_LABEL: &str = "PowerPC-PReP-boot";
#[derive(clap::ValueEnum, Default, Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) enum BlockSetup {
#[default]
Direct,
Tpm2Luks,
}
impl Display for BlockSetup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value().unwrap().get_name().fmt(f)
}
}
/// Options for installing to a block device
#[derive(Debug, Clone, clap::Args, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct InstallBlockDeviceOpts {
/// Target block device for installation. The entire device will be wiped.
pub(crate) device: Utf8PathBuf,
/// Automatically wipe all existing data on device
#[clap(long)]
#[serde(default)]
pub(crate) wipe: bool,
/// Target root block device setup.
///
/// direct: Filesystem written directly to block device
/// tpm2-luks: Bind unlock of filesystem to presence of the default tpm2 device.
#[clap(long, value_enum)]
pub(crate) block_setup: Option<BlockSetup>,
/// Target root filesystem type.
#[clap(long, value_enum)]
pub(crate) filesystem: Option<Filesystem>,
/// Size of the root partition (default specifier: M). Allowed specifiers: M (mebibytes), G (gibibytes), T (tebibytes).
///
/// By default, all remaining space on the disk will be used.
#[clap(long)]
pub(crate) root_size: Option<String>,
}
impl BlockSetup {
/// Returns true if the block setup requires a separate /boot aka XBOOTLDR partition.
pub(crate) fn requires_bootpart(&self) -> bool {
match self {
BlockSetup::Direct => false,
BlockSetup::Tpm2Luks => true,
}
}
}
#[cfg(feature = "install-to-disk")]
fn mkfs<'a>(
dev: &str,
fs: Filesystem,
label: &str,
wipe: bool,
opts: impl IntoIterator<Item = &'a str>,
) -> Result<uuid::Uuid> {
let devinfo = bootc_blockdev::list_dev(dev.into())?;
let size = ostree_ext::glib::format_size(devinfo.size);
// Generate a random UUID for the filesystem
let u = uuid::Uuid::new_v4();
let mut t = Task::new(
&format!("Creating {label} filesystem ({fs}) on device {dev} (size={size})"),
format!("mkfs.{fs}"),
);
match fs {
Filesystem::Xfs => {
if wipe {
t.cmd.arg("-f");
}
t.cmd.arg("-m");
t.cmd.arg(format!("uuid={u}"));
}
Filesystem::Btrfs | Filesystem::Ext4 => {
t.cmd.arg("-U");
t.cmd.arg(u.to_string());
}
};
// Today all the above mkfs commands take -L
t.cmd.args(["-L", label]);
t.cmd.args(opts);
t.cmd.arg(dev);
// All the mkfs commands are unnecessarily noisy by default
t.cmd.stdout(Stdio::null());
// But this one is notable so let's print the whole thing with verbose()
t.verbose().run()?;
Ok(u)
}
pub(crate) fn wipefs(dev: &Utf8Path) -> Result<()> {
println!("Wiping device {dev}");
Command::new("wipefs")
.args(["-a", dev.as_str()])
.run_inherited_with_cmd_context()
}
pub(crate) fn udev_settle() -> Result<()> {
// There's a potential window after rereading the partition table where
// udevd hasn't yet received updates from the kernel, settle will return
// immediately, and lsblk won't pick up partition labels. Try to sleep
// our way out of this.
std::thread::sleep(std::time::Duration::from_millis(200));
let st = super::run_in_host_mountns("udevadm")?
.arg("settle")
.status()?;
if !st.success() {
anyhow::bail!("Failed to run udevadm settle: {st:?}");
}
Ok(())
}
#[context("Creating rootfs")]
#[cfg(feature = "install-to-disk")]
pub(crate) fn install_create_rootfs(
state: &State,
opts: InstallBlockDeviceOpts,
) -> Result<RootSetup> {
let install_config = state.install_config.as_ref();
let luks_name = "root";
// Ensure we have a root filesystem upfront
let root_filesystem = opts
.filesystem
.or(install_config
.and_then(|c| c.filesystem_root())
.and_then(|r| r.fstype))
.ok_or_else(|| anyhow::anyhow!("No root filesystem specified"))?;
// Verify that the target is empty (if not already wiped in particular, but it's
// also good to verify that the wipe worked)
let mut device = bootc_blockdev::list_dev(&opts.device)?;
// Always disallow writing to mounted device
if is_mounted_in_pid1_mountns(&device.path())? {
anyhow::bail!("Device {} is mounted", device.path())
}
// Handle wiping any existing data
if opts.wipe {
let dev = &opts.device;
for child in device.children.iter().flatten() {
let child = child.path();
println!("Wiping {child}");
wipefs(Utf8Path::new(&child))?;
}
println!("Wiping {dev}");
wipefs(dev)?;
} else if device.has_children() {
anyhow::bail!(
"Detected existing partitions on {}; use e.g. `wipefs` or --wipe if you intend to overwrite",
opts.device
);
}
let run_bootc = Utf8Path::new(RUN_BOOTC);
let mntdir = run_bootc.join("mounts");
if mntdir.exists() {
std::fs::remove_dir_all(&mntdir)?;
}
// Use the install configuration to find the block setup, if we have one
let block_setup = if let Some(config) = install_config {
config.get_block_setup(opts.block_setup.as_ref().copied())?
} else if opts.filesystem.is_some() {
// Otherwise, if a filesystem is specified then we default to whatever was
// specified via --block-setup, or the default
opts.block_setup.unwrap_or_default()
} else {
// If there was no default filesystem, then there's no default block setup,
// and we need to error out.
anyhow::bail!("No install configuration found, and no filesystem specified")
};
let serial = device.serial.as_deref().unwrap_or("<unknown>");
let model = device.model.as_deref().unwrap_or("<unknown>");
let discoverable = use_discoverable_partitions(state);
println!("Block setup: {block_setup}");
println!(" Size: {}", device.size);
println!(" Serial: {serial}");
println!(" Model: {model}");
println!(
" Partitions: {}",
if discoverable { "Discoverable" } else { "UUID" }
);
let root_size = opts
.root_size
.as_deref()
.map(bootc_blockdev::parse_size_mib)
.transpose()
.context("Parsing root size")?;
// Load the policy from the container root, which also must be our install root
let sepolicy = state.load_policy()?;
let sepolicy = sepolicy.as_ref();
// Create a temporary directory to use for mount points. Note that we're
// in a mount namespace, so these should not be visible on the host.
let physical_root_path = mntdir.join("rootfs");
std::fs::create_dir_all(&physical_root_path)?;
let bootfs = mntdir.join("boot");
std::fs::create_dir_all(bootfs)?;
// Generate partitioning spec as input to sfdisk
let mut partno = 0;
let mut partitioning_buf = String::new();
writeln!(partitioning_buf, "label: gpt")?;
let random_label = uuid::Uuid::new_v4();
writeln!(&mut partitioning_buf, "label-id: {random_label}")?;
if cfg!(target_arch = "x86_64") {
partno += 1;
writeln!(
&mut partitioning_buf,
r#"size=1MiB, bootable, type=21686148-6449-6E6F-744E-656564454649, name="BIOS-BOOT""#
)?;
} else if cfg!(target_arch = "powerpc64") {
// PowerPC-PReP-boot
partno += 1;
let label = PREPBOOT_LABEL;
let uuid = PREPBOOT_GUID;
writeln!(
&mut partitioning_buf,
r#"size=4MiB, bootable, type={uuid}, name="{label}""#
)?;
} else if cfg!(any(target_arch = "aarch64", target_arch = "s390x")) {
// No bootloader partition is necessary
} else {
anyhow::bail!("Unsupported architecture: {}", std::env::consts::ARCH);
}
let esp_partno = if super::ARCH_USES_EFI {
let esp_guid = crate::discoverable_partition_specification::ESP;
partno += 1;
let esp_size = if state.composefs_options.composefs_backend {
CFS_EFIPN_SIZE_MB
} else {
EFIPN_SIZE_MB
};
writeln!(
&mut partitioning_buf,
r#"size={esp_size}MiB, type={esp_guid}, name="EFI-SYSTEM""#
)?;
Some(partno)
} else {
None
};
// Initialize the /boot filesystem. Note that in the future, we may match
// what systemd/uapi-group encourages and make /boot be FAT32 as well, as
// it would aid systemd-boot.
let boot_partno = if block_setup.requires_bootpart() {
partno += 1;
writeln!(
&mut partitioning_buf,
r#"size={BOOTPN_SIZE_MB}MiB, name="boot""#
)?;
Some(partno)
} else {
None
};
let rootpn = partno + 1;
let root_size = root_size
.map(|v| Cow::Owned(format!("size={v}MiB, ")))
.unwrap_or_else(|| Cow::Borrowed(""));
let rootpart_uuid =
uuid::Uuid::parse_str(crate::discoverable_partition_specification::this_arch_root())?;
writeln!(
&mut partitioning_buf,
r#"{root_size}type={rootpart_uuid}, name="root""#
)?;
tracing::debug!("Partitioning: {partitioning_buf}");
Task::new("Initializing partitions", "sfdisk")
.arg("--wipe=always")
.arg(device.path())
.quiet()
.run_with_stdin_buf(Some(partitioning_buf.as_bytes()))
.context("Failed to run sfdisk")?;
tracing::debug!("Created partition table");
// Full udev sync; it'd obviously be better to await just the devices
// we're targeting, but this is a simple coarse hammer.
udev_settle()?;
// Re-read partition table to get updated children
device.refresh()?;
let root_device = device.find_device_by_partno(rootpn)?;
// Verify the partition type matches the DPS root partition type for this architecture
let expected_parttype = crate::discoverable_partition_specification::this_arch_root();
if !root_device
.parttype
.as_ref()
.is_some_and(|pt| pt.eq_ignore_ascii_case(expected_parttype))
{
anyhow::bail!(
"root partition {rootpn} has type {}; expected {expected_parttype}",
root_device.parttype.as_deref().unwrap_or("<none>")
);
}
let (rootdev_path, root_blockdev_kargs) = match block_setup {
BlockSetup::Direct => (root_device.path(), None),
BlockSetup::Tpm2Luks => {
let uuid = uuid::Uuid::new_v4().to_string();
// This will be replaced via --wipe-slot=all when binding to tpm below
let dummy_passphrase = uuid::Uuid::new_v4().to_string();
let mut tmp_keyfile = tempfile::NamedTempFile::new()?;
tmp_keyfile.write_all(dummy_passphrase.as_bytes())?;
tmp_keyfile.flush()?;
let tmp_keyfile = tmp_keyfile.path();
let dummy_passphrase_input = Some(dummy_passphrase.as_bytes());
let root_devpath = root_device.path();
Task::new("Initializing LUKS for root", "cryptsetup")
.args(["luksFormat", "--uuid", uuid.as_str(), "--key-file"])
.args([tmp_keyfile])
.arg(&root_devpath)
.run()?;
// The --wipe-slot=all removes our temporary passphrase, and binds to the local TPM device.
// We also use .verbose() here as the details are important/notable.
Task::new("Enrolling root device with TPM", "systemd-cryptenroll")
.args(["--wipe-slot=all", "--tpm2-device=auto", "--unlock-key-file"])
.args([tmp_keyfile])
.arg(&root_devpath)
.verbose()
.run_with_stdin_buf(dummy_passphrase_input)?;
Task::new("Opening root LUKS device", "cryptsetup")
.args(["luksOpen", &root_devpath, luks_name])
.run()?;
let rootdev = format!("/dev/mapper/{luks_name}");
let kargs = vec![
format!("luks.uuid={uuid}"),
format!("luks.options=tpm2-device=auto,headless=true"),
];
(rootdev, Some(kargs))
}
};
// Initialize the /boot filesystem
let bootdev = if let Some(bootpn) = boot_partno {
Some(device.find_device_by_partno(bootpn)?)
} else {
None
};
let boot_uuid = if let Some(bootdev) = bootdev {
Some(
mkfs(&bootdev.path(), root_filesystem, "boot", opts.wipe, [])
.context("Initializing /boot")?,
)
} else {
None
};
// Unconditionally enable fsverity for ext4
let mkfs_options = match root_filesystem {
Filesystem::Ext4 => ["-O", "verity"].as_slice(),
_ => [].as_slice(),
};
// Initialize rootfs
let root_uuid = mkfs(
&rootdev_path,
root_filesystem,
"root",
opts.wipe,
mkfs_options.iter().copied(),
)?;
let bootsrc = boot_uuid.as_ref().map(|uuid| format!("UUID={uuid}"));
let bootarg = bootsrc.as_deref().map(|bootsrc| format!("boot={bootsrc}"));
let boot = bootsrc.map(|bootsrc| MountSpec {
source: bootsrc,
target: "/boot".into(),
fstype: MountSpec::AUTO.into(),
options: Some("ro".into()),
});
let mut kargs = Cmdline::new();
// Add root blockdev kargs (e.g., LUKS parameters)
if let Some(root_blockdev_kargs) = root_blockdev_kargs {
for karg in root_blockdev_kargs {
kargs.extend(&Cmdline::from(karg.as_str()));
}
}
// When discoverable-partitions is enabled, omit root= so that
// systemd-gpt-auto-generator discovers root by its DPS type GUID.
if discoverable {
kargs.extend(&Cmdline::from(RW_KARG));
} else {
let rootarg = format!("root=UUID={root_uuid}");
kargs.extend(&Cmdline::from(format!("{rootarg} {RW_KARG}")));
}
// Add boot= argument if present
if let Some(bootarg) = bootarg {
kargs.extend(&Cmdline::from(bootarg.as_str()));
}
// Add CLI kargs
if let Some(cli_kargs) = state.config_opts.karg.as_ref() {
for karg in cli_kargs {
kargs.extend(karg);
}
}
bootc_mount::mount(&rootdev_path, &physical_root_path)?;
let target_rootfs = Dir::open_ambient_dir(&physical_root_path, cap_std::ambient_authority())?;
crate::lsm::ensure_dir_labeled(&target_rootfs, "", Some("/".into()), 0o755.into(), sepolicy)?;
let physical_root = Dir::open_ambient_dir(&physical_root_path, cap_std::ambient_authority())?;
let bootfs = physical_root_path.join("boot");
// Create the underlying mount point directory, which should be labeled
crate::lsm::ensure_dir_labeled(&target_rootfs, "boot", None, 0o755.into(), sepolicy)?;
if let Some(bootdev) = bootdev {
bootc_mount::mount(&bootdev.path(), &bootfs)?;
}
// And we want to label the root mount of /boot
crate::lsm::ensure_dir_labeled(&target_rootfs, "boot", None, 0o755.into(), sepolicy)?;
// Create the EFI system partition, if applicable
if let Some(esp_partno) = esp_partno {
let espdev = device.find_device_by_partno(esp_partno)?;
Task::new("Creating ESP filesystem", "mkfs.fat")
.args([&espdev.path(), "-n", "EFI-SYSTEM"])
.verbose()
.quiet_output()
.run()?;
let efifs_path = bootfs.join(crate::bootloader::EFI_DIR);
std::fs::create_dir(&efifs_path).context("Creating efi dir")?;
}
let luks_device = match block_setup {
BlockSetup::Direct => None,
BlockSetup::Tpm2Luks => Some(luks_name.to_string()),
};
Ok(RootSetup {
luks_device,
device_info: device,
physical_root_path,
physical_root,
target_root_path: None,
rootfs_uuid: Some(root_uuid.to_string()),
boot,
kargs,
skip_finalize: false,
})
}