Skip to content

Commit

Permalink
Provide usbs into VMs (#996)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
xpivarc authored Jun 23, 2023
1 parent 013718a commit f661bfe
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions cluster-provision/centos9/scripts/vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
29 changes: 28 additions & 1 deletion cluster-provision/gocli/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f661bfe

Please sign in to comment.