Take the URI to the libvirtd as a command-line argument.

This commit is contained in:
Scott Ellis 2023-12-15 00:47:10 -08:00
parent 1b5416f2ca
commit 4330d40012
2 changed files with 11 additions and 6 deletions

View File

@ -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`).

View File

@ -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) {
@ -53,7 +55,7 @@ func main() {
if err != nil {
log.Fatalf("Error with packet: %v", err)
}
WakeVirtualMachine(mac)
WakeVirtualMachine(mac, libvirturi)
}
}
@ -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)
}