Wire most of the work from today together

This commit is contained in:
AlexSSD7 2023-08-31 16:23:40 +01:00
commit e57519e58d
12 changed files with 328 additions and 132 deletions

View file

@ -247,3 +247,64 @@ pasv_address=` + extIP.String() + `
return nil
}
func (fm *FileManager) StartSMB(pwd string) error {
// This timeout is for the SCP client exclusively.
scpCtx, scpCtxCancel := context.WithTimeout(fm.vm.ctx, time.Second*5)
defer scpCtxCancel()
scpClient, err := fm.vm.DialSCP()
if err != nil {
return errors.Wrap(err, "dial scp")
}
defer scpClient.Close()
sambaCfg := `[global]
workgroup = WORKGROUP
dos charset = cp866
unix charset = utf-8
read raw = yes
write raw = yes
socket options = TCP_NODELAY IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072
min receivefile size = 16384
use sendfile = true
aio read size = 16384
aio write size = 16384
server signing = no
[linsk]
browseable = yes
writeable = yes
path = /mnt
force user = linsk
force group = linsk
create mask = 0664`
err = scpClient.CopyFile(scpCtx, strings.NewReader(sambaCfg), "/etc/samba/smb.conf", "0400")
if err != nil {
return errors.Wrap(err, "copy samba config file")
}
scpClient.Close()
sc, err := fm.vm.DialSSH()
if err != nil {
return errors.Wrap(err, "dial ssh")
}
defer func() { _ = sc.Close() }()
_, err = sshutil.RunSSHCmd(fm.vm.ctx, sc, "rc-update add samba && rc-service samba start")
if err != nil {
return errors.Wrap(err, "add and start samba service")
}
err = sshutil.ChangeSambaPass(fm.vm.ctx, sc, "linsk", pwd)
if err != nil {
return errors.Wrap(err, "change samba pass")
}
return nil
}

37
vm/net.go Normal file
View file

@ -0,0 +1,37 @@
package vm
import (
"context"
"fmt"
"net"
"github.com/AlexSSD7/linsk/sshutil"
"github.com/AlexSSD7/linsk/utils"
"github.com/alessio/shellescape"
"github.com/pkg/errors"
)
func (vi *VM) ConfigureInterfaceStaticNet(ctx context.Context, iface string, cidr string) error {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return errors.Wrap(err, "invalid cidr")
}
if !utils.IsIPv6IP(ip) {
return fmt.Errorf("ipv6 addresses accepted only (have '%v')", ip)
}
sc, err := vi.DialSSH()
if err != nil {
return errors.Wrap(err, "dial ssh")
}
defer func() { _ = sc.Close() }()
_, err = sshutil.RunSSHCmd(ctx, sc, "ifconfig "+shellescape.Quote(iface)+" up && ip addr add "+shellescape.Quote(cidr)+" dev "+shellescape.Quote(iface))
if err != nil {
return errors.Wrap(err, "run net conf cmds")
}
return nil
}

View file

@ -18,6 +18,7 @@ import (
"log/slog"
"github.com/AlexSSD7/linsk/nettap"
"github.com/AlexSSD7/linsk/sshutil"
"github.com/AlexSSD7/linsk/utils"
"github.com/alessio/shellescape"
@ -64,6 +65,10 @@ type DriveConfig struct {
SnapshotMode bool
}
type TapConfig struct {
Name string
}
type VMConfig struct {
CdromImagePath string
BIOSPath string
@ -74,14 +79,17 @@ type VMConfig struct {
PassthroughConfig PassthroughConfig
ExtraPortForwardingRules []PortForwardingRule
// Networking
UnrestrictedNetworking bool
Taps []TapConfig
// Timeouts
OSUpTimeout time.Duration
SSHUpTimeout time.Duration
// Mostly debug-related options.
UnrestrictedNetworking bool
ShowDisplay bool
InstallBaseUtilities bool
ShowDisplay bool
InstallBaseUtilities bool
}
func NewVM(logger *slog.Logger, cfg VMConfig) (*VM, error) {
@ -150,6 +158,17 @@ func NewVM(logger *slog.Logger, cfg VMConfig) (*VM, error) {
cmdArgs = append(cmdArgs, "-device", "e1000,netdev=net0", "-netdev", netdevOpts)
for i, tap := range cfg.Taps {
err := nettap.ValidateTapName(tap.Name)
if err != nil {
return nil, errors.Wrapf(err, "validate network tap #%v name", i)
}
netdevName := "net" + fmt.Sprint(1+i)
cmdArgs = append(cmdArgs, "-device", "e1000,netdev="+netdevName, "-netdev", "tap,id="+netdevName+",ifname="+shellescape.Quote(tap.Name)+",script=no,downscript=no")
}
if !cfg.ShowDisplay {
cmdArgs = append(cmdArgs, "-display", "none")
} else if runtime.GOARCH == "arm64" {
@ -193,6 +212,7 @@ func NewVM(logger *slog.Logger, cfg VMConfig) (*VM, error) {
for _, dev := range cfg.PassthroughConfig.Block {
// It's always a user's responsibility to ensure that no drives are mounted
// in both host and guest system. This should serve as the last resort.
// TODO: Windows support.
{
seemsMounted, err := checkDeviceSeemsMounted(dev.Path)
if err != nil {
@ -204,7 +224,7 @@ func NewVM(logger *slog.Logger, cfg VMConfig) (*VM, error) {
}
}
cmdArgs = append(cmdArgs, "-drive", "file="+shellescape.Quote(dev.Path)+",format=raw,aio=native,cache=none")
cmdArgs = append(cmdArgs, "-drive", "file="+shellescape.Quote(strings.ReplaceAll(dev.Path, "\\", "/"))+",format=raw,cache=none")
}
// We're not using clean `cdromImagePath` here because it is set to "."