Skip to content

Commit

Permalink
config: Add VirtualMachine.Cmd()
Browse files Browse the repository at this point in the history
This returns a standard golang exec.Command, and takes care of setting
ExtraFiles as needed, ... Longer term we can add more APIs to start the
vfkit process, monitor it, ...
  • Loading branch information
cfergeau authored and anjannath committed Apr 19, 2023
1 parent 4979be1 commit 223460b
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package config

import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
)
Expand Down Expand Up @@ -75,6 +77,36 @@ func (vm *VirtualMachine) ToCmdLine() ([]string, error) {
return args, nil
}

func (vm *VirtualMachine) extraFiles() []*os.File {
extraFiles := []*os.File{}
for _, dev := range vm.Devices {
virtioNet, ok := dev.(*VirtioNet)
if !ok {
continue
}
if virtioNet.Socket != nil {
extraFiles = append(extraFiles, virtioNet.Socket)
}
}

return extraFiles
}

// Cmd creates an exec.Cmd to start vfkit with the configured devices.
// In particular it will set ExtraFiles appropriately when mapping
// a file with a network interface.
func (vm *VirtualMachine) Cmd(vfkitPath string) (*exec.Cmd, error) {
args, err := vm.ToCmdLine()
if err != nil {
return nil, err
}

cmd := exec.Command(vfkitPath, args...)
cmd.ExtraFiles = vm.extraFiles()

return cmd, nil
}

func (vm *VirtualMachine) AddDevicesFromCmdLine(cmdlineOpts []string) error {
for _, deviceOpts := range cmdlineOpts {
dev, err := deviceFromCmdLine(deviceOpts)
Expand Down

0 comments on commit 223460b

Please sign in to comment.