2023-08-31 16:23:40 +01:00
|
|
|
package vm
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
|
|
|
|
|
"github.com/AlexSSD7/linsk/sshutil"
|
|
|
|
|
"github.com/AlexSSD7/linsk/utils"
|
|
|
|
|
"github.com/alessio/shellescape"
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-01 18:15:32 +01:00
|
|
|
func (vm *VM) ConfigureInterfaceStaticNet(ctx context.Context, iface string, cidr string) error {
|
2023-08-31 16:23:40 +01:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 18:15:32 +01:00
|
|
|
sc, err := vm.DialSSH()
|
2023-08-31 16:23:40 +01:00
|
|
|
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
|
|
|
|
|
}
|