Skip to content

Instantly share code, notes, and snippets.

@mtongnz
Created November 19, 2020 10:10
Show Gist options
  • Save mtongnz/77ea8adb481908477e3f830620d111d8 to your computer and use it in GitHub Desktop.
Save mtongnz/77ea8adb481908477e3f830620d111d8 to your computer and use it in GitHub Desktop.
Hi-jack an unraid server power button to start and stop a VM
################################################################
# edit file: /boot/config/acpi_handler.sh
# change "power)" line to point at vmStartStop script location (shown at top of web UI when editing User Script)
################################################################
#!/bin/sh
# Default acpi script that takes an entry for all actions
IFS=${IFS}/
set $@
case "$1" in
button)
case "$2" in
power) . /boot/config/plugins/user.scripts/scripts/vmStartStop/script
;;
*) logger "ACPI action $2 is not defined"
;;
esac
;;
*)
# logger "ACPI group $1 / action $2 is not defined"
;;
esac
################################################################
# User Script: powerButtonRepurpose
# set to "run at first array start only"
################################################################
#!/bin/bash
cp /boot/config/acpi_handler.sh /etc/acpi/acpi_handler.sh && chmod 755 /etc/acpi/acpi_handler.sh
################################################################
# User Script: vmStartStop
# change "MainPC" to the name of your VM
# optional: to boot VM when unraid starts, set to "run at first array start only"
# In windows, you can set what the power button does (ie. hibernate) but below must send a shutdown command still
################################################################
#! /bin/bash
# if domain is running, shut down
if virsh list | grep "MainPC .*running" ; then
virsh shutdown "MainPC"
# resume domain if it's paused
elif virsh list | grep "MainPC .*paused" ; then
virsh resume "MainPC"
elif virsh list | grep "MainPC .*pmsuspended" ; then
virsh dompmwakeup "MainPC"
# otherwise start domain
else
virsh start "MainPC"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment