|
1 | 1 | package vm
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "errors" |
5 | 4 | "fmt"
|
6 |
| - "net" |
7 | 5 | "os"
|
8 | 6 | "os/exec"
|
9 | 7 | "path/filepath"
|
10 |
| - "time" |
| 8 | + "strconv" |
11 | 9 |
|
12 | 10 | "github.com/containers/podman-bootc/pkg/config"
|
13 | 11 | "github.com/containers/podman-bootc/pkg/utils"
|
@@ -56,31 +54,7 @@ func (b *BootcVMMac) CloseConnection() {
|
56 | 54 | }
|
57 | 55 |
|
58 | 56 | 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 |
84 | 58 | }
|
85 | 59 |
|
86 | 60 | func (b *BootcVMMac) GetConfig() (cfg *BootcVMConfig, err error) {
|
@@ -118,48 +92,24 @@ func (b *BootcVMMac) Run(params RunVMParameters) (err error) {
|
118 | 92 | }
|
119 | 93 | }
|
120 | 94 |
|
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() |
140 | 96 | if err != nil {
|
141 |
| - return err |
| 97 | + return fmt.Errorf("getting executable path: %w", err) |
142 | 98 | }
|
143 | 99 |
|
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) |
155 | 103 | }
|
156 | 104 |
|
157 |
| - cmd, err := b.createQemuCommand() |
| 105 | + execPath, err = filepath.Abs(execPath) |
158 | 106 | if err != nil {
|
159 |
| - return err |
| 107 | + return fmt.Errorf("getting executable absolute path: %w", err) |
160 | 108 | }
|
161 | 109 |
|
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 | + |
163 | 113 | logrus.Debugf("Executing: %v", cmd.Args)
|
164 | 114 | cmd.Stdout = os.Stdout
|
165 | 115 | cmd.Stderr = os.Stderr
|
@@ -214,37 +164,6 @@ func (b *BootcVMMac) Exists() (bool, error) {
|
214 | 164 | return utils.FileExists(b.pidFile)
|
215 | 165 | }
|
216 | 166 |
|
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 |
| - |
248 | 167 | func (v *BootcVMMac) Unlock() error {
|
249 | 168 | return v.cacheDirLock.Unlock()
|
250 | 169 | }
|
0 commit comments