11package configs
22
33import (
4+ "errors"
45 "fmt"
56 "os"
7+ "os/exec"
68 "regexp"
79 "strings"
810
9- "github.com/HikariKnight/ls-iommu/pkg/errorcheck"
11+ "github.com/klauspost/cpuid/v2"
12+
13+ "github.com/HikariKnight/quickpassthrough/internal/common"
1014 "github.com/HikariKnight/quickpassthrough/internal/logger"
1115 "github.com/HikariKnight/quickpassthrough/pkg/command"
1216 "github.com/HikariKnight/quickpassthrough/pkg/fileio"
13- "github.com/klauspost/cpuid/v2"
1417)
1518
1619// This function just adds what bootloader the system has to our config.bootloader value
@@ -70,39 +73,32 @@ func Set_Cmdline(gpu_IDs []string) {
7073 fileio .AppendContent (fmt .Sprintf (" vfio_pci.ids=%s" , strings .Join (gpu_IDs , "," )), config .Path .CMDLINE )
7174}
7275
73- // Configures systemd-boot using kernelstub
74- func Set_KernelStub () string {
76+ // Set_KernelStub configures systemd-boot using kernelstub.
77+ func Set_KernelStub (isRoot bool ) {
7578 // Get the config
7679 config := GetConfig ()
7780
7881 // Get the kernel args
7982 kernel_args := fileio .ReadFile (config .Path .CMDLINE )
8083
81- // Write to logger
82- logger .Printf ("Running command:\n sudo kernelstub -a \" %s\" \n " , kernel_args )
83-
84- // Run the command
85- _ , err := command .Run ("sudo" , "kernelstub" , "-a" , kernel_args )
86- errorcheck .ErrorCheck (err , "Error, kernelstub command returned exit code 1" )
87-
88- // Return what we did
89- return fmt .Sprintf ("Executed: sudo kernelstub -a \" %s\" " , kernel_args )
84+ // Run and log, check for errors
85+ common .ErrorCheck (
86+ command .ExecAndLogSudo (isRoot , true , "kernelstub" , "-a" , kernel_args ),
87+ "Error, kernelstub command returned exit code 1" ,
88+ )
9089}
9190
92- // Configures grub2 and/or systemd-boot using grubby
93- func Set_Grubby () string {
91+ // Set_Grubby configures grub2 and/or systemd-boot using grubby
92+ func Set_Grubby (isRoot bool ) string {
9493 // Get the config
9594 config := GetConfig ()
9695
9796 // Get the kernel args
9897 kernel_args := fileio .ReadFile (config .Path .CMDLINE )
9998
100- // Write to logger
101- logger .Printf ("Running command:\n sudo grubby --update-kernel=ALL --args=\" %s\" \n " , kernel_args )
102-
103- // Run the command
104- _ , err := command .Run ("sudo" , "grubby" , "--update-kernel=ALL" , fmt .Sprintf ("--args=%s" , kernel_args ))
105- errorcheck .ErrorCheck (err , "Error, grubby command returned exit code 1" )
99+ // Run and log, check for errors
100+ err := command .ExecAndLogSudo (isRoot , true , "grubby" , "--update-kernel=ALL" , fmt .Sprintf ("--args=%s" , kernel_args ))
101+ common .ErrorCheck (err , "Error, grubby command returned exit code 1" )
106102
107103 // Return what we did
108104 return fmt .Sprintf ("Executed: sudo grubby --update-kernel=ALL --args=\" %s\" " , kernel_args )
@@ -116,8 +112,8 @@ func Configure_Grub2() {
116112 conffile := fmt .Sprintf ("%s/grub" , config .Path .DEFAULT )
117113
118114 // Make sure we start from scratch by deleting any old file
119- if fileio .FileExist (conffile ) {
120- os .Remove (conffile )
115+ if exists , _ := fileio .FileExist (conffile ); exists {
116+ _ = os .Remove (conffile )
121117 }
122118
123119 // Make a regex to get the system path instead of the config path
@@ -201,8 +197,8 @@ func clean_Grub2_Args(old_kernel_args []string) []string {
201197 return clean_kernel_args
202198}
203199
204- // This function copies our config to /etc/default/grub and updates grub
205- func Set_Grub2 () ([] string , error ) {
200+ // Set_Grub2 copies our config to /etc/default/grub and updates grub
201+ func Set_Grub2 (isRoot bool ) error {
206202 // Get the config
207203 config := GetConfig ()
208204
@@ -213,38 +209,45 @@ func Set_Grub2() ([]string, error) {
213209 sysfile_re := regexp .MustCompile (`^config` )
214210 sysfile := sysfile_re .ReplaceAllString (conffile , "" )
215211
216- // Write to logger
217- logger .Printf ("Executing command:\n sudo cp -v \" %s\" %s\n " , conffile , sysfile )
218-
219- // Make our output slice
220- var output []string
212+ // [CopyToSystem] will log the operation
213+ // logger.Printf("Executing command:\nsudo cp -v \"%s\" %s\n", conffile, sysfile)
221214
222- // Copy files to system
223- output = append ( output , CopyToSystem ( conffile , sysfile ) )
215+ // Copy files to system, logging and error checking is done in the function
216+ CopyToSystem ( isRoot , conffile , sysfile )
224217
225218 // Set a variable for the mkconfig command
226- mkconfig := "grub-mkconfig"
219+ var mkconfig string
220+ var grubPath = "/boot/grub/grub.cfg"
221+ var lpErr error
222+
227223 // Check for grub-mkconfig
228- _ , err := command .Run ("which" , "grub-mkconfig" )
229- if err == nil {
230- // Set binary as grub-mkconfig
231- mkconfig = "grub-mkconfig"
232- } else {
233- mkconfig = "grub2-mkconfig"
224+ mkconfig , lpErr = exec .LookPath ("grub-mkconfig" )
225+ switch {
226+ case errors .Is (lpErr , exec .ErrNotFound ) || mkconfig == "" :
227+ // Check for grub2-mkconfig
228+ mkconfig , lpErr = exec .LookPath ("grub2-mkconfig" )
229+ if lpErr == nil && mkconfig != "" {
230+ grubPath = "/boot/grub2/grub.cfg"
231+ break // skip below, we found grub2-mkconfig
232+ }
233+ if lpErr == nil {
234+ // we know mkconfig is empty despite no error;
235+ // so set an error for [common.ErrorCheck].
236+ lpErr = errors .New ("neither grub-mkconfig or grub2-mkconfig found" )
237+ }
238+ common .ErrorCheck (lpErr , lpErr .Error ()+ "\n " )
239+ return lpErr // note: unreachable as [common.ErrorCheck] calls fatal
240+ default :
234241 }
235242
236- // Update grub.cfg
237- if fileio .FileExist ("/boot/grub/grub.cfg" ) {
238- output = append (output , fmt .Sprintf ("Executed: sudo %s -o /boot/grub/grub.cfg\n See debug.log for more detailed output" , mkconfig ))
239- _ , mklog , err := command .RunErr ("sudo" , mkconfig , "-o" , "/boot/grub/grub.cfg" )
240- logger .Printf (strings .Join (mklog , "\n " ))
241- errorcheck .ErrorCheck (err , "Failed to update /boot/grub/grub.cfg" )
242- } else {
243- output = append (output , fmt .Sprintf ("Executed: sudo %s -o /boot/grub/grub.cfg\n See debug.log for more detailed output" , mkconfig ))
244- _ , mklog , err := command .RunErr ("sudo" , mkconfig , "-o" , "/boot/grub2/grub.cfg" )
245- logger .Printf (strings .Join (mklog , "\n " ))
246- errorcheck .ErrorCheck (err , "Failed to update /boot/grub/grub.cfg" )
247- }
243+ _ , mklog , err := command .RunErrSudo (isRoot , mkconfig , "-o" , grubPath )
244+
245+ // tabulate the output, [command.RunErrSudo] logged the execution.
246+ logger .Printf ("\t " + strings .Join (mklog , "\n \t " ))
247+ common .ErrorCheck (err , "Failed to update /boot/grub/grub.cfg" )
248248
249- return output , err
249+ // always returns nil as [common.ErrorCheck] calls fatal
250+ // keeping the ret signature, as we should consider passing down errors
251+ // but that's a massive rabbit hole to go down for this codebase as a whole
252+ return err
250253}
0 commit comments