diff --git a/cmd/dhcp/dhcp.go b/cmd/dhcp/dhcp.go index ed82b21..194dacf 100644 --- a/cmd/dhcp/dhcp.go +++ b/cmd/dhcp/dhcp.go @@ -235,8 +235,12 @@ func main() { } if b, ok := opts[dhcp4.OptionDomainNameServer]; ok { + resolvConf := "/etc/resolv.conf" + if dest, err := os.Readlink("/etc/resolv.conf"); err == nil && dest == "/tmp/resolv.conf" { + resolvConf = "/tmp/resolv.conf" + } // Get the symlink out of the way, if any. - if err := os.Remove("/etc/resolv.conf"); err != nil && !os.IsNotExist(err) { + if err := os.Remove(resolvConf); err != nil && !os.IsNotExist(err) { log.Fatalf("resolv.conf: %v", err) } var lines []string @@ -245,7 +249,7 @@ func main() { lines = append(lines, fmt.Sprintf("search %s", string(domain))) } lines = append(lines, fmt.Sprintf("nameserver %v", net.IP(b))) - if err := ioutil.WriteFile("/etc/resolv.conf", []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil { + if err := ioutil.WriteFile(resolvConf, []byte(strings.Join(lines, "\n")+"\n"), 0644); err != nil { log.Fatalf("resolv.conf: %v", err) } } diff --git a/gokrazy.go b/gokrazy.go index c41c6e4..c06f19f 100644 --- a/gokrazy.go +++ b/gokrazy.go @@ -68,7 +68,7 @@ func watchdog() { // // - mounts /dev, /tmp, /proc, /sys and /perm file systems // - mounts and populate /etc tmpfs overlay -// - sets hostname from the /hostname file +// - sets hostname from the /etc/hostname file // - sets HTTP password from the gokr-pw.txt file // - configures the loopback network interface // @@ -87,7 +87,10 @@ func Boot(userBuildTimestamp string) error { return err } - hostnameb, err := ioutil.ReadFile("/hostname") + hostnameb, err := ioutil.ReadFile("/etc/hostname") + if err != nil && os.IsNotExist(err) { + hostnameb, err = ioutil.ReadFile("/hostname") + } if err != nil { return err } @@ -98,10 +101,13 @@ func Boot(userBuildTimestamp string) error { pw, err := ioutil.ReadFile("/perm/gokr-pw.txt") if err != nil { + pw, err = ioutil.ReadFile("/etc/gokr-pw.txt") + } + if err != nil && os.IsNotExist(err) { pw, err = ioutil.ReadFile("/gokr-pw.txt") - if err != nil { - return fmt.Errorf("could read neither /perm/gokr-pw.txt nor /gokr-pw.txt: %v", err) - } + } + if err != nil { + return fmt.Errorf("could read neither /perm/gokr-pw.txt, nor /etc/gokr-pw.txt, nor /gokr-pw.txt: %v", err) } httpPassword = strings.TrimSpace(string(pw)) diff --git a/mount.go b/mount.go index b0046bc..944ee30 100644 --- a/mount.go +++ b/mount.go @@ -8,11 +8,9 @@ import ( "syscall" ) -func mountfs() error { - if err := syscall.Mount("tmpfs", "/tmp", "tmpfs", syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_RELATIME, "size=50M"); err != nil { - return fmt.Errorf("tmpfs on /tmp: %v", err) - } - +// mountCompat deals with old FAT root file systems, to cover the case where +// users use an old gokr-packer with a new github.com/gokrazy/gokrazy package. +func mountCompat() error { // Symlink /etc/resolv.conf. We cannot do this in the root file // system itself, as FAT does not support symlinks. if err := syscall.Mount("tmpfs", "/etc", "tmpfs", syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_RELATIME, "size=1M"); err != nil { @@ -40,6 +38,23 @@ func mountfs() error { if err := ioutil.WriteFile("/etc/hosts", []byte("127.0.0.1 localhost\n::1 localhost\n"), 0644); err != nil { return fmt.Errorf("/etc/hosts: %v", err) } + return nil +} + +func mountfs() error { + if err := syscall.Mount("tmpfs", "/tmp", "tmpfs", syscall.MS_NOSUID|syscall.MS_NODEV|syscall.MS_RELATIME, "size=50M"); err != nil { + return fmt.Errorf("tmpfs on /tmp: %v", err) + } + + if err := os.Symlink("/proc/net/pnp", "/tmp/resolv.conf"); err != nil { + return fmt.Errorf("etc: %v", err) + } + + if _, err := os.Lstat("/etc/resolv.conf"); err != nil && os.IsNotExist(err) { + if err := mountCompat(); err != nil { + return err + } + } if err := syscall.Mount("devtmpfs", "/dev", "devtmpfs", 0, ""); err != nil { if sce, ok := err.(syscall.Errno); ok && sce == syscall.EBUSY {