Start process to away from bubbletea to a more simpler solution for this projects needs

This commit is contained in:
HikariKnight 2023-10-26 20:03:15 +02:00
parent cc6db38d74
commit c1ea5e5163
10 changed files with 298 additions and 65 deletions

View file

@ -0,0 +1,71 @@
package lsiommu
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"github.com/HikariKnight/quickpassthrough/internal/logger"
"github.com/klauspost/cpuid/v2"
)
func GetIOMMU(args ...string) []string {
var stdout, stderr bytes.Buffer
// Write to logger
logger.Printf("Executing: utils/ls-iommu %s", strings.Join(args, " "))
// Configure the ls-iommu command
cmd := exec.Command("utils/ls-iommu", args...)
cmd.Stderr = &stderr
cmd.Stdout = &stdout
// Execute the command
err := cmd.Run()
// Generate the correct iommu string for the system
var iommu_args string
cpuinfo := cpuid.CPU
// Write the argument based on which cpu the user got
switch cpuinfo.VendorString {
case "AuthenticAMD":
iommu_args = "iommu=pt amd_iommu=on"
case "GenuineIntel":
iommu_args = "iommu=pt intel_iommu=on"
}
// If ls-iommu returns an error then IOMMU is disabled
if err != nil {
fmt.Printf(
"IOMMU disabled in either UEFI/BIOS or in bootloader, or run inside container!\n"+
"For your bootloader, make sure you have added the kernel arguments:\n"+
"%s\n",
iommu_args,
)
os.Exit(1)
}
// Read the output
var items []string
output, _ := io.ReadAll(&stdout)
// Write to logger
logger.Printf("ls-iommu query returned\n%s", string(output))
// Make regex to shorten vendor names
shortenVendor := regexp.MustCompile(` Corporation:|Advanced Micro Devices, Inc\. \[(AMD)\/ATI\]:`)
// Parse the output line by line
scanner := bufio.NewScanner(strings.NewReader(string(output)))
for scanner.Scan() {
// Write the objects into the list
items = append(items, shortenVendor.ReplaceAllString(scanner.Text(), "${1}"))
}
// Return our list of items
return items
}

View file

@ -0,0 +1,39 @@
package pages
import (
"github.com/HikariKnight/quickpassthrough/pkg/command"
"github.com/HikariKnight/quickpassthrough/pkg/menu"
"github.com/gookit/color"
)
// Welcome page
func Welcome() {
// Clear screen
command.Clear()
// Write title
color.Bold.Println("Welcome to Quickpassthrough!")
// Write welcome message
color.Println(
" This script is meant to make it easier to setup GPU passthrough for\n",
"Qemu based systems. WITH DIFFERENT 2 GPUS ON THE HOST SYSTEM\n",
"However due to the complexity of GPU passthrough\n",
"This script assumes you know how to do (or have done) the following.\n\n",
"* You have already enabled IOMMU, VT-d, SVM and/or AMD-v\n inside your UEFI/BIOS advanced settings.\n",
"* Know how to edit your bootloader\n",
"* Have a bootloader timeout of at least 3 seconds to access the menu\n",
"* Enable & Configure kernel modules\n",
"* Have a backup/snapshot of your system in case the script causes your\n system to be unbootable\n\n",
"By continuing you accept that I am not liable if your system\n",
"becomes unbootable, as you will be asked to verify the files generated\n",
)
// Make user accept responsibility
choice := menu.YesNo("Are you sure you want to continue?")
// If yes, go to next page
if choice == "y" {
SelectGPU()
}
}

View file

@ -0,0 +1,72 @@
package pages
import (
"fmt"
lsiommu "github.com/HikariKnight/quickpassthrough/internal/lsiommu"
"github.com/HikariKnight/quickpassthrough/pkg/command"
"github.com/HikariKnight/quickpassthrough/pkg/menu"
"github.com/gookit/color"
)
func SelectGPU() {
// Clear the screen
command.Clear()
// Get the users GPUs
gpus := lsiommu.GetIOMMU("-g", "-F", "vendor:,prod_name,optional_revision:,device_id")
// Generate a list of choices based on the GPUs and get the users selection
choice := menu.GenIOMMUMenu("Select a GPU to view the IOMMU groups of", gpus)
// View the selected GPU
ViewGPU(choice)
}
func ViewGPU(id string, ext ...int) {
// Clear the screen
command.Clear()
// Set mode to relative
mode := "-r"
// Set mode to relative extended
if len(ext) > 0 {
mode = "-rr"
}
// Get the IOMMU listings for GPUs
group := lsiommu.GetIOMMU("-g", mode, "-i", id, "-F", "vendor:,prod_name,optional_revision:,device_id")
// Write a title
color.Bold.Println("This list should only show devices related to your GPU")
// Print all the gpus
for _, v := range group {
fmt.Println(v)
}
// Add a new line for tidyness
fmt.Println("")
// Make an empty string
var choice string
// Change choices depending on if we have done an extended search or not
if len(ext) > 0 {
choice = menu.YesNo("Use this GPU (any extra devices listed may or may not be linked to it) for passthrough?")
} else {
choice = menu.YesNoEXT("Use this GPU (and related devices) for passthrough?")
}
// Parse the choice
switch choice {
case "ext":
// Run an extended relative search
ViewGPU(id, 1)
case "n":
// Go back to selecting a gpu
SelectGPU()
}
}

View file

@ -7,6 +7,7 @@ import (
"os"
"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
"github.com/HikariKnight/quickpassthrough/internal/pages"
tea "github.com/charmbracelet/bubbletea"
)
@ -18,11 +19,13 @@ func Tui() {
errorcheck.ErrorCheck(err, "Error creating log file")
defer logfile.Close()
pages.Welcome()
// Make a blank model to keep our state in
m := NewModel()
/*m := NewModel()
// Start the program with the model
p := tea.NewProgram(m, tea.WithAltScreen())
_, err = p.Run()
errorcheck.ErrorCheck(err, "Failed to initialize UI")
errorcheck.ErrorCheck(err, "Failed to initialize UI")*/
}