You are not logged in.

#1 2015-09-05 09:23:52

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

[Solved] How to prevent poweroff and reboot if "specific condition"?

Hi Arch community!

Is there a way to prevent poweroff and reboot if some drive is mounted? Like if it is mounted, pressing "Power" button on system unit won't do anything, neither commands "poweroff" and "reboot" will. I need it because I have TrueCrypt volume. And it is supposed to be unmounted with "truecrypt" program. And I might forget to unmount it manually and power the machine down. It would be awesome if while this volume is mounted you couldn't turn off computer with any command/signal.

Last edited by Mr. Alex (2015-09-12 07:47:16)

Offline

#2 2015-09-05 10:04:26

rikn00
Member
Registered: 2014-06-26
Posts: 21

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

Isn't systemd able to do that automatically with some configuration?

Edit: I mean that systemd unmounts the volume automatically

Last edited by rikn00 (2015-09-05 10:05:36)

Offline

#3 2015-09-05 10:40:59

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

Not TrueCrypt volume AFAIK.

Offline

#4 2015-09-05 10:53:02

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,962
Website

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

You could create a systemd unit that runs some script that unmounts your truecrypt volum before shutdown

[Unit]
Description=Your description here
Before=shutdown.target
[Service]
ExecStart=/bin/true
ExecStop=/your/script/here
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

Offline

#5 2015-09-06 15:43:56

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

ugjka, thanks, but I would like exactly prevention of shutting down.

Offline

#6 2015-09-06 16:06:06

Chazza
Wiki Maintainer
Registered: 2013-06-02
Posts: 506

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

Assuming your WM or DE calls /usr/bin/poweroff to shut down (I don't know whether this is true or not) you could try something like creating the file /usr/local/bin/poweroff as executables in /usr/local/bin take precedence over those in /usr/bin. Then in that file you could add something along the lines of the following:

#!/bin/sh

if [ ! <volume is mounted> ]; then
  /usr/bin/poweroff
fi

I don't know what <volume is mounted> should be as I have no experience with TrueCrypt, you will probably know better than I. You can do the same for reboot as well. Like I said though, this may not work because your DE may not use /usr/bin/power or /usr/bin/reboot

EDIT: If it's a DE like Xfce for instance, you could try a similar approach, but overriding xfce4-session-logout instead.

Last edited by Chazza (2015-09-06 16:20:40)

Offline

#7 2015-09-10 19:26:46

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

Chazza, thanks, I got it. When I perform "poweroff", it doesn't call local bin first, goes straight to /usr/bin. But that can be fixed by aliasing. How do I configure power button on system unit that way? I use IceWM and no DE. If I can configure power button, the question will be solved. :-)

Offline

#8 2015-09-10 21:48:51

Chazza
Wiki Maintainer
Registered: 2013-06-02
Posts: 506

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

I think that logind handles the power key. I don't think you can tell logind to do anthing fancy like run a script though what you could do is stop logind from handling the power key altogether and then perhaps the power key can be handled some way from within icewm? Not too sure on that one but anyway - to stop logind from handling the power button, edit /etc/systemd/logind.conf and change the HandlePowerKey value to ignore and then restart the systemd-logind service.

Offline

#9 2015-09-12 07:47:01

Mr. Alex
Member
Registered: 2010-08-26
Posts: 623

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

Chazza, thanks, I got it working now! :-D

Offline

#10 2015-09-12 08:30:02

genericity
Member
Registered: 2014-09-20
Posts: 37

Re: [Solved] How to prevent poweroff and reboot if "specific condition"?

You should use systemd-inhibit to run a script. The system can't go poweroff / reboot until that script exits.
http://www.freedesktop.org/software/sys … hibit.html

That script could be:

#!/bin/bash
while (mount | grep <insert your mount point here> > /dev/null); do
  sleep 1
done

BTW, if you run systemd-inhibit in some console/xterm in a desktop environment (e.g. KDE), and use DE's poweroff function, the system will go poweroff because DE kills xterm (along with systemd-inhibit) before calling poweroff. So you should either run the command outside of DE, or nohup, or as a systemd job.

Last edited by genericity (2015-09-12 08:41:24)

Offline

Board footer

Powered by FluxBB