Skip to content

Commit

Permalink
Take the URI to the libvirtd as a command-line argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottESanDiego committed Dec 15, 2023
1 parent 1b5416f commit 4330d40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ When started, this daemon will use `libpcap` to make a listener on the specified

Upon receipt of a (probable) WOL packet, the daemon extracts the first MAC address (WOL packets are supposed to repeat the target machine MAC a few times).

With a MAC address in-hand, the program then connects to the local `libvirt` daemon via `/var/run/libvirt/libvirt-sock`, and gets an XML formatted list of every Virtual Machine configured (yuck). An XML query to list all of the MAC addresses in the configured VMs, and compares that with the MAC from the WOL packet. If a match is found, and the VM isn't already running, the daemon asks `libvirtd` to start the associated VM.
With a MAC address in-hand, the program then connects to a `libvirtd` daemon via , supplied libvirt URI and gets an XML formatted list of every Virtual Machine configured (yuck), and iterates through all interfaces getting the MAC address. That MAC is then compared with the MAC from the WOL packet. If a match is found, the `libvirtd` daemon is asked to start the associated VM.

## Usage
Usage is pretty staightforward, as the command only needs one argument: the name of the network interface to listen on. Specify this with the `--interface` flag (e.g., `--interface enp44s0`).
Usage is pretty staightforward, as the command needs two arguments:
1. The name of the network interface to listen on. Specify this with the `--interface` flag (e.g., `--interface enp44s0`).
2. The URI to the `libvirtd` to be used. Specify this with the `--libvirturi` flag (e.g., `qemu+tcp:///system`).


The daemon will keep running until killed with a SIGINT (`^c`).

Expand Down
10 changes: 6 additions & 4 deletions virtwold.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import (

func main() {
var iface string // Interface we'll listen on
var libvirturi string // URI to the libvirt daemon
var buffer = int32(1600) // Buffer for packets received
var filter = "udp and broadcast and (len = 102 or len = 144 or len=234)" // PCAP filter to catch UDP WOL packets

flag.StringVar(&iface, "interface", "", "Network interface name to listen on")
flag.StringVar(&iface, "interface", "eth0", "Network interface name to listen on")
flag.StringVar(&libvirturi, "libvirturi", "qemu+tcp:///system", "URI to libvirt daemon, such as qemu:///system")
flag.Parse()

if !deviceExists(iface) {
Expand All @@ -53,7 +55,7 @@ func main() {
if err != nil {
log.Fatalf("Error with packet: %v", err)
}
WakeVirtualMachine(mac)
WakeVirtualMachine(mac, libvirturi)
}
}

Expand All @@ -69,9 +71,9 @@ func GrabMACAddr(packet gopacket.Packet) (string, error) {
return "", errors.New("no MAC found in packet")
}

func WakeVirtualMachine(mac string) bool {
func WakeVirtualMachine(mac string, libvirturi string) bool {
// Connect to the local libvirt socket
connection, err := libvirt.NewConnect("qemu+tcp:///system")
connection, err := libvirt.NewConnect(libvirturi)
if err != nil {
log.Fatalf("failed to connect: %v", err)
}
Expand Down

0 comments on commit 4330d40

Please sign in to comment.