Skip to content

Commit 5e59e1b

Browse files
authored
Merge pull request #15 from AkihiroSuda/a
add `127.0.0.1 $HOSTNAME` to /etc/hosts
2 parents 28cb879 + 3a961f8 commit 5e59e1b

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

pkg/child/child.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ func setupNet(msg *common.Message, etcWasCopied bool) error {
214214
if err := writeResolvConf(msg.DNS); err != nil {
215215
return err
216216
}
217+
if err := writeEtcHosts(); err != nil {
218+
return err
219+
}
217220
} else {
218221
logrus.Warn("Mounting /etc/resolv.conf without copying-up /etc. " +
219222
"Note that /etc/resolv.conf in the namespace will be unmounted when it is recreated on the host. " +
@@ -222,6 +225,9 @@ func setupNet(msg *common.Message, etcWasCopied bool) error {
222225
if err := mountResolvConf(msg.StateDir, msg.DNS); err != nil {
223226
return err
224227
}
228+
if err := mountEtcHosts(msg.StateDir); err != nil {
229+
return err
230+
}
225231
}
226232
return nil
227233
}

pkg/child/hosts.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package child
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
9+
"github.com/pkg/errors"
10+
11+
"github.com/rootless-containers/rootlesskit/pkg/common"
12+
)
13+
14+
// generateEtcHosts makes sure the current hostname is resolved into
15+
// 127.0.0.1 or ::1, not into the host eth0 IP address.
16+
//
17+
// Note that /etc/hosts is not used by nslookup/dig. (Use `getent ahostsv4` instead.)
18+
func generateEtcHosts() ([]byte, error) {
19+
etcHosts, err := ioutil.ReadFile("/etc/hosts")
20+
if err != nil {
21+
return nil, err
22+
}
23+
hostname, err := os.Hostname()
24+
if err != nil {
25+
return nil, err
26+
}
27+
// FIXME: no need to add the entry if already added
28+
s := fmt.Sprintf("%s\n127.0.0.1 %s\n::1 %s\n",
29+
string(etcHosts), hostname, hostname)
30+
return []byte(s), nil
31+
}
32+
33+
// writeEtcHosts is akin to writeResolvConf
34+
// TODO: dedupe
35+
func writeEtcHosts() error {
36+
newEtcHosts, err := generateEtcHosts()
37+
if err != nil {
38+
return err
39+
}
40+
// remove copied-up link
41+
_ = os.Remove("/etc/hosts")
42+
if err := ioutil.WriteFile("/etc/hosts", newEtcHosts, 0644); err != nil {
43+
return errors.Wrapf(err, "writing /etc/hosts")
44+
}
45+
return nil
46+
}
47+
48+
// mountEtcHosts is akin to mountResolvConf
49+
// TODO: dedupe
50+
func mountEtcHosts(tempDir string) error {
51+
newEtcHosts, err := generateEtcHosts()
52+
if err != nil {
53+
return err
54+
}
55+
myEtcHosts := filepath.Join(tempDir, "hosts")
56+
if err := ioutil.WriteFile(myEtcHosts, newEtcHosts, 0644); err != nil {
57+
return errors.Wrapf(err, "writing %s", myEtcHosts)
58+
}
59+
cmds := [][]string{
60+
{"mount", "--bind", myEtcHosts, "/etc/hosts"},
61+
}
62+
if err := common.Execs(os.Stderr, os.Environ(), cmds); err != nil {
63+
return errors.Wrapf(err, "executing %v", cmds)
64+
}
65+
return nil
66+
}

0 commit comments

Comments
 (0)