Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #608 from luxas/write_hostname
Browse files Browse the repository at this point in the history
Write /etc/hostname. Needed get the right hostname for e.g. Ubuntu 20
  • Loading branch information
luxas authored Jun 2, 2020
2 parents 0b5abce + af12e7c commit 3bdc85d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/dmlegacy/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ func copyToOverlay(vm *api.VM) error {
}

// Write /etc/hosts for the VM
if err := writeEtcHosts(vm, mp.Path, vm.GetUID().String(), ip); err != nil {
if err := writeEtcHosts(mp.Path, vm.GetUID().String(), ip); err != nil {
return err
}

// Write the UID to /etc/hostname for the VM
if err := writeEtcHostname(mp.Path, vm.GetUID().String()); err != nil {
return err
}

Expand Down Expand Up @@ -173,7 +178,7 @@ func copyKernelToOverlay(vm *api.VM, mountPoint string) error {

// writeEtcHosts populates the /etc/hosts file to avoid errors like
// sudo: unable to resolve host 4462576f8bf5b689
func writeEtcHosts(vm *api.VM, tmpDir, hostname string, primaryIP net.IP) error {
func writeEtcHosts(tmpDir, hostname string, primaryIP net.IP) error {
hostFilePath := filepath.Join(tmpDir, "/etc/hosts")
empty, err := util.FileIsEmpty(hostFilePath)
if err != nil {
Expand All @@ -188,6 +193,11 @@ func writeEtcHosts(vm *api.VM, tmpDir, hostname string, primaryIP net.IP) error
return ioutil.WriteFile(hostFilePath, content, 0644)
}

func writeEtcHostname(tmpDir, hostname string) error {
hostnameFilePath := filepath.Join(tmpDir, "/etc/hostname")
return ioutil.WriteFile(hostnameFilePath, []byte(hostname), 0644)
}

// Generate a new SSH keypair for the vm
func newSSHKeypair(vm *api.VM) (string, error) {
privKeyPath := path.Join(vm.ObjectPath(), fmt.Sprintf(constants.VM_SSH_KEY_TEMPLATE, vm.GetUID()))
Expand Down

0 comments on commit 3bdc85d

Please sign in to comment.