mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
Merge pull request #7937 from rhatdan/size
Populate /etc/hosts file when run in a user namespace
This commit is contained in:
@ -976,6 +976,21 @@ func (c *Container) completeNetworkSetup() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// check if we have a bindmount for /etc/hosts
|
||||||
|
if hostsBindMount, ok := state.BindMounts["/etc/hosts"]; ok && len(c.cniHosts()) > 0 {
|
||||||
|
ctrHostPath := filepath.Join(c.state.RunDir, "hosts")
|
||||||
|
if hostsBindMount == ctrHostPath {
|
||||||
|
// read the existing hosts
|
||||||
|
b, err := ioutil.ReadFile(hostsBindMount)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := ioutil.WriteFile(hostsBindMount, append(b, []byte(c.cniHosts())...), 0644); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// check if we have a bindmount for resolv.conf
|
// check if we have a bindmount for resolv.conf
|
||||||
resolvBindMount := state.BindMounts["/etc/resolv.conf"]
|
resolvBindMount := state.BindMounts["/etc/resolv.conf"]
|
||||||
if len(outResolvConf) < 1 || resolvBindMount == "" || len(c.config.NetNsCtr) > 0 {
|
if len(outResolvConf) < 1 || resolvBindMount == "" || len(c.config.NetNsCtr) > 0 {
|
||||||
@ -997,6 +1012,15 @@ func (c *Container) completeNetworkSetup() error {
|
|||||||
return ioutil.WriteFile(resolvBindMount, []byte(strings.Join(outResolvConf, "\n")), 0644)
|
return ioutil.WriteFile(resolvBindMount, []byte(strings.Join(outResolvConf, "\n")), 0644)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Container) cniHosts() string {
|
||||||
|
var hosts string
|
||||||
|
if len(c.state.NetworkStatus) > 0 && len(c.state.NetworkStatus[0].IPs) > 0 {
|
||||||
|
ipAddress := strings.Split(c.state.NetworkStatus[0].IPs[0].Address.String(), "/")[0]
|
||||||
|
hosts += fmt.Sprintf("%s\t%s %s\n", ipAddress, c.Hostname(), c.Config().Name)
|
||||||
|
}
|
||||||
|
return hosts
|
||||||
|
}
|
||||||
|
|
||||||
// Initialize a container, creating it in the runtime
|
// Initialize a container, creating it in the runtime
|
||||||
func (c *Container) init(ctx context.Context, retainRetries bool) error {
|
func (c *Container) init(ctx context.Context, retainRetries bool) error {
|
||||||
span, _ := opentracing.StartSpanFromContext(ctx, "init")
|
span, _ := opentracing.StartSpanFromContext(ctx, "init")
|
||||||
|
@ -1543,10 +1543,7 @@ func (c *Container) getHosts() string {
|
|||||||
// When using slirp4netns, the interface gets a static IP
|
// When using slirp4netns, the interface gets a static IP
|
||||||
hosts += fmt.Sprintf("# used by slirp4netns\n%s\t%s %s\n", "10.0.2.100", c.Hostname(), c.Config().Name)
|
hosts += fmt.Sprintf("# used by slirp4netns\n%s\t%s %s\n", "10.0.2.100", c.Hostname(), c.Config().Name)
|
||||||
}
|
}
|
||||||
if len(c.state.NetworkStatus) > 0 && len(c.state.NetworkStatus[0].IPs) > 0 {
|
hosts += c.cniHosts()
|
||||||
ipAddress := strings.Split(c.state.NetworkStatus[0].IPs[0].Address.String(), "/")[0]
|
|
||||||
hosts += fmt.Sprintf("%s\t%s %s\n", ipAddress, c.Hostname(), c.Config().Name)
|
|
||||||
}
|
|
||||||
return hosts
|
return hosts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -477,6 +477,17 @@ var _ = Describe("Podman run networking", func() {
|
|||||||
Expect(session.ExitCode()).To(Equal(0))
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman run --uidmap /etc/hosts contains --hostname", func() {
|
||||||
|
SkipIfRootless("uidmap population of cninetworks not supported for rootless users")
|
||||||
|
session := podmanTest.Podman([]string{"run", "--uidmap", "0:100000:1000", "--rm", "--hostname", "foohostname", ALPINE, "grep", "foohostname", "/etc/hosts"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(0))
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"run", "--uidmap", "0:100000:1000", "--rm", "--hostname", "foohostname", "-v", "/etc/hosts:/etc/hosts", ALPINE, "grep", "foohostname", "/etc/hosts"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(Equal(1))
|
||||||
|
})
|
||||||
|
|
||||||
It("podman run network in user created network namespace", func() {
|
It("podman run network in user created network namespace", func() {
|
||||||
SkipIfRootless("ip netns is not supported for rootless users")
|
SkipIfRootless("ip netns is not supported for rootless users")
|
||||||
if Containerized() {
|
if Containerized() {
|
||||||
|
Reference in New Issue
Block a user