setup vcan interface to be activated on boot

I want to setup a virtual SocketCAN interface on boot. The following lines do what I want (manually):

ip link add dev vcan0 type vcan
ip link set up vcan0

(or)

ip link add dev vcan0 up type vcan

I have a method to bring up a physical USB CAN interface on hotplugging - I add the following lines to /etc/network/interfaces:

allow-hotplug can0
iface can0 can static bitrate 250000 up /sbin/ip link set $IFACE down up /sbin/ip link set $IFACE up type can

I now want to bring up the vcan interface on boot, too. So I auto-added the vcan module and added those lines to /etc/network/interfaces:

auto vcan0
iface vcan0 can static bitrate 0 # NEEDED but not supported pre-up /sbin/ip link add dev $IFACE type vcan up /sbin/ip link set $IFACE up 

But strangely this approach does not work: on boot or when I run ifup vcan0 I get eighter the following error:

Configuring interface vcan0=vcan0 (can)
/sbin/ip link add dev $IFACE type vcan
...
ip link set vcan0 type can bitrate 0
RTNETLINK answers: Operation not supported
Failed to bring up vcan0.

.. when I add the line bitrate <somevalue> or I get

Configuring interface vcan0=vcan0 (can)
Missing required variable: bitrate
Missing required configuration variables for interface vcan0/can.
Failed to bring up vcan0.

.. when I omit the bitrate setting.

So it looks like I have to set bitrate and must not set it - at the same time.

What am I doing wrong here?

p.s. of course I could simply run the ip link add .. on startup but I'd like to use the same approach for both interfaces.

1 Answer

You have to load the vcan module at boot time. Edit /etc/modules for this and add the line

vcan

Next, edit /etc/network/interfaces

auto vcan0 iface vcan0 inet manual pre-up /sbin/ip link add dev $IFACE type vcan up /sbin/ifconfig $IFACE up

Finally, restart the interfaces:

sudo /etc/init.d/networking restart

The vcan0 interface should pop up if you type

ifconfig

in a terminal.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like