Skip to content

Commit 8381194

Browse files
authored
Merge pull request #40 from cgwalters/install-cleanups
Install cleanups
2 parents 5825091 + 6526fb8 commit 8381194

File tree

2 files changed

+68
-20
lines changed

2 files changed

+68
-20
lines changed

lib/src/install.rs

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::fmt::Display;
2+
use std::io::BufWriter;
3+
use std::io::Write;
24
use std::process::Command;
35
use std::process::Stdio;
46
use std::sync::Arc;
@@ -245,9 +247,10 @@ fn bind_mount_from_host(src: impl AsRef<Utf8Path>, dest: impl AsRef<Utf8Path>) -
245247
async fn initialize_ostree_root_from_self(
246248
state: &State,
247249
containerstate: &ContainerExecutionInfo,
248-
rootfs: &Utf8Path,
250+
root_setup: &RootSetup,
249251
kargs: &[&str],
250252
) -> Result<InstallAleph> {
253+
let rootfs = root_setup.rootfs.as_path();
251254
let opts = &state.opts;
252255
let cancellable = gio::Cancellable::NONE;
253256

@@ -306,11 +309,10 @@ async fn initialize_ostree_root_from_self(
306309

307310
let repopath = &rootfs.join("ostree/repo");
308311
for (k, v) in [("sysroot.bootloader", "none"), ("sysroot.readonly", "true")] {
309-
Task::new_and_run(
310-
"Configuring ostree repo",
311-
"ostree",
312-
["config", "--repo", repopath.as_str(), "set", k, v],
313-
)?;
312+
Task::new("Configuring ostree repo", "ostree")
313+
.args(["config", "--repo", repopath.as_str(), "set", k, v])
314+
.quiet()
315+
.run()?;
314316
}
315317
Task::new_and_run(
316318
"Initializing sysroot",
@@ -359,6 +361,32 @@ async fn initialize_ostree_root_from_self(
359361

360362
drop(temporary_dir);
361363

364+
// Write the entry for /boot to /etc/fstab. TODO: Encourage OSes to use the karg?
365+
// Or better bind this with the grub data.
366+
sysroot.load(cancellable)?;
367+
let deployment = sysroot
368+
.deployments()
369+
.into_iter()
370+
.next()
371+
.ok_or_else(|| anyhow::anyhow!("Failed to find deployment"))?;
372+
// SAFETY: There must be a path
373+
let path = sysroot.deployment_dirpath(&deployment).unwrap();
374+
let sysroot_dir = cap_std::fs::Dir::open_ambient_dir(rootfs, cap_std::ambient_authority())
375+
.context("Opening rootfs")?;
376+
let root = sysroot_dir
377+
.open_dir(path.as_str())
378+
.context("Opening deployment dir")?;
379+
let mut f = {
380+
let mut opts = cap_std::fs::OpenOptions::new();
381+
root.open_with("etc/fstab", opts.append(true).write(true).create(true))
382+
.context("Opening etc/fstab")
383+
.map(BufWriter::new)?
384+
};
385+
let boot_uuid = &root_setup.boot_uuid;
386+
let bootfs_type_str = root_setup.bootfs_type.to_string();
387+
writeln!(f, "UUID={boot_uuid} /boot {bootfs_type_str} defaults 1 2")?;
388+
f.flush()?;
389+
362390
let uname = cap_std_ext::rustix::process::uname();
363391

364392
let aleph = InstallAleph {
@@ -423,6 +451,7 @@ fn skopeo_supports_containers_storage() -> Result<bool> {
423451
struct RootSetup {
424452
device: Utf8PathBuf,
425453
rootfs: Utf8PathBuf,
454+
bootfs_type: Filesystem,
426455
boot_uuid: uuid::Uuid,
427456
kargs: Vec<String>,
428457
}
@@ -551,10 +580,12 @@ fn install_create_rootfs(state: &State) -> Result<RootSetup> {
551580
BlockSetup::Tpm2Luks => anyhow::bail!("tpm2-luks is not implemented yet"),
552581
}
553582

583+
// TODO: make this configurable
584+
let bootfs_type = Filesystem::Ext4;
585+
554586
// Initialize the /boot filesystem
555587
let bootdev = &format!("{device}{BOOTPN}");
556-
let boot_uuid =
557-
mkfs(bootdev, Filesystem::Ext4, Some("boot"), []).context("Initializing /boot")?;
588+
let boot_uuid = mkfs(bootdev, bootfs_type, Some("boot"), []).context("Initializing /boot")?;
558589

559590
// Initialize rootfs
560591
let rootdev = &format!("{device}{ROOTPN}");
@@ -575,11 +606,10 @@ fn install_create_rootfs(state: &State) -> Result<RootSetup> {
575606

576607
// Create the EFI system partition, if applicable
577608
if let Some(espdev) = espdev {
578-
Task::new_and_run(
579-
"Creating ESP filesystem",
580-
"mkfs.fat",
581-
[espdev.as_str(), "-n", "EFI-SYSTEM"],
582-
)?;
609+
Task::new("Creating ESP filesystem", "mkfs.fat")
610+
.args([espdev.as_str(), "-n", "EFI-SYSTEM"])
611+
.quiet_output()
612+
.run()?;
583613
let efifs_path = bootfs.join("efi");
584614
std::fs::create_dir(&efifs_path).context("Creating efi dir")?;
585615
mount(&espdev, &efifs_path)?;
@@ -588,6 +618,7 @@ fn install_create_rootfs(state: &State) -> Result<RootSetup> {
588618
Ok(RootSetup {
589619
device,
590620
rootfs,
621+
bootfs_type,
591622
boot_uuid,
592623
kargs,
593624
})
@@ -715,13 +746,9 @@ pub(crate) async fn install(opts: InstallOpts) -> Result<()> {
715746
kargs.push(crate::bootloader::IGNITION_VARIABLE);
716747
}
717748

718-
let aleph = initialize_ostree_root_from_self(
719-
&state,
720-
&container_state,
721-
&rootfs.rootfs,
722-
kargs.as_slice(),
723-
)
724-
.await?;
749+
let aleph =
750+
initialize_ostree_root_from_self(&state, &container_state, &rootfs, kargs.as_slice())
751+
.await?;
725752

726753
let aleph = serde_json::to_string(&aleph)?;
727754
std::fs::write(rootfs.rootfs.join(BOOTC_ALEPH_PATH), aleph).context("Writing aleph version")?;

lib/src/task.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
ffi::OsStr,
3+
io::Seek,
34
process::{Command, Stdio},
45
};
56

@@ -8,6 +9,7 @@ use anyhow::{Context, Result};
89
pub(crate) struct Task {
910
description: String,
1011
quiet: bool,
12+
quiet_output: bool,
1113
pub(crate) cmd: Command,
1214
}
1315

@@ -23,6 +25,7 @@ impl Task {
2325
Self {
2426
description,
2527
quiet: false,
28+
quiet_output: false,
2629
cmd,
2730
}
2831
}
@@ -32,6 +35,12 @@ impl Task {
3235
self
3336
}
3437

38+
// Do not print stdout/stderr, unless the command fails
39+
pub(crate) fn quiet_output(mut self) -> Self {
40+
self.quiet_output = true;
41+
self
42+
}
43+
3544
pub(crate) fn args<S: AsRef<OsStr>>(mut self, args: impl IntoIterator<Item = S>) -> Self {
3645
self.cmd.args(args);
3746
self
@@ -44,9 +53,21 @@ impl Task {
4453
if !self.quiet {
4554
println!("{description}");
4655
}
56+
let mut output = None;
57+
if self.quiet_output {
58+
let tmpf = tempfile::tempfile()?;
59+
cmd.stdout(Stdio::from(tmpf.try_clone()?));
60+
cmd.stderr(Stdio::from(tmpf.try_clone()?));
61+
output = Some(tmpf);
62+
}
4763
tracing::debug!("exec: {cmd:?}");
4864
let st = cmd.status()?;
4965
if !st.success() {
66+
if let Some(mut output) = output {
67+
output.seek(std::io::SeekFrom::Start(0))?;
68+
let mut stderr = std::io::stderr().lock();
69+
std::io::copy(&mut output, &mut stderr)?;
70+
}
5071
anyhow::bail!("Task {description} failed: {st:?}");
5172
}
5273
Ok(())

0 commit comments

Comments
 (0)