Skip to content
New issue

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

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

Already on GitHub? # to your account

support USB Controllers #178

Merged
merged 2 commits into from
Jan 6, 2025
Merged

support USB Controllers #178

merged 2 commits into from
Jan 6, 2025

Conversation

Code-Hex
Copy link
Owner

@Code-Hex Code-Hex commented Nov 24, 2024

Which issue(s) this PR fixes:

Fixes #177

Additional documentation

The same content has been added to the wiki:
https://github.com/Code-Hex/vz/wiki/Attach-Detach-USB-Device-example-for-macOS-15-and-above

Create USB disk image on macOS:

# format as FAT32
hdiutil create -size 4g -fs FAT32 -volname "USB" usb_disk.dmg

Write code like below:

config, err := setupVMConfiguration(platformConfig)
if err != nil {
	return err
}
vm, err := vz.NewVirtualMachine(config)
if err != nil {
	return err
}

// ...

usbCtrls := vm.USBControllers()

// Attach USB device
if len(usbCtrls) > 0 {
	diskAttachment, err := vz.NewDiskImageStorageDeviceAttachment("/path/to/usb_disk.dmg", isReadOnly)
	if err != nil {
		log.Fatal(err)
	}
	conf, _ := vz.NewUSBMassStorageDeviceConfiguration(diskAttachment)
	usbDevice, _ := vz.NewUSBMassStorageDevice(conf)
	log.Println("loading usb", len(usbCtrls), usbDevice.UUID())
	if err := usbCtrls[0].Attach(usbDevice); err != nil {
		log.Printf("failed to attach usb device: %q", err)
	} else {
		log.Println("attached")
	}
}

// Detach USB device
if len(usbCtrls) > 0 {
	log.Println("loading usb in cleanup")
	for _, dev := range usbCtrls[0].USBDevices() {
		if err := usbCtrls[0].Detach(dev); err != nil {
			log.Printf("failed to detach usb device: %q", err)
		} else {
			log.Println("detached", dev.UUID())
		}
	}
}

usb.go Outdated Show resolved Hide resolved
@Code-Hex Code-Hex changed the title WIP: support USB Controllers support USB Controllers Jan 5, 2025
@Code-Hex Code-Hex marked this pull request as ready for review January 5, 2025 07:17
@Code-Hex Code-Hex requested review from cfergeau and AkihiroSuda and removed request for cfergeau January 5, 2025 07:17
@Code-Hex Code-Hex merged commit d800419 into main Jan 6, 2025
8 checks passed
@Code-Hex Code-Hex deleted the add/usb branch January 6, 2025 06:01
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support USBDevices
3 participants