linsk/cmd/run.go
2023-08-31 16:23:40 +01:00

115 lines
3.8 KiB
Go

package cmd
import (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"strings"
"github.com/AlexSSD7/linsk/share"
"github.com/AlexSSD7/linsk/vm"
"github.com/sethvargo/go-password/password"
"github.com/spf13/cobra"
)
var runCmd = &cobra.Command{
Use: "run",
Short: "Start a VM and expose an FTP file share.",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
vmMountDevName := args[1]
fsType := args[2]
newBackendFunc := share.GetBackend(shareBackendFlag)
if newBackendFunc == nil {
slog.Error("Unknown file share backend", "type", shareBackendFlag)
os.Exit(1)
}
cfg, err := share.RawUserConfiguration{
ListenIP: shareListenIPFlag,
FTPExtIP: ftpExtIPFlag,
SMBExtMode: smbUseExternAddrFlag,
}.Process(shareBackendFlag, slog.With("caller", "share-config"))
if err != nil {
slog.Error("Failed to process raw configuration", "error", err.Error())
os.Exit(1)
}
backend, vmOpts, err := newBackendFunc(cfg)
if err != nil {
slog.Error("Failed to initialize share backend", "backend", shareBackendFlag, "error", err.Error())
os.Exit(1)
}
os.Exit(runVM(args[0], func(ctx context.Context, i *vm.VM, fm *vm.FileManager, tapCtx *share.NetTapRuntimeContext) int {
slog.Info("Mounting the device", "dev", vmMountDevName, "fs", fsType, "luks", luksFlag)
err := fm.Mount(vmMountDevName, vm.MountOptions{
FSType: fsType,
LUKS: luksFlag,
})
if err != nil {
slog.Error("Failed to mount the disk inside the VM", "error", err.Error())
return 1
}
sharePWD, err := password.Generate(16, 10, 0, false, false)
if err != nil {
slog.Error("Failed to generate ephemeral password for the network file share", "error", err.Error())
return 1
}
shareURI, err := backend.Apply(ctx, sharePWD, &share.VMShareContext{
Instance: i,
FileManager: fm,
NetTapCtx: tapCtx,
})
if err != nil {
slog.Error("Failed to apply (start) file share backend", "backend", shareBackendFlag, "error", err.Error())
return 1
}
slog.Info("Started the network share successfully", "type", "ftp")
fmt.Fprintf(os.Stderr, "============================\n[Network File Share Config]\nThe network file share was started. Please use the credentials below to connect to the file server.\n\nType: "+strings.ToUpper(shareBackendFlag)+"\nURL: %v\nUsername: linsk\nPassword: %v\n===========================\n", shareURI, sharePWD)
<-ctx.Done()
return 0
}, vmOpts.Ports, unrestrictedNetworkingFlag, vmOpts.EnableTap))
},
}
var luksFlag bool
var shareListenIPFlag string
var ftpExtIPFlag string
var shareBackendFlag string
var smbUseExternAddrFlag bool
func init() {
runCmd.Flags().BoolVarP(&luksFlag, "luks", "l", false, "Use cryptsetup to open a LUKS volume (password will be prompted).")
var defaultShareType string
switch runtime.GOOS {
case "windows":
defaultShareType = "smb"
default:
defaultShareType = "ftp"
}
runCmd.Flags().StringVar(&shareBackendFlag, "share-backend", defaultShareType, "Specifies the file share backend to use. The default value is OS-specific.")
runCmd.Flags().StringVar(&shareListenIPFlag, "share-listen", share.GetDefaultListenIPStr(), "Specifies the IP to bind the network share port to. NOTE: For FTP, changing the bind address is not enough to connect remotely. You should also specify --ftp-extip.")
smbExternDefault := false
if runtime.GOOS == "windows" {
smbExternDefault = true
}
runCmd.Flags().StringVar(&ftpExtIPFlag, "ftp-extip", share.GetDefaultListenIPStr(), "Specifies the external IP the FTP server should advertise.")
runCmd.Flags().BoolVar(&smbUseExternAddrFlag, "smb-extern", smbExternDefault, "Specifies whether Linsk emulate external networking for the VM's SMB server. This is the default for Windows as there is no way to specify ports in Windows SMB client.")
// TODO: log the use of smbUseExternAddrFlag when SMB is not enabled.
}