Skip to content

Commit 5236935

Browse files
committed
Switch to krunkit on macOS
Signed-off-by: German Maglione <[email protected]>
1 parent 8949137 commit 5236935

File tree

1 file changed

+12
-93
lines changed

1 file changed

+12
-93
lines changed

pkg/vm/vm_darwin.go

Lines changed: 12 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package vm
22

33
import (
4-
"errors"
54
"fmt"
6-
"net"
75
"os"
86
"os/exec"
97
"path/filepath"
10-
"time"
8+
"strconv"
119

1210
"github.com/containers/podman-bootc/pkg/config"
1311
"github.com/containers/podman-bootc/pkg/utils"
@@ -56,31 +54,7 @@ func (b *BootcVMMac) CloseConnection() {
5654
}
5755

5856
func (b *BootcVMMac) PrintConsole() (err error) {
59-
//qemu seems to asynchronously create the socket file
60-
//so this will wait up to a few seconds for socket to be created
61-
socketCreationTimeout := 5 * time.Second
62-
elapsed := 0 * time.Millisecond
63-
interval := 100 * time.Millisecond
64-
for elapsed < socketCreationTimeout {
65-
time.Sleep(interval) //always sleep a little bit at the start
66-
elapsed += interval
67-
if _, err = os.Stat(b.socketFile); err == nil {
68-
break
69-
}
70-
}
71-
72-
c, err := net.Dial("unix", b.socketFile)
73-
if err != nil {
74-
return fmt.Errorf("error connecting to socket %s", err)
75-
}
76-
for {
77-
buf := make([]byte, 8192)
78-
_, err := c.Read(buf)
79-
if err != nil {
80-
return fmt.Errorf("error reading socket %s", err)
81-
}
82-
print(string(buf))
83-
}
57+
return nil
8458
}
8559

8660
func (b *BootcVMMac) GetConfig() (cfg *BootcVMConfig, err error) {
@@ -118,48 +92,24 @@ func (b *BootcVMMac) Run(params RunVMParameters) (err error) {
11892
}
11993
}
12094

121-
var args []string
122-
args = append(args, "-display", "none")
123-
args = append(args, "-chardev", fmt.Sprintf("socket,id=char0,server=on,wait=off,path=%s", b.socketFile), "-serial", "chardev:char0")
124-
125-
args = append(args, "-cpu", "host")
126-
args = append(args, "-m", "2G")
127-
args = append(args, "-smp", "2")
128-
args = append(args, "-snapshot")
129-
nicCmd := fmt.Sprintf("user,model=virtio-net-pci,hostfwd=tcp::%d-:22", b.sshPort)
130-
args = append(args, "-nic", nicCmd)
131-
132-
vmPidFile := filepath.Join(b.cacheDir, "run.pid")
133-
args = append(args, "-pidfile", vmPidFile)
134-
135-
vmDiskImage := filepath.Join(b.cacheDir, config.DiskImage)
136-
driveCmd := fmt.Sprintf("if=virtio,format=raw,file=%s", vmDiskImage)
137-
args = append(args, "-drive", driveCmd)
138-
139-
err = b.ParseCloudInit()
95+
execPath, err := os.Executable()
14096
if err != nil {
141-
return err
97+
return fmt.Errorf("getting executable path: %w", err)
14298
}
14399

144-
if b.hasCloudInit {
145-
args = append(args, "-cdrom", b.cloudInitArgs)
146-
}
147-
148-
if b.sshIdentity != "" {
149-
smbiosCmd, err := b.oemString()
150-
if err != nil {
151-
return err
152-
}
153-
154-
args = append(args, "-smbios", smbiosCmd)
100+
execPath, err = filepath.EvalSymlinks(execPath)
101+
if err != nil {
102+
return fmt.Errorf("following executable symlink: %w", err)
155103
}
156104

157-
cmd, err := b.createQemuCommand()
105+
execPath, err = filepath.Abs(execPath)
158106
if err != nil {
159-
return err
107+
return fmt.Errorf("getting executable absolute path: %w", err)
160108
}
161109

162-
cmd.Args = append(cmd.Args, args...)
110+
args := []string{"vmmon", b.imageID, b.vmUsername, b.sshIdentity, strconv.Itoa(b.sshPort)}
111+
cmd := exec.Command(execPath, args...)
112+
163113
logrus.Debugf("Executing: %v", cmd.Args)
164114
cmd.Stdout = os.Stdout
165115
cmd.Stderr = os.Stderr
@@ -214,37 +164,6 @@ func (b *BootcVMMac) Exists() (bool, error) {
214164
return utils.FileExists(b.pidFile)
215165
}
216166

217-
func (b *BootcVMMac) createQemuCommand() (*exec.Cmd, error) {
218-
qemuInstallPath, err := getQemuInstallPath()
219-
if err != nil {
220-
return nil, err
221-
}
222-
223-
path := qemuInstallPath + "/bin/qemu-system-aarch64"
224-
args := []string{
225-
"-accel", "hvf",
226-
"-cpu", "host",
227-
"-M", "virt,highmem=on",
228-
"-drive", "file=" + qemuInstallPath + "/share/qemu/edk2-aarch64-code.fd" + ",if=pflash,format=raw,readonly=on",
229-
}
230-
return exec.Command(path, args...), nil
231-
}
232-
233-
// Search for a qemu binary, let's check if is shipped with podman v4
234-
// or if it's installed using homebrew.
235-
// This function will no longer be necessary as soon as we use libvirt on macos.
236-
func getQemuInstallPath() (string, error) {
237-
dirs := []string{"/opt/homebrew", "/opt/podman/qemu"}
238-
for _, d := range dirs {
239-
qemuBinary := filepath.Join(d, "bin/qemu-system-aarch64")
240-
if _, err := os.Stat(qemuBinary); err == nil {
241-
return d, nil
242-
}
243-
}
244-
245-
return "", errors.New("QEMU binary not found")
246-
}
247-
248167
func (v *BootcVMMac) Unlock() error {
249168
return v.cacheDirLock.Unlock()
250169
}

0 commit comments

Comments
 (0)