Skip to content
Merged
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
6 changes: 2 additions & 4 deletions cmd/vm/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ func (h *Handler) Stop(cmd *cobra.Command, args []string) error {
}
terminate(ctx, r, grace)
quiesceNet(cmd, r)
stopVNCProxy(ctx, dir)
r.PID, r.VNCDisp, r.VNCPass = 0, -1, "" // VNC is launch-scoped: gone with the qemu it belonged to
return saveRec(dir, r)
return saveStopped(ctx, dir, r)
}); err != nil {
return err
}
Expand Down Expand Up @@ -264,6 +262,7 @@ func (h *Handler) launch(cmd *cobra.Command, dir string, r *record) error {
}
pidfile := filepath.Join(dir, "qemu.pid")
args := append(spec.Args(), "-daemonize", "-pidfile", pidfile)
stopVNCProxy(ctx, dir)
ensureNetnsLoopback(ctx, r) // CNI: a fresh netns has lo DOWN, so qemu's -vnc 127.0.0.1 would fail to bind
if r.Netns != "" {
logger.Debugf(ctx, "running qemu in netns %s via `ip netns exec`", filepath.Base(r.Netns))
Expand All @@ -272,7 +271,6 @@ func (h *Handler) launch(cmd *cobra.Command, dir string, r *record) error {
c := launchCmd(r, args) // CNI: wraps in `ip netns exec` so -netdev tap finds the in-netns TAP
c.Stdout, c.Stderr = os.Stdout, os.Stderr
if err := c.Run(); err != nil {
stopVNCProxy(ctx, dir)
return fmt.Errorf("launch qemu: %w", err)
}
pid, err := utils.ReadPIDFile(pidfile)
Expand Down
4 changes: 3 additions & 1 deletion cmd/vm/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (h *Handler) Restore(cmd *cobra.Command, args []string) error {
return fmt.Errorf("vm %q is running; stop it first or pass --force to stop+restore", r.Name)
}
terminate(ctx, r, stopGracePeriod)
r.PID = 0
if err := saveStopped(ctx, dir, r); err != nil {
return err
}
}
tag, _ = cmd.Flags().GetString("tag")
if tag == "" {
Expand Down
7 changes: 7 additions & 0 deletions cmd/vm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ func terminate(ctx context.Context, r *record, grace time.Duration) {
}
}

// VNC is launch-scoped: the proxy and display go with the qemu process.
func saveStopped(ctx context.Context, dir string, r *record) error {
stopVNCProxy(ctx, dir)
r.PID, r.VNCDisp, r.VNCPass = 0, -1, ""
return saveRec(dir, r)
}

func graceFromFlags(cmd *cobra.Command) time.Duration {
if force, _ := cmd.Flags().GetBool("force"); force {
return 0
Expand Down