Add initramfs functions

This commit is contained in:
HikariKnight 2023-04-09 03:20:56 +02:00
parent e03c27bf77
commit 8c35e8d328
2 changed files with 83 additions and 28 deletions

View file

@ -0,0 +1,83 @@
package configs
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/pkg/uname"
)
func readHeader(lines int, fileName string) string {
// Open the file
f, err := os.Open(fileName)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening %s", fileName))
defer f.Close()
header_re := regexp.MustCompile(`^#`)
var header []string
// Make a new scanner
scanner := bufio.NewScanner(f)
// Read the first N lines
for i := 0; i < lines; i++ {
scanner.Scan()
if header_re.MatchString(scanner.Text()) {
header = append(header, scanner.Text())
}
}
// Return the header
return fmt.Sprintf("%s\n", strings.Join(header, "\n"))
}
func addModules(conffile string) {
// Make a regex to get the system path instead of the config path
syspath_re := regexp.MustCompile(`^config`)
// Make a regex to skip specific modules and comments
skipmodules_re := regexp.MustCompile(`(^#|vendor-reset)`)
// Get the syspath
syspath := syspath_re.ReplaceAllString(conffile, "")
// Open the system file for reading
sysfile, err := os.Open(syspath)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening file for reading %s", syspath))
defer sysfile.Close()
// Open config file for writing
out, err := os.OpenFile(conffile, os.O_APPEND|os.O_WRONLY, os.ModePerm)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening file for writing %s", conffile))
defer out.Close()
// Make a list of modules
//var modules []string
// Scan the file line by line
scanner := bufio.NewScanner(sysfile)
for scanner.Scan() {
if scanner.Text() == "vendor-reset" {
out.WriteString(scanner.Text())
} else if !skipmodules_re.MatchString(scanner.Text()) {
writeContent(fmt.Sprintf("%s\n", scanner.Text()), conffile)
sysinfo := uname.New()
writeContent(fmt.Sprintf("%s\n%s\n%s\n%s\n", sysinfo.Nodename, sysinfo.Sysname, sysinfo.Domainname, sysinfo.Machine), conffile)
}
}
}
func writeContent(content string, fileName string) {
// Open the file
f, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModePerm)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error opening %s", fileName))
defer f.Close()
// Make a new scanner
_, err = f.WriteString(content)
errorcheck.ErrorCheck(err, fmt.Sprintf("Error writing to %s", fileName))
}

View file

@ -1,28 +0,0 @@
package configs
import (
"github.com/HikariKnight/quickpassthrough/pkg/command"
)
func getBootloader(config *Config) {
// Check what bootloader handler we are using
// Check for grub-mkconfig
_, err := command.Run("which", "grub-mkconfig")
if err == nil {
// Mark bootloader as grub2
config.bootloader = "grub2"
}
// Check for grubby (used by fedora)
_, err = command.Run("which", "grubby")
if err == nil {
// Mark it as unknown as i do not support it yet
config.bootloader = "unknown"
}
// Check for kernelstub (used by pop os)
_, err = command.Run("which", "kernelstub")
if err == nil {
config.bootloader = "kernelstub"
}
}