How do I automatically run modprobe v4l2loopback on boot?

I'm using OBS Studio and would like to have a virtual video output saved permanently so that it can be run when the application is launched.

This command creates the virtual output:

sudo modprobe v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1

But I must run the command in console on each boot. How can I make this permanent? The plugin that utilizes the output in OBS Studio is configured to auto start but /dev/video10 is not available after a reboot of the system.

$ modinfo v4l2loopback | grep -i parm
parm: debug:debugging level (higher values == more verbose) (int)
parm: max_buffers:how many buffers should be allocated (int)
parm: max_openers:how many users can open loopback device (int)
parm: devices:how many devices should be created (int)
parm: video_nr:video device numbers (-1=auto, 0=/dev/video0, etc.) (array of int)
parm: card_label:card labels for every device (array of charp)
parm: exclusive_caps:whether to announce OUTPUT/CAPTURE capabilities exclusively or not (array of bool)
parm: max_width:maximum frame width (int)
parm: max_height:maximum frame height (int)
3

2 Answers

Normally kernel modules can be added to /etc/modules for loading at boot time.


Add this to /etc/modules...

v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1

However, I don't know if you can pass parameters there. So here's another way to do it.

Add this to /etc/modules...

v4l2loopback

Create /etc/modprobe.d/v4l2loopback.conf

options v4l2loopback video_nr=10 card_label="OBS Video Source" exclusive_caps=1

Then...

sudo update-initramfs -c -k $(uname -r)
reboot

Confirm module loading with...

lsmod | grep -i v4l2loopback
14

Easy way to do it un Ubuntu 20.04 and others:

$ sudo echo "v4l2loopback" > /etc/modules-load.d/v4l2loopback.conf
$ sudo echo "options v4l2loopback video_nr=10 card_label=\"OBS Video Source\" exclusive_caps=1" > /etc/modprobe.d/v4l2loopback.conf

You can restart to check it works!

NOTICE

Be aware of a bug on v4l2loopback-dkms 0.12.3-1ubuntu0.1

More info on this stackoverflow post.

EDIT - Using tee

As pointed by renyhp, it's better to use the tee command:

$ sudo echo "v4l2loopback" | tee /etc/modules-load.d/v4l2loopback.conf
$ sudo echo "options v4l2loopback video_nr=10 card_label=\"OBS Video Source\" exclusive_caps=1" | tee /etc/modprobe.d/v4l2loopback.conf

EDIT - Update modules

As commented, it seems that in some systems after rebooting some problems can be experienced. To avoid that, it's always a good idea to update the modules:

sudo update-initramfs -c -k $(uname -r)
5

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