theforeman / foreman_fog_proxmox

Foreman plugin to add Proxmox compute resource based on fog-proxmox gem
GNU General Public License v3.0
105 stars 30 forks source link

Mac Address Change when Edit Host #213

Closed danielgohlke closed 1 year ago

danielgohlke commented 2 years ago

Describe the bug If values such as CPU, memory or similar are changed in a VM via the WebUI of foreman (Edit VM), a new NIC Mac address is generated each time. This Mac address is not known to foreman and its DHCP, so the VM in question gets a new IP from the DHCP and the name resolution no longer works.

Foreman Version Foreman 2.5.4

Proxmox Version 7.0-8

To Reproduce Steps to reproduce the behavior:

  1. Go to Foreman -> Hosts -> All Hosts
  2. Click on a Host
  3. Click on Edit
  4. Click on Virtual Machine Tab
  5. Check the CPU Box for example
  6. Edit CPU Core count value
  7. Scroll down to Submit
  8. Click on Submit

Expected behavior The MAC Address should not be changed

Screenshots

pve2 - Proxmox Virtual Environment 2021-10-26 12-37-30

Desktop (please complete the following information):

danielgohlke commented 2 years ago

Hello @tristanrobert,

Have you had a chance to look at the issue?

bk-lg commented 2 years ago

I experienced the same bug while building a lab setup. I have near to zero ruby programming skills but feel free to reach out to me, if I can support you in solving this bug.

bk-lg commented 2 years ago

@tristanrobert is thera any update on this issue or a way to help you with this?

danielgohlke commented 2 years ago

would also like to help you if you tell me where I can start to look (file)

mleo2003 commented 2 years ago

Not to bring up old issues, but I also ran into this recently, and I did some digging. This seems to be a behavior of the Proxmox API whenever an edit to networking is done without supplying the MAC address.

This issue on their forum from 2020 shows it happens even from the CLI: https://forum.proxmox.com/threads/proxmox-api-change-vm-network-configuration.68764/

As does this from 2015: https://forum.proxmox.com/threads/api-changes-mac-address-unessescarily.21625/

I believe the previous behavior was for the plugin to return the MAC address back to the API on update calls, to avoid having Proxmox regenerate new MAC addresses like this, but that seems to have broken (likely due to other changes related to things like #223). The "fix" should be to ensure the MAC (upper or lower case, from my CLI tests) is always given back to the API when any other edit of the VM is done (as this happens even when editing number of CPU cores as the original issue shows).

bk-lg commented 2 years ago

@mleo2003 thank you for researching this behaviour. I hope there will now a way to fix this.

gerg91 commented 2 years ago

This definitely seems to be an issue with Proxmox's API, but surprisingly it doesn't happen when using pvesh.

I tested on both 6.2-4 and 7.1-7 (seen in example below)

From api:

root@foreman:~# curl -s -k -H "$PROXMOX_HEADER" -X GET $PMX/api2/json/nodes/$NODE/qemu/100/config|jq .data.net0
"virtio=62:75:64:B5:E5:0F"
root@foreman:~# curl -s -k -H "$PROXMOX_HEADER" -X POST $PMX/api2/json/nodes/$NODE/qemu/100/config -d 'net0=virtio=52:54:00:de:bc:20' 
{"data":"UPID:$PMX:002FE784:05A46643:62858671:qmconfig:100:root@pam!foreman-test:"}
root@foreman:~# curl -s -k -H "$PROXMOX_HEADER" -X GET $PMX/api2/json/nodes/$NODE/qemu/100/config|jq .data.net0
"virtio=DA:72:1D:D6:8A:65"
root@foreman:~# curl -s -k -H "$PROXMOX_HEADER" -X POST $PMX/api2/json/nodes/$NODE/qemu/100/config -d 'net0=virtio=52:54:00:de:bc:20' 
{"data":"UPID:$PMX:002FE7DE:05A46AF9:6285867D:qmconfig:100:root@pam!foreman-test:"}
root@foreman:~# curl -s -k -H "$PROXMOX_HEADER" -X GET $PMX/api2/json/nodes/$NODE/qemu/100/config|jq .data.net0
"virtio=FE:A7:79:11:E5:01"

Using pvesh:

root@proxmox:~# pvesh get /nodes/$PMX/qemu/100/config --output-format=json|jq .net0
"virtio=FE:A7:79:11:E5:01"
root@proxmox:~# pvesh set /nodes/$PMX/qemu/100/config -net0 virtio=52:54:00:de:bc:20
update VM 100: -net0 virtio=52:54:00:de:bc:20
root@proxmox:~# pvesh get /nodes/$PMX/qemu/100/config --output-format=json|jq .net0
"virtio=52:54:00:de:bc:20"

According to the Proxmox API docs pvesh set is the equivalent of PUT, while pvesh create is a POST. I attempted the curl command above with a PUT, but unfortunately no dice.

I think we're at the mercy of proxmox on this one, I'm personally looking into some sort of remote execution to reactively change the mac back using pvesh, at least for now.

As long as hotswapping the nic is disabled, having it changed back shortly after editing the host shouldn't cause an issue. With hotswapping enabled the MAC changes go through while the system is running, which isn't ideal in a prod environment, ha.

bk-lg commented 2 years ago

I think this is no real solution. Many networks requires specific mac adresses. Wouldn't if better to read the mac before each request and set it explicitly with every call to the api or do I understand this wrong?

gerg91 commented 2 years ago

I was just showing when you make command line calls to the Proxmox API it still behaves the same. Meaning this is a bug with Proxmox's API.

In the top example I made two calls to the api asking for a specific MAC, it ignored the requested MAC changed to another random one each time.

mleo2003 commented 2 years ago

Just to follow up again, I now see this behavior on any change to the managed host, including just adding or removing classes, etc. I even tried to not manage the network card details, and it still does this.

At this point, I may have to unmanage hosts to keep this from constantly making changes.