Wake-on-LAN Proxmox Backup Server

Inspired by https://wiki.archlinux.org/title/Wake-on-LAN

  1. Enable Wake on LAN (Power ON By PME) in BIOS on the target computer motherboard

     

  2. Enable WoL on the network adapter of the target computer (Linux)
    Depending on the hardware, the network driver may have WoL switched off by default.
    To query this status or to change the settings, install ethtool, determine the name of the network interface, and query it using the command:

    # ethtool enp1s0 | grep Wake-on
    Supports Wake-on: pumbag
    Wake-on: d

    The Wake-on values define what activity triggers wake up: d (disabled), p (PHY activity), u (unicast activity), m (multicast activity), b (broadcast activity), a (ARP activity), and g (magic packet activity). The value g is required for WoL to work, if not, the following command enables the WoL feature in the driver:

    # ethtool -s enp1s0 wol g

    This command might not last beyond the next reboot and in this case must be repeated via some mechanism. Common solutions are listed in the following subsections.

  3. Find MAC addres of the network card on the target computer
    To trigger WoL on a target machine, its MAC address must be known.
    To obtain it, execute the following command on the target machine:

    $ ip link
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    2: enp1s0: <BROADCAST,MULTICAST,PROMISC,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UP group default qlen 1000 link/ether 48:05:ca:09:0e:6a brd ff:ff:ff:ff:ff:ff
  4. Make WoL on the network adapter of the target computer persistent using systemd.link
    Link-level configuration is possible through systemd-networkd#link files.
    The actual setup is performed by the net_setup_link udev builtin.
    Make a file

    /etc/systemd/network/50-wired.link

    with the following content

    [Match]
    MACAddress=aa:bb:cc:dd:ee:ff
    
    [Link]
    NamePolicy=kernel database onboard slot path
    MACAddressPolicy=persistent
    WakeOnLan=magic
  5. Test the setup
    On the target machine us netcat (nc) :

    nc -ulp 9 | hexdump

    On the client machine

    wakeonlan 50:46:5d:a3:93:20

    or

    wakeonlan -i 192.168.0.201 50:46:5d:a3:93:20

    On the target machine you should see something similar to this:

    0000000 ffff ffff ffff 4650 a35d 2093 4650 a35d
    0000010 2093 4650 a35d 2093 4650 a35d 2093 4650
    0000020 a35d 2093 4650 a35d 2093 4650 a35d 2093
    0000030 4650 a35d 2093 4650 a35d 2093 4650 a35d
    0000040 2093 4650 a35d 2093 4650 a35d 2093 4650
    0000050 a35d 2093 4650 a35d 2093 4650 a35d 2093

    Shut down the target machine issuing command

    shutdown -h now

    or remotely

    /usr/bin/ssh -t root@192.168.0.201 'shutdown -h now'

    !!! DO NOT USE halt COMMAND !!! to shut down target machine. halt command puts the system into complete power-off mode, cutting out power supply on the machine completely, so network card can not communicate with network.

    On the client machine execute again:

    wakeonlan 50:46:5d:a3:93:20

    You should see target machine start to boot

    Configure Proxmox Backup Server

    Add an API token (and record the API token secret – it will only be displayed once)

    Add / or /system/tasks permission (path) for created token

    Than following request should return the number of active backup tasks (if backup task is not running 0 is returned)

    curl -s -k -H 'Authorization: PBSAPIToken=root@pam!testN:424e33be-7bc1-44c6-82b7-474860adb495' https://192.168.0.201:8007/api2/json/nodes/localhost/tasks\?running=true | jq .total

    You should modify above mentioned request using info on the proxmox web

    https://pbs.proxmox.com/docs/api-viewer/index.html#/nodes/{node}/tasks

    Instead of about mentioned bash curl command you can use python script to test if backup task is not running

    import requests
    import time
    import subprocess
    
    import warnings
    from urllib3.exceptions import InsecureRequestWarning
    
    # Suppress the InsecureRequestWarning
    warnings.filterwarnings("ignore", category=InsecureRequestWarning)
    
    url = 'https://192.168.0.201:8007/api2/json/nodes/localhost/tasks?running=true'
    headers = {'Authorization': 'PBSAPIToken=root@pam!testN:424e33be-7bc1-44c6-82b7-474860adb495'}
    
    count = 0
    
    while True:
        try:
            response = requests.get(url, headers=headers, verify=False)
            data = response.json()
            total = data['total']
    
            print(f"Total: {total}")
    
            if total == 0:
                count += 1
            else:
                count = 0
    
            if count == 10:
                print("10 consecutive occurrences of total being 0.")
                break
    
        except requests.exceptions.RequestException as e:
            if "No route to host" in str(e):
                print("No route to host. Exiting the loop.")
                break
            else:
                raise e
    
        time.sleep(5)
    
    if count == 10:
        command = ["/usr/bin/ssh", "-t", "root@192.168.0.201", "shutdown", "-h", "now"]
    
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
        output, error = process.communicate()
    
        if process.returncode == 0:
            print("Command executed successfully.")
        else:
            print("An error occurred while executing the command.")
            if error is not None:
                print("Error:", error.decode())
    
  6. than, you can write in to the /etc/crontab following

    40 23 * * *     root    /usr/bin/wakeonlan 50:46:5d:a3:93:20 > /dev/null 2>&1
    #
    1 0 * * *	root	/usr/bin/python3 /root/shutDownPBS.py >> /var/log/pbsShutDown.log 2>&1 
    #

 


Add Your Comment