Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide usbs into VMs #996

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
xpivarc marked this conversation as resolved.
Show resolved Hide resolved
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"
xpivarc marked this conversation as resolved.
Show resolved Hide resolved
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