From 550cb07fc0c0ce64caca6b17788eb4fabd0c79e8 Mon Sep 17 00:00:00 2001 From: Sergio Lopez Date: Mon, 20 May 2024 16:59:14 +0200 Subject: [PATCH] Enable libkrun provider to open a debug console When running with "log-level=debug" and libkrun as machine provider, spawn a Terminal to execute "krunkit" to enable users to have full access to the VMs console for debugging purposes. Users obtain an interactive, text console with scrollback. It's possible to interact with both the kernel and GRUB2. To obtain even additional debugging information, users can add "console=hvc0" to the linux kernel command line through GRUB2 (it may be worth considering extending the initial configuration of the VM to add that argument by default). Signed-off-by: Sergio Lopez --- pkg/machine/apple/apple.go | 37 +++++++++++++++++++++++++++++++++++++ pkg/machine/apple/vfkit.go | 8 +++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/pkg/machine/apple/apple.go b/pkg/machine/apple/apple.go index 93201407e9..0e81569650 100644 --- a/pkg/machine/apple/apple.go +++ b/pkg/machine/apple/apple.go @@ -8,6 +8,7 @@ import ( "fmt" "net" "os" + "os/exec" "syscall" "time" @@ -275,6 +276,42 @@ func StartGenericAppleVM(mc *vmconfigs.MachineConfig, cmdBinary string, bootload logrus.Debugf("helper command-line: %v", cmd.Args) + if mc.LibKrunHypervisor != nil && logrus.IsLevelEnabled(logrus.DebugLevel) { + rtDir, err := mc.RuntimeDir() + if err != nil { + return nil, nil, err + } + kdFile, err := rtDir.AppendToNewVMFile("krunkit-debug.sh", nil) + if err != nil { + return nil, nil, err + } + f, err := os.Create(kdFile.Path) + if err != nil { + return nil, nil, err + } + err = os.Chmod(kdFile.Path, 0744) + if err != nil { + return nil, nil, err + } + + _, err = f.WriteString("#!/bin/sh\nexec ") + if err != nil { + return nil, nil, err + } + for _, arg := range cmd.Args { + _, err = f.WriteString(fmt.Sprintf("%q ", arg)) + if err != nil { + return nil, nil, err + } + } + err = f.Close() + if err != nil { + return nil, nil, err + } + + cmd = exec.Command("/usr/bin/open", "-Wa", "Terminal", kdFile.Path) + } + if err := cmd.Start(); err != nil { return nil, nil, err } diff --git a/pkg/machine/apple/vfkit.go b/pkg/machine/apple/vfkit.go index 6369441466..301167a78d 100644 --- a/pkg/machine/apple/vfkit.go +++ b/pkg/machine/apple/vfkit.go @@ -9,6 +9,7 @@ import ( "github.com/containers/podman/v5/pkg/machine/vmconfigs" vfConfig "github.com/crc-org/vfkit/pkg/config" "github.com/crc-org/vfkit/pkg/rest" + "github.com/sirupsen/logrus" ) func GetDefaultDevices(mc *vmconfigs.MachineConfig) ([]vfConfig.VirtioDevice, *define.VMFile, error) { @@ -41,7 +42,12 @@ func GetDefaultDevices(mc *vmconfigs.MachineConfig) ([]vfConfig.VirtioDevice, *d if err != nil { return nil, nil, err } - devices = append(devices, disk, rng, serial, readyDevice) + devices = append(devices, disk, rng, readyDevice) + if mc.LibKrunHypervisor == nil || !logrus.IsLevelEnabled(logrus.DebugLevel) { + // If libkrun is the provider and we want to show the debug console, + // don't add a virtio serial device to avoid redirecting the output. + devices = append(devices, serial) + } if mc.AppleHypervisor != nil && mc.AppleHypervisor.Vfkit.Rosetta { rosetta := &vfConfig.RosettaShare{