Improve /etc/hosts generator
Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
parent
4c88af9424
commit
b233e6a315
@ -20,11 +20,17 @@ __attribute__((constructor)) void init(void) {
|
||||
}
|
||||
|
||||
// Unshare a new mntns so our mounts don't leak
|
||||
if (unshare(CLONE_NEWNS | CLONE_NEWPID) < 0) {
|
||||
if (unshare(CLONE_NEWNS | CLONE_NEWPID | CLONE_NEWUTS) < 0) {
|
||||
fprintf(stderr, "Failed to unshare namespaces: %s\n", strerror(errno));
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
// Hardcode the hostname to "distrobuilder"
|
||||
if (sethostname("distrobuilder", 13) < 0) {
|
||||
fprintf(stderr, "Failed to set hostname: %s\n", strerror(errno));
|
||||
_exit(1);
|
||||
}
|
||||
|
||||
// Prevent mount propagation back to initial namespace
|
||||
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) < 0) {
|
||||
fprintf(stderr, "Failed to mark / private: %s\n", strerror(errno));
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
lxd "github.com/lxc/lxd/shared"
|
||||
"github.com/lxc/lxd/shared/api"
|
||||
|
||||
"github.com/lxc/distrobuilder/image"
|
||||
@ -17,6 +18,12 @@ type HostnameGenerator struct{}
|
||||
// RunLXC creates a hostname template.
|
||||
func (g HostnameGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
|
||||
defFile shared.DefinitionFile) error {
|
||||
|
||||
// Skip if the file doesn't exist
|
||||
if !lxd.PathExists(filepath.Join(sourceDir, defFile.Path)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Store original file
|
||||
err := StoreFile(cacheDir, sourceDir, defFile.Path)
|
||||
if err != nil {
|
||||
@ -43,6 +50,12 @@ func (g HostnameGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImag
|
||||
// RunLXD creates a hostname template.
|
||||
func (g HostnameGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
|
||||
defFile shared.DefinitionFile) error {
|
||||
|
||||
// Skip if the file doesn't exist
|
||||
if !lxd.PathExists(filepath.Join(sourceDir, defFile.Path)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
templateDir := filepath.Join(cacheDir, "templates")
|
||||
|
||||
err := os.MkdirAll(templateDir, 0755)
|
||||
|
@ -76,6 +76,8 @@ func TestHostnameGeneratorRunLXD(t *testing.T) {
|
||||
t.Fatalf("Unexpected error: %s", err)
|
||||
}
|
||||
|
||||
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hostname"), "hostname")
|
||||
|
||||
err = generator.RunLXD(cacheDir, rootfsDir, image,
|
||||
shared.DefinitionFile{Path: "/etc/hostname"})
|
||||
if err != nil {
|
||||
|
@ -1,12 +1,14 @@
|
||||
package generators
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/lxc/distrobuilder/image"
|
||||
"github.com/lxc/distrobuilder/shared"
|
||||
lxd "github.com/lxc/lxd/shared"
|
||||
"github.com/lxc/lxd/shared/api"
|
||||
)
|
||||
|
||||
@ -16,21 +18,37 @@ type HostsGenerator struct{}
|
||||
// RunLXC creates a LXC specific entry in the hosts file.
|
||||
func (g HostsGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
|
||||
defFile shared.DefinitionFile) error {
|
||||
|
||||
// Skip if the file doesn't exist
|
||||
if !lxd.PathExists(filepath.Join(sourceDir, defFile.Path)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Store original file
|
||||
err := StoreFile(cacheDir, sourceDir, defFile.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filepath.Join(sourceDir, defFile.Path),
|
||||
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
// Read the current content
|
||||
content, err := ioutil.ReadFile(filepath.Join(sourceDir, defFile.Path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Append hosts entry
|
||||
file.WriteString("127.0.0.1\tLXC_NAME\n")
|
||||
// Replace hostname with placeholder
|
||||
content = []byte(strings.Replace(string(content), "distrobuilder", "LXC_NAME", -1))
|
||||
|
||||
// Add a new line if needed
|
||||
if !strings.Contains(string(content), "LXC_NAME") {
|
||||
content = append([]byte("127.0.1.1\tLXC_NAME\n"), content...)
|
||||
}
|
||||
|
||||
// Overwrite the file
|
||||
err = ioutil.WriteFile(filepath.Join(sourceDir, defFile.Path), content, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add hostname path to LXC's templates file
|
||||
return img.AddTemplate(defFile.Path)
|
||||
@ -39,6 +57,12 @@ func (g HostsGenerator) RunLXC(cacheDir, sourceDir string, img *image.LXCImage,
|
||||
// RunLXD creates a hosts template.
|
||||
func (g HostsGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
|
||||
defFile shared.DefinitionFile) error {
|
||||
|
||||
// Skip if the file doesn't exist
|
||||
if !lxd.PathExists(filepath.Join(sourceDir, defFile.Path)) {
|
||||
return nil
|
||||
}
|
||||
|
||||
templateDir := filepath.Join(cacheDir, "templates")
|
||||
|
||||
// Create templates path
|
||||
@ -47,22 +71,20 @@ func (g HostsGenerator) RunLXD(cacheDir, sourceDir string, img *image.LXDImage,
|
||||
return err
|
||||
}
|
||||
|
||||
// Create hosts template
|
||||
file, err := os.Create(filepath.Join(templateDir, "hosts.tpl"))
|
||||
// Read the current content
|
||||
content, err := ioutil.ReadFile(filepath.Join(sourceDir, defFile.Path))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
hostsFile, err := os.Open(filepath.Join(sourceDir, defFile.Path))
|
||||
// Replace hostname with placeholder
|
||||
content = []byte(strings.Replace(string(content), "distrobuilder", "{{ container.name }}", -1))
|
||||
|
||||
// Write the template
|
||||
err = ioutil.WriteFile(filepath.Join(templateDir, "hosts.tpl"), content, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer hostsFile.Close()
|
||||
|
||||
// Copy old content, and append LXD specific entry
|
||||
io.Copy(file, hostsFile)
|
||||
file.WriteString("127.0.0.1\t{{ container.name }}\n")
|
||||
|
||||
img.Metadata.Templates[defFile.Path] = &api.ImageMetadataTemplate{
|
||||
Template: "hosts.tpl",
|
||||
|
@ -34,7 +34,7 @@ func TestHostsGeneratorRunLXC(t *testing.T) {
|
||||
}
|
||||
|
||||
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
|
||||
"127.0.0.1\tlocalhost\n")
|
||||
"127.0.0.1\tlocalhost\n127.0.0.1\tdistrobuilder\n")
|
||||
|
||||
err = generator.RunLXC(cacheDir, rootfsDir, image,
|
||||
shared.DefinitionFile{Path: "/etc/hosts"})
|
||||
@ -43,7 +43,7 @@ func TestHostsGeneratorRunLXC(t *testing.T) {
|
||||
}
|
||||
|
||||
validateTestFile(t, filepath.Join(cacheDir, "tmp", "etc", "hosts"),
|
||||
"127.0.0.1\tlocalhost\n")
|
||||
"127.0.0.1\tlocalhost\n127.0.0.1\tdistrobuilder\n")
|
||||
validateTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
|
||||
"127.0.0.1\tlocalhost\n127.0.0.1\tLXC_NAME\n")
|
||||
|
||||
@ -53,7 +53,7 @@ func TestHostsGeneratorRunLXC(t *testing.T) {
|
||||
}
|
||||
|
||||
validateTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
|
||||
"127.0.0.1\tlocalhost\n")
|
||||
"127.0.0.1\tlocalhost\n127.0.0.1\tdistrobuilder\n")
|
||||
}
|
||||
|
||||
func TestHostsGeneratorRunLXD(t *testing.T) {
|
||||
@ -81,7 +81,7 @@ func TestHostsGeneratorRunLXD(t *testing.T) {
|
||||
}
|
||||
|
||||
createTestFile(t, filepath.Join(cacheDir, "rootfs", "etc", "hosts"),
|
||||
"127.0.0.1\tlocalhost\n")
|
||||
"127.0.0.1\tlocalhost\n127.0.0.1\tdistrobuilder\n")
|
||||
|
||||
err = generator.RunLXD(cacheDir, rootfsDir, image,
|
||||
shared.DefinitionFile{Path: "/etc/hosts"})
|
||||
|
Loading…
Reference in New Issue
Block a user