背景

自己写的驱动在用的时候经常需要手动加载,这很麻烦,本文介绍如何开机自动进行驱动加载。

第一种方法,适合部署阶段,也就是你的驱动不会再改了

step1

准备好你的.ko文件,将它复制到

/lib/modules/xxx/kernel/drivers/xxx

第一个XXX代表了你机器的架构,比如你是4.9-Linux,第二个XXX是你依据你的驱动类型归类的文件夹,比如你的驱动是基于SPI的驱动,那就放在SPI下面。

Step2

建立驱动map文件

depmod -a

Step3

写一个配置文件在该目录下,并把你的驱动名字加载进去,比如这里是test.ko

gedit /etc/modprobe.d/test.conf

在test.conf中加入

test

重启,发现驱动已经加载

第二种方法,适合开发环境,适合经常要改动驱动的情况

这个方法的核心就是开机自动执行insmod的命令,其实可以是任何命令。

首先ubuntu18.04已经不能用rc.local来直接添加命令了,但是我们可以重现这种方式。

step1

在这两个地方分别创建服务文件rc-local.service,如果有的话就直接修改就行了。

/lib/systemd/system/
/etc/systemd/system/

添加的具体内容如下,主要是添加了[INSTALL]的部分。 

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=multi-user.target
Alias=rc-local.service

step2

在/etc中我们手动创建rc.local文件,并将你的脚本写进去。

#! /bin/bash
sudo insmod /xxx/test.ko
exit 0

重启,可以看到驱动加载了

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