Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install: Give better error if not run in a podman container #136

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib/src/containerenv.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Helpers for parsing the `/run/.containerenv` file generated by podman.

use std::fs::File;
use std::io::{BufRead, BufReader};

use anyhow::{Context, Result};
use anyhow::Result;
use cap_std_ext::cap_std::fs::Dir;
use cap_std_ext::prelude::CapStdExtDirExt;
use fn_error_context::context;

const PATH: &str = "/run/.containerenv";
const PATH: &str = "run/.containerenv";

#[derive(Debug, Default)]
pub(crate) struct ContainerExecutionInfo {
Expand All @@ -18,11 +19,14 @@ pub(crate) struct ContainerExecutionInfo {
}

/// Load and parse the `/run/.containerenv` file.
#[context("Parsing {PATH}")]
pub(crate) fn get_container_execution_info() -> Result<ContainerExecutionInfo> {
let f = File::open(PATH)
.with_context(|| format!("Opening {PATH}"))
.map(BufReader::new)?;
#[context("Querying container")]
pub(crate) fn get_container_execution_info(rootfs: &Dir) -> Result<ContainerExecutionInfo> {
let f = match rootfs.open_optional(PATH)? {
Some(f) => BufReader::new(f),
None => {
anyhow::bail!("This command must be executed inside a podman container (missing {PATH}")
}
};
let mut r = ContainerExecutionInfo::default();
for line in f.lines() {
let line = line?;
Expand Down
5 changes: 4 additions & 1 deletion lib/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,11 @@ async fn prepare_install(
crate::cli::require_root()?;
require_systemd_pid1()?;

let rootfs = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())
.context("Opening /")?;

// This command currently *must* be run inside a privileged container.
let container_info = crate::containerenv::get_container_execution_info()?;
let container_info = crate::containerenv::get_container_execution_info(&rootfs)?;
let source = SourceInfo::from_container(&container_info)?;

ensure_var()?;
Expand Down
Loading