Prevent appending multiple times to the file

This commit is contained in:
HikariKnight 2023-04-11 02:44:00 +02:00
parent ff5d20f4f9
commit 68aec7fdb8
2 changed files with 39 additions and 9 deletions

View file

@ -27,8 +27,14 @@ func Set_Dracut() {
// Write the dracut config file
fileio.AppendContent(fmt.Sprintf("add_drivers+=\" %s \"\n", strings.Join(vfio_modules(), " ")), dracutConf)
// Get the current kernel arguments we have generated
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
// If the kernel argument is not already in the file
if !strings.Contains(kernel_args, "rd.driver.pre=vfio_pci") {
// Add to our kernel arguments file that vfio_pci should load early (dracut does this using kernel arguments)
fileio.AppendContent(" rd.driver.pre=vfio_pci", config.Path.CMDLINE)
}
// Make a backup of dracutConf if there is one there
backupFile(strings.Replace(dracutConf, "config", "", 1))

View file

@ -2,7 +2,10 @@ package configs
import (
"fmt"
"os"
"strings"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/internal/logger"
"github.com/HikariKnight/quickpassthrough/pkg/fileio"
)
@ -16,6 +19,26 @@ func DisableVFIOVideo(i int) {
// Write to logger
logger.Printf("Adding vfio_pci.disable_vga=%v to %s", i, config.Path.CMDLINE)
// Get the current kernel arguments we have generated
kernel_args := fileio.ReadFile(config.Path.CMDLINE)
// If the kernel argument is already in the file
if strings.Contains(kernel_args, "vfio_pci.disable_vga") {
// Remove the old file
err := os.Remove(config.Path.CMDLINE)
errorcheck.ErrorCheck(err, fmt.Sprintf("Could not rewrite %s", config.Path.CMDLINE))
// Enable or disable the VGA based on our given value
if i == 0 {
kernel_args = strings.Replace(kernel_args, "vfio_pci.disable_vga=1", "vfio_pci.disable_vga=0", 1)
} else {
kernel_args = strings.Replace(kernel_args, "vfio_pci.disable_vga=0", "vfio_pci.disable_vga=1", 1)
}
// Rewrite the kernel_args file
fileio.AppendContent(kernel_args, config.Path.CMDLINE)
} else {
// Add to the kernel arguments that we want to disable VFIO video output on the host
fileio.AppendContent(
fmt.Sprintf(
@ -23,4 +46,5 @@ func DisableVFIOVideo(i int) {
),
config.Path.CMDLINE,
)
}
}