diff --git a/src/cmdVM/vmCreate.go b/src/cmdVM/vmCreate.go index e1c3f797684a78d135d12b37b57b83cc3aa8b6f7..d3f7e9f33ada473a583dbf781800a1e6a1c2affa 100644 --- a/src/cmdVM/vmCreate.go +++ b/src/cmdVM/vmCreate.go @@ -2,7 +2,6 @@ package cmdVM import ( "fmt" - "strings" "strconv" u "nexus-client/utils" g "nexus-client/globals" @@ -68,7 +67,7 @@ func (cmd *Create)Run(args []string) int { return 1 } nic := NicType(args[3]) - usbDevs := cmd.str2UsbDevices(args[4]) + usbDevs := u.Str2UsbDevices(args[4]) templateID, err := uuid.Parse(args[5]) if err != nil { u.PrintlnErr(err) @@ -153,17 +152,3 @@ func (cmd *Create)Run(args []string) int { return statusCode } - -// Convert a string of USB devices of the form "1fc9:001d,067b:2303" -// into a slice of string where each element is a string of the form "1fc9:001d". -// Returns an empty slice if the input string is "none". -func (cmd *Create)str2UsbDevices(s string) []string { - usbDevs := []string{} - if s != "none" { - devs := strings.Split(s, ",") // Extracts USB devices - for _, dev := range devs { - usbDevs = append(usbDevs, dev) - } - } - return usbDevs -} diff --git a/src/cmdVM/vmEdit.go b/src/cmdVM/vmEdit.go index c2685361ddd3ffdf6c3008bdaff4bd91438913d7..58d30cfcfcaf6ffbd3c2eb19e5c82f72fc8e4c54 100644 --- a/src/cmdVM/vmEdit.go +++ b/src/cmdVM/vmEdit.go @@ -16,7 +16,8 @@ type vmEditParameters struct { Name string Cpus int Ram int - Nic string + Nic NicType + UsbDevs []string } func (cmd *Edit)GetName() string { @@ -34,13 +35,16 @@ func (cmd *Edit)PrintUsage() { u.PrintlnErr(desc) } u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") - u.PrintlnErr("USAGE: "+cmd.GetName()+" [ID ...] [regex ...] [name=\"new name\"] [cpus=n] [ram=n] [nic=none/user]") + u.PrintlnErr("USAGE: "+cmd.GetName()+" [ID ...] [regex ...] [name=...] [cpus=...] [ram=...] [nic=...] [usb=...]") u.PrintlnErr("―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――") const usage string = `Parameters that can be changed (at least one must be specified): name Name of the VM. cpus Number of CPUs, between 1 and 16. ram Amount of RAM in MB, between 512 and 32768. -nic Network interface, either "none" (no network) or "user" (network access).` +nic Network interface, either "none" (no network) or "user" (network access). +usb List of USB devices exposed in the VM; either "none" or a list of comma separated + vendorID:productID (4-digit hex number), each specifying a USB device; example + exposing two USB devices: 1fc9:001d,067b:2303` u.PrintlnErr(usage) u.PrintlnErr("") printRegexUsageDetails() @@ -141,7 +145,13 @@ func (cmd *Edit)parseArgs(args []string) (*vmEditParameters, []string, error) { } s = getStringVal(arg, "nic=") if s != "" { - vmParams.Nic = s + vmParams.Nic = NicType(s) + atLeastOneArg = true + continue + } + s = getStringVal(arg, "usb=") + if s != "" { + vmParams.UsbDevs = u.Str2UsbDevices(s) atLeastOneArg = true continue } diff --git a/src/utils/utils.go b/src/utils/utils.go index 5f9d2d1b39c5cd0ff4f9460103428a91f4e64337..edd2e8cd52ee4dcc85df8bc08a5a57b04ea5cbdf 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -7,6 +7,7 @@ import ( "io/fs" "bytes" "errors" + "strings" "net/mail" "archive/tar" "path/filepath" @@ -112,3 +113,17 @@ func IsEmail(email string) bool { _, err := mail.ParseAddress(email) return err == nil } + +// Convert a string of USB devices of the form "1fc9:001d,067b:2303" +// into a slice of string where each element is a string of the form "1fc9:001d". +// Returns an empty slice if the input string is "none". +func Str2UsbDevices(s string) []string { + usbDevs := []string{} + if s != "none" { + devs := strings.Split(s, ",") // Extracts USB devices + for _, dev := range devs { + usbDevs = append(usbDevs, dev) + } + } + return usbDevs +}