From 3c717fde114d2e5ad7d9b7fba14fbfcd0b356c72 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Tue, 9 Jan 2024 12:26:54 -0500 Subject: [PATCH] Add and use a helper to run subprocess In most cases e.g. we do want to show stdout/stderr, and it's handy to have a debug log when we're running a subprocess. While we're here, switch to just forking `cp` in the setup code. Signed-off-by: Colin Walters --- bib/internal/setup/setup.go | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/bib/internal/setup/setup.go b/bib/internal/setup/setup.go index e2a3c23..08d00a0 100644 --- a/bib/internal/setup/setup.go +++ b/bib/internal/setup/setup.go @@ -2,9 +2,7 @@ package setup import ( "fmt" - "io" "os" - "os/exec" "path/filepath" "github.com/osbuild/bootc-image-builder/bib/internal/podmanutil" @@ -39,36 +37,21 @@ func EnsureEnvironment() error { return err } if !utils.IsMountpoint(runTmp) { - if err := exec.Command("mount", "-t", "tmpfs", "tmpfs", runTmp).Run(); err != nil { - return fmt.Errorf("failed to mount tmpfs to %s: %w", runTmp, err) + if err := utils.RunCmdSync("mount", "-t", "tmpfs", "tmpfs", runTmp); err != nil { + return err } } - src, err := os.Open("/usr/bin/osbuild") - if err != nil { - return err - } - defer src.Close() destPath := filepath.Join(runTmp, "osbuild") - dst, err := os.Create(destPath) - if err != nil { + if err := utils.RunCmdSync("cp", "-p", "/usr/bin/osbuild", destPath); err != nil { return err } - defer dst.Close() - if _, err := io.Copy(dst, src); err != nil { + if err := utils.RunCmdSync("chcon", installType, destPath); err != nil { return err } - if err := dst.Chmod(0o755); err != nil { - return err - } - dst.Close() - if err := exec.Command("chcon", installType, destPath).Run(); err != nil { - return fmt.Errorf("failed to chcon: %w", err) - } - // Create a bind mount into our target location; we can't copy it because // again we have to perserve the SELinux label. - if err := exec.Command("mount", "--bind", destPath, osbuildPath).Run(); err != nil { - return fmt.Errorf("failed to bind mount to %s: %w", osbuildPath, err) + if err := utils.RunCmdSync("mount", "--bind", destPath, osbuildPath); err != nil { + return err } return nil }