How to activate NetworkManager profile through systemd

1

Here's the problem I'm trying to solve: I have a wireless device, I would like it to act as a client in a certain moment and as an access point in another moment.

I've set up NetworkManager using two profiles, one for the client, one for the Access Point. Everything works as long as I'm switching manually.

Problems begin when I try to tie these two profiles to systemd units. I would like to be able to associate a NetworkManager profile to a systemd unit. I also wrote the unit using simple nmcli commands, but whenever I start the unit, the connection goes up and then goes down.

Here's the unit:

[Service]
type=oneshot
ExecStart=nmcli con up Connection2
ExecStop=nmcli con down Connection2

Here's what journalctl shows from the unit:

Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)

Here's what journalctl shows from the NetworkManager unit (truncated):

<info> Activation (wlan0) successful, device activated.
<info> (wlan0): device state change: activated -> deactivating (reason 'user-requested') [100 11
<info> NetworkManager state is now DISCONNECTING
<info> (wlan0): device state change: deactivating -> disconnected (reason 'user-requested') [110
<info> (wlan0): deactivating device (reason 'user-requested') [39]
<info> Removing DNS information from /sbin/resolvconf
<info> NetworkManager state is now DISCONNECTED

Can someone point me to the right way of doing this? Also I would like to avoid the "ifupdown" approach and use NetworkManager (or systemd-networkd). I've had no success using the latter, since I can't find an easy way to set the mode of the device. Thank you.

Kei

Posted 2016-11-05T18:31:58.383

Reputation: 133

Answers

1

Oneshot units usually need RemainAfterExit=yes, otherwise they immediately "deactivate" and run the ExecStop command.

user1686

Posted 2016-11-05T18:31:58.383

Reputation: 283 655