Skip to content

Commit

Permalink
Merge pull request #112 from cfergeau/vzvm
Browse files Browse the repository at this point in the history
Add vf.VirtualMachine and vf.VirtualMachineConfiguration types
  • Loading branch information
praveenkumar authored Mar 15, 2024
2 parents 4bb16bd + c0e9175 commit e04ae65
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 125 deletions.
14 changes: 5 additions & 9 deletions cmd/vfkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func newVMConfiguration(opts *cmdline.Options) (*config.VirtualMachine, error) {
return vmConfig, nil
}

func waitForVMState(vm *vz.VirtualMachine, state vz.VirtualMachineState, timeout <-chan time.Time) error {
func waitForVMState(vm *vf.VirtualMachine, state vz.VirtualMachineState, timeout <-chan time.Time) error {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGPIPE)

Expand All @@ -113,34 +113,30 @@ func waitForVMState(vm *vz.VirtualMachine, state vz.VirtualMachineState, timeout
func runVFKit(vmConfig *config.VirtualMachine, opts *cmdline.Options) error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
vzVMConfig, err := vf.ToVzVirtualMachineConfig(vmConfig)
if err != nil {
return err
}

gpuDevs := vmConfig.VirtioGPUDevices()
if opts.UseGUI && len(gpuDevs) > 0 {
gpuDevs[0].UsesGUI = true
}

vm, err := vz.NewVirtualMachine(vzVMConfig)
vfVM, err := vf.NewVirtualMachine(*vmConfig)
if err != nil {
return err
}

// Do not enable the rests server if user sets scheme to None
if opts.RestfulURI != cmdline.DefaultRestfulURI {
restVM := restvf.NewVzVirtualMachine(vm, vzVMConfig, vmConfig)
restVM := restvf.NewVzVirtualMachine(vfVM)
srv, err := rest.NewServer(restVM, restVM, opts.RestfulURI)
if err != nil {
return err
}
srv.Start()
}
return runVirtualMachine(vmConfig, vm)
return runVirtualMachine(vmConfig, vfVM)
}

func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vz.VirtualMachine) error {
func runVirtualMachine(vmConfig *config.VirtualMachine, vm *vf.VirtualMachine) error {
if err := vm.Start(); err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions cmd/vfkit/timesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net"
"time"

"github.com/Code-Hex/vz/v3"
"github.com/crc-org/vfkit/pkg/config"
"github.com/crc-org/vfkit/pkg/vf"
sleepnotifier "github.com/prashantgupta24/mac-sleep-notifier/notifier"
Expand Down Expand Up @@ -34,7 +33,7 @@ func syncGuestTime(conn net.Conn) error {
return nil
}

func watchWakeupNotifications(vm *vz.VirtualMachine, vsockPort uint) {
func watchWakeupNotifications(vm *vf.VirtualMachine, vsockPort uint) {
var vsockConn net.Conn
defer func() {
if vsockConn != nil {
Expand All @@ -60,10 +59,9 @@ func watchWakeupNotifications(vm *vz.VirtualMachine, vsockPort uint) {
}
}
}

}

func setupGuestTimeSync(vm *vz.VirtualMachine, timesync *config.TimeSync) error {
func setupGuestTimeSync(vm *vf.VirtualMachine, timesync *config.TimeSync) error {
if timesync == nil {
return nil
}
Expand Down
55 changes: 6 additions & 49 deletions pkg/rest/vf/state_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rest
import (
"fmt"

"github.com/Code-Hex/vz/v3"
"github.com/crc-org/vfkit/pkg/rest/define"
"github.com/sirupsen/logrus"
)
Expand All @@ -15,61 +14,19 @@ func (vm *VzVirtualMachine) ChangeState(newState define.StateChange) error {
)
switch newState {
case define.Pause:
logrus.Debug("pausing virtual machine")
response = vm.Pause()
case define.Resume:
logrus.Debug("resuming machine")
response = vm.Resume()
case define.Stop:
response = vm.Stop()
logrus.Debug("stopping machine")
_, response = vm.RequestStop()
case define.HardStop:
response = vm.HardStop()
logrus.Debug("force stopping machine")
response = vm.Stop()
default:
logrus.Error(response)
return fmt.Errorf("invalid new VMState: %s", newState)
}
return response
}

// GetState returns state of the VM
func (vm *VzVirtualMachine) GetState() vz.VirtualMachineState {
return vm.VzVM.State()
}

func (vm *VzVirtualMachine) Pause() error {
logrus.Debug("pausing virtual machine")
return vm.VzVM.Pause()
}

func (vm *VzVirtualMachine) Resume() error {
logrus.Debug("resuming machine")
return vm.VzVM.Resume()
}

func (vm *VzVirtualMachine) Stop() error {
logrus.Debug("stopping machine")
_, err := vm.VzVM.RequestStop()
return err
}
func (vm *VzVirtualMachine) HardStop() error {
logrus.Debug("force stopping machine")
return vm.VzVM.Stop()
}

func (vm *VzVirtualMachine) CanStart() bool {
return vm.VzVM.CanStart()
}

func (vm *VzVirtualMachine) CanPause() bool {
return vm.VzVM.CanPause()
}

func (vm *VzVirtualMachine) CanResume() bool {
return vm.VzVM.CanResume()
}

func (vm *VzVirtualMachine) CanStop() bool {
return vm.VzVM.CanRequestStop()
}

func (vm *VzVirtualMachine) CanHardStop() bool {
return vm.VzVM.CanStop()
}
19 changes: 8 additions & 11 deletions pkg/rest/vf/vm_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,36 @@ package rest
import (
"net/http"

"github.com/Code-Hex/vz/v3"
"github.com/crc-org/vfkit/pkg/config"
"github.com/crc-org/vfkit/pkg/rest/define"
"github.com/crc-org/vfkit/pkg/vf"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

type VzVirtualMachine struct {
VzVM *vz.VirtualMachine
config *vz.VirtualMachineConfiguration
vmConfig *config.VirtualMachine
*vf.VirtualMachine
}

func NewVzVirtualMachine(vm *vz.VirtualMachine, config *vz.VirtualMachineConfiguration, vmConfig *config.VirtualMachine) *VzVirtualMachine {
return &VzVirtualMachine{config: config, VzVM: vm, vmConfig: vmConfig}
func NewVzVirtualMachine(vm *vf.VirtualMachine) *VzVirtualMachine {
return &VzVirtualMachine{vm}
}

// Inspect returns information about the virtual machine like hw resources
// and devices
func (vm *VzVirtualMachine) Inspect(c *gin.Context) {
c.JSON(http.StatusOK, vm.vmConfig)
c.JSON(http.StatusOK, vm.Config())
}

// GetVMState retrieves the current vm state
func (vm *VzVirtualMachine) GetVMState(c *gin.Context) {
current := vm.GetState()
current := vm.State()
c.JSON(http.StatusOK, gin.H{
"state": current.String(),
"canStart": vm.CanStart(),
"canPause": vm.CanPause(),
"canResume": vm.CanResume(),
"canStop": vm.CanStop(),
"canHardStop": vm.CanHardStop(),
"canStop": vm.CanRequestStop(),
"canHardStop": vm.CanStop(),
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/vf/bootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func toVzEFIBootloader(bootloader *config.EFIBootloader) (vz.BootLoader, error)
)
}

func ToVzBootloader(bootloader config.Bootloader) (vz.BootLoader, error) {
func toVzBootloader(bootloader config.Bootloader) (vz.BootLoader, error) {
switch b := bootloader.(type) {
case *config.LinuxBootloader:
return toVzLinuxBootloader(b)
Expand Down
2 changes: 1 addition & 1 deletion pkg/vf/rosetta_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import (
"fmt"
)

func (dev *RosettaShare) AddToVirtualMachineConfig(_ *vzVirtualMachineConfiguration) error {
func (dev *RosettaShare) AddToVirtualMachineConfig(_ *VirtualMachineConfiguration) error {
return fmt.Errorf("rosetta is unsupported on non-arm64 platforms")
}
2 changes: 1 addition & 1 deletion pkg/vf/rosetta_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (dev *RosettaShare) toVz() (vz.DirectorySharingDeviceConfiguration, error)
return config, nil
}

func (dev *RosettaShare) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *RosettaShare) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
fileSystemDeviceConfig, err := dev.toVz()
if err != nil {
return err
Expand Down
26 changes: 13 additions & 13 deletions pkg/vf/virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (dev *NVMExpressController) toVz() (vz.StorageDeviceConfiguration, error) {
return devConfig, nil
}

func (dev *NVMExpressController) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *NVMExpressController) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
storageDeviceConfig, err := dev.toVz()
if err != nil {
return err
Expand Down Expand Up @@ -69,7 +69,7 @@ func (dev *VirtioBlk) toVz() (vz.StorageDeviceConfiguration, error) {
return devConfig, nil
}

func (dev *VirtioBlk) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *VirtioBlk) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
storageDeviceConfig, err := dev.toVz()
if err != nil {
return err
Expand Down Expand Up @@ -99,7 +99,7 @@ func (dev *VirtioInput) toVz() (interface{}, error) {
return inputConfig, nil
}

func (dev *VirtioInput) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *VirtioInput) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
inputDeviceConfig, err := dev.toVz()
if err != nil {
return err
Expand All @@ -117,7 +117,7 @@ func (dev *VirtioInput) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConf
return nil
}

func (dev *VirtioGPU) toVZ() (vz.GraphicsDeviceConfiguration, error) {
func (dev *VirtioGPU) toVz() (vz.GraphicsDeviceConfiguration, error) {
gpuDeviceConfig, err := vz.NewVirtioGraphicsDeviceConfiguration()
if err != nil {
return nil, fmt.Errorf("failed to initialize virtio graphic device: %w", err)
Expand All @@ -133,8 +133,8 @@ func (dev *VirtioGPU) toVZ() (vz.GraphicsDeviceConfiguration, error) {
return gpuDeviceConfig, nil
}

func (dev *VirtioGPU) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
gpuDeviceConfig, err := dev.toVZ()
func (dev *VirtioGPU) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
gpuDeviceConfig, err := dev.toVz()
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (dev *VirtioFs) toVz() (vz.DirectorySharingDeviceConfiguration, error) {
return fileSystemDeviceConfig, nil
}

func (dev *VirtioFs) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *VirtioFs) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
fileSystemDeviceConfig, err := dev.toVz()
if err != nil {
return err
Expand All @@ -188,7 +188,7 @@ func (dev *VirtioRng) toVz() (*vz.VirtioEntropyDeviceConfiguration, error) {
return vz.NewVirtioEntropyDeviceConfiguration()
}

func (dev *VirtioRng) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *VirtioRng) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
log.Infof("Adding virtio-rng device")
entropyConfig, err := dev.toVz()
if err != nil {
Expand Down Expand Up @@ -235,9 +235,9 @@ func (dev *VirtioSerial) toVz() (*vz.VirtioConsoleDeviceSerialPortConfiguration,
}

return vz.NewVirtioConsoleDeviceSerialPortConfiguration(serialPortAttachment)

}
func (dev *VirtioSerial) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {

func (dev *VirtioSerial) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
if dev.LogFile != "" {
log.Infof("Adding virtio-serial device (logFile: %s)", dev.LogFile)
}
Expand All @@ -254,7 +254,7 @@ func (dev *VirtioSerial) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineCon
return nil
}

func (dev *VirtioVsock) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *VirtioVsock) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
if len(vmConfig.socketDevicesConfiguration) != 0 {
log.Debugf("virtio-vsock device already present, not adding a second one")
return nil
Expand All @@ -269,7 +269,7 @@ func (dev *VirtioVsock) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConf
return nil
}

func AddToVirtualMachineConfig(dev config.VirtioDevice, vmConfig *vzVirtualMachineConfiguration) error {
func AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration, dev config.VirtioDevice) error {
switch d := dev.(type) {
case *config.USBMassStorage:
return (*USBMassStorage)(d).AddToVirtualMachineConfig(vmConfig)
Expand Down Expand Up @@ -317,7 +317,7 @@ func (dev *USBMassStorage) toVz() (vz.StorageDeviceConfiguration, error) {
return vz.NewUSBMassStorageDeviceConfiguration(attachment)
}

func (dev *USBMassStorage) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *USBMassStorage) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
storageDeviceConfig, err := dev.toVz()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/vf/virtionet.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (dev *VirtioNet) toVz() (*vz.VirtioNetworkDeviceConfiguration, error) {
return networkConfig, nil
}

func (dev *VirtioNet) AddToVirtualMachineConfig(vmConfig *vzVirtualMachineConfiguration) error {
func (dev *VirtioNet) AddToVirtualMachineConfig(vmConfig *VirtualMachineConfiguration) error {
log.Infof("Adding virtio-net device (nat: %t macAddress: [%s])", dev.Nat, dev.MacAddress)
if dev.Socket != nil {
log.Infof("Using fd %d", dev.Socket.Fd())
Expand Down
Loading

0 comments on commit e04ae65

Please sign in to comment.