From f661bfe0e3678e5409c057855951c50a912571a0 Mon Sep 17 00:00:00 2001 From: xpivarc <41989919+xpivarc@users.noreply.github.com> Date: Fri, 23 Jun 2023 05:38:34 -0400 Subject: [PATCH] Provide usbs into VMs (#996) In order to test usb pass-through we need few usb devices available in the VM. In order to use it, export following before cluster-up: export KUBEVIRT_PROVIDER_EXTRA_ARGS="--usb 20M --usb 30M" Signed-off-by: L. Pivarc --- cluster-provision/centos9/scripts/vm.sh | 11 ++++++++++ cluster-provision/gocli/cmd/run.go | 29 ++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cluster-provision/centos9/scripts/vm.sh b/cluster-provision/centos9/scripts/vm.sh index 48c2d096bb..7c50ac3f1f 100755 --- a/cluster-provision/centos9/scripts/vm.sh +++ b/cluster-provision/centos9/scripts/vm.sh @@ -22,6 +22,7 @@ while true; do -s | --block-device-size ) BLOCK_DEV_SIZE="$2"; shift 2 ;; -n | --nvme-device-size ) NVME_DISK_SIZES+="$2 "; shift 2 ;; -t | --scsi-device-size ) SCSI_DISK_SIZES+="$2 "; shift 2 ;; + -u | --usb-device-size ) USB_SIZES+="$2 "; shift 2 ;; -- ) shift; break ;; * ) break ;; esac @@ -141,6 +142,16 @@ for size in ${SCSI_DISK_SIZES[@]}; do let "disk_num+=1" done + +disk_num=0 +for size in ${USB_SIZES[@]}; do + echo "Creating disk "$size" for USB disk emulation" + disk="/usb-"${disk_num}".img" + qemu-img create -f raw $disk $size + let "disk_num+=1" +done + + exec qemu-system-x86_64 -enable-kvm -drive format=qcow2,file=${next},if=virtio,cache=unsafe ${block_dev_arg} \ -device virtio-net-pci,netdev=network0,mac=52:55:00:d1:55:${n} \ -netdev tap,id=network0,ifname=tap${n},script=no,downscript=no \ diff --git a/cluster-provision/gocli/cmd/run.go b/cluster-provision/gocli/cmd/run.go index 9110d23d2d..a1f31f2900 100644 --- a/cluster-provision/gocli/cmd/run.go +++ b/cluster-provision/gocli/cmd/run.go @@ -58,6 +58,7 @@ var soundcardPCIIDs = []string{"8086:2668", "8086:2415"} var cli *client.Client var nvmeDisks []string var scsiDisks []string +var usbDisks []string type dockerSetting struct { Proxy string @@ -114,6 +115,7 @@ func NewRunCommand() *cobra.Command { run.Flags().Bool("enable-psa", false, "Pod Security Admission") run.Flags().Bool("single-stack", false, "enable single stack IPv6") run.Flags().Bool("enable-audit", false, "enable k8s audit for all metadata events") + run.Flags().StringArrayVar(&usbDisks, "usb", []string{}, "size of the emulate USB disk to pass to the node") return run } @@ -475,6 +477,23 @@ func run(cmd *cobra.Command, args []string) (retErr error) { } } + var vmArgsUSBDisks []string + const bus = " -device qemu-xhci,id=bus%d" + const drive = " -drive if=none,id=stick%d,format=raw,file=/usb-%d.img" + const dev = " -device usb-storage,bus=bus%d.0,drive=stick%d" + const usbSizefmt = " --usb-device-size %s" + if len(usbDisks) > 0 { + for i, size := range usbDisks { + resource.MustParse(size) + if i%2 == 0 { + nodeQemuArgs += fmt.Sprintf(bus, i/2) + } + nodeQemuArgs += fmt.Sprintf(drive, i, i) + nodeQemuArgs += fmt.Sprintf(dev, i/2, i) + vmArgsUSBDisks = append(vmArgsUSBDisks, fmt.Sprintf(usbSizefmt, size)) + } + } + additionalArgs := []string{} if len(nodeQemuArgs) > 0 { additionalArgs = append(additionalArgs, "--qemu-args", shellescape.Quote(nodeQemuArgs)) @@ -503,7 +522,15 @@ func run(cmd *cobra.Command, args []string) (retErr error) { Env: []string{ fmt.Sprintf("NODE_NUM=%s", nodeNum), }, - Cmd: []string{"/bin/bash", "-c", fmt.Sprintf("/vm.sh -n /var/run/disk/disk.qcow2 --memory %s --cpu %s %s %s %s %s", memory, strconv.Itoa(int(cpu)), blockDev, strings.Join(vmArgsSCSIDisks, " "), strings.Join(vmArgsNvmeDisks, " "), strings.Join(additionalArgs, " "))}, + Cmd: []string{"/bin/bash", "-c", fmt.Sprintf("/vm.sh -n /var/run/disk/disk.qcow2 --memory %s --cpu %s %s %s %s %s %s", + memory, + strconv.Itoa(int(cpu)), + blockDev, + strings.Join(vmArgsSCSIDisks, " "), + strings.Join(vmArgsNvmeDisks, " "), + strings.Join(vmArgsUSBDisks, " "), + strings.Join(additionalArgs, " "), + )}, } if cephEnabled {