Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

allow graceful shutdown on 'ops run' if qmp is passed #1659

Merged
merged 4 commits into from
Sep 17, 2024
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
7 changes: 7 additions & 0 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func RunCommand() *cobra.Command {
PersistNightlyCommandFlags(persistentFlags)
PersistNanosVersionCommandFlags(persistentFlags)

persistentFlags.Bool("qmp", false, "qmp [local only]")

return cmdRun
}

Expand Down Expand Up @@ -61,6 +63,11 @@ func runCommandHandler(cmd *cobra.Command, args []string) {
exitWithError(err.Error())
}

qmp, _ := cmd.Flags().GetBool("qmp")
if qmp {
c.RunConfig.QMP = true
}

if c.Mounts != nil {
err = onprem.AddVirtfsShares(c)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cmd/run_local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func RunLocalInstance(c *types.Config) (err error) {

c.RunConfig.Kernel = c.Kernel

if c.RunConfig.QMP {
c.RunConfig.Mgmt = qemu.GenMgmtPort()
}

fmt.Printf("booting %s ...\n", c.RunConfig.ImageName)
hypervisor.Start(&c.RunConfig)

Expand Down
37 changes: 4 additions & 33 deletions provider/onprem/onprem_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"os"
"os/exec"
Expand All @@ -32,13 +31,6 @@ func (p *OnPrem) CreateInstancePID(ctx *lepton.Context) (string, error) {
return p.createInstance(ctx)
}

// genMgmtPort should generate mgmt before launching instance and
// persist in instance metadata
func genMgmtPort() string {
dd := rand.Int31n(10000) + 40000
return strconv.Itoa(int(dd))
}

func (p *OnPrem) createInstance(ctx *lepton.Context) (string, error) {
c := ctx.Config()

Expand Down Expand Up @@ -106,7 +98,7 @@ func (p *OnPrem) createInstance(ctx *lepton.Context) (string, error) {
c.RunConfig.ImageName = imgpath
c.RunConfig.Background = true

c.RunConfig.Mgmt = genMgmtPort()
c.RunConfig.Mgmt = qemu.GenMgmtPort()

err := hypervisor.Start(&c.RunConfig)
if err != nil {
Expand Down Expand Up @@ -686,31 +678,10 @@ func (p *OnPrem) StartInstance(ctx *lepton.Context, instancename string) error {
`{ "execute": "cont" }`,
}

executeQMP(commands, last)
qemu.ExecuteQMP(commands, last)
return nil
}

func executeQMP(commands []string, last string) {
c, err := net.Dial("tcp", "localhost:"+last)
if err != nil {
fmt.Println(err)
}
defer c.Close()

for i := 0; i < len(commands); i++ {
_, err := c.Write([]byte(commands[i] + "\n"))
if err != nil {
fmt.Println(err)
}
received := make([]byte, 1024)
_, err = c.Read(received)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}

type qmpResponse struct {
qmpReturn `json:"return"`
}
Expand Down Expand Up @@ -779,7 +750,7 @@ func (p *OnPrem) RebootInstance(ctx *lepton.Context, instancename string) error
`{ "execute": "system_reset" }`,
}

executeQMP(commands, last)
qemu.ExecuteQMP(commands, last)

return nil
}
Expand All @@ -798,7 +769,7 @@ func (p *OnPrem) StopInstance(ctx *lepton.Context, instancename string) error {
`{ "execute": "stop" }`,
}

executeQMP(commands, last)
qemu.ExecuteQMP(commands, last)
return nil
}

Expand Down
3 changes: 2 additions & 1 deletion provider/onprem/onprem_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/nanovms/ops/lepton"
"github.com/nanovms/ops/log"
"github.com/nanovms/ops/qemu"
"github.com/nanovms/ops/types"
)

Expand Down Expand Up @@ -109,7 +110,7 @@ func (op *OnPrem) AttachVolume(ctx *lepton.Context, instanceName string, volumeN
deviceAddCmd,
}

executeQMP(commands, last)
qemu.ExecuteQMP(commands, last)

return nil
}
Expand Down
29 changes: 29 additions & 0 deletions qemu/qemu_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
"bufio"
"bytes"
"crypto/rand"
mrand "math/rand"
"time"

"errors"
"fmt"
"os"
Expand All @@ -26,6 +29,13 @@ import (
"github.com/nanovms/ops/types"
)

// GenMgmtPort should generate mgmt before launching instance and
// persist in instance metadata
func GenMgmtPort() string {
dd := mrand.Int31n(10000) + 40000
return strconv.Itoa(int(dd))
}

func qemuBaseCommand() string {
x86 := "qemu-system-x86_64"
arm := "qemu-system-aarch64"
Expand Down Expand Up @@ -65,6 +75,7 @@ type qemu struct {
flags []string
kernel string
atExitHook string
mgmt string // not sure why we have this as string..
}

func newQemu() Hypervisor {
Expand All @@ -77,6 +88,17 @@ func (q *qemu) SetKernel(kernel string) {

func (q *qemu) Stop() {
if q.cmd != nil {

commands := []string{
`{ "execute": "qmp_capabilities" }`,
`{ "execute": "system_powerdown" }`,
}

if q.mgmt != "" {
ExecuteQMP(commands, q.mgmt)
time.Sleep(2 * time.Second)
}

if err := q.cmd.Process.Kill(); err != nil {
log.Error(err)
}
Expand Down Expand Up @@ -133,12 +155,19 @@ func (q *qemu) Start(rconfig *types.RunConfig) error {
q.cmd.Stderr = os.Stderr
}

if rconfig.QMP {
q.mgmt = rconfig.Mgmt
}

if rconfig.Background {
err := q.cmd.Start()
if err != nil {
log.Error(err)
}
} else {
q.cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true,
}
if err := q.cmd.Run(); err != nil {
log.Error(err)
}
Expand Down
33 changes: 33 additions & 0 deletions qemu/qmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//go:build linux || darwin || freebsd
// +build linux darwin freebsd

package qemu

import (
"fmt"
"net"
"os"
)

// ExecuteQMP ships a list of commands to the QMP to execute.
// TODO: turn me private and have the actual commands be exportable.
func ExecuteQMP(commands []string, last string) {
c, err := net.Dial("tcp", "localhost:"+last)
if err != nil {
fmt.Println(err)
}
defer c.Close()

for i := 0; i < len(commands); i++ {
_, err := c.Write([]byte(commands[i] + "\n"))
if err != nil {
fmt.Println(err)
}
received := make([]byte, 1024)
_, err = c.Read(received)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
}