[TUTORIAL] Wake On LAN (WOL) for VMs and Containers

you have to configure it to watch the vmbr that is connected to your pfSense system sending the packets.

did you modify the script based on the pfsense WoL port number?
 
pfSense is on vmbr2. i have modified the script with both the interfaces (vmbr0 and vmbr2) but no success. also use port 40000 instead of 9. if you look at the script it basically does a lookup for the mac address you are sending the wol packet to, and then attempts to start the vm which has that "virtual" nic with that mac address. so not sure if the interface (vmbr0 or vmbr2) should really matter, as long you have one of them to route your packet to the mac address. i could be wrong though.
 
pfSense is on vmbr2. i have modified the script with both the interfaces (vmbr0 and vmbr2) but no success. also use port 40000 instead of 9. if you look at the script it basically does a lookup for the mac address you are sending the wol packet to, and then attempts to start the vm which has that "virtual" nic with that mac address. so not sure if the interface (vmbr0 or vmbr2) should really matter, as long you have one of them to route your packet to the mac address. i could be wrong though.
What does the debug output show if you watch it from the console when you send a WoL packet?
 
Great script, it's working like a charm.
Is there any possibility to have that script listening on port 9 AND 40000 so normal clients and OPNsense can wake up vms?

Or shall I simply create another script with the 2nd port set?!

Best, goeste
 
Thanks. You are free to modify it.
It's about 30 years old and has been modified numerous times.
 
Hello all,

I have updated the script to support both starting and resuming vm's using WOL:

To the other people that tried:
(There seems to be a bug or a feature in : qm that when used with the -verbose flag.
suspended and paused systems show as running.)

Bash:
#!/bin/bash

#Define bridge device in use on your system!
# E.G: vmbr0 or vmbr1

vmbr="vmbr0"
#We will use UDP port 9
portnr="9"

# fix: 10-12-24 Can now besides starting also "unpause/resume vm's" (also via Moonlight I tested)
# I will give "NO support" nor "emply" that is this code deemed fit for use!
# Use at your own risk! (people should use API instead)

while true; do

  sleep 5
  wake_mac=$(tcpdump -c 1 -UlnXi ${vmbr} ether proto 0x0842 or udp port ${portnr} 2>/dev/null |\

  sed -nE 's/^.*20:  (ffff|.... ....) (..)(..) (..)(..) (..)(..).*$/\2:\3:\4:\5:\6:\7/p')

  echo "Captured magic packet for address: \"${wake_mac}\""

  echo -n "Looking for existing VM: "

  matches=($(grep -il ${wake_mac} /etc/pve/qemu-server/*))

  if [[ ${#matches[*]} -eq 0 ]]; then

    echo "${#matches[*]} found"

  echo -n "Looking for existing LXC: "

  matches=($(grep -il ${wake_mac} /etc/pve/lxc/*))

  if [[ ${#matches[*]} -eq 0 ]]; then

    echo "${#matches[*]} found"

    continue

  elif [[ ${#matches[*]} -gt 1 ]]; then

    echo "${#matches[*]} found, using first found"

  else

    echo "${#matches[*]} found"

  fi

  vm_file=$(basename ${matches[0]})

  vm_id=${vm_file%.*}

  details=$(pct status ${vm_id} -verbose | egrep "^name|^status")

  name=$(echo ${details} | awk '{print $2}')

  status=$(echo ${details} | awk '{print $4}')

  if [[ "${status}" != "stopped" ]]; then

    echo "SKIPPED CONTAINER ${vm_id} : ${name} is ${status}"

  else

    echo "STARTING CONTAINER ${vm_id} : ${name} is ${status}"

    pct start ${vm_id}

  fi

    continue

  elif [[ ${#matches[*]} -gt 1 ]]; then

    echo "${#matches[*]} found, using first found"

  else

    echo "${#matches[*]} found"

  fi

  vm_file=$(basename ${matches[0]})

  vm_id=${vm_file%.*}

  # (Due to 'bug/odd feature' in qm): qm status ${vm_id} -verbose
  # will show paused and suspended vms as 'status: running')
  # solution querying only name using 'qm status -verbose'

  name=$(qm status ${vm_id} -verbose | egrep "^name" | awk '{print $2}')

  # And querying the status using just 'qm status'
  status=$(qm status ${vm_id} | awk '{print $2}')


  #So now it is possible to start/resume stopped/paused/suspended vms!
  if [[ "${status}" != "stopped" ]]; then

         if [[ "${status}" == "suspended" ]] || [[ "${status}" == "paused" ]]; then
         echo "Resuming VM ${vm_id}  Status of ${name} was ${status}"
         qm resume ${vm_id}
         elif [[ "${status}" != "suspended" ]] || [[ "${status}" != "paused" ]]; then
         echo "SKIPPED VM ${vm_id} because ${name} is ${status}"
         fi
  else
    echo "STARTING VM ${vm_id} : ${name} is ${status}"
    qm start ${vm_id}
  fi
done

Good luck! It works fine with moonlight to wake up a dozed off GameVM
 
Last edited:
  • Like
Reactions: Onoitsu2
My proxmox node has a dedicated interface on vmbr0 and all my vms and containers use vmbr2. ive tried wol_hack.sh with both interfaces and it wont capture incoming magic packets from any client (moonlight in my case). no error messages either. having said that my pfsense instance (on the same node) can send wol packets successfully to vms and wakes them up in 2 secs.

any ideas?
I've found some software that sends WOL sends it to subnet 255.255.255.255, but you need use 255.255.255.0 for this, so your proxmox host should see it. In my own testing and implementation of this, with UpSnap and some custom Ping and Shutdown commands, I have had to mess with all kinds of things. I have VM's and LXC's on their own isolated SDN's and can spin them up, so long as you are sending from an IP within the same range as the proxmox host IP, so like 192.168.1.0/24, versus 192.168.2.0/24 even while they are on a different numbering scheme entirely.
 
Not a network genius but:

Correct me if I am wrong but from what I understand is:

The host that is sending the WOL packet must be on the vmbr2,
as each bridge would have their own Broadcasting range.

As either the networks for vmbr1 vmbr2 are split in to different netmasks,
with different gateways or there is a different range all together.

Broadcasting stays with in its netmask

WOL is sent as a broacast on Mac address.

Did you cover the obvious? Firewall rules?
-Where are you triggering the WOL request from?

Maybe you need to add a route?
 
Last edited:

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!