How to install Kernel Modules from Source Code. Error while make process - linux

I want to install the kernel modules to lib/modules/ . Actually there has to be created a folder in lib/modules/(uname-r) after doing make modules , but there are only created 3 folders called "build", "kernel" and "source". I also get an error after make modules:
DEPMOD 3.4.79
WARNING: COULDN't open directory /lib/modules/3.4.79: No such file or direcoty
FATAL: Could not open /lib/modules/3.4.79/modules.dep.temp for writing: No suhc file or directory
make: *** [_modinst_post] Error 1
Indeed there is no directory called that, but how can I add it?

Did you try \"make modules_install\"?
to install modules
make modules_install
to install kernel
make install

Out-of-tree modules
For the benefit of future Googlers, this is what Buildroot 2018.05 does to install out-of-tree modules for LInux v4.19:
cd linux_kernel_source
mkdir -p /build/dir/default/x86_64/target/lib/modules/4.19.0/extra
/usr/bin/make -f ./scripts/Makefile.modinst
mkdir -p /build/dir/default/x86_64/target/lib/modules/4.19.0/extra
cp /build/dir/default/x86_64/build/kernel_modules-1.0/./buildroot_dep.ko /build/dir/default/x86_64/target/lib/modules/4.19.0/extra
/build/dir/default/x86_64/host/bin/x86_64-buildroot-linux-uclibc-strip --strip-debug /build/dir/default/x86_64/target/lib/modules/4.19.0/extra/buildroot_dep.ko
/bin/bash ./scripts/depmod.sh /build/dir/default/x86_64/host/sbin/depmod 4.19.0
So we can see that it uses the ./scripts/Makefile.modinst and ./scripts/depmod.sh in tree scripts, plus some ugly manual copying of the modules.
Found by building a package that contains kernel modules with V=1 and kernel_modules_package_name-reconfigure and simplifying the output commands, works every time :-).
The only missing question is how depmod was obtained, so we do another verbose build with host-kmod-reconfigure, and basically obtain:
./configure
--prefix="/build/dir/default/x86_64/host"
--sysconfdir="/build/dir/default/x86_64/host/etc"
--localstatedir="/build/dir/default/x86_64/host/var"
where the source is obtained from: https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git
In tree modules are installed with make modules_install as mentioned at: How to install Kernel Modules from Source Code. Error while make process

Related

Build custom driver for linux

I am new to building C lib and have mostly worked with python. My goal is to take the source code from https://github.com/torvalds/linux and build a custom driver for USB/IP (https://github.com/torvalds/linux/tree/master/drivers/usb/usbip) module (some modification).
I copied only /tool/usbip/ assuming that USB and USB-IP are already present in the alpine.
I have set up a docker Image:
FROM alpine
COPY . .
RUN apk add build-base autoconf automake libtool eudev-dev libusb-dev
WORKDIR /tool/usbip/
RUN ./autogen.sh
RUN ./configure
RUN make install
I am getting the following error for make install:
Step 7/7 : RUN make install
---> Running in 48f53c225a99
Making install in libsrc
make[1]: Entering directory '/tool/usbip/libsrc'
CC libusbip_la-names.lo
In file included from names.c:23:
usbip_common.h:18:10: fatal error: linux/usb/ch9.h: No such file or directory
18 | #include <linux/usb/ch9.h>
| ^~~~~~~~~~~~~~~~~
compilation terminated.
make[1]: *** [Makefile:459: libusbip_la-names.lo] Error 1
make[1]: Leaving directory '/tool/usbip/libsrc'
make: *** [Makefile:500: install-recursive] Error 1
This could mean that alpine doesn't have a USB drive. How do I compile and install the driver in that docker?
Another solution can be to build the entire Linux code from the repo, but can I use alpine and add a USB-IP driver there, as I can see that alpine is very lightweight?
I see some Kconfig and Makefile, but I need guidance on building the required driver, as my task also requires modifying drivers/USB/usbip code and building the driver for usbip.
Some blog links or youtube videos on build drivers will also help, but I was not able to find any good resources online.
Updated docker file:
FROM alpine:latest
COPY . /linux
RUN apk add build-base autoconf \
automake libtool eudev-dev \
linux-headers flex bison gmp-dev \
mpc1-dev mpfr-dev
WORKDIR /linux
RUN zcat /proc/config.gz > .config
RUN make olddefconfig
RUN make modules_prepare
RUN make M=drivers/usb/usbip modules
WORKDIR /linux/tools/usb/usbip/
RUN ./autogen.sh
RUN ./configure
RUN make install
Download kernel sources and go to its root.
Copy .config for system you want to build for. E.g. if it is your running system and it provides /proc/config.gz then zcat /proc/config.gz > .config
make olddefconfig
Ensure CONFIG_USBIP and other modules are enabled as a modules:
$ grep CONFIG_USBIP .config
CONFIG_USBIP_CORE=m
CONFIG_USBIP_VHCI_HCD=m
CONFIG_USBIP_VHCI_HC_PORTS=8
CONFIG_USBIP_VHCI_NR_HCS=1
CONFIG_USBIP_HOST=m
# CONFIG_USBIP_DEBUG is not set
If not, run make nconfig (or make menuconfig), navigate to Device Drivers->USB support->USB/IP support and enable it as a module (<M>); save configuration.
make modules_prepare
make M=drivers/usb/usbip modules
Your modules are in drivers/usb/usbip/

