Skip to content

Commit

Permalink
Add virtiofs device support
Browse files Browse the repository at this point in the history
This makes it possible to share files with the VM. For now it's limited
to one shared directory, which should be enough for our initial needs.

Code-Hex/vz#28 is not merged yet, so this relies
on a Code-Hex/vz fork in my own GitHub namespace which has the shared
folder API.

This fixes crc-org#2
  • Loading branch information
cfergeau committed Jul 25, 2022
1 parent 658f600 commit ebb1e11
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ require (
golang.org/x/sys v0.0.0-20220519141025-dcacdad47464
inet.af/tcpproxy v0.0.0-20210824174053-2e577fef49e2
)

replace github.com/Code-Hex/vz => github.com/cfergeau/vz v0.0.5-0.20220629154958-9aad23fce70e
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Code-Hex/vz v0.0.5-0.20220406150231-a2ebc854a261 h1:7cU//dP3rZzrIdk1ytc2h5/N3ikjVJljvb5KbU7+lGs=
github.com/Code-Hex/vz v0.0.5-0.20220406150231-a2ebc854a261/go.mod h1:BMgSoDpuZx8xZe5Ycxf5DJ5OewlWadVu40v/O1ZmVuQ=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
Expand All @@ -72,6 +70,8 @@ github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cfergeau/vz v0.0.5-0.20220629154958-9aad23fce70e h1:W34omdFFqjYSMud3wFeomOWqG9fO6MJ8hcADzEK4RKk=
github.com/cfergeau/vz v0.0.5-0.20220629154958-9aad23fce70e/go.mod h1:BMgSoDpuZx8xZe5Ycxf5DJ5OewlWadVu40v/O1ZmVuQ=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down
23 changes: 23 additions & 0 deletions pkg/client/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type virtioSerial struct {
logFile string
}

type virtioFs struct {
sharedDir string
mountTag string
}

func NewVirtualMachine(vcpus uint, memoryBytes uint64, bootloader *Bootloader) *VirtualMachine {
return &VirtualMachine{
vcpus: vcpus,
Expand Down Expand Up @@ -194,3 +199,21 @@ func (dev *virtioSerial) ToCmdLine() ([]string, error) {
}
return []string{"--device", fmt.Sprintf("virtio-serial,logFilePath=%s", dev.logFile)}, nil
}

func VirtioFsNew(sharedDir string, mountTag string) (VirtioDevice, error) {
return &virtioFs{
sharedDir: sharedDir,
mountTag: mountTag,
}, nil
}

func (dev *virtioFs) ToCmdLine() ([]string, error) {
if dev.sharedDir == "" {
return nil, fmt.Errorf("virtio-fs needs the path to the directory to share")
}
if dev.mountTag != "" {
return []string{"--device", fmt.Sprintf("virtio-fs,sharedDir=%s,mountTag=%s", dev.sharedDir, dev.mountTag)}, nil
} else {
return []string{"--device", fmt.Sprintf("virtio-fs,sharedDir=%s", dev.sharedDir)}, nil
}
}
50 changes: 50 additions & 0 deletions pkg/config/virtio.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"net"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -73,6 +74,8 @@ func deviceFromCmdLine(deviceOpts string) (VirtioDevice, error) {
switch opts[0] {
case "virtio-blk":
dev = &virtioBlk{}
case "virtio-fs":
dev = &virtioFs{}
case "virtio-net":
dev = &virtioNet{}
case "virtio-rng":
Expand Down Expand Up @@ -249,3 +252,50 @@ func (dev *VirtioVsock) AddToVirtualMachineConfig(vmConfig *vz.VirtualMachineCon

return nil
}

type virtioFs struct {
sharedDir string
mountTag string
}

func (dev *virtioFs) FromOptions(options []option) error {
for _, option := range options {
switch option.key {
case "sharedDir":
dev.sharedDir = option.value
case "mountTag":
dev.mountTag = option.value
default:
return fmt.Errorf("Unknown option for virtio-fs devices: %s", option.key)
}
}
return nil
}

func (dev *virtioFs) AddToVirtualMachineConfig(vmConfig *vz.VirtualMachineConfiguration) error {
log.Infof("Adding virtio-fs device")
if dev.sharedDir == "" {
return fmt.Errorf("missing mandatory 'sharedDir' option for virtio-fs device")
}
var mountTag string
if dev.mountTag != "" {
mountTag = dev.mountTag
} else {
mountTag = filepath.Base(dev.sharedDir)
}

sharedDir, err := vz.NewSharedDirectory(dev.sharedDir, false)
if err != nil {
return err
}
sharedDirConfig := vz.NewSingleDirectoryShare(sharedDir)
fileSystemDeviceConfig, err := vz.NewVirtioFileSystemDeviceConfiguration(mountTag)
if err != nil {
return err
}
fileSystemDeviceConfig.SetDirectoryShare(sharedDirConfig)
vmConfig.SetDirectorySharingDevicesVirtualMachineConfiguration([]vz.DirectorySharingDeviceConfiguration{
fileSystemDeviceConfig,
})
return nil
}

0 comments on commit ebb1e11

Please sign in to comment.