github.com/rootless-containers/rootlesskit/v2@v2.3.4/pkg/network/parentutils/parentutils.go (about)

     1  package parentutils
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/rootless-containers/rootlesskit/v2/pkg/common"
     9  )
    10  
    11  func PrepareTap(childPID int, childNetNsPath string, tap string) error {
    12  	cmds := [][]string{
    13  		nsenter(childPID, childNetNsPath, []string{"ip", "tuntap", "add", "name", tap, "mode", "tap"}),
    14  		nsenter(childPID, childNetNsPath, []string{"ip", "link", "set", tap, "up"}),
    15  	}
    16  	if err := common.Execs(os.Stderr, os.Environ(), cmds); err != nil {
    17  		return fmt.Errorf("executing %v: %w", cmds, err)
    18  	}
    19  	return nil
    20  }
    21  
    22  func nsenter(childPID int, childNetNsPath string, cmd []string) []string {
    23  	fullCmd := []string{"nsenter", "-t", strconv.Itoa(childPID)}
    24  	if childNetNsPath != "" {
    25  		fullCmd = append(fullCmd, "-n"+childNetNsPath)
    26  	} else {
    27  		fullCmd = append(fullCmd, "-n")
    28  	}
    29  	fullCmd = append(fullCmd, []string{"-m", "-U", "--preserve-credentials"}...)
    30  	fullCmd = append(fullCmd, cmd...)
    31  	return fullCmd
    32  }