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

devshell: Print status as we're booting #1369

Merged
merged 1 commit into from
Apr 21, 2020
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
59 changes: 50 additions & 9 deletions mantle/cmd/kola/devshell.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"os"
Expand All @@ -28,6 +29,8 @@ import (
"syscall"
"time"

"golang.org/x/crypto/ssh/terminal"

"github.com/coreos/mantle/util"
"github.com/pkg/errors"

Expand Down Expand Up @@ -64,6 +67,10 @@ func readTrimmedLine(r *bufio.Reader) (string, error) {
}

func runDevShellSSH(builder *platform.QemuBuilder, conf *v3types.Config) error {
if !terminal.IsTerminal(0) {
return fmt.Errorf("stdin is not a tty")
}

tmpd, err := ioutil.TempDir("", "kola-devshell")
if err != nil {
return err
Expand Down Expand Up @@ -151,16 +158,33 @@ WantedBy=multi-user.target`, readinessSignalChan)
return errors.Wrapf(err, "rendering config")
}

serialPipe, err := builder.SerialPipe()
if err != nil {
return err
}
serialLog, err := ioutil.TempFile("", "cosa-run-serial")
if err != nil {
return err
}
os.Remove(serialLog.Name())
serialTee := io.TeeReader(serialPipe, serialLog)

builder.InheritConsole = false
inst, err := builder.Exec()
if err != nil {
return err
}
defer inst.Destroy()

statusChan, statusErrChan := inst.ParseSerialConsoleState(serialTee)
qemuWaitChan := make(chan error)
errchan := make(chan error)
readychan := make(chan struct{})
go func() {
// Just proxy this one
err := <-statusErrChan
errchan <- err
}()
go func() {
buf, err := inst.WaitIgnitionError()
if err != nil {
Expand All @@ -187,18 +211,35 @@ WantedBy=multi-user.target`, readinessSignalChan)
readychan <- s
}()

select {
case err := <-errchan:
if err == platform.ErrInitramfsEmergency {
return fmt.Errorf("instance failed in initramfs; try rerunning with --devshell-console")
loop:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a fan of labels, but meh

for {
select {
case err := <-errchan:
if err == platform.ErrInitramfsEmergency {
return fmt.Errorf("instance failed in initramfs; try rerunning with --devshell-console")
}
return err
case err := <-qemuWaitChan:
return errors.Wrapf(err, "qemu exited before setup")
case status := <-statusChan:
fmt.Printf("\033[2K\rstate: %s", status)
case _ = <-readychan:
fmt.Printf("\033[2K\rvirtio: connected\n")
break loop
}
return err
case err := <-qemuWaitChan:
return errors.Wrapf(err, "qemu exited before setup")
case _ = <-readychan:
fmt.Println("virtio: connected")
}

// Ignore other status messages, and just print errors for now
go func() {
for {
select {
case _ = <-statusChan:
case err := <-errchan:
fmt.Fprintf(os.Stderr, "errchan: %v", err)
}
}
}()

var ip string
err = util.Retry(6, 5*time.Second, func() error {
var err error
Expand Down
45 changes: 45 additions & 0 deletions mantle/platform/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ var (
ErrInitramfsEmergency = errors.New("entered emergency.target in initramfs")
)

const (
StateInitial = "qemu"
StateKernel = "kernel"
StateInitramfs = "initramfs"
StateTargetRoot = "root"
)

type MachineOptions struct {
AdditionalDisks []Disk
}
Expand Down Expand Up @@ -144,6 +151,44 @@ func (inst *QemuInstance) Wait() error {
return nil
}

func runParseSerialConsoleState(r io.Reader, schan chan<- string, echan chan<- error) {
bufr := bufio.NewReader(r)
state := StateInitial
for {
buf, _, err := bufr.ReadLine()
if err != nil {
echan <- err
break
}
line := string(buf)
switch state {
case StateInitial:
// Yes, all this is heuristic. But it's just informational.
if strings.Contains(line, "Hypervisor detected: KVM") {
state = StateKernel
schan <- state
}
case StateKernel:
if strings.Contains(line, "Running in initial RAM disk.") {
state = StateInitramfs
schan <- state
}
case StateInitramfs:
if strings.Contains(line, "initrd-switch-root.service: Succeeded.") {
state = StateTargetRoot
schan <- state
}
}
}
}

func (inst *QemuInstance) ParseSerialConsoleState(r io.Reader) (<-chan string, <-chan error) {
schan := make(chan string)
echan := make(chan error)
go runParseSerialConsoleState(r, schan, echan)
return schan, echan
}

// WaitIgnitionError will only return if the instance
// failed inside the initramfs. The resulting string will
// be a newline-delimited stream of JSON strings, as returned
Expand Down