A blog about systems and statements
There’s no better welcome home than a humming PC
There’s no better welcome home than a humming PC

There’s no better welcome home than a humming PC

Every day I come home from work or whatever outing I was involved in, and I walk into my flat with my PC already booted and running. And no, it's not running all day without a break - it automatically boots when I open my door. No, there's no fancy sensor on the door - my PC boots through the magical power of Wake-On-LAN.

How it started

So I recently installed Windows 10 on my PC because I essentially gave up trying to bugfix every game I tried to run on Linux. (Btw there have been awesome strides in the world of Linux gaming and I believe everyone should give it a go before doing the unholy uninstall I did). But besides the usual Windows weirdnessess, I discovered another odd behaviour: after I shut down my PC, the ethernet port on the motherboard would remain on and the green and yellow lights would keep flickering, indicating a connection to my local network.

An ethernet cable plugged in to a port, with lights indicating activity

After some research I discovered that this was to allow a feature called Wake-On-LAN.

How to wake a slumbering beast

So there are various use cases for this, but the core ability that Wake-On-LAN (or WOL for short) grants, is triggering a boot from a network message. And, importantly, doing this trigger without an operating system (which would obviously require the machine to be running).

So how does it work? Through a wonderful invention called the magic packet. The magic packet is a special kind of basic network message (technically called an Ethernet Frame) which essentially just contains the MAC address of the target machine in a very particular format. On machines that support WOL, the hardware network adapter (technically called a NIC) listens for a magic packet with its particular MAC in the message. When it receives said message, it triggers a boot in the host machine.

But how can a NIC listen for any network traffic without a running OS? The answer lies in the somewhat complicated layered design of computer networks. Briefly, there are 7 conceptual layers to computer networking (described below), which range from the most simple piece of data (a bit), to the most complex (an HTTP request).

OSI model table provided by Wikipedia. Come to think of it, there are networking layers built on top of L7 (.e.g, the S3 protocol is built on HTTP), but I suppose it could be argued that anything beyond L6 is just part of the huge category of L7. Anyways…

The important takeaway here is that a network can (and does) function on a subset of these layers, and the lower your layer, the less computing power you need to parse the message. This is why network switches don't need full operating systems to work. Everything they do is typically at L3. And it's also why our wonderful WOL NIC doesn't need an OS: to interpret a WOL message (magic packet), you just need to interpret an L2 message, which is super simple.

Another question you might have is how exactly a magic packet gets distributed. The short answer is that the sending device performs an Ethernet Broadcast, essentially sending the exact same packet to all devices it can find on the network. And before you ask, yes, this has its limitations: because the packet only exists at L2, it can't get routed, so you're limited to WOL'ing the devices in your immediate LAN. But that's fine for my use case.

Automation station

So this is wonderful Jans, but now how do you wake your computer when you walk through the door? The solution is pretty simple: I use a Raspberry Pi to regularly check if my phone is connected to the network, and if it is, it sends a WOL packet to my PC. So when I walk through the door (or moments before), my phone auto-connects to my WiFi network, and that triggers the Pi to send the WOL, and my PC boots automagically!

There's a bit more logic that goes into it (e.g., ensuring we only send the WOL packet max once every 12 hours), but it really is that simple. On Ubuntu there's a cool tool called wakeonlan that does exactly what you think it does, and a built-in tool called arp which scans your LAN for devices. Here's the script I wrote to auto-WOL my PC when my phone connects:




last_wol=$(cat "$last_wol_file")
now=$(date +"%s")

declare -i time_passed_h="(now - last_wol)/(60*60)"

echo "Hours since last wol: $time_passed_h"

if [[ "$time_passed_h" -le 12 ]]; then
        echo "Doing nothing."

echo "Scanning..."
found=$(/usr/sbin/arp | tail -n +2 | grep "$PHONE_MAC" | wc -l)

if [[ "$found" -eq 1 ]]; then
        echo "Phone found! Sending wol..."
        wakeonlan "$PC_MAC"
        echo "$now" > "$last_wol_file"
        echo "Phone not found. Doing nothing"

Call that script on a cron (ensure it doesn't run overnight else your PC may boot at strange times), and Bob's your uncle! Enjoy having your PC up and humming every time you get home.

Small caveat

For obvious reasons this will not work during Load Shedding :'(
Perhaps it's time to invest in an inverter?

Maak 'n opvolg-bydrae

Jou e-posadres sal nie gepubliseer word nie. Verpligte velde word met * aangedui