Prevent Idle when VM is running(KVM)

General support questions
Post Reply
hunter86_bg
Posts: 2019
Joined: 2015/02/17 15:14:33
Location: Bulgaria
Contact:

Prevent Idle when VM is running(KVM)

Post by hunter86_bg » 2017/06/16 09:54:23

Hello people,

I'm using "IdleAction=poweroff" in /etc/systemd/logind.conf but I want to prevent Workstation getting into Idle mode when a VM (I use KVM) is running.

I was thinking about "systemd-inhibit" but I am not sure it will do the trick.Do you have any ideas?

hunter86_bg
Posts: 2019
Joined: 2015/02/17 15:14:33
Location: Bulgaria
Contact:

Re: Prevent Idle when VM is running(KVM)

Post by hunter86_bg » 2017/06/26 20:48:26

Here is a brute-force method for anyone who needs this:

Set in root's crontab a task that runs every 5 min executing the following script:

Code: Select all

VM_COUNT=$(ps -aux | grep ^[q]emu | wc -l)

if [ "$VM_COUNT" -ge "1" ]
	then systemd-inhibit --mode=block sleep 300 &
fi
This script will count the running VMs and if they are equal to 1 or more (you can use "$VM_COUNT" -gt "0" ) - then just run a sleep process for 5 min with a systemd-inhibitor. Once the script is over (and no more VMs are running) - the inhibitor is also removed.

Post Reply