Skip to content

Commit 1507a58

Browse files
authored
Merge pull request #881 from ckyrouac/container-check
install: Check if running in container earlier
2 parents 4cf27d9 + c0c4ccf commit 1507a58

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

lib/src/containerenv.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub(crate) struct ContainerExecutionInfo {
2020
pub(crate) rootless: Option<String>,
2121
}
2222

23+
pub(crate) fn is_container(rootfs: &Dir) -> bool {
24+
rootfs.exists(PATH)
25+
}
26+
2327
/// Load and parse the `/run/.containerenv` file.
2428
#[context("Querying container")]
2529
pub(crate) fn get_container_execution_info(rootfs: &Dir) -> Result<ContainerExecutionInfo> {

lib/src/install.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ pub(crate) struct State {
326326
pub(crate) install_config: Option<config::InstallConfiguration>,
327327
/// The parsed contents of the authorized_keys (not the file path)
328328
pub(crate) root_ssh_authorized_keys: Option<String>,
329+
#[allow(dead_code)]
330+
pub(crate) host_is_container: bool,
329331
/// The root filesystem of the running container
330332
pub(crate) container_root: Dir,
331333
pub(crate) tempdir: TempDir,
@@ -1159,14 +1161,17 @@ async fn prepare_install(
11591161
tracing::trace!("Preparing install");
11601162
// We need full root privileges, i.e. --privileged in podman
11611163
crate::cli::require_root()?;
1162-
require_host_pidns()?;
1163-
11641164
let rootfs = cap_std::fs::Dir::open_ambient_dir("/", cap_std::ambient_authority())
11651165
.context("Opening /")?;
11661166

1167+
let host_is_container = crate::containerenv::is_container(&rootfs);
11671168
let external_source = source_opts.source_imgref.is_some();
11681169
let source = match source_opts.source_imgref {
11691170
None => {
1171+
if !host_is_container {
1172+
anyhow::bail!("Either --source-imgref must be defined or this command must be executed inside a podman container.")
1173+
}
1174+
require_host_pidns()?;
11701175
// Out of conservatism we only verify the host userns path when we're expecting
11711176
// to do a self-install (e.g. not bootc-image-builder or equivalent).
11721177
require_host_userns()?;
@@ -1274,6 +1279,7 @@ async fn prepare_install(
12741279
root_ssh_authorized_keys,
12751280
container_root: rootfs,
12761281
tempdir,
1282+
host_is_container,
12771283
});
12781284

12791285
Ok(state)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provision:
2+
how: virtual
3+
# Generated by make test-tmt
4+
image: file://./target/testvm/disk.qcow2
5+
disk: 20
6+
summary: Execute tests for installing outside of a container
7+
execute:
8+
how: tmt
9+
script: exec nu tests/booted/test-install-outside-container.nu
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std assert
2+
use tap.nu
3+
4+
# setup filesystem
5+
mkdir /var/mnt
6+
truncate -s 100M disk.img
7+
mkfs.ext4 disk.img
8+
mount -o loop disk.img /var/mnt
9+
10+
# attempt to install to filesystem without specifying a source-imgref
11+
let result = bootc install to-filesystem /var/mnt e>| ansi strip
12+
assert equal $result "ERROR Installing to filesystem: Either --source-imgref must be defined or this command must be executed inside a podman container."
13+
14+
tap ok

0 commit comments

Comments
 (0)