github.com/containers/podman/v4@v4.9.4/pkg/specgen/generate/namespaces_freebsd.go (about)

     1  //go:build !remote
     2  // +build !remote
     3  
     4  package generate
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  
    10  	"github.com/containers/podman/v4/libpod"
    11  	"github.com/containers/podman/v4/pkg/specgen"
    12  	"github.com/opencontainers/runtime-tools/generate"
    13  	"github.com/sirupsen/logrus"
    14  )
    15  
    16  func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt *libpod.Runtime, pod *libpod.Pod) error {
    17  	// UTS
    18  
    19  	hostname := s.Hostname
    20  	if hostname == "" {
    21  		switch {
    22  		case s.UtsNS.NSMode == specgen.FromPod:
    23  			hostname = pod.Hostname()
    24  		case s.UtsNS.NSMode == specgen.FromContainer:
    25  			utsCtr, err := rt.LookupContainer(s.UtsNS.Value)
    26  			if err != nil {
    27  				return fmt.Errorf("looking up container to share uts namespace with: %w", err)
    28  			}
    29  			hostname = utsCtr.Hostname()
    30  		case (s.NetNS.NSMode == specgen.Host && hostname == "") || s.UtsNS.NSMode == specgen.Host:
    31  			tmpHostname, err := os.Hostname()
    32  			if err != nil {
    33  				return fmt.Errorf("unable to retrieve hostname of the host: %w", err)
    34  			}
    35  			hostname = tmpHostname
    36  		default:
    37  			logrus.Debug("No hostname set; container's hostname will default to runtime default")
    38  		}
    39  	}
    40  
    41  	g.RemoveHostname()
    42  	if s.Hostname != "" || s.UtsNS.NSMode != specgen.Host {
    43  		// Set the hostname in the OCI configuration only if specified by
    44  		// the user or if we are creating a new UTS namespace.
    45  		// TODO: Should we be doing this for pod or container shared
    46  		// namespaces?
    47  		g.SetHostname(hostname)
    48  	}
    49  	if _, ok := s.Env["HOSTNAME"]; !ok && s.Hostname != "" {
    50  		g.AddProcessEnv("HOSTNAME", hostname)
    51  	}
    52  
    53  	return nil
    54  }