diff --git a/go.mod b/go.mod index 316f7be7..64218fbc 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4b799594..6903d4df 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/pkg/client/cmdline.go b/pkg/client/cmdline.go index 8a0a4d1c..7bf2925d 100644 --- a/pkg/client/cmdline.go +++ b/pkg/client/cmdline.go @@ -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, @@ -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 + } +} diff --git a/pkg/config/virtio.go b/pkg/config/virtio.go index 11ea4d23..9fbf05b5 100644 --- a/pkg/config/virtio.go +++ b/pkg/config/virtio.go @@ -3,6 +3,7 @@ package config import ( "fmt" "net" + "path/filepath" "strconv" "strings" @@ -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": @@ -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 +}