From 4330d40012b9fc66ac198548702eefc26d497416 Mon Sep 17 00:00:00 2001 From: Scott Ellis Date: Fri, 15 Dec 2023 00:47:10 -0800 Subject: [PATCH] Take the URI to the libvirtd as a command-line argument. --- README.md | 7 +++++-- virtwold.go | 10 ++++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eab27db..b68231d 100644 --- a/README.md +++ b/README.md @@ -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`). diff --git a/virtwold.go b/virtwold.go index b2e3a19..f475f23 100644 --- a/virtwold.go +++ b/virtwold.go @@ -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) }