[systemd-devel] starting/stopping network interfaces with systemd

Mirco Tischler mt-ml at gmx.de
Fri Oct 7 16:01:23 PDT 2011


2011/10/7 Cliff Brake <cliff.brake at gmail.com>:
> Hi,
>
> I'm trying to customize systemd to start/stop network interfaces in an
> embedded system (OpenEmbedded/Angstrom).  Angstrom has some support
> for systemd and its working fairly well so far.  Are there any
> examples I can start with?  Basically, when a network device is
> installed (USB), I'd like to run "ifup eth0", etc.
>
> this is what I have so far:
>
> [Unit]
> Description=Start networking services
> After=syslog.target
>
> [Service]
> ExecStart=/sbin/ifup eth0
> Type=oneshot
> ExecStop=/sbin/ifdown eth0
> StandardOutput=syslog+console
>
> [Install]
> WantedBy=multi-user.target
>
> but its not working:
>
> systemctl start network.service
> asix 1-2:1.0: eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1
> ifup[756]: udhcpc (v1.18.5) started
> ifup[756]: Sending discover...
> ifup[756]: Sending select for 192.168.1.100...
> ifup[756]: Lease of 192.168.1.100 obtained, lease time 3600
> ifup[756]: adding dns 192.168.1.1
>
> This all looks good, but then when I do ifconfig, the network devices
> is not configured:
>
> root at cm-x270:/lib/systemd/system# ifconfig
> lo        Link encap:Local Loopback
>          inet addr:127.0.0.1  Mask:255.0.0.0
>          inet6 addr: ::1/128 Scope:Host
>          UP LOOPBACK RUNNING  MTU:16436  Metric:1
>          RX packets:4 errors:0 dropped:0 overruns:0 frame:0
>          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0
>          collisions:0 txqueuelen:0
>          RX bytes:268 (268.0 B)  TX bytes:268 (268.0 B)
>
> running ifup eth0 manually works fine.
The problem is, as soon as ifup exits systemd thinks the service is dead and
calls the ExecStop line, which of cause brings down the interface
again instantly.
The solution should be to add RemainAfterExit=yes to your service file.

> Beyond this, it would be nice to somehow trigger on when a USB NIC is
> installed and automatically run ifup.  Any pointers would be
> appreciated.
An udev rule which starts a template unit file is probably the right
thing to do.
Something like this may do the job:

SUBSYSTEM=="net", KERNEL!="lo", ENV{SYSTEMD_WANTS}="network@$name.service"

and a service file like this:

cat networl at .service:
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/sbin/ifup %i
ExecStop=/sbin/ifdown %i
StandardOutput=syslog+console

Haven't tested this or even done something like this before, so no
guarantee it will work like this.

> Thanks,
> Cliff
Mirco


More information about the systemd-devel mailing list