-
Notifications
You must be signed in to change notification settings - Fork 187
qemu: Support monitoring for failures in the initramfs #1290
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Copyright 2020 Red Hat, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ignition | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "time" | ||
|
|
||
| ignv3types "github.com/coreos/ignition/v2/config/v3_0/types" | ||
| "github.com/pkg/errors" | ||
|
|
||
| "github.com/coreos/mantle/kola" | ||
| "github.com/coreos/mantle/kola/cluster" | ||
| "github.com/coreos/mantle/kola/register" | ||
| "github.com/coreos/mantle/platform" | ||
| "github.com/coreos/mantle/util" | ||
| ) | ||
|
|
||
| func init() { | ||
| register.RegisterTest(®ister.Test{ | ||
| Name: "coreos.ignition.failure", | ||
| Run: runIgnitionFailure, | ||
| ClusterSize: 0, | ||
| Platforms: []string{"qemu-unpriv"}, | ||
| }) | ||
| } | ||
|
|
||
| func runIgnitionFailure(c cluster.TestCluster) { | ||
| if err := ignitionFailure(c); err != nil { | ||
| c.Fatal(err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func ignitionFailure(c cluster.TestCluster) error { | ||
| // TODO remove this once the feature lands | ||
| c.H.Skip("Needs https://github.com/coreos/ignition-dracut/pull/146") | ||
| // We can't create files in / due to the immutable bit OSTree creates, so | ||
| // this is a convenient way to test Ignition failure. | ||
| failConfig := ignv3types.Config{ | ||
| Ignition: ignv3types.Ignition{ | ||
| Version: "3.0.0", | ||
| }, | ||
| Storage: ignv3types.Storage{ | ||
| Files: []ignv3types.File{ | ||
| { | ||
| Node: ignv3types.Node{ | ||
| Path: "/notwritable.txt", | ||
| }, | ||
| FileEmbedded1: ignv3types.FileEmbedded1{ | ||
| Contents: ignv3types.FileContents{ | ||
| Source: util.StrToPtr("data:,hello%20world%0A"), | ||
| }, | ||
| Mode: util.IntToPtr(420), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| builder := platform.NewBuilder() | ||
| builder.SetConfig(failConfig, kola.Options.IgnitionVersion == "v2") | ||
| builder.AddPrimaryDisk(&platform.Disk{ | ||
| BackingFile: kola.QEMUOptions.DiskImage, | ||
| }) | ||
| builder.Memory = 1024 | ||
| inst, err := builder.Exec() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer inst.Destroy() | ||
| errchan := make(chan error) | ||
| go func() { | ||
| err := inst.WaitAll() | ||
| if err == nil { | ||
| err = fmt.Errorf("Ignition unexpectedly succeeded") | ||
| } else if err == platform.ErrInitramfsEmergency { | ||
| // The expected case | ||
| err = nil | ||
| } else { | ||
| err = errors.Wrapf(err, "expected initramfs emergency.target error") | ||
| } | ||
| errchan <- err | ||
| }() | ||
| go func() { | ||
| time.Sleep(2 * time.Minute) | ||
| proc := os.Process{ | ||
| Pid: inst.Pid(), | ||
| } | ||
| proc.Kill() | ||
| errchan <- errors.New("timed out waiting for initramfs error") | ||
| }() | ||
| return <-errchan | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| package platform | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
|
|
@@ -37,6 +38,10 @@ import ( | |
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| var ( | ||
| ErrInitramfsEmergency = errors.New("entered emergency.target in initramfs") | ||
| ) | ||
|
|
||
| type MachineOptions struct { | ||
| AdditionalDisks []Disk | ||
| } | ||
|
|
@@ -63,6 +68,8 @@ type QemuInstance struct { | |
| swtpm exec.Cmd | ||
| tmpFiles []string | ||
| nbdServers []exec.Cmd | ||
|
|
||
| journalPipe *os.File | ||
| } | ||
|
|
||
| func (inst *QemuInstance) Pid() int { | ||
|
|
@@ -127,6 +134,7 @@ func (inst *QemuInstance) SSHAddress() (string, error) { | |
| return "", fmt.Errorf("didn't find an address") | ||
| } | ||
|
|
||
| // Wait for the qemu process to exit | ||
| func (inst *QemuInstance) Wait() error { | ||
| if inst.qemu != nil { | ||
| err := inst.qemu.Wait() | ||
|
|
@@ -136,10 +144,69 @@ func (inst *QemuInstance) Wait() error { | |
| return nil | ||
| } | ||
|
|
||
| // 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 | ||
| // by `journalctl -o json`. | ||
| func (inst *QemuInstance) WaitIgnitionError() (string, error) { | ||
| b := bufio.NewReaderSize(inst.journalPipe, 64768) | ||
| var r strings.Builder | ||
| iscorrupted := false | ||
| _, err := b.Peek(1) | ||
| if err != nil { | ||
| if err == io.EOF { | ||
| return "", nil | ||
| } | ||
| return "", errors.Wrapf(err, "Reading from journal") | ||
| } | ||
| for { | ||
| line, prefix, err := b.ReadLine() | ||
| if err != nil { | ||
| return r.String(), errors.Wrapf(err, "Reading from journal channel") | ||
| } | ||
| if prefix { | ||
| iscorrupted = true | ||
| } | ||
| if len(line) == 0 || string(line) == "{}" { | ||
| break | ||
| } | ||
| r.Write(line) | ||
| } | ||
| if iscorrupted { | ||
| return r.String(), fmt.Errorf("journal was truncated due to overly long line") | ||
| } | ||
| return r.String(), nil | ||
| } | ||
|
|
||
| // WaitAll wraps the process exit as well as WaitIgnitionError, | ||
| // returning an error if either fail. | ||
| func (inst *QemuInstance) WaitAll() error { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, do we actually need this new interface? Can we have it part of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Funny you ask. I actually did it that way first. And then I went to go test out a FCOS build failing in the initramfs (which btw if you want to conveniently test, use e.g. After that I made it a separate interface, because for that reason we don't always want to die if there's a failure in the initramfs. (Now, theoretically the caller could distinguish and explicitly call |
||
| c := make(chan error) | ||
| go func() { | ||
| buf, err := inst.WaitIgnitionError() | ||
| if err != nil { | ||
| c <- err | ||
| } else { | ||
| // TODO parse buf and try to nicely render something | ||
| if buf != "" { | ||
| c <- ErrInitramfsEmergency | ||
| } | ||
| } | ||
| }() | ||
| go func() { | ||
| c <- inst.Wait() | ||
| }() | ||
| return <-c | ||
| } | ||
|
|
||
| func (inst *QemuInstance) Destroy() { | ||
| for _, fN := range inst.tmpFiles { | ||
| os.RemoveAll(fN) | ||
| } | ||
| if inst.journalPipe != nil { | ||
| inst.journalPipe.Close() | ||
| inst.journalPipe = nil | ||
| } | ||
| // check if qemu is dead before trying to kill it | ||
| if inst.qemu != nil { | ||
| if err := inst.qemu.Kill(); err != nil { | ||
|
|
@@ -885,6 +952,17 @@ func (builder *QemuBuilder) Exec() (*QemuInstance, error) { | |
| "-tpmdev", "emulator,id=tpm0,chardev=chrtpm", "-device", "tpm-tis,tpmdev=tpm0") | ||
| } | ||
|
|
||
| // Set up the virtio channel to get Ignition failures by default | ||
| journalPipeR, journalPipeW, err := os.Pipe() | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "creating journal pipe") | ||
| } | ||
| inst.journalPipe = journalPipeR | ||
| argv = append(argv, "-device", "virtio-serial") | ||
| // https://www.redhat.com/archives/libvir-list/2015-December/msg00305.html | ||
| argv = append(argv, "-chardev", fmt.Sprintf("file,id=ignition-dracut,path=%s,append=on", builder.AddFd(journalPipeW))) | ||
| argv = append(argv, "-device", "virtserialport,chardev=ignition-dracut,name=com.coreos.ignition.journal") | ||
|
|
||
| fdnum := 3 // first additional file starts at position 3 | ||
| for i, _ := range builder.fds { | ||
| fdset := i + 1 // Start at 1 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.