Skip to content

Create a RPC framework #1

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
vmType: null

# Arch: "default", "x86_64", "aarch64".
# 🟢 Builtin default: "default" (corresponds to the host architecture)
arch: null

# OpenStack-compatible disk image.
# 🟢 Builtin default: none (must be specified)
# 🔵 This file: Ubuntu images
images:
# Try to use release-yyyyMMdd image if available. Note that release-yyyyMMdd will be removed after several months.
- location: "https://cloud-images.ubuntu.com/releases/24.10/release-20250129/ubuntu-24.10-server-cloudimg-amd64.img"
arch: "x86_64"
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/lima-vm/lima

go 1.23.0

replace github.com/lima-vm/lima => /lima //my local path

require (
al.essio.dev/pkg/shellescape v1.5.1
github.com/AlecAivazis/survey/v2 v2.3.7
Expand Down Expand Up @@ -139,3 +141,5 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

require github.com/saz97/lima v0.8.2
2 changes: 1 addition & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,4 @@ sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3/go.mod h1:18nIHnGi6636UCz6m8
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 h1:nbCitCK2hfnhyiKo6uf2HxUPTCodY6Qaf85SbDIaMBk=
sigs.k8s.io/structured-merge-diff/v4 v4.5.0/go.mod h1:N8f93tFZh9U6vpxwRArLiikrE5/2tiu1w1AGfACIGE4=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY=
18 changes: 18 additions & 0 deletions pkg/plugin/rpc_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package plugin

type VMDriver interface {
// Start starts the VM.
Start(args StartArgs) (string, error)
}

type StartArgs struct {
InstanceName string
ConfigData []byte
InstanceDir string
SSHLocalPort int
SSHAddress string
}

type StopArgs struct {
InstanceName string
}
197 changes: 65 additions & 132 deletions pkg/qemu/qemu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"net"
"net/rpc"
"os"
"os/exec"
"path/filepath"
Expand All @@ -26,9 +26,11 @@ import (
"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/networks/usernet"
"github.com/lima-vm/lima/pkg/plugin"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

type LimaQemuDriver struct {
Expand All @@ -37,12 +39,51 @@ type LimaQemuDriver struct {
qWaitCh chan error

vhostCmds []*exec.Cmd

client *rpc.Client
ProcessId string
}

func New(driver *driver.BaseDriver) *LimaQemuDriver {
return &LimaQemuDriver{
l := &LimaQemuDriver{
BaseDriver: driver,
}

cmd := exec.Command("/lima/qemu-plugin/lima-qemu-plugin")

if err := cmd.Start(); err != nil {
logrus.Errorf("[LimaQemuDriver] failed to start external driver: %v", err)
return nil
}

if err := waitForPlugin("127.0.0.1:9991", 10*time.Second); err != nil {
logrus.Errorf("[LimaQemuDriver] failed to wait for plugin: %v", err)
return nil
}

client, err := rpc.Dial("tcp", "127.0.0.1:9991")
if err != nil {
logrus.Errorf("[LimaQemuDriver] failed to dial driver RPC: %v", err)
return nil
}
logrus.Info("[LimaQemuDriver] connected to driver RPC")
l.client = client
l.ProcessId = ""

return l
}

func waitForPlugin(address string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
conn, err := net.DialTimeout("tcp", address, 1*time.Second)
if err == nil {
conn.Close()
return nil
}
time.Sleep(500 * time.Millisecond) // Retry interval
}
return errors.New("timeout waiting for plugin to start")
}

func (l *LimaQemuDriver) Validate() error {
Expand All @@ -63,142 +104,34 @@ func (l *LimaQemuDriver) CreateDisk(ctx context.Context) error {
}

func (l *LimaQemuDriver) Start(ctx context.Context) (chan error, error) {
ctx, cancel := context.WithCancel(ctx)
defer func() {
if l.qCmd == nil {
cancel()
}
}()

qCfg := Config{
Name: l.Instance.Name,
InstanceDir: l.Instance.Dir,
LimaYAML: l.Instance.Config,
SSHLocalPort: l.SSHLocalPort,
SSHAddress: l.Instance.SSHAddress,
}
qExe, qArgs, err := Cmdline(ctx, qCfg)
if err != nil {
return nil, err
}

var vhostCmds []*exec.Cmd
if *l.Instance.Config.MountType == limayaml.VIRTIOFS {
vhostExe, err := FindVirtiofsd(qExe)
if err != nil {
return nil, err
}

for i := range l.Instance.Config.Mounts {
args, err := VirtiofsdCmdline(qCfg, i)
if err != nil {
return nil, err
}

vhostCmds = append(vhostCmds, exec.CommandContext(ctx, vhostExe, args...))
}
}

var qArgsFinal []string
applier := &qArgTemplateApplier{}
for _, unapplied := range qArgs {
applied, err := applier.applyTemplate(unapplied)
if err != nil {
return nil, err
}
qArgsFinal = append(qArgsFinal, applied)
}
qCmd := exec.CommandContext(ctx, qExe, qArgsFinal...)
qCmd.ExtraFiles = append(qCmd.ExtraFiles, applier.files...)
qStdout, err := qCmd.StdoutPipe()
if err != nil {
return nil, err
}
go logPipeRoutine(qStdout, "qemu[stdout]")
qStderr, err := qCmd.StderrPipe()
if err != nil {
return nil, err
}
go logPipeRoutine(qStderr, "qemu[stderr]")
ch := make(chan error, 1)

for i, vhostCmd := range vhostCmds {
vhostStdout, err := vhostCmd.StdoutPipe()
if err != nil {
return nil, err
}
go logPipeRoutine(vhostStdout, fmt.Sprintf("virtiofsd-%d[stdout]", i))
vhostStderr, err := vhostCmd.StderrPipe()
go func() {
defer close(ch)
configData, err := yaml.Marshal(l.Instance.Config)
if err != nil {
return nil, err
}
go logPipeRoutine(vhostStderr, fmt.Sprintf("virtiofsd-%d[stderr]", i))
}

for i, vhostCmd := range vhostCmds {
logrus.Debugf("vhostCmd[%d].Args: %v", i, vhostCmd.Args)
if err := vhostCmd.Start(); err != nil {
return nil, err
}

vhostWaitCh := make(chan error)
go func() {
vhostWaitCh <- vhostCmd.Wait()
}()

vhostSock := filepath.Join(l.Instance.Dir, fmt.Sprintf(filenames.VhostSock, i))
vhostSockExists := false
for attempt := 0; attempt < 5; attempt++ {
logrus.Debugf("Try waiting for %s to appear (attempt %d)", vhostSock, attempt)

if _, err := os.Stat(vhostSock); err != nil {
if !errors.Is(err, fs.ErrNotExist) {
logrus.Warnf("Failed to check for vhost socket: %v", err)
}
} else {
vhostSockExists = true
break
}

retry := time.NewTimer(200 * time.Millisecond)
select {
case err = <-vhostWaitCh:
return nil, fmt.Errorf("virtiofsd never created vhost socket: %w", err)
case <-retry.C:
}
return
}

if !vhostSockExists {
return nil, fmt.Errorf("vhost socket %s never appeared", vhostSock)
logrus.Info("[LimaQemuDriver]Starting QEMU")
args := plugin.StartArgs{
InstanceName: l.Instance.Name,
ConfigData: configData,
InstanceDir: l.Instance.Dir,
SSHLocalPort: l.SSHLocalPort,
SSHAddress: l.Instance.SSHAddress,
}

go func() {
if err := <-vhostWaitCh; err != nil {
logrus.Errorf("Error from virtiofsd instance #%d: %v", i, err)
}
}()
}

logrus.Infof("Starting QEMU (hint: to watch the boot progress, see %q)", filepath.Join(qCfg.InstanceDir, "serial*.log"))
logrus.Debugf("qCmd.Args: %v", qCmd.Args)
if err := qCmd.Start(); err != nil {
return nil, err
}
l.qCmd = qCmd
l.qWaitCh = make(chan error)
go func() {
l.qWaitCh <- qCmd.Wait()
}()
l.vhostCmds = vhostCmds
go func() {
if usernetIndex := limayaml.FirstUsernetIndex(l.Instance.Config); usernetIndex != -1 {
client := usernet.NewClientByName(l.Instance.Config.Networks[usernetIndex].Lima)
err := client.ConfigureDriver(ctx, l.BaseDriver)
if err != nil {
l.qWaitCh <- err
}
logrus.Infof("[LimaQemuDriver]Starting VM: %s", args.InstanceName)
var reply string
if err := l.client.Call("QemuPlugin.Start", args, &reply); err != nil {
ch <- fmt.Errorf("RPC call failed: %v", err)
return
}
l.ProcessId = reply
logrus.Infof("[LimaQemuDriver]Started QEMU process: %s", l.ProcessId)
ch <- nil
}()
return l.qWaitCh, nil
logrus.Info("QEMU started")
return ch, nil
}

func (l *LimaQemuDriver) Stop(ctx context.Context) error {
Expand Down
77 changes: 77 additions & 0 deletions qemu-plugin/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module github.com/saz97/lima-qemu-plugin

go 1.23.0

require github.com/lima-vm/lima v0.8.2

require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/a8m/envsubst v1.4.2 // indirect
github.com/alecthomas/participle/v2 v2.1.1 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/balajiv113/fd v0.0.0-20230330094840-143eec500f3e // indirect
github.com/bmatcuk/doublestar/v4 v4.7.1 // indirect
github.com/braydonk/yaml v0.9.0 // indirect
github.com/cheggaaa/pb/v3 v3.1.7 // indirect
github.com/containerd/containerd v1.7.26 // indirect
github.com/containerd/continuity v0.4.5 // indirect
github.com/containerd/errdefs v1.0.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containers/gvisor-tap-vsock v0.8.3 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/digitalocean/go-libvirt v0.0.0-20220804181439-8648fbde413e // indirect
github.com/digitalocean/go-qemu v0.0.0-20221209210016-f035778c97f7 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/diskfs/go-diskfs v1.5.1 // indirect
github.com/djherbis/times v1.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/elliotchance/orderedmap v1.7.1 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/goccy/go-json v0.10.4 // indirect
github.com/goccy/go-yaml v1.15.23 // indirect
github.com/google/btree v1.1.3 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/yamlfmt v0.16.0 // indirect
github.com/insomniacslk/dhcp v0.0.0-20240710054256-ddd8a41251c9 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/lima-vm/go-qcow2reader v0.6.0 // indirect
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2 // indirect
github.com/magiconair/properties v1.8.9 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/mdlayher/vsock v1.2.1 // indirect
github.com/miekg/dns v1.1.63 // indirect
github.com/mikefarah/yq/v4 v4.45.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 // indirect
github.com/sirupsen/logrus v1.9.4-0.20230606125235-dd1b4c2e81af // indirect
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect
github.com/yuin/gopher-lua v1.1.1 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.36.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.7.0 // indirect
golang.org/x/tools v0.28.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
google.golang.org/grpc v1.71.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gvisor.dev/gvisor v0.0.0-20240916094835-a174eb65023f // indirect
)

replace github.com/lima-vm/lima => /lima
Loading