This commit is contained in:
parent
e20248db68
commit
92c2ea4772
4 changed files with 79 additions and 15 deletions
|
@ -82,9 +82,8 @@ func Set_KernelStub(isRoot bool) {
|
||||||
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
|
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
|
||||||
|
|
||||||
// Run and log, check for errors
|
// Run and log, check for errors
|
||||||
common.ErrorCheck(command.ExecAndLogSudo(isRoot, true,
|
common.ErrorCheck(
|
||||||
"kernelstub -a "+fmt.Sprintf("\"%s\"", kernel_args),
|
command.ExecAndLogSudo(isRoot, true, "kernelstub", "-a", kernel_args),
|
||||||
),
|
|
||||||
"Error, kernelstub command returned exit code 1",
|
"Error, kernelstub command returned exit code 1",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -98,7 +97,7 @@ func Set_Grubby(isRoot bool) string {
|
||||||
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
|
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
|
||||||
|
|
||||||
// Run and log, check for errors
|
// Run and log, check for errors
|
||||||
err := command.ExecAndLogSudo(isRoot, true, "grubby --update-kernel=ALL "+fmt.Sprintf("--args=%s", kernel_args))
|
err := command.ExecAndLogSudo(isRoot, true, "grubby", "--update-kernel=ALL", fmt.Sprintf("--args=%s", kernel_args))
|
||||||
common.ErrorCheck(err, "Error, grubby command returned exit code 1")
|
common.ErrorCheck(err, "Error, grubby command returned exit code 1")
|
||||||
|
|
||||||
// Return what we did
|
// Return what we did
|
||||||
|
|
|
@ -201,7 +201,7 @@ func installPassthrough(config *configs.Config) {
|
||||||
// Copy the modules file to /etc/modules
|
// Copy the modules file to /etc/modules
|
||||||
configs.CopyToSystem(config.IsRoot, config.Path.ETCMODULES, "/etc/modules")
|
configs.CopyToSystem(config.IsRoot, config.Path.ETCMODULES, "/etc/modules")
|
||||||
|
|
||||||
if err = command.ExecAndLogSudo(config.IsRoot, true, "update-initramfs -u"); err != nil {
|
if err = command.ExecAndLogSudo(config.IsRoot, true, "update-initramfs", "-u"); err != nil {
|
||||||
log.Fatalf("Failed to update initramfs: %s", err)
|
log.Fatalf("Failed to update initramfs: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -212,7 +212,7 @@ func installPassthrough(config *configs.Config) {
|
||||||
// Get systeminfo
|
// Get systeminfo
|
||||||
sysinfo := uname.New()
|
sysinfo := uname.New()
|
||||||
|
|
||||||
if err = command.ExecAndLogSudo(config.IsRoot, true, "dracut -f -v --kver "+sysinfo.Release); err != nil {
|
if err = command.ExecAndLogSudo(config.IsRoot, true, "dracut", "-f", "-v", "--kver", sysinfo.Release); err != nil {
|
||||||
log.Fatalf("Failed to update initramfs: %s", err)
|
log.Fatalf("Failed to update initramfs: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,7 +220,7 @@ func installPassthrough(config *configs.Config) {
|
||||||
// Copy dracut config to /etc/dracut.conf.d/vfio
|
// Copy dracut config to /etc/dracut.conf.d/vfio
|
||||||
configs.CopyToSystem(config.IsRoot, config.Path.MKINITCPIO, "/etc/mkinitcpio.conf")
|
configs.CopyToSystem(config.IsRoot, config.Path.MKINITCPIO, "/etc/mkinitcpio.conf")
|
||||||
|
|
||||||
if err = command.ExecAndLogSudo(config.IsRoot, true, "mkinitcpio -P"); err != nil {
|
if err = command.ExecAndLogSudo(config.IsRoot, true, "mkinitcpio", "-P"); err != nil {
|
||||||
log.Fatalf("Failed to update initramfs: %s", err)
|
log.Fatalf("Failed to update initramfs: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,17 +120,22 @@ func Clear() {
|
||||||
// - noisy determines if we should print the command to the user
|
// - noisy determines if we should print the command to the user
|
||||||
// noisy isn't set to true by our copy caller, as it logs differently,
|
// noisy isn't set to true by our copy caller, as it logs differently,
|
||||||
// but other callers set it.
|
// but other callers set it.
|
||||||
func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
|
func ExecAndLogSudo(isRoot, noisy bool, exe string, args ...string) error {
|
||||||
if !isRoot && !strings.HasPrefix(cmd, "sudo") {
|
if !isRoot && exe != "sudo" {
|
||||||
cmd = fmt.Sprintf("sudo %s", cmd)
|
og := exe
|
||||||
|
exe = "sudo"
|
||||||
|
newArgs := make([]string, 0)
|
||||||
|
newArgs = append(newArgs, og)
|
||||||
|
newArgs = append(newArgs, args...)
|
||||||
|
args = newArgs
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to logger
|
// Write to logger
|
||||||
logger.Printf("Executing (elevated): %s\n", cmd)
|
logger.Printf("Executing (elevated): %s %s\n", exe, strings.Join(args, " "))
|
||||||
|
|
||||||
if noisy {
|
if noisy {
|
||||||
// Print to the user
|
// Print to the user
|
||||||
fmt.Printf("Executing (elevated): %s\nSee debug.log for detailed output\n", cmd)
|
fmt.Printf("Executing (elevated): %s %s\nSee debug.log for detailed output\n", exe, strings.Join(args, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
wd, err := os.Getwd()
|
wd, err := os.Getwd()
|
||||||
|
@ -138,8 +143,7 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cs := strings.Fields(cmd)
|
r := exec.Command(exe, args...)
|
||||||
r := exec.Command(cs[0], cs[1:]...)
|
|
||||||
r.Dir = wd
|
r.Dir = wd
|
||||||
|
|
||||||
cmdCombinedOut, err := r.CombinedOutput()
|
cmdCombinedOut, err := r.CombinedOutput()
|
||||||
|
@ -155,7 +159,7 @@ func ExecAndLogSudo(isRoot, noisy bool, cmd string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("failed to execute %s: %w\n%s", cmd, err, outStr)
|
err = fmt.Errorf("failed to execute %s: %w\n%s", exe, err, outStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
|
61
pkg/command/command_test.go
Normal file
61
pkg/command/command_test.go
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
const fakeSudo = `#!/bin/sh
|
||||||
|
"$@" -qptest`
|
||||||
|
|
||||||
|
const fakeUtil = `#!/bin/sh
|
||||||
|
echo "$@"
|
||||||
|
if [ "$4" = "-qptest" ]; then exit 0; else exit 1; fi`
|
||||||
|
|
||||||
|
func setupExecTestEnv(t *testing.T) (string, string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
tmpDir := t.TempDir()
|
||||||
|
fakeSudoPath := filepath.Join(tmpDir, "sudo")
|
||||||
|
fakeUtilPath := filepath.Join(tmpDir, "util")
|
||||||
|
|
||||||
|
if err := os.WriteFile(fakeSudoPath, []byte(fakeSudo), 0755); err != nil {
|
||||||
|
t.Fatalf("failed to write fake sudo stub: %s", err.Error())
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(fakeUtilPath, []byte(fakeUtil), 0755); err != nil {
|
||||||
|
t.Fatalf("failed to write fake util stub: %s", err.Error())
|
||||||
|
}
|
||||||
|
t.Setenv("PATH", tmpDir+":"+os.Getenv("PATH"))
|
||||||
|
|
||||||
|
return fakeSudoPath, fakeUtilPath
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExecAndLogSudo(t *testing.T) {
|
||||||
|
_, fakeUtilPath := setupExecTestEnv(t)
|
||||||
|
|
||||||
|
args := []string{"i am a string with spaces", "i came to ruin parsers and chew bubble gum", "and I'm all out of bubblegum."}
|
||||||
|
|
||||||
|
t.Run("is_not_root", func(t *testing.T) {
|
||||||
|
if err := ExecAndLogSudo(false, false, "util", args...); err != nil {
|
||||||
|
t.Errorf("unexpected error: %s", err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("is_root", func(t *testing.T) {
|
||||||
|
newFakeUtil := strings.Replace(fakeUtil, "exit 1", "exit 0", 1)
|
||||||
|
newFakeUtil = strings.Replace(newFakeUtil, "exit 0", "exit 1", 1)
|
||||||
|
if err := os.WriteFile(fakeUtilPath, []byte(newFakeUtil), 0755); err != nil {
|
||||||
|
t.Fatalf("failed to overwrite fake util with modified stub: %s", err.Error())
|
||||||
|
}
|
||||||
|
if err := ExecAndLogSudo(false, false, "util", args...); err == nil {
|
||||||
|
t.Errorf("expected error when using modified util with sudo, got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ExecAndLogSudo(true, true, "util", args...); err != nil {
|
||||||
|
t.Errorf("unexpected error: %s", err.Error())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue