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

     1  package child
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  // generateEtcHosts makes sure the current hostname is resolved into
     9  // 127.0.0.1 or ::1, not into the host eth0 IP address.
    10  //
    11  // Note that /etc/hosts is not used by nslookup/dig. (Use `getent ahostsv4` instead.)
    12  func generateEtcHosts() ([]byte, error) {
    13  	etcHosts, err := os.ReadFile("/etc/hosts")
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	hostname, err := os.Hostname()
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	// FIXME: no need to add the entry if already added
    22  	s := fmt.Sprintf("%s\n127.0.0.1 %s\n::1 %s\n",
    23  		string(etcHosts), hostname, hostname)
    24  	return []byte(s), nil
    25  }