Skip to content

utils: ビルドしたモジュールを /lib/modules に配置して再起動後も残るようにする - #3

Open
NOPLAB wants to merge 1 commit into
ros2from
fix/install-module-into-lib-modules
Open

utils: ビルドしたモジュールを /lib/modules に配置して再起動後も残るようにする#3
NOPLAB wants to merge 1 commit into
ros2from
fix/install-module-into-lib-modules

Conversation

@NOPLAB

@NOPLAB NOPLAB commented Aug 3, 2026

Copy link
Copy Markdown

問題

utils/build_install_*.bash の 4 本は、いずれも同じ形で終わっています。

make
sudo insmod rtmouse.ko

insmod はビルドディレクトリからモジュールを直接ロードするだけで、インストールは
しません。そのため /lib/modules/$(uname -r)rtmouse.ko は置かれず、
modprobe rtmouse は機能しません。

結果として、起動時にドライバをロードする仕組みは毎回モジュールをコンパイルし直す
ことになります。Raspberry Pi 4 では 1 回の起動あたり数分の CPU 時間です。
Ubuntu 22.04 / カーネル 5.15.0-1098-raspi の実機で、raspicat_setup_scripts
raspicat.service が起動のたびに build_install_header_from_apt_raspi4.bash
入り直しているのを確認しました。

もう 1 つ、小さい問題があります。モジュールが既にロード済みのとき insmod
File exists で失敗します。これらのスクリプトはすべて set -e の下で動くため、
再実行すると途中で異常終了します。

変更内容

 make
-sudo insmod rtmouse.ko
+sudo install -D -m 644 rtmouse.ko /lib/modules/$(uname -r)/extra/rtmouse.ko
+sudo depmod -a
+sudo modprobe rtmouse

4 つのバリアント(apt / source × raspi2 / raspi4)すべてに同じ変更を入れ、
挙動をそろえています。extra/ はツリー外モジュールの慣例的な置き場で、
make modules_install が使う場所と同じです。depmod -a により modprobe から
解決できるようになります。modprobe でロードすることで、再実行しても
問題なく通るようになります。

動作確認

上記の実機でビルドとインストールを行いました。

$ modinfo -F vermagic /lib/modules/5.15.0-1098-raspi/extra/rtmouse.ko
5.15.0-1098-raspi SMP preempt mod_unload modversions aarch64

この状態で modprobe rtmouse はコンパイルなしにドライバをロードし、
/dev/rtmotor0/dev/rtlightsensor0 などもこれまでどおり生成されます。

この PR に含めていないもの

src/drivers/Makefileinstall: ターゲットは 50-rtmouse.rules
/etc/udev/rules.d/ にコピーしますが、これを呼ぶ箇所がどこにもありません。
そのため、これらのスクリプトはロードのたびに sudo chmod 666 /dev/rt*
必要としています。ここまで入れると変更の趣旨が変わるため別件としましたが、
この PR に含めたほうがよければ対応します。

🤖 Generated with Claude Code

https://claude.ai/code/session_01V3ZzEzNKDxANptPX4QNnHn

The build scripts end with `sudo insmod rtmouse.ko`, which loads the
module straight out of the build directory and never installs it. After
a reboot the module is gone, so anything that loads the driver at boot
has to compile it again -- on a Raspberry Pi 4 that is several minutes
of CPU on every single boot.

Install the module into `/lib/modules/$(uname -r)/extra/` and run
`depmod` so `modprobe rtmouse` works, then load it with `modprobe`
instead of `insmod`.

Using `modprobe` also makes the scripts idempotent: `insmod` fails with
"File exists" when the module is already loaded, and with `set -e` that
aborts the script.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V3ZzEzNKDxANptPX4QNnHn

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the utils/build_install_header_from_* helper scripts to install the built rtmouse.ko into the running kernel’s module tree so it can be loaded via modprobe (and persists across reboots), rather than loading directly from the build directory with insmod.

Changes:

  • Replace sudo insmod rtmouse.ko with installation into /lib/modules/$(uname -r)/extra/rtmouse.ko.
  • Run depmod -a so the module dependency database includes the newly installed module.
  • Load the module using sudo modprobe rtmouse (more re-runnable than insmod).

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
utils/build_install_header_from_source_raspi4.bash Install rtmouse.ko into /lib/modules and load via depmod + modprobe.
utils/build_install_header_from_source_raspi2.bash Install rtmouse.ko into /lib/modules and load via depmod + modprobe.
utils/build_install_header_from_apt_raspi4.bash Install rtmouse.ko into /lib/modules and load via depmod + modprobe.
utils/build_install_header_from_apt_raspi2.bash Install rtmouse.ko into /lib/modules and load via depmod + modprobe.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +17 to +18
sudo depmod -a
sudo modprobe rtmouse
Comment on lines 17 to 18

# initialize the driver
Comment on lines +15 to +16
sudo depmod -a
sudo modprobe rtmouse
Comment on lines 15 to +16
make
sudo insmod rtmouse.ko
sudo install -D -m 644 rtmouse.ko /lib/modules/$(uname -r)/extra/rtmouse.ko
@NOPLAB NOPLAB changed the title utils: install the built module into /lib/modules so it survives a reboot utils: ビルドしたモジュールを /lib/modules に配置して再起動後も残るようにする Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants