with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very...
Transcript of with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very...
![Page 1: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/1.jpg)
Lucas De MarchiProFUSION Embedded Systems
Embedded Linux Conference 2012Redwood Shores, CA
Managing kernel modules with kmod
![Page 2: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/2.jpg)
● Software Engineer at ProFUSION
● Contributor to some open source projects: Kernel,
BlueZ, oFono, ConnMan, EFL, WebKit
● Creator of others: dietsplash, codespell, and... kmod
● http://www.politreco.com/
● IRC: demarchi
Who am I?
![Page 3: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/3.jpg)
● Introduction
● How module management works on Linux
● Current status: desktop, embedded, Android
● Packaging, coding, testing
Outline
![Page 4: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/4.jpg)
Introduction
![Page 5: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/5.jpg)
What's kmod?
About
"The goal of the new library libkmod is to offer to other programs the needed flexibility and fine
grained control over insertion, removal, configuration and listing of kernel modules."
Lucas De Marchi -- announcement of kmod 1
![Page 6: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/6.jpg)
What's wrong with module-init-tools (m-i-t)?
About - Why?
"module-init-tools: provide a proper libmodprobe.so from module-init-tools:
Early boot tools, installers, driver install disks want to access information about available
modules to optimize bootup handling."Plumber's Wish List for Linux - October, 2011
That means: udev, systemd, initrd tools and others
![Page 7: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/7.jpg)
What's different from m-i-t?
About - Why?
● Library is designed first
Initial goal was to export only part of the needed functions (libmodprobe.so), later we decided to export all of them (libkmod.so) Based on libabc (See Kay's e Lennart's talk at Kernel Summit 2011)
● Tools are created on top of the library
Project is renamed to kmod
![Page 8: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/8.jpg)
History
12/17/11Jon Masters joins the project
12/20/11m-i-t is declared deprecated
kmod 401/16/12
Feature complete
kmod 502/06/12
Stabilization, testsuite, cleanup. systemd starts using libkmod
kmod 6??/??/??
kmod 112/15/11
kmod 212/20/11
kmod 301/06/11
Almost complete - udev starts using libkmod
t
![Page 9: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/9.jpg)
How module management works on Linux
![Page 10: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/10.jpg)
module insertion:
long init_module(const void *mem, unsigned long len, const char *args)
module removal:
long delete_module(const char *name, unsigned int flags)
module list, params, state:
/sys/module
Module management
![Page 11: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/11.jpg)
Module management
● Pretty simple interface with kernel, but...
● Much more complicated when all use cases must be
handled:
- Hotplug (resolving aliases)- Blacklist- Dependencies and soft-dependencies- Install and remove commands- ELF tweaking
![Page 12: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/12.jpg)
Dependencies
● A module may depend on symbols from another
module
● Too heavy to check dependencies at insertion time
- Offload calculation: depmod
- Read dependencies info and do TheRightThing®
![Page 13: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/13.jpg)
Dependencies - depmod
● Read .symtab and .ksymtab sections of each module
● Match who provides a symbol with who requires a
symbol
● Calculate dependencies (topological sort) and write
modules.dep.kernel/drivers/bluetooth/btusb.ko: \ kernel/net/bluetooth/bluetooth.ko
● modules.dep.bin: same information, but stored in a
Trie
![Page 14: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/14.jpg)
Dependencies - depmod
● Actually it does a bit more. Indexes:
- modules.alias{,.bin}- modules.dep{,.bin}- modules.devname- modules.softdep- modules.symbols{,.bin}
● All indexes are saved per-kernel:
- /lib/modules/$(uname -r)
![Page 15: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/15.jpg)
Dependencies - modprobe
● Basically it reads dependencies and load modules in
the right order
● Configurations:
- Blacklist- Alias- Install and remove commands- Options- Softdeps
![Page 16: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/16.jpg)
Dependencies - modprobe
● --force-modversion, --force-vermagic, -f
These are the bad guys
● Kernel refuses to load modules with mismatching
versions. It checks the .modinfo section (the same
read by modinfo)
● Solution: remove that information from module before
handing over to kernel
![Page 17: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/17.jpg)
Current status
![Page 18: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/18.jpg)
m-i-t phase out plan
1. Put all (part) of the features inside a library
2. Port all (part of the) tools to use the library
3. Allow library to be installed in parallel to m-i-t
4. Allow kmod to completely replace m-i-t
5. Eventually get rid of all tools and use only 'kmod' tool
(a la git, systemctl, udevadm and others)
![Page 19: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/19.jpg)
Status● ~ 14.5 KLOC (libkmod and tools - insmod, rmmod,
modprobe, depmod, modinfo and lsmod)
![Page 20: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/20.jpg)
Status
● Close to release v6 (waiting some pending bugs and
repository on kernel.org)
● Udev, systemd and other initrd tools already depend
on libkmod
● Architectures supported: x86, x86-64, ARM, PPC,
PPC64, SH4, MIPS, SPARCv9, SPARC64, HPPA, S390
● libc: known to work with glibc, eglibc, uClibc and
dietlibc (with some patches)
![Page 21: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/21.jpg)
Status - Desktop distros
● Major distros adopting kmod
- Archlinux: replaced m-i-t with kmod 5
- Fedora (F17): replaced m-i-t with kmod 5
- Opensuse: replaced m-i-t with kmod 5
- Debian: package in Experimental
- Ubuntu: ??
- Mageia, Openmamba and others reported to be
using
![Page 22: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/22.jpg)
Status - embedded
● Angstrom: using libkmod 3
● Buildroot: using libkmod 5
● Poky, Yocto: ??? (Darren Hart said there are patches
pending to add kmod)
● Android: ... more later
![Page 23: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/23.jpg)
Status - embedded
● Why embedded should care about kmod?
- Allow module loading / hotplug
- Link init/udev/mdev/your-home-made-solution
directly to libkmod:
>> avoid several fork/exec calls during boot
>> having configurations and indexes in
memory, we can be faster
![Page 24: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/24.jpg)
Module loading on Android
● Very primitive module handling - the equivalent of
insmod/rmmod
- Used by toolbox (adb shell)
- Used by init (it's a command available for init.rc
file)
● Vendors don't allow module loading -> no external
devices. See talk at ABS 2012: "USB Device Support
Workshop", Bernie Thompson - Plugable Technologies
![Page 25: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/25.jpg)
Module loading on Android
● Linking Android's init to libkmod
- Very few code to add support for module loading
with all the necessary goodies for hotplug
![Page 26: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/26.jpg)
Packaging, coding, testing
![Page 27: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/27.jpg)
Packaging
● 2 ways of using kmod
- Only as a library
- As a replacement to m-i-t
● ./configure [ --enable-tools ] && make &&
make install
![Page 28: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/28.jpg)
Packaging
● Create symlinks (there's only 1 tool, named kmod)
Typical configuration:
/usr/bininsmod -> kmodkmodlsmod -> kmodmodinfo -> kmodrmmod -> kmod
/sbin/modprobe -> ../usr/bin/kmoddepmod -> ../usr/bin/kmod
![Page 29: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/29.jpg)
Coding
● How to use libkmod?
● Steps:
i. Init library: grab context object, setup logging
function, pre-load indexes, etc
ii. Create module object by path, name or through
index lookup
iii. Operate on that module: insert, insert with
dependency handling, remove, get info, etc
![Page 30: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/30.jpg)
Coding - example
Hands on - udev or systemd
![Page 31: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/31.jpg)
Automated testing
![Page 32: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/32.jpg)
Testsuite
● Testsuite added on kmod 5
● Need to address regression reports that were being
received from different architectures and different
distributions
![Page 33: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/33.jpg)
Testsuite - features
● Each test runs isolated on a separate process
● Trap calls to libc functions, modifying the result:
- All functions dealing with path: open(),
fopen(), opendir(), stat(), etc
- uname()
- init_module()
- delete_module()
![Page 34: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/34.jpg)
Testsuite - features
● Goal of function traps: allow each test to have a fake
rootfs and don't touch current state of the system
● Test both library API and tools:
- Inline tests in test definition
- Exec built binaries: modprobe, insmod, modinfo,
depmod, etc
![Page 35: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/35.jpg)
Testsuite - anatomy
Hands on - Anatomy of a test
![Page 36: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/36.jpg)
Thanks
![Page 37: with kmod Managing kernel modules - eLinux · 2016. 7. 6. · Module loading on Android Very primitive module handling - the equivalent of insmod/rmmod - Used by toolbox (adb shell)](https://reader033.fdocuments.net/reader033/viewer/2022060813/60920f192ab7bf73cb1651f2/html5/thumbnails/37.jpg)
Thank you for your attentionQuestions?
Repository: git://git.profusion.mobi/kmod.gitMailing list: [email protected]: #kmod at freenode