hashicorp / packer-plugin-hyperv

Packer plugin for Hyper-V Builder
https://www.packer.io/docs/builders/hyperv
Mozilla Public License 2.0
18 stars 24 forks source link

SSH timeout because IP address changes #17

Open ghost opened 3 years ago

ghost commented 3 years ago

This issue was originally opened by @matthew-hickok as hashicorp/packer#5642. It was migrated here as a result of the Packer plugin split. The original body of the issue is below.


I'm getting stuck creating a Hyper-V image for CoreOS. I've get everything working on VMware Workstation, but with Hyper-V I get stuck waiting for SSH to become available. I noticed that when Packer initially creates the VM, it boots to disc, has one IP address, and then when it reboots after the install to hard disk, the IP address is different. So it makes sense that SSH times out.

How do people typically get around this? And how does Packer normally detect the IP address of the guest?

camjjack commented 2 years ago

I had a similar issue creating a Hyper-V image for Arch Linux. I was able to get around it by configuring the dhcp client to use the hostname in its dhcp request rather than a unique id which changed between the install media and the new OS. No idea if this makes sense with CoreOS or not.

For Arch I have the following in my install script. Obviously I also set the hostname to the same as the install media.

# ensure hostname is used for dhcp (avoid the first-run getting a different IP to the arch iso)
sed -i 's/^#hostname/hostname/' /etc/dhcpcd.conf
sed -i 's/^duid/#duid/' /etc/dhcpcd.conf
domoran commented 1 year ago

Well basically there are two options that you can use for configuring your guest network: DHCP or Static IP

Packer does not provide any DHCP functionality out of the box, neither does Hyper-V ... That means if your guest starts with DHCP configured you need to make sure that packer creates it on a network with a DHCP server.

Here comes the switch into play. If you have the machine use an "external" switch, it means that the Machine will be put on the same network as your host server (i.e. where HyperV runs on). So the success will depend on if there is a DHCP server on your network. As always with DHCP, you are not supposed to know the IP of the machine, it is chosen from a range by the DHCP server. That means, that in this case, you should not try to access the server via its IP address, but via its hostname. Usually your DHCP Server and your DNS Server should be together - if you for example have a FritzBox it will give the hosts an IP and assign their hostname on the internal DNS Server. If you have a complex firewall on your network you will have the same. So basically it runs down, that when your hostname is configured on your guest to be "mytestserver", that you should be able to reach your VM via ssh under mytestserver.yourdomain.com:22 ... So the correct way to address the VM when using DHCP is to use a hostname instead of an IP to connect to the machine. There are "hacks", where you somehow try to communicate the IP of the machine to the packer process, but I would not recommend them.

So now lets talk about the second options, static IP addresses. In this case, you can in your boot script configure the IP address of the guest statically, e.g. 192.168.2.1 / 24 ... In this case, you will always know how to reach your VM. but you need to make sure that your host server can actually reach the VM. This is only possible, when the host PC is in the same network as the guest. To achieve this you can use an "internal" Switch. However when creating an internal switch by default, it will not have a static IP address set. So that means, you need to find the windows network adapter of the internal switch and assign a static IP to it in the same address range as the VM, e.g. 192.168.2.2 / 24 ... This way, your server will be able to reach the VM via the assigned static ip. However the problem is, that unless your Host Server is configured as a gateway and you explicitly configure your host ip as a gateway inside your boot script, you will not be able to access the internet from the virtual machine.

There are solutions to this problem too. You can share your internet connection with the VM (effectively setting your PC up as a gateway), install a proxy server on your pc to access the internet, etc ...

Anyway where does this leave us?

You have to keep in mind, that with packer depending on SSH access to the virtual machine, the host server effectively is part of the "build environment" of that machine, so inevitably you need to configure your HyperV environment correctly for the build to work smoothly. Since your Packer Image is supposed to work in a certain environment your HyperV should be configured the same way, as the environment in which the packer image will be working. Will you deploy it inside a private network with access via a gateway and DHCP? Create a gateway VM (e.g. opensense, or simpler), give it two network cards, set one to an external switch, one to an internal switch with a fixed ip and network, configure DHCP and have packer deploy the machine connected to the internal switch with DHCP, just like in the environment in which the image will be deployed. You want to use static IPs ? Do it, but make sure the VM has internet access by configuring the network and host accordingly.

I hope this clears things up or maybe I am making stuff to complicated and there is a dead simple receipe.