2023-08-25 15:12:19 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-25 16:54:58 +01:00
|
|
|
"context"
|
2023-08-25 15:12:19 +01:00
|
|
|
"fmt"
|
2023-08-25 16:54:58 +01:00
|
|
|
"log/slog"
|
2023-08-25 15:12:19 +01:00
|
|
|
"os"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2023-08-26 09:16:52 +01:00
|
|
|
"github.com/AlexSSD7/linsk/vm"
|
2023-08-25 15:12:19 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var lsCmd = &cobra.Command{
|
2023-08-29 10:37:52 +01:00
|
|
|
Use: "ls",
|
|
|
|
|
Short: "Start a VM and list all user drives within the VM. Uses lsblk command under the hood.",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
2023-08-27 15:30:51 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2023-08-27 13:44:57 +01:00
|
|
|
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.VM, fm *vm.FileManager) int {
|
2023-08-25 16:54:58 +01:00
|
|
|
lsblkOut, err := fm.Lsblk()
|
|
|
|
|
if err != nil {
|
2023-08-29 10:59:50 +01:00
|
|
|
slog.Error("Failed to list block devices in the VM", "error", err.Error())
|
2023-08-26 11:27:38 +01:00
|
|
|
return 1
|
2023-08-25 16:54:58 +01:00
|
|
|
}
|
2023-08-25 15:12:19 +01:00
|
|
|
|
2023-08-29 10:59:50 +01:00
|
|
|
if len(lsblkOut) == 0 {
|
|
|
|
|
fmt.Printf("<empty lsblk output>\n")
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Print(string(lsblkOut))
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-26 11:27:38 +01:00
|
|
|
return 0
|
2023-08-26 16:43:04 +01:00
|
|
|
}, nil, false))
|
2023-08-25 15:12:19 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getDevicePassthroughConfig(val string) vm.USBDevicePassthroughConfig {
|
|
|
|
|
valSplit := strings.Split(val, ":")
|
|
|
|
|
if want, have := 2, len(valSplit); want != have {
|
2023-08-25 19:55:11 +01:00
|
|
|
slog.Error("Bad device passthrough syntax", "error", fmt.Errorf("wrong items split by ':' count: want %v, have %v", want, have))
|
2023-08-25 15:12:19 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch valSplit[0] {
|
|
|
|
|
case "usb":
|
|
|
|
|
usbValsSplit := strings.Split(valSplit[1], ",")
|
|
|
|
|
if want, have := 2, len(usbValsSplit); want != have {
|
2023-08-25 19:55:11 +01:00
|
|
|
slog.Error("Bad USB device passthrough syntax", "error", fmt.Errorf("wrong args split by ',' count: want %v, have %v", want, have))
|
2023-08-25 15:12:19 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 18:07:51 +01:00
|
|
|
vendorID, err := strconv.ParseUint(usbValsSplit[0], 16, 32)
|
2023-08-25 15:12:19 +01:00
|
|
|
if err != nil {
|
2023-08-27 18:07:51 +01:00
|
|
|
slog.Error("Bad USB vendor ID", "value", usbValsSplit[0])
|
2023-08-25 15:12:19 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 18:07:51 +01:00
|
|
|
productID, err := strconv.ParseUint(usbValsSplit[1], 16, 32)
|
2023-08-25 15:12:19 +01:00
|
|
|
if err != nil {
|
2023-08-27 18:07:51 +01:00
|
|
|
slog.Error("Bad USB product ID", "value", usbValsSplit[1])
|
2023-08-25 15:12:19 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return vm.USBDevicePassthroughConfig{
|
2023-08-27 18:07:51 +01:00
|
|
|
VendorID: uint16(vendorID),
|
|
|
|
|
ProductID: uint16(productID),
|
2023-08-25 15:12:19 +01:00
|
|
|
}
|
|
|
|
|
default:
|
2023-08-25 16:54:58 +01:00
|
|
|
slog.Error("Unknown device passthrough type", "value", valSplit[0])
|
2023-08-25 15:12:19 +01:00
|
|
|
os.Exit(1)
|
|
|
|
|
// This unreachable code is required to compile.
|
|
|
|
|
return vm.USBDevicePassthroughConfig{}
|
|
|
|
|
}
|
|
|
|
|
}
|