From 3d1d522504ab8739b7c647f2a9b4a242552c9fcd Mon Sep 17 00:00:00 2001 From: Raj Perera Date: Tue, 28 Dec 2021 03:29:06 +0000 Subject: [PATCH 1/2] feat: ability to wake up from various domain states --- virtwold.go | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/virtwold.go b/virtwold.go index ca2a50b..b42dedf 100644 --- a/virtwold.go +++ b/virtwold.go @@ -111,20 +111,32 @@ func WakeVirtualMachine(mac string) bool { for _, list := range xmlquery.Find(querydoc, "//domain/devices/interface/mac/@address") { // Use the strings.EqualFold function to do a case-insensitive comparison of MACs if strings.EqualFold(list.InnerText(), mac) { - active, err := l.DomainIsActive(d) + stateInt, _, err := l.DomainGetState(d, 0) if err != nil { - log.Fatalf("failed to check domain active state: %v", err) + log.Fatalf("failed to check domain state: %v", err) } - // Check that the system isn't already active - // Silly that "active" is an int, not a bool - if active != 0 { - fmt.Printf("System %s is already awake\n", d.Name) - } else { + state := libvirt.DomainState(stateInt) + // fmt.Printf("Domain state is %v\n", state) + + switch state { + case libvirt.DomainShutoff, libvirt.DomainCrashed: fmt.Printf("Waking system: %s at MAC %s\n", d.Name, mac) if err := l.DomainCreate(d); err != nil { log.Fatalf("Failed to start domain: %v", err) } + case libvirt.DomainPmsuspended: + fmt.Printf("PM Wakeup system: %s at MAC %s\n", d.Name, mac) + if err := l.DomainPmWakeup(d, 0); err != nil { + log.Fatalf("Failed to pm wakeup domain: %v", err) + } + case libvirt.DomainPaused: + fmt.Printf("Resume system: %s at MAC %s\n", d.Name, mac) + if err := l.DomainResume(d); err != nil { + log.Fatalf("Failed to resume domain: %v", err) + } + default: + fmt.Printf("System %s is in a state that cannot be woken up. State: %d", d.Name, state) } } } From 224a54b84027ae741cda00ff8e829d413b021711 Mon Sep 17 00:00:00 2001 From: Raj Perera Date: Mon, 27 Dec 2021 22:56:00 -0500 Subject: [PATCH 2/2] fix: missing newline after log msg --- virtwold.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virtwold.go b/virtwold.go index b42dedf..f00da00 100644 --- a/virtwold.go +++ b/virtwold.go @@ -136,7 +136,7 @@ func WakeVirtualMachine(mac string) bool { log.Fatalf("Failed to resume domain: %v", err) } default: - fmt.Printf("System %s is in a state that cannot be woken up. State: %d", d.Name, state) + fmt.Printf("System %s is already running or in a state that cannot be woken from. State: %d\n", d.Name, state) } } }