Clean cmd + VM image pruning
This commit is contained in:
parent
6529ba690a
commit
8666a21d0b
4 changed files with 97 additions and 8 deletions
41
cmd/clean.go
Normal file
41
cmd/clean.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var cleanCmd = &cobra.Command{
|
||||
Use: "clean",
|
||||
Short: "Remove all downloaded VM images.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
store := createStore()
|
||||
|
||||
fmt.Fprintf(os.Stderr, "Will delete all VM images in the data directory. Proceed? (y/n) > ")
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
answer, err := reader.ReadBytes('\n')
|
||||
if err != nil {
|
||||
slog.Error("Failed to read answer", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if strings.ToLower(string(answer)) != "y\n" {
|
||||
fmt.Fprintf(os.Stderr, "Aborted.\n")
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
deleted, err := store.CleanImages(false)
|
||||
if err != nil {
|
||||
slog.Error("Failed to clean images", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
slog.Info("Successful VM image cleanup", "deleted", deleted)
|
||||
},
|
||||
}
|
||||
|
|
@ -41,6 +41,7 @@ func init() {
|
|||
rootCmd.AddCommand(lsCmd)
|
||||
rootCmd.AddCommand(runCmd)
|
||||
rootCmd.AddCommand(shellCmd)
|
||||
rootCmd.AddCommand(cleanCmd)
|
||||
|
||||
rootCmd.PersistentFlags().BoolVar(&vmDebugFlag, "vmdebug", false, "Enables the VM debug mode. This will open an accessible VM monitor. You can log in with root user and no password.")
|
||||
rootCmd.PersistentFlags().BoolVar(&unrestrictedNetworkingFlag, "unrestricted-networking", false, "Enables unrestricted networking. This will allow the VM to connect to the internet.")
|
||||
|
|
|
|||
10
cmd/utils.go
10
cmd/utils.go
|
|
@ -54,14 +54,19 @@ func doUSBRootCheck() {
|
|||
}
|
||||
}
|
||||
|
||||
func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManager) int, forwardPortsRules []vm.PortForwardingRule, unrestrictedNetworking bool) int {
|
||||
func createStore() *storage.Storage {
|
||||
store, err := storage.NewStorage(slog.With("caller", "storage"), dataDirFlag)
|
||||
if err != nil {
|
||||
slog.Error("Failed to create Linsk data storage", "error", err.Error(), "data-dir", dataDirFlag)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err = store.ValidateImageHashOrDownload()
|
||||
return store
|
||||
}
|
||||
|
||||
func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManager) int, forwardPortsRules []vm.PortForwardingRule, unrestrictedNetworking bool) int {
|
||||
store := createStore()
|
||||
_, err := store.ValidateImageHashOrDownload()
|
||||
if err != nil {
|
||||
slog.Error("Failed to validate image hash or download image", "error", err.Error())
|
||||
os.Exit(1)
|
||||
|
|
@ -92,7 +97,6 @@ func runVM(passthroughArg string, fn func(context.Context, *vm.VM, *vm.FileManag
|
|||
ShowDisplay: vmDebugFlag,
|
||||
}
|
||||
|
||||
// TODO: Alpine image should be downloaded from somewhere.
|
||||
vi, err := vm.NewVM(slog.Default().With("caller", "vm"), vmCfg)
|
||||
if err != nil {
|
||||
slog.Error("Failed to create vm instance", "error", err.Error())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue