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: 4 additions & 2 deletions mantle/cmd/kola/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func syncOptionsImpl(useCosa bool) error {
}
}

if kola.Options.IgnitionVersion == "" && kola.QEMUOptions.DiskImage != "" {
kola.Options.IgnitionVersion = sdk.TargetIgnitionVersionFromName(kola.QEMUOptions.DiskImage)
}

units, _ := root.PersistentFlags().GetStringSlice("debug-systemd-units")
for _, unit := range units {
kola.Options.SystemdDropins = append(kola.Options.SystemdDropins, platform.SystemdDropin{
Expand Down Expand Up @@ -234,8 +238,6 @@ func syncCosaOptions() error {
if kola.Options.IgnitionVersion == "" {
if kola.CosaBuild != nil {
kola.Options.IgnitionVersion = sdk.TargetIgnitionVersion(kola.CosaBuild)
} else if kola.QEMUOptions.DiskImage != "" {
kola.Options.IgnitionVersion = sdk.TargetIgnitionVersionFromName(kola.QEMUOptions.DiskImage)
}
}

Expand Down
87 changes: 87 additions & 0 deletions mantle/cmd/kola/qemuexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"

ignconverter "github.com/coreos/ign-converter"
v3 "github.com/coreos/ignition/v2/config/v3_0"
"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/coreos/mantle/kola"
"github.com/coreos/mantle/platform"
"github.com/coreos/mantle/platform/conf"
)

var (
Expand All @@ -41,21 +49,100 @@ var (
ignition string
knetargs string

ignitionFragments []string

forceConfigInjection bool
)

func init() {
root.AddCommand(cmdQemuExec)
cmdQemuExec.Flags().StringVarP(&knetargs, "knetargs", "", "", "Arguments for Ignition networking on kernel commandline")
cmdQemuExec.Flags().BoolVarP(&usernet, "usernet", "U", false, "Enable usermode networking")
cmdQemuExec.Flags().StringSliceVar(&ignitionFragments, "add-ignition", nil, "Append well-known Ignition fragment: [\"autologin\"]")
cmdQemuExec.Flags().StringVarP(&hostname, "hostname", "", "", "Set hostname via DHCP")
cmdQemuExec.Flags().IntVarP(&memory, "memory", "m", 0, "Memory in MB")
cmdQemuExec.Flags().StringVarP(&ignition, "ignition", "i", "", "Path to ignition config")
cmdQemuExec.Flags().BoolVarP(&forceConfigInjection, "inject-ignition", "", false, "Force injecting Ignition config using guestfs")
}

func renderFragments() (string, error) {
buf, err := ioutil.ReadFile(ignition)
if err != nil {
return "", err
}
config, _, err := v3.Parse(buf)
for _, fragtype := range ignitionFragments {
switch fragtype {
case "autologin":
newconf := v3.Merge(config, conf.GetAutologin())
config = newconf
break
default:
return "", fmt.Errorf("Unknown fragment: %s", fragtype)
}
}

newbuf, err := json.Marshal(config)
if err != nil {
return "", err
}
tmpf, err := ioutil.TempFile("", "qemuexec-ign")
if err != nil {
return "", err
}
if _, err := tmpf.Write(newbuf); err != nil {
return "", err
}

return tmpf.Name(), nil
}

func runQemuExec(cmd *cobra.Command, args []string) error {
var err error
cleanupIgnition := false
if len(ignitionFragments) > 0 {
newconfig, err := renderFragments()
if err != nil {
return errors.Wrapf(err, "rendering fragments")
}
ignition = newconfig
cleanupIgnition = true
}
if kola.Options.IgnitionVersion == "v2" {
Copy link
Member

Choose a reason for hiding this comment

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

Minor: just so I understand, this if-statement isn't related to the autologin draining, right? Feels like it's worth its own commit.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's necessary now because previously cosa run worked with RHCOS, but kola qemuexec required external translation.

Copy link
Contributor

Choose a reason for hiding this comment

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

We have a similar version of this down translation code in a few places. Might end up being worthwhile to move more into the Conf / UserData struct and providing a stable interface to perform translation from there.

buf, err := ioutil.ReadFile(ignition)
if err != nil {
return err
}
config, _, err := v3.Parse(buf)
if err != nil {
return errors.Wrapf(err, "parsing %s", ignition)
}
ignc2, err := ignconverter.Translate3to2(config)
if err != nil {
return err
}
ignc2buf, err := json.Marshal(ignc2)
if err != nil {
return err
}
tmpf, err := ioutil.TempFile("", "qemuexec-ign-conv")
if err != nil {
return err
}
if _, err := tmpf.Write(ignc2buf); err != nil {
return err
}
if cleanupIgnition {
os.Remove(ignition)
}
cleanupIgnition = true
ignition = tmpf.Name()
}
defer func() {
if cleanupIgnition {
os.Remove(ignition)
}
}()

builder := platform.NewBuilder(ignition, forceConfigInjection)
if len(knetargs) > 0 {
Expand Down
29 changes: 29 additions & 0 deletions mantle/platform/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,32 @@ func (c *Conf) IsIgnition() bool {
func (c *Conf) IsEmpty() bool {
return !c.IsIgnition() && c.cloudconfig == nil && c.script == ""
}

func getAutologinFragment(name, args string) v3types.Unit {
contents := fmt.Sprintf(`[Service]
ExecStart=
ExecStart=-/sbin/agetty --autologin core -o '-p -f core' %s %%I $TERM
`, args)
return v3types.Unit{
Name: name,
Dropins: []v3types.Dropin{
{
Name: "10-autologin.conf",
Contents: &contents,
},
},
}
}

// GetAutologin returns an Ignition config to automatic login on consoles
func GetAutologin() v3types.Config {
conf := v3types.Config{
Ignition: v3types.Ignition{
Version: "3.0.0",
},
Systemd: v3types.Systemd{},
}
conf.Systemd.Units = append(conf.Systemd.Units, getAutologinFragment("getty@.service", "--noclear"))
conf.Systemd.Units = append(conf.Systemd.Units, getAutologinFragment("serial-getty@.service", "--keep-baud 115200,38400,9600"))
return conf
}
12 changes: 1 addition & 11 deletions src/cmd-run
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,6 @@ EOF
rowcol=$(stty -a | tr ';' '\n' | grep -e 'rows\|columns' | tr '\n' ' ' )
rowcol=$(echo "stty ${rowcol}" | base64 --wrap 0)

append_systemd_unit "{
\"name\": \"serial-getty@${DEFAULT_TERMINAL}.service\",
\"dropins\": [
{
\"name\": \"autologin-core.conf\",
\"contents\": \"[Service]\\nTTYVTDisallocate=no\\nExecStart=\\nExecStart=-/usr/sbin/agetty --autologin ${TARGET_USER} --noclear %I \$TERM\\n\"
}
]
}"

f=$(mktemp)
cat > "${f}" <<EOF
{
Expand Down Expand Up @@ -279,4 +269,4 @@ case "${IMAGE_TYPE}" in
esac

set -x
exec kola qemuexec -U --qemu-image "${VM_DISK}" --ignition "${IGNITION_CONFIG_FILE}" --memory "${VM_MEMORY}" "${kola_args[@]}" -- "$@"
exec kola qemuexec --add-ignition=autologin -U --qemu-image "${VM_DISK}" --ignition "${IGNITION_CONFIG_FILE}" --memory "${VM_MEMORY}" "${kola_args[@]}" -- "$@"