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

Write /etc/hostname. Needed get the right hostname for e.g. Ubuntu 20 #608

Merged
merged 1 commit into from
Jun 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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