1
0
out-of-tree/cmd/images.go

115 lines
2.1 KiB
Go
Raw Permalink Normal View History

// Copyright 2023 Mikhail Klementev. All rights reserved.
// Use of this source code is governed by a AGPLv3 license
// (or later) that can be found in the LICENSE file.
2024-02-17 22:38:43 +00:00
package cmd
import (
2023-02-15 10:17:57 +00:00
"errors"
"fmt"
"os"
"path/filepath"
2023-04-07 21:30:03 +00:00
"strings"
2023-02-15 10:17:57 +00:00
"time"
"code.dumpstack.io/tools/out-of-tree/config"
2024-02-20 13:25:31 +00:00
"code.dumpstack.io/tools/out-of-tree/config/dotfiles"
"code.dumpstack.io/tools/out-of-tree/distro"
"code.dumpstack.io/tools/out-of-tree/fs"
2023-02-15 10:17:57 +00:00
"code.dumpstack.io/tools/out-of-tree/qemu"
)
2023-02-15 10:17:57 +00:00
type ImageCmd struct {
List ImageListCmd `cmd:"" help:"list images"`
Edit ImageEditCmd `cmd:"" help:"edit image"`
}
type ImageListCmd struct{}
func (cmd *ImageListCmd) Run(g *Globals) (err error) {
2024-02-20 13:25:31 +00:00
entries, err := os.ReadDir(dotfiles.Dir("images"))
2023-02-15 10:17:57 +00:00
if err != nil {
return
}
for _, e := range entries {
fmt.Println(e.Name())
}
return
}
type ImageEditCmd struct {
2023-04-07 21:30:03 +00:00
Name string `help:"image name" required:""`
DryRun bool `help:"do nothing, just print commands"`
2023-02-15 10:17:57 +00:00
}
func (cmd *ImageEditCmd) Run(g *Globals) (err error) {
2024-02-20 13:25:31 +00:00
image := filepath.Join(dotfiles.Dir("images"), cmd.Name)
if !fs.PathExists(image) {
2023-02-15 10:17:57 +00:00
fmt.Println("image does not exist")
}
kcfg, err := config.ReadKernelConfig(g.Config.Kernels)
if err != nil {
return
}
if len(kcfg.Kernels) == 0 {
2024-02-20 11:37:19 +00:00
return errors.New("no kernels found")
2023-02-15 10:17:57 +00:00
}
ki := distro.KernelInfo{}
2023-02-15 10:17:57 +00:00
for _, k := range kcfg.Kernels {
if k.RootFS == image {
ki = k
break
}
}
kernel := qemu.Kernel{
KernelPath: ki.KernelPath,
InitrdPath: ki.InitrdPath,
}
q, err := qemu.NewSystem(qemu.X86x64, kernel, ki.RootFS)
q.Mutable = true
2023-04-07 21:30:03 +00:00
if cmd.DryRun {
s := q.Executable()
for _, arg := range q.Args() {
if strings.Contains(arg, " ") ||
strings.Contains(arg, ",") {
s += fmt.Sprintf(` "%s"`, arg)
} else {
s += fmt.Sprintf(" %s", arg)
}
}
fmt.Println(s)
fmt.Println(q.GetSSHCommand())
return
}
2023-02-15 10:17:57 +00:00
err = q.Start()
if err != nil {
fmt.Println("Qemu start error:", err)
return
}
defer q.Stop()
fmt.Print("ssh command:\n\n\t")
fmt.Println(q.GetSSHCommand())
fmt.Print("\npress enter to stop")
fmt.Scanln()
q.Command("root", "poweroff")
for !q.Died {
time.Sleep(time.Second)
}
return
}