Is it meaningful to execute "make modules_install" without executing "make modules"?

From KernelBuild tutorial from http://kernelnewbies.org, I don't see "make modules" command. This article only executes "make" command before "sudo make modules_install install".
But from make help output:
all - Build all targets marked with [*]
* vmlinux - Build the bare kernel
* modules - Build all modules
modules_install - Install all modules to INSTALL_MOD_PATH (default: /)
It seems that without "make modules" command, there is no need to execute "make modules_install" command. Is it meaningful to execute "make modules_install" without executing "make modules"?
Is it meaningful to execute “make modules_install” without executing “make modules”?
Yes, but only if you have already executed make before the make modules_install. You can't execute modules_install if modules weren't built with the make modules or just make. There is only one difference between make and make modules: the make modules compiles only modules or source code which is set as
obj-CONFIG_OPTION_NAME=m
in the Makefile. If you want just to build Linux kernel, just use make, the make modules already included there.
No, if you understand the procedure correctly.
make modules This will just compile all modules just like any other program. Creating object files.
make modules_install Once your files are compiled without any error then this command will put generated .so files to the appropriate directories.
So if you run modules_install without make modules then there will be nothing to copy.
I hope this helps.

fmem compile error with make

I am a beginner in the Linux world and I'm trying to figure out how to run the make command. I'm trying to make use of fmem (memory dump tool), and it is said that "make" must be run from a terminal in the folder.
However I get this:
root#bakie:/root/Desktop/fmem/fmem_1.6-0# make
rm -f *.o *.ko *.mod.c Module.symvers Module.markers modules.order \.*.o.cmd \.*.ko.cmd \.*.o.d
rm -rf \.tmp_versions
make -C /lib/modules/`uname -r`/build SUBDIRS=`pwd` modules
make: *** /lib/modules/3.10-3-686-pae/build: No such file or directory. Stop.
make: *** [fmem] Error 2
I used Debian (Kali ) 3.10-3-686-pae
How can i solved this error to compile ?
fmem is a module that get inserted into the kernel and through which you can access the /dev/fmem (substitute for /dev/mem which was discountinued from linux kernel 2.6 for security reasons).
Like any other kernel driver module, to compile it, you need to have the kernel headers which seem to be missing on your linux. See this link on how to install kernel headers. Once you have the kernel headers, it should compile fine.
make command works if your current directory contains Makefile named file

building /lib/modules/$(uname -r)/build while compiling a kernel

I am cross-compiling 3.4.0 kernel for an embedded device. Then I would like to install compat-wireless drivers which require /lib/modules/3.4/build directory and sub-files. Could anyone explain how can I build that directory so that when I do INSTALL_MOD_PATH=newmodules make modules_install it would load /lib/modules/$(uname -r)/build directory as well? I would appreciate for a clear explanation.
I am using debian distro. I know I can install the kernel headers by apt-get install linux-headers-$(uname -r), but I doubt it would be a good idea since the kernel sources might not be identical.
Typically /lib/modules/$(uname -r)/build is a soft-link to the directory where performed the build. So the way to do this is to simply do a
make modules_install INSTALL_MOD_PATH=/some/root/
in the build directory of the kernel where /some/root is where you want your cross compile pieces to end up. This will create a link to your kernel build path in /some/root/lib/modules/$(uname -r) ... verify that.
Now when you build the compat_wireless drivers specify the kernel build directory in the Makefile as /some/root using the KLIB_BUILD variable (read the Makefile)
make modules KLIB_BUILD=/some/root/lib/modules/$(uname -r)/build
this should do the trick for you.
EDIT A
In answer to your comment below:
Keep "newmodules" outside the kernel directory it's a bad idea to put it in the kernel directory. so mkdir newmodules somewhere like /home/foo or /tmp or something. This is one of the reasons your build link is screwed up
ALSO .../build is a soft link /to/kernel/build/location it will only copy over as a soft-link. You also need to copy over the actual kernel source / kernel build directory to your microSD, using the same relative location. For example,
Let's say your kernel source is in:
/usr/src/linux-3.5.0/
Your kernel build directory is:
/usr/src/linux-3.5.0-build/
Your newmodules (after following 1.) is in:
/tmp/newmodules/
So under /tmp/newmodules/ you see the modules installed in a tree like:
lib/modules/$(uname -r)/
when you do an ls -al in this directory, you'll see that build is a soft link to:
build -> /usr/src/linux-3.5.0-build/
Now let's say your microSD is mounted under /mnt/microSD
then you need to do the following
mkdir -p /mnt/microSD/usr/src
cp -a /usr/src/linux-3.5.0 /usr/src/linux-3.5.0-build /mnt/microSD/usr/src
cp -a /tmp/newmodules/lib /mnt/microSD/lib
Now you have all the content you need to bring over to your embedded environment. I take it you are doing the compat_wireless build on your target system rather than cross compiling it?
NOTE
If your kernel build is the same as the kernel source then just copy over the kernel source and ignore the linux-3.5.0-build in copy instructions above
This is old, but some people will need this information.
I have spent many hours to figure out where build folder comes from, and why it is just a link when I compile my own kernel. Finally figured it out;
Linux kernel usually just links the build and source folders to the source folder.
But!
Arch linux (probably some other distros too); has a manual script for deleting those links, and adding (filtered) files to build folder.
https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/linux
I've extracted that script to work standalone (in a kernel source tree) here: https://gist.github.com/furkanmustafa/9e73feb64b0b18942047fd7b7e2fd53e

How do I build the ManDVD package from compressed source on Linux?

When I try to install any compressed file into Linux I never can, because:
When I execute ./configure the system says bash: ./configure: No such file or directory.
When I execute sudo ./configure the system says sudo: ./configure: command not found.
Worst , when I execute make the system says make: *** No rule to make target '/usr/lib64/qt-3.3/mkspecs/default/qmake.conf', needed by 'Makefile'. Stop.
With make install the system says make: *** No rule to make target '/usr/lib64/qt-3.3/mkspecs/default/qmake.conf', needed by 'Makefile'. Stop.
Completion: I can't install nothing that is not in the repository into my system.
Note: I use Linux Mint XFCE 13 (Maya).
What can I do to solve this unpleasant problem?
As ManDVD's Makefile says:
#############################################################################
# Makefile for building: mandvd
# Generated by qmake (1.07a) (Qt 3.3.8b) on: Sun Mar 8 18:32:50 2009
# Project: mandvd.pro
# Template: app
# Command: $(QMAKE) -o Makefile mandvd.pro
#############################################################################
Makefile was autogenerated by qmake.
I'm no expert at using qmake, but I think executing qmake will re-generate Makefile, so then you can run make && sudo make install as INSTALL file says.
You should've already discovered project is kind of messy (if not discontinued at all), so take care, and may the force be with you :)

Resources