2019-08-20 09:09:38 +00:00
|
|
|
// Copyright 2019 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.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-02-15 10:17:57 +00:00
|
|
|
"errors"
|
2019-08-20 09:09:38 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2023-05-13 09:43:06 +00:00
|
|
|
"net/url"
|
2019-08-20 09:09:38 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-02-15 10:17:57 +00:00
|
|
|
"os/user"
|
2023-04-07 21:30:03 +00:00
|
|
|
"strings"
|
2023-02-15 10:17:57 +00:00
|
|
|
"time"
|
|
|
|
|
2023-05-13 09:43:06 +00:00
|
|
|
"github.com/cavaliergopher/grab/v3"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2023-02-15 10:17:57 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/config"
|
2023-05-13 09:17:57 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/fs"
|
2023-02-15 10:17:57 +00:00
|
|
|
"code.dumpstack.io/tools/out-of-tree/qemu"
|
2019-08-20 09:09:38 +00:00
|
|
|
)
|
|
|
|
|
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) {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := os.ReadDir(usr.HomeDir + "/.out-of-tree/images/")
|
|
|
|
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) {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
image := usr.HomeDir + "/.out-of-tree/images/" + cmd.Name
|
2023-05-13 09:17:57 +00:00
|
|
|
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 {
|
|
|
|
return errors.New("No kernels found")
|
|
|
|
}
|
|
|
|
|
|
|
|
ki := config.KernelInfo{}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-08-20 09:09:38 +00:00
|
|
|
func unpackTar(archive, destination string) (err error) {
|
|
|
|
// NOTE: If you're change anything in tar command please check also
|
|
|
|
// BSD tar (or if you're using macOS, do not forget to check GNU Tar)
|
|
|
|
// Also make sure that sparse files are extracting correctly
|
|
|
|
cmd := exec.Command("tar", "-Sxf", archive)
|
|
|
|
cmd.Dir = destination + "/"
|
|
|
|
|
2023-03-19 13:14:14 +00:00
|
|
|
log.Debug().Msgf("%v", cmd)
|
|
|
|
|
2019-08-20 09:09:38 +00:00
|
|
|
rawOutput, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("%v: %s", err, rawOutput)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func downloadImage(path, file string) (err error) {
|
2023-04-07 16:43:56 +00:00
|
|
|
tmp, err := ioutil.TempDir(tempDirBase, "out-of-tree_")
|
2019-08-20 09:09:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2023-05-13 09:43:06 +00:00
|
|
|
fileurl, err := url.JoinPath(imagesBaseURL, file+".tar.gz")
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-08-20 09:09:38 +00:00
|
|
|
|
2023-05-13 09:43:06 +00:00
|
|
|
resp, err := grab.Get(tmp, fileurl)
|
2019-08-20 09:09:38 +00:00
|
|
|
if err != nil {
|
2023-05-13 09:43:06 +00:00
|
|
|
err = fmt.Errorf("Cannot download %s. It looks like you need "+
|
|
|
|
"to generate it manually and place it "+
|
|
|
|
"to ~/.out-of-tree/images/. "+
|
|
|
|
"Check documentation for additional information.",
|
|
|
|
fileurl)
|
2019-08-20 09:09:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-13 09:43:06 +00:00
|
|
|
err = unpackTar(resp.Filename, path)
|
2019-08-20 09:09:38 +00:00
|
|
|
return
|
|
|
|
}
|