Skip to content

Commit

Permalink
json: Add usb-mass-storage support
Browse files Browse the repository at this point in the history
It was missing from the implementation.

Signed-off-by: Christophe Fergeau <[email protected]>
  • Loading branch information
cfergeau authored and baude committed Jun 26, 2023
1 parent e3c6969 commit fb98993
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
32 changes: 24 additions & 8 deletions pkg/config/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ const (
linuxBootloader vmComponentKind = "linuxBootloader"

// VirtIO device kinds
vfNet vmComponentKind = "virtionet"
vfVsock vmComponentKind = "virtiosock"
vfBlk vmComponentKind = "virtioblk"
vfFs vmComponentKind = "virtiofs"
vfRng vmComponentKind = "virtiorng"
vfSerial vmComponentKind = "virtioserial"
vfGpu vmComponentKind = "virtiogpu"
vfInput vmComponentKind = "virtioinput"
vfNet vmComponentKind = "virtionet"
vfVsock vmComponentKind = "virtiosock"
vfBlk vmComponentKind = "virtioblk"
vfFs vmComponentKind = "virtiofs"
vfRng vmComponentKind = "virtiorng"
vfSerial vmComponentKind = "virtioserial"
vfGpu vmComponentKind = "virtiogpu"
vfInput vmComponentKind = "virtioinput"
usbMassStorage vmComponentKind = "usbmassstorage"
)

type jsonKind struct {
Expand Down Expand Up @@ -123,6 +124,10 @@ func unmarshalDevice(rawMsg json.RawMessage) (VirtioDevice, error) {
var newDevice VirtioInput
err = json.Unmarshal(rawMsg, &newDevice)
dev = &newDevice
case usbMassStorage:
var newDevice USBMassStorage
err = json.Unmarshal(rawMsg, &newDevice)
dev = &newDevice
default:
return nil, fmt.Errorf("unknown 'kind' field: '%s'", kind)
}
Expand Down Expand Up @@ -287,3 +292,14 @@ func (dev *VirtioInput) MarshalJSON() ([]byte, error) {
VirtioInput: *dev,
})
}

func (dev *USBMassStorage) MarshalJSON() ([]byte, error) {
type devWithKind struct {
jsonKind
USBMassStorage
}
return json.Marshal(devWithKind{
jsonKind: kind(usbMassStorage),
USBMassStorage: *dev,
})
}
11 changes: 5 additions & 6 deletions pkg/config/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,14 @@ var jsonTests = map[string]jsonTest{
err = vm.AddDevice(dev)
require.NoError(t, err)
// USB mass storage
//dev, err = USBMassStorageNew("/usbmassstorage")
//require.NoError(t, err)
//err = vm.AddDevice(dev)
//require.NoError(t, err)
dev, err = USBMassStorageNew("/usbmassstorage")
require.NoError(t, err)
err = vm.AddDevice(dev)
require.NoError(t, err)

return vm
},
//expectedJSON: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"linuxBootloader","VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"},"devices":[{"kind":"virtioserial","LogFile":"/virtioserial","UsesStdio":false},{"inputType":"keyboard"},{"usesGUI":false,"height":800,"width":600},{"kind":"virtionet","Nat":true,"MacAddress":"ABEiM0RV","Socket":null,"UnixSocketPath":""},{"kind":"virtiorng"},{"kind":"virtioblk","DevName":"virtio-blk","ImagePath":"/virtioblk","ReadOnly":false,"DeviceIdentifier":""},{"kind":"virtiosock","Port":1234,"SocketURL":"/virtiovsock","Listen":false},{"kind":"virtiofs","SharedDir":"/virtiofs","MountTag":"tag"},{"DevName":"usb-mass-storage","ImagePath":"/usbmassstorage","ReadOnly":false}]}`,
expectedJSON: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"linuxBootloader","VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"},"devices":[{"kind":"virtioserial","LogFile":"/virtioserial","UsesStdio":false},{"kind":"virtioinput","inputType":"keyboard"},{"kind":"virtiogpu","usesGUI":false,"height":800,"width":600},{"kind":"virtionet","Nat":true,"MacAddress":"ABEiM0RV","Socket":null,"UnixSocketPath":""},{"kind":"virtiorng"},{"kind":"virtioblk","DevName":"virtio-blk","ImagePath":"/virtioblk","ReadOnly":false,"DeviceIdentifier":""},{"kind":"virtiosock","Port":1234,"SocketURL":"/virtiovsock","Listen":false},{"kind":"virtiofs","SharedDir":"/virtiofs","MountTag":"tag"}]}`,
expectedJSON: `{"vcpus":3,"memoryBytes":4000000000,"bootloader":{"kind":"linuxBootloader","VmlinuzPath":"/vmlinuz","KernelCmdLine":"/initrd","InitrdPath":"console=hvc0"},"devices":[{"kind":"virtioserial","LogFile":"/virtioserial","UsesStdio":false},{"kind":"virtioinput","inputType":"keyboard"},{"kind":"virtiogpu","usesGUI":false,"height":800,"width":600},{"kind":"virtionet","Nat":true,"MacAddress":"ABEiM0RV","Socket":null,"UnixSocketPath":""},{"kind":"virtiorng"},{"kind":"virtioblk","DevName":"virtio-blk","ImagePath":"/virtioblk","ReadOnly":false,"DeviceIdentifier":""},{"kind":"virtiosock","Port":1234,"SocketURL":"/virtiovsock","Listen":false},{"kind":"virtiofs","SharedDir":"/virtiofs","MountTag":"tag"},{"kind":"usbmassstorage","DevName":"usb-mass-storage","ImagePath":"/usbmassstorage","ReadOnly":false}]}`,
},
}

Expand Down

0 comments on commit fb98993

Please sign in to comment.