0

Main Problem

I am attempting to automate the loading of drivers for my system. The drivers were built manually and must be loaded manually. Another caveat is that one of the system drivers must be loaded and offloaded before manually inserting the built drivers. From the CLI, it looks something like this:

modprobe gspca_main && rmmod gspca_main && modprobe videodev && insmod gspca_main.ko && insmod gspca_kinect2.ko

Options

I want to automate this at boot so that I don't have run the above each time. As I currently see it, I have several options:

  1. Cron

It looks as though I could use the @reboot string here to run the script

  1. Init

Alternatively, I thought about creating an init script but I don't necessarily need all the control over runlevel's or execution timing. Here I haven't seen any instances of multiples Exec's which would be necessary per "the other thing".

  1. Modprobe

Adding a conf file under /etc/modprobe.d seemed to make the most sense since, after all, I am trying to load some drivers. The only problem with this option is that I am not sure if drivers can be sequentially onloaded and then offloaded.

The other thing

The other part in all this is that after the drivers are loaded, I need to run a script as well:

#!/bin/sh
sudo rmmod v4l2loopback
sudo modprobe v4l2loopback video_nr=10 card_label="Kinect v2"
ffmpeg \
    -i /dev/video0 \
    -vsync drop \
    -filter:v fps=30,scale=1280:-1,hflip \
    -pix_fmt yuyv422 \
    -color_trc bt709 \
    -color_primaries bt709 \
    -color_range tv \
    -f v4l2 \
    /dev/video10

Which would seem to better fit under cron or init.

Which option is the best compromise of stability and ease of configuration?

Thanks in advance for any/all help!

2 Answers 2

0

You've omitted one possibility: using systemd. Personally I think this is most elegant way of tweaking your system (e.g see here for a slightly related problem).

Open a terminal and create a new service: sudo nano /etc/systemd/system/mydrivers.service(choose anyName.service)

(Instead of using nano you could also use sudo -H gedit /etc/..)

It's content:

[Unit]
Description= Your description here

[Service]
Type=simple
ExecStart= /usr/local/bin/myExecutableBash.sh #or whatever is executable

[Install]
WantedBy=multi-user.target

To enable that service execute:

sudo systemctl enable mydrivers.service(or your service name)

To start the service right away:

sudo systemctl start mydrivers.service

Your second part can be solved similar: You need to create another service that is dependent on your first service.
So the service should start with:

[Unit]
Description= Your description here
After=mydrivers.service  

[Service]
... your code here

For this service I'd prefer to write your code into an executable bashfile and start it in the "Exec" line

1
  • I guess that's true - I just assumed that systemd was only useful for services you wanted start/stop control over but yeah if I never stop the service then that does what i want! Aug 4, 2020 at 19:18
0

Here is what I ended up doing:

  1. Copied built *.ko modules to /lib/modules/5.4.0-42-generic/kernel/drivers/kinect

  2. Renamed the built module with name gspca_main.ko to gspca_kinect_main.ko to prevent conflict with main driver

  3. Ran depmod

  4. Added:

    videodev
    gspca_kinect_main
    gspca_kinect2
    v4l2loopback 
    

    to /etc/modules to load at boot

  5. Created file /etc/modprobe.d/v4l2loopback.conf with contents: options video_nr=10 card_label="Kinect v2"

  6. Created ffmpeg service (v4l2loopback.service) for loopback device and enabled it:

    [Unit]
    Description=V4L2 Loopback Service for Kinect Webcam
    
    [Service]
    Type=simple
    ExecStart=ffmpeg -i /dev/video0 -vsync drop -filter:v fps=30,scale=1280:-1,hflip -pix_fmt yuyv422 -color_trc bt709 -color_primaries bt709 -color_range tv -f v4l2 /dev/video10
    
    [Install]
    WantedBy=multi-user.target
    

    systemctl v4l2loopback enable

Questions I couldn't answer: - Why is the gspca_kinect_main.ko module loaded as gspca_main in lsmod output?

- Two additional devices now appear: /dev/video3 and /dev/video4 that are not functional; where did they come from?

I could have created a dependency for gspca_kinect_main and gspca_kinect2 but there is currently no need based on /etc/modules

Full install file:

apt-get install ffmpeg

echo "Cloning GSPCA Repo..."
git clone https://github.com/grandchild/gspca-kinect2.git && cd gspca-kinect2
echo "Building & Installing Modules (copying into /lib/modules/`uname -r`/kernel/drivers/kinect)"
make -C /lib/modules/`uname -r`/build  M=`pwd` SRCROOT=`pwd` clean modules
cp gspca_main.ko gspca_kinect_main.ko
mkdir /lib/modules/`uname -r`/kernel/drivers/kinect
cp gspca_kinect_main.ko /lib/modules/`uname -r`/kernel/drivers/kinect/gspca_kinect_main.ko
cp gspca_kinect2.ko /lib/modules/`uname -r`/kernel/drivers/kinect/gspca_kinect2.ko
cd ..

echo "Cloning V4L2Loopback Repo..." 
git clone https://github.com/umlaeute/v4l2loopback.git && cd v4l2loopback
echo "Building and Installing Modules"
make && make install
cd ..
echo "Resolving Module Dependencies"
depmod -a
echo "Enabling GSPCA & V4L2Loopback Modules at boot"
for module in "videodev" "gspca_kinect_main" "gspca_kinect2" "v4l2loopback"
do
    if grep -Fxq $module /etc/modules
    then
            echo "$module found - skipping..."
    else
            echo "$module not found - adding to /etc/modules"
                echo $module >> /etc/modules
    fi
done

echo "options video_nr=10 card_label='Kinect v2'" > /etc/modprobe.d/v4l2loopback.conf

echo "Starting Modules"
modprobe videodev
modprobe gspca_kinect_main
modprobe gspca_kinect2
modprobe v4l2loopback video_nr=10 card_label="Kinect v2"

echo "Creating ffmpeg service v4l2-kinect.service"
echo "[Unit]
Description=V4L2 Loopback Service for Kinect Webcam

[Service]
Type=simple
ExecStart=ffmpeg -i /dev/video0 -vsync drop -filter:v fps=30,scale=1280:-1,hflip -pix_fmt yuyv422 -color_trc bt709 -color_primaries bt709 -color_range tv -f v4l2 /dev/video10

[Install]
WantedBy=multi-user.target" > /lib/systemd/system/v4l2-kinect.service

echo "Enabling v4l2-kinect.service at boot"
systemctl enable v4l2-kinect

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .