Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] openvpn, dhcp, suspend and hooks
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 352
Location: Nicosia, Cyprus

PostPosted: Wed Mar 12, 2025 7:31 am    Post subject: [SOLVED] openvpn, dhcp, suspend and hooks Reply with quote

My openvpn crashes when resuming from the hibernation.
The reason is quite clear: the physical network is inactive because of the DHCP.
Both elogind and dhcpcd have hooks to handle the problem, but the solution I have found is quite "spaghetti".

dhcpcd is not aware of suspend, so the only way is to use a pre-suspend elogind hook.
On the other side, elogind is not aware of the dhcpcd procedure after resume (lost carrier, rebinding, bla bla), so the only way is a post-binding dhcpcd hook.

The two hooks work, but I find a bit strange to manage two complementary operations with different tools.
Better ideas?

/etc/elogind/system-sleep/stop-openvpn-before-suspend:
#!/bin/bash
# -- Stop openvpn before suspend
#
# NOTE: openvpn is stopped here, before suspend, but NOT
#       restarted here, because a bounded dhcp address is
#       necessary. A dhcp hook is used for that.
#       See: /etc/dhcpcd.exit-hook

case $1/$2 in
  pre/*)
    /usr/bin/logger -p daemon.info "Stopping openvpn.home before suspend."
    /etc/init.d/openvpn.home stop
    ;;
esac

/etc/dhcpcd.exit-hook:
# -- Start openvpn after suspend
#
# NOTE: the runlevel is restored here, but openvpn has been
#       stopped with an elogind hook.
#       See: /etc/elogind/system-sleep/stop-openvpn-before-suspend

if [ "${interface}" = "eth0" ]; then
    if [ "${reason}" = "BOUND" ]; then
        /usr/bin/logger -p daemon.info "Restoring runlevel after bound."
        /usr/bin/openrc
    fi
fi


Regards,
HUjuice
_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.


Last edited by hujuice on Thu Mar 13, 2025 8:28 am; edited 1 time in total
Back to top
View user's profile Send private message
szatox
Advocate
Advocate


Joined: 27 Aug 2013
Posts: 3550

PostPosted: Wed Mar 12, 2025 11:45 am    Post subject: Reply with quote

I used to have an acpid hook that would restart my wifi after suspend. It was basically a crash recovery action.
It didn't even matter whether it was triggered by suspend event or resume event, because a suspended machine doesn't do anything useful anyway, so the restart would be relayed until after resume.
_________________
Make Computing Fun Again
Back to top
View user's profile Send private message
hujuice
Guru
Guru


Joined: 16 Oct 2007
Posts: 352
Location: Nicosia, Cyprus

PostPosted: Thu Mar 13, 2025 8:27 am    Post subject: Reply with quote

The solution is quite simple: manage the appropriate rc dependencies and stop the network before suspend.
openrc will do the job.

/etc/conf.d/openvpn:
rc_need="net.eth0"

/etc/elogind/system-sleep/stop-openvpn-before-suspend:
#!/bin/bash
# Stop the network to avoid openpvn crashes

NET="net.eth0"

case $1 in
    pre)
        if /etc/init.d/"${NET}" status; then
            /usr/bin/logger -p daemon.info "Stopping network before suspend."
            /etc/init.d/"${NET}" stop
        fi
        ;;
    post)
        /usr/bin/logger -p daemon.info "Restoring runlevel."
        /usr/bin/openrc
        ;;
esac

_________________
Who hasn't a spine, should have a method.
Chi non ha carattere, deve pur avere un metodo.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum