Skip to content

Commit

Permalink
status: support running in a container
Browse files Browse the repository at this point in the history
The output in container would be:
```
bash-5.2# bootupctl status
Available: BIOS EFI
```

Fixes: coreos#788
  • Loading branch information
HuijingHei committed Dec 5, 2024
1 parent 33b552f commit 5fecba3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
- uses: actions/checkout@v4
- name: build
run: sudo podman build -t localhost/bootupd:latest -f ci/Containerfile.c9s .
- name: bootupctl status in container
run: |
set -xeuo pipefail
output=$(sudo podman run --rm -ti localhost/bootupd:latest bootupctl status)
[ "Available: BIOS EFI" == ${output} ]
- name: bootc install to disk
run: |
set -xeuo pipefail
Expand Down
16 changes: 16 additions & 0 deletions src/cli/bootupctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ impl CtlCommand {

/// Runner for `status` verb.
fn run_status(opts: StatusOpts) -> Result<()> {
if crate::util::running_in_container() {
return run_status_in_container();
}
ensure_running_in_systemd()?;
let r = bootupd::status()?;
if opts.json {
Expand Down Expand Up @@ -171,3 +174,16 @@ fn ensure_running_in_systemd() -> Result<()> {
}
Ok(())
}

/// If running in container, just print the available payloads
fn run_status_in_container() -> Result<()> {
let all_components = crate::bootupd::get_components();
if all_components.is_empty() {
return Ok(());
}
let avail: Vec<_> = all_components.keys().cloned().collect();
if !avail.is_empty() {
println!("Available: {}", avail.join(" "));
}
Ok(())
}
19 changes: 19 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,22 @@ pub(crate) fn cmd_output(cmd: &mut Command) -> Result<String> {
String::from_utf8(result.stdout)
.with_context(|| format!("decoding as UTF-8 output of `{:#?}`", cmd))
}

/// Copy from https://github.com/containers/bootc/blob/main/ostree-ext/src/container_utils.rs#L20
/// Attempts to detect if the current process is running inside a container.
/// This looks for the `container` environment variable or the presence
/// of Docker or podman's more generic `/run/.containerenv`.
/// This is a best-effort function, as there is not a 100% reliable way
/// to determine this.
pub fn running_in_container() -> bool {
if std::env::var_os("container").is_some() {
return true;
}
// https://stackoverflow.com/questions/20010199/how-to-determine-if-a-process-runs-inside-lxc-docker
for p in ["/run/.containerenv", "/.dockerenv"] {
if Path::new(p).exists() {
return true;
}
}
false
}

0 comments on commit 5fecba3

Please sign in to comment.