-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lower the bits to do "inject Ignition via coreos-installer embed iso" into the qemu layer. Copy the "qemu-unpriv" platform code into a "qemu-iso" platform (both are mostly thin wrappers around `qemu.go`). This makes it completely trivial to get a `ssh` shell in an ISO: `cosa run -p qemu-iso --memory 8192` And even better means we can start running a lot of the kola tests directly against the ISO (though not everything will work, e.g. tests that reboot can't persist state). I did verify this works: `kola run -p qemu-iso --qemu-memory 8192 basic`
- Loading branch information
1 parent
2d1366f
commit 3b9bbdc
Showing
9 changed files
with
447 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
// Copyright 2020 Red Hat | ||
// | ||
// 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 qemuiso | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
|
||
"sync" | ||
"time" | ||
|
||
"github.com/pborman/uuid" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/coreos/mantle/platform" | ||
"github.com/coreos/mantle/platform/conf" | ||
"github.com/coreos/mantle/util" | ||
) | ||
|
||
// Cluster is a local cluster of QEMU-based virtual machines. | ||
// | ||
// XXX: must be exported so that certain QEMU tests can access struct members | ||
// through type assertions. | ||
type Cluster struct { | ||
*platform.BaseCluster | ||
flight *flight | ||
|
||
mu sync.Mutex | ||
} | ||
|
||
func (qc *Cluster) NewMachine(userdata *conf.UserData) (platform.Machine, error) { | ||
return qc.NewMachineWithOptions(userdata, platform.MachineOptions{}) | ||
} | ||
|
||
func (qc *Cluster) NewMachineWithOptions(userdata *conf.UserData, options platform.MachineOptions) (platform.Machine, error) { | ||
return qc.NewMachineWithQemuOptions(userdata, platform.QemuMachineOptions{ | ||
MachineOptions: options, | ||
}) | ||
} | ||
|
||
func (qc *Cluster) NewMachineWithQemuOptions(userdata *conf.UserData, options platform.QemuMachineOptions) (platform.Machine, error) { | ||
id := uuid.New() | ||
|
||
dir := filepath.Join(qc.RuntimeConf().OutputDir, id) | ||
if err := os.Mkdir(dir, 0777); err != nil { | ||
return nil, err | ||
} | ||
|
||
// hacky solution for cloud config ip substitution | ||
// NOTE: escaping is not supported | ||
qc.mu.Lock() | ||
|
||
conf, err := qc.RenderUserData(userdata, map[string]string{}) | ||
if err != nil { | ||
qc.mu.Unlock() | ||
return nil, err | ||
} | ||
qc.mu.Unlock() | ||
|
||
var confPath string | ||
if conf.IsIgnition() { | ||
confPath = filepath.Join(dir, "ignition.json") | ||
if err := conf.WriteFile(confPath); err != nil { | ||
return nil, err | ||
} | ||
} else if conf.IsEmpty() { | ||
} else { | ||
return nil, fmt.Errorf("qemuiso only supports Ignition or empty configs") | ||
} | ||
|
||
journal, err := platform.NewJournal(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
qm := &machine{ | ||
qc: qc, | ||
id: id, | ||
journal: journal, | ||
consolePath: filepath.Join(dir, "console.txt"), | ||
} | ||
|
||
builder := platform.NewBuilder() | ||
builder.ConfigFile = confPath | ||
defer builder.Close() | ||
builder.Uuid = qm.id | ||
if qc.flight.opts.Firmware != "" { | ||
builder.Firmware = qc.flight.opts.Firmware | ||
} | ||
builder.Hostname = fmt.Sprintf("qemu%d", qc.BaseCluster.AllocateMachineSerial()) | ||
builder.ConsoleToFile(qm.consolePath) | ||
|
||
if qc.flight.opts.Memory != "" { | ||
memory, err := strconv.ParseInt(qc.flight.opts.Memory, 10, 32) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "parsing memory option") | ||
} | ||
builder.Memory = int(memory) | ||
} | ||
|
||
builder.AddIso(qc.flight.opts.IsoPath, "") | ||
|
||
for _, disk := range options.AdditionalDisks { | ||
if err = builder.AddDisk(&platform.Disk{ | ||
Size: disk, | ||
}); err != nil { | ||
return nil, errors.Wrapf(err, "adding additional disk") | ||
} | ||
} | ||
|
||
if len(options.HostForwardPorts) > 0 { | ||
builder.EnableUsermodeNetworking(options.HostForwardPorts) | ||
} else { | ||
h := []platform.HostForwardPort{ | ||
{Service: "ssh", HostPort: 0, GuestPort: 22}, | ||
} | ||
builder.EnableUsermodeNetworking(h) | ||
} | ||
|
||
inst, err := builder.Exec() | ||
if err != nil { | ||
return nil, err | ||
} | ||
qm.inst = *inst | ||
|
||
err = util.Retry(6, 5*time.Second, func() error { | ||
var err error | ||
qm.ip, err = inst.SSHAddress() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := platform.StartMachine(qm, qm.journal); err != nil { | ||
qm.Destroy() | ||
return nil, err | ||
} | ||
|
||
qc.AddMach(qm) | ||
|
||
return qm, nil | ||
} | ||
|
||
func (qc *Cluster) Destroy() { | ||
qc.BaseCluster.Destroy() | ||
qc.flight.DelCluster(qc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2020 Red Hat | ||
// | ||
// 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 qemuiso | ||
|
||
import ( | ||
"github.com/coreos/pkg/capnslog" | ||
|
||
"github.com/coreos/mantle/platform" | ||
) | ||
|
||
const ( | ||
Platform platform.Name = "qemu-iso" | ||
) | ||
|
||
// Options contains QEMU-specific options for the flight. | ||
type Options struct { | ||
// IsoPath is the full path to the ISO image to boot in QEMU | ||
IsoPath string | ||
Firmware string | ||
Memory string | ||
|
||
*platform.Options | ||
} | ||
|
||
type flight struct { | ||
*platform.BaseFlight | ||
opts *Options | ||
} | ||
|
||
var ( | ||
plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "platform/machine/qemuiso") | ||
) | ||
|
||
func NewFlight(opts *Options) (platform.Flight, error) { | ||
bf, err := platform.NewBaseFlight(opts.Options, Platform, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
qf := &flight{ | ||
BaseFlight: bf, | ||
opts: opts, | ||
} | ||
|
||
return qf, nil | ||
} | ||
|
||
// NewCluster creates a Cluster instance, suitable for running virtual | ||
// machines in QEMU. | ||
func (qf *flight) NewCluster(rconf *platform.RuntimeConfig) (platform.Cluster, error) { | ||
bc, err := platform.NewBaseCluster(qf.BaseFlight, rconf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
qc := &Cluster{ | ||
BaseCluster: bc, | ||
flight: qf, | ||
} | ||
|
||
qf.AddCluster(qc) | ||
|
||
return qc, nil | ||
} |
Oops, something went wrong.