map power button to be used by KVM virtual machine?


Recommended Posts

  • 1 year later...

I am no expert on Unriad, but linux has a config file that lets you choose a command to send if the power-button is pressed. So if you can find a way to start a VM from command line you can I theory  start a VM with the power-button.

Unfortunately that is going to clash with the power down system of unraid. When unraid receives a power button signal, it starts the shutdown process. Remapping that to start a VM is probably not a good idea.
Link to comment
  • 3 months later...

I know this is an old thread but is the only on that came up when searching how to solve this issue, after some research and testing I have successfully used the power button to start and shutdown a vm.

 

I discovered that  /etc/acpi/acpi_handler.sh is the script that is run on a short press of the power button, so i edited that script to run another script instead of shutting down unraid.

 

Below is my edited acpi_handler.sh modified to run my custom script vmstartstop.sh instead of shutting down.

 

nano /etc/acpi/acpi_handler.sh

 

#!/bin/sh
# Default acpi script that takes an entry for all actions
# limetech - power off via webGui

IFS=${IFS}/
set $@

case "$1" in
  button)
    case "$2" in
      power) /mnt/disk1/vm/vmstartstop.sh
         ;;
      *) logger "ACPI action $2 is not defined"
         ;;
    esac
    ;;
  *)
    logger "ACPI group $1 / action $2 is not defined"
    ;;
esac

 

 

vmstartstop.sh checks to see if my vm named windows10 is running and starts it if it is not running if it is running it will shut down the windows10 vm.(not my script this is part of another script i found on another forum that does exactly what is needed)

 

#! /bin/bash

  # only start domain if it's not already running
  if ! virsh list | grep -q " ${Windows10} .*running" ; then
    virsh start "Windows10"
  else
    # optionally shutdown domain otherwise
    virsh shutdown "Windows10"
  fi

 

I hope this helps others , as stated previously in the thread you will no longer be able to shut down unraid using the power button and will need to use the web gui to stop the array then power down or restart unraid.

 

  • Like 1
  • Thanks 1
  • Upvote 1
Link to comment
  • 4 years later...
  • 7 months later...

Well, it has been yet a while again, but I similarly ran into the issue of the script not running, even though the server would no longer shut down.  I did some more troubleshooting, and it appears there is some kind of issue calling the second script from the first.  I do not care about being able to shut down the vm with the button, only starting it, and since virsh has its own error handling I was able to use this modification of the acpi script:

#!/bin/sh
# Default acpi script that takes an entry for all actions
# limetech - power off via webGui

IFS=${IFS}/
set $@

case "$1" in
  button)
    case "$2" in
      power) virsh start "your_vm_name_here_in_quotes" &> /dev/null
         ;;
      *) logger "ACPI action $2 is not defined"
         ;;
    esac
    ;;
  *)
    logger "ACPI group $1 / action $2 is not defined"
    ;;
esac
Link to comment
  • 2 weeks later...

And also, if you want your changes to persist reboot (as Unraid holds /etc in RAM), you should create a user script that runs at the startup of array, with the command:

sed -i -e 's/\/sbin\/init 0/virsh start "your_vm_name_here_in_quotes" \&> \/dev\/null/g' /etc/acpi/acpi_handler.sh

This way it will replace the normal shutdown command in the file with your "start vm" command on every boot. 

 

EDIT: 

Beginning with Unraid 6.10, there are now additional modifications that need to be made, per this thread:

I therefore added the following lines to my script:

/etc/rc.d/rc.acpid restart
sed -i -e 's/#HandlePowerKey=poweroff/HandlePowerKey=ignore/g' /etc/elogind/logind.conf
sed -i -e 's/#HandleSuspendKey=suspend/HandleSuspendKey=ignore/g' /etc/elogind/logind.conf
sed -i -e 's/#HandleHibernateKey=hibernate/HandleHibernateKey=ignore/g' /etc/elogind/logind.conf
/etc/rc.d/rc.elogind restart

 

 

 

Edited by Iron_Eagl
Changes to Unraid
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.