-
Notifications
You must be signed in to change notification settings - Fork 217
Internals bwrap #5438
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
base: main
Are you sure you want to change the base?
Internals bwrap #5438
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,108 @@ | ||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||||||||||||||
|
|
||||||||||||||
| use std::{ | ||||||||||||||
| io::{BufRead as _, Seek}, | ||||||||||||||
| os::fd::IntoRawFd, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| use anyhow::{Context as _, Result}; | ||||||||||||||
| use cap_std::fs::Dir; | ||||||||||||||
| use cap_std_ext::cap_tempfile; | ||||||||||||||
| use clap::Parser; | ||||||||||||||
| use ostree_ext::gio; | ||||||||||||||
|
|
||||||||||||||
| use crate::{bwrap, ffi::BubblewrapMutability, impl_sealed_memfd}; | ||||||||||||||
|
|
||||||||||||||
| #[derive(Debug, Parser)] | ||||||||||||||
| #[clap(rename_all = "kebab-case")] | ||||||||||||||
| /// Main options struct | ||||||||||||||
| struct Internals { | ||||||||||||||
| #[clap(subcommand)] | ||||||||||||||
| cmd: Cmd, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[derive(Debug, Parser)] | ||||||||||||||
| #[clap(rename_all = "kebab-case")] | ||||||||||||||
| /// Options for invoking bubblewrap | ||||||||||||||
| struct BwrapOpts { | ||||||||||||||
| /// Path to rootfs | ||||||||||||||
| root: String, | ||||||||||||||
|
|
||||||||||||||
| /// Arguments | ||||||||||||||
| args: Vec<String>, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[derive(Debug, Parser)] | ||||||||||||||
| #[clap(rename_all = "kebab-case")] | ||||||||||||||
| /// Options for invoking bubblewrap | ||||||||||||||
| struct BwrapScriptOpts { | ||||||||||||||
| /// Path to rootfs | ||||||||||||||
| root: String, | ||||||||||||||
|
|
||||||||||||||
| /// Path to interpeter | ||||||||||||||
| interp: String, | ||||||||||||||
|
|
||||||||||||||
| /// Path to script | ||||||||||||||
| script: String, | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[derive(Debug, clap::Subcommand)] | ||||||||||||||
| #[clap(rename_all = "kebab-case")] | ||||||||||||||
| /// Subcommands | ||||||||||||||
| enum Cmd { | ||||||||||||||
| /// Invoke bubblewrap | ||||||||||||||
| Bwrap(BwrapOpts), | ||||||||||||||
| /// Invoke bubblewrap the same way rpm-ostree does for scripts. | ||||||||||||||
| BwrapScript(BwrapScriptOpts), | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl BwrapOpts { | ||||||||||||||
| fn run(self) -> Result<()> { | ||||||||||||||
| let root = &Dir::open_ambient_dir(&self.root, cap_std::ambient_authority())?; | ||||||||||||||
| let mut bwrap = | ||||||||||||||
| bwrap::Bubblewrap::new_with_mutability(root, BubblewrapMutability::MutateFreely)?; | ||||||||||||||
| bwrap.append_child_argv(self.args.iter().map(|s| s.as_str())); | ||||||||||||||
| bwrap.run_inner(gio::Cancellable::NONE)?; | ||||||||||||||
| Ok(()) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl BwrapScriptOpts { | ||||||||||||||
| fn run(self) -> Result<()> { | ||||||||||||||
| let authority = cap_std::ambient_authority(); | ||||||||||||||
| let root = &Dir::open_ambient_dir(&self.root, authority)?; | ||||||||||||||
| let mut bwrap = | ||||||||||||||
| bwrap::Bubblewrap::new_with_mutability(root, BubblewrapMutability::MutateFreely)?; | ||||||||||||||
| let td = Dir::open_ambient_dir("/var/tmp", authority)?; | ||||||||||||||
| let mut output = cap_tempfile::TempFile::new_anonymous(&td)?.into_std(); | ||||||||||||||
| bwrap.append_child_arg(&self.interp); | ||||||||||||||
| bwrap.take_stdout_and_stderr_fd(output.try_clone()?.into_raw_fd()); | ||||||||||||||
| let script = std::fs::read_to_string(self.script)?; | ||||||||||||||
| let mfd = impl_sealed_memfd("script", script.as_bytes())?; | ||||||||||||||
| bwrap.take_fd(mfd.into_raw_fd(), 5); | ||||||||||||||
| bwrap.append_child_arg("/proc/self/fd/5"); | ||||||||||||||
|
Comment on lines
+82
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file descriptor
Suggested change
|
||||||||||||||
| bwrap.run_inner(gio::Cancellable::NONE)?; | ||||||||||||||
| output.seek(std::io::SeekFrom::Start(0))?; | ||||||||||||||
| let output = std::io::BufReader::new(output); | ||||||||||||||
| for line in output.lines() { | ||||||||||||||
| let line = line.context("Reading line")?; | ||||||||||||||
| println!("script: {line}"); | ||||||||||||||
| } | ||||||||||||||
| Ok(()) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| impl Cmd { | ||||||||||||||
| fn run(self) -> Result<()> { | ||||||||||||||
| match self { | ||||||||||||||
| Cmd::Bwrap(args) => args.run(), | ||||||||||||||
| Cmd::BwrapScript(args) => args.run(), | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| pub fn main(argv: &[&str]) -> Result<i32> { | ||||||||||||||
| let opt = Internals::parse_from(argv.into_iter().skip(1)); | ||||||||||||||
| opt.cmd.run()?; | ||||||||||||||
| Ok(0) | ||||||||||||||
| } | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/bin/bash | ||
| set -euo pipefail | ||
|
|
||
| dn=$(cd $(dirname $0) && pwd) | ||
Check warningCode scanning / shellcheck Quote this to prevent word splitting. Warning test
Quote this to prevent word splitting.
|
||
| commondir=${dn}/../common | ||
| . "$commondir/libtest.sh" | ||
|
|
||
| set -x | ||
|
|
||
| # We use TAP | ||
| echo 1..1 | ||
|
|
||
| # Run a script in the host environment | ||
| td=$(mktemp -d) | ||
| cd $td | ||
| cat >script <<EOF | ||
| echo hello | ||
| echo someerr 1>&2 | ||
| echo world | ||
| EOF | ||
| rpm-ostree internals bwrap-script / /bin/bash $(pwd)/script >out.txt | ||
Check warningCode scanning / shellcheck Quote this to prevent word splitting. Warning test
Quote this to prevent word splitting.
|
||
| assert_file_has_content_literal out.txt 'script: hello | ||
| script: someerr | ||
| script: world' | ||
|
|
||
| echo "ok bwrap script" | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding
/var/tmpmight reduce portability. This will fail if the tool is run in an environment where/var/tmpdoes not exist or is not writable. Usingstd::env::temp_dir()is more robust as it returns a platform-specific temporary directory.