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

Add readonly option for storage devices #179

Merged
merged 3 commits into from
Aug 22, 2024
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
5 changes: 3 additions & 2 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,13 @@ The `--device usb-mass-storage` option adds a USB mass storage device to the vir

#### Arguments
- `path`: the absolute path to the disk image file.
- `readonly`: if specified the device will be read only.

#### Example

This adds a USB mass storage device to the VM which will be backed by the ISO image at `/Users/virtuser/distro.iso`:
This adds a read only USB mass storage device to the VM which will be backed by the ISO image at `/Users/virtuser/distro.iso`:
```
--device usb-mass-storage,path=/Users/virtuser/distro.iso
--device usb-mass-storage,path=/Users/virtuser/distro.iso,readonly
```


Expand Down
3 changes: 2 additions & 1 deletion pkg/config/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var jsonTests = map[string]jsonTest{
// USB mass storage
usb, err := USBMassStorageNew("/usbmassstorage")
require.NoError(t, err)
usb.SetReadOnly(true)
// rosetta
rosetta, err := RosettaShareNew("vz-rosetta")
require.NoError(t, err)
Expand All @@ -164,7 +165,7 @@ var jsonTests = map[string]jsonTest{

return vm
},
expectedJSON: `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtioserial","logFile":"/virtioserial"},{"kind":"virtioinput","inputType":"keyboard"},{"kind":"virtiogpu","usesGUI":false,"width":800,"height":600},{"kind":"virtionet","nat":true,"macAddress":"00:11:22:33:44:55"},{"kind":"virtiorng"},{"kind":"virtioblk","devName":"virtio-blk","imagePath":"/virtioblk"},{"kind":"virtiosock","port":1234,"socketURL":"/virtiovsock"},{"kind":"virtiofs","mountTag":"tag","sharedDir":"/virtiofs"},{"kind":"usbmassstorage","devName":"usb-mass-storage","imagePath":"/usbmassstorage"},{"kind":"rosetta","mountTag":"vz-rosetta","installRosetta":false}]}`,
expectedJSON: `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtioserial","logFile":"/virtioserial"},{"kind":"virtioinput","inputType":"keyboard"},{"kind":"virtiogpu","usesGUI":false,"width":800,"height":600},{"kind":"virtionet","nat":true,"macAddress":"00:11:22:33:44:55"},{"kind":"virtiorng"},{"kind":"virtioblk","devName":"virtio-blk","imagePath":"/virtioblk"},{"kind":"virtiosock","port":1234,"socketURL":"/virtiovsock"},{"kind":"virtiofs","mountTag":"tag","sharedDir":"/virtiofs"},{"kind":"usbmassstorage","devName":"usb-mass-storage","imagePath":"/usbmassstorage","readOnly":true},{"kind":"rosetta","mountTag":"vz-rosetta","installRosetta":false}]}`,
},
}

Expand Down
17 changes: 15 additions & 2 deletions pkg/config/virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
switch option.key {
case VirtioInputPointingDevice, VirtioInputKeyboardDevice:
if option.value != "" {
return fmt.Errorf(fmt.Sprintf("unexpected value for virtio-input %s option: %s", option.key, option.value))

Check failure on line 273 in pkg/config/virtio.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Errorf (govet)
}
dev.InputType = option.key
default:
Expand Down Expand Up @@ -315,14 +315,14 @@
case VirtioGPUResolutionHeight:
height, err := strconv.Atoi(option.value)
if err != nil || height < 1 {
return fmt.Errorf(fmt.Sprintf("Invalid value for virtio-gpu %s: %s", option.key, option.value))

Check failure on line 318 in pkg/config/virtio.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Errorf (govet)
}

dev.Height = height
case VirtioGPUResolutionWidth:
width, err := strconv.Atoi(option.value)
if err != nil || width < 1 {
return fmt.Errorf(fmt.Sprintf("Invalid value for virtio-gpu %s: %s", option.key, option.value))

Check failure on line 325 in pkg/config/virtio.go

View workflow job for this annotation

GitHub Actions / lint

printf: non-constant format string in call to fmt.Errorf (govet)
}

dev.Width = width
Expand Down Expand Up @@ -667,13 +667,17 @@

// USBMassStorageNew creates a new USB disk to use in the virtual machine. It will use
// the file at imagePath as the disk image. This image must be in raw or ISO format.
func USBMassStorageNew(imagePath string) (VMComponent, error) {
func USBMassStorageNew(imagePath string) (*USBMassStorage, error) {
usbMassStorage := usbMassStorageNewEmpty()
usbMassStorage.ImagePath = imagePath

return usbMassStorage, nil
}

func (dev *USBMassStorage) SetReadOnly(readOnly bool) {
dev.StorageConfig.ReadOnly = readOnly
}

// StorageConfig configures a disk device.
type StorageConfig struct {
DevName string `json:"devName"`
Expand All @@ -685,14 +689,23 @@
if config.ImagePath == "" {
return nil, fmt.Errorf("%s devices need the path to a disk image", config.DevName)
}
return []string{"--device", fmt.Sprintf("%s,path=%s", config.DevName, config.ImagePath)}, nil
value := fmt.Sprintf("%s,path=%s", config.DevName, config.ImagePath)
if config.ReadOnly {
value += ",readonly"
}
return []string{"--device", value}, nil
}

func (config *StorageConfig) FromOptions(options []option) error {
for _, option := range options {
switch option.key {
case "path":
config.ImagePath = option.value
case "readonly":
if option.value != "" {
return fmt.Errorf("unexpected value for virtio-blk 'readonly' option: %s", option.value)
}
config.ReadOnly = true
default:
return fmt.Errorf("unknown option for %s devices: %s", config.DevName, option.key)
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/virtio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ var virtioDevTests = map[string]virtioDevTest{
},
expectedCmdLine: []string{"--device", "usb-mass-storage,path=/foo/bar"},
},
"NewUSBMassStorageReadOnly": {
newDev: func() (VirtioDevice, error) {
dev, err := USBMassStorageNew("/foo/bar")
if err != nil {
return nil, err
}
dev.SetReadOnly(true)
return dev, err
},
expectedDev: &USBMassStorage{
StorageConfig: StorageConfig{
DevName: "usb-mass-storage",
ImagePath: "/foo/bar",
ReadOnly: true,
},
},
expectedCmdLine: []string{"--device", "usb-mass-storage,path=/foo/bar,readonly"},
},
"NewVirtioInputWithPointingDevice": {
newDev: func() (VirtioDevice, error) { return VirtioInputNew("pointing") },
expectedDev: &VirtioInput{
Expand Down
Loading