What is the complete procedure to update a kernel module currently in use?

6,065

If you update a module for the currently running kernel, you need to discontinue use of the module (ie: umount all the filesystems), rmmod zfs, have the new module available, and modprobe zfs to load it again. Rebooting is usually easier, but you may find example elsewhere to help accomplish this while the system is running, if desired.

  1. # # Stop using the module
  2. # rmmod zfs
  3. # modprobe zfs

If you wish to automatically compile and install updates for a module each time you update your kernel, you may be interested in DKMS. There is a great DKMS Packaging Guide which discusses this in detail.

The idea is that you have a module, or some change to the kernel that you want applied with each kernel update. You can automate the compiling and installation of the modules when apt updates the kernel used. You can find good examples of this with VirtualBox as well with some NVIDIA DKMS drivers.

Another good example that shows how to set this up is patjak's bcwc_pcie. The procedure to have bcwc_pcie, or facetimehd module compiled against and made available to new kernels is documented:

Setting up DKMS (auto-compile on kernel update)

This assumes you have already followed the Debian/Ubuntu deb package steps. You will need to verify dkms.conf that the module name facetimehd and version number 0.1 are correct and either update the dkms.conf or adjust the instructions where -m and -v are used.

  • Install needed packages: # apt install debhelper dkms
  • Remove old package if installed: # dpkg -r bcwc-pcie
  • Make a directory to work from: # mkdir /usr/src/facetimehd-0.1
  • Change into the git repo dir: $ cd bcwc_pcie
  • Copy files over: # cp -r * /usr/src/facetimehd-0.1/
  • Change into that dir: # cd /usr/src/facetimehd-0.1/
  • Remove any previous debs and backups: # rm backup-*tgz bcwc-pcie_*deb
  • Clear out previous compile: # make clean
  • Register the new module with DKMS: # dkms add -m facetimehd -v 0.1
  • Build the module: # dkms build -m facetimehd -v 0.1
  • Build a Debian source package: # dkms mkdsc -m facetimehd -v 0.1 --source-only
  • Build a Debian binary package: # dkms mkdeb -m facetimehd -v 0.1 --source-only
  • Copy deb locally: # cp /var/lib/dkms/facetimehd/0.1/deb/facetimehd-dkms_0.1_all.deb /root/
  • Get rid of the local build files: # rm -r /var/lib/dkms/facetimehd/
  • Install the new deb package: # dpkg -i /root/facetimehd-dkms_0.1_all.deb

If you have any trouble, please read this guide on making a DKMS package: http://www.xkyle.com/building-linux-packages-for-kernel-drivers/


The only part missing from this bcwc_pcie example is that this codebase isn't updated, while the module code you are compiling may change over time. This can be most easily done if you can find a latest download, vs specific versions of the source you're trying to compile.

We can look at the dkms.conf at the DKMS Community Doc:

$ cat dkms.conf 
MAKE="make -C src/ KERNELDIR=/lib/modules/${kernelver}/build"
CLEAN="make -C src/ clean"
BUILT_MODULE_NAME=awesome
BUILT_MODULE_LOCATION=src/
PACKAGE_NAME=awesome
PACKAGE_VERSION=1.1
REMAKE_INITRD=yes

You can also add options to call scripts before or after build or install, provide additional (conditional) make commands, patch commands, etc. The dkms.conf is in fact sourced into a shell script, so a fair amount of trickery can be done if necessary. These options and more are described in the dkms.conf section in man dkms.

and man page:

PRE_BUILD= The name of the script to be run before a build is performed. The path should be given relative to the root directory of your source.

You can create a directory to contain a script which downloads and extracts the latest version of ZFS in preparation for the build. This will help to automate the process. You could use git or download and extract the latest source. Here is an example of how to determine the latest download URL for ZFS:

$ curl -s https://api.github.com/repos/zfsonlinux/zfs/releases/latest | jq '.assets[].browser_download_url' | tr -d '"' | grep -E 'tar.gz$'
https://github.com/zfsonlinux/zfs/releases/download/zfs-0.8.1/zfs-0.8.1.tar.gz

You mention a complication with systemd services being disabled. Does setting up your own ZFS dkms setup resolve this issue? Do you have packages installed that rely on the actual zfs-dkms package that would cause it to conflict? At the very least, you could hook into POST_INSTALL and run systemctl enable to enable whatever services are being disabled.


That being said, ZFS is a pretty popular filesystem, and should have a DKMS package already available. It seems that this is true, and that zfs-dkms is available in universe. You can look into specifics of this package and see if this already available zfs-dkms does the trick for you.

Share:
6,065

Related videos on Youtube

FarO
Author by

FarO

Updated on September 18, 2022

Comments

  • FarO
    FarO over 1 year

    I have a server 18.04 with custom ZFS kernel modules (0.8.x vs provided 0.7.x).

    Periodically, when I update the kernel, I have to recompile the ZFS kernel module, which otherwise would get automatically disabled.

    Last time I had to fiddle a while to get it done so this time I would like to know the correct steps involved, in advance.

    I think

    1. updating the kernel and kernel sources
    2. downloading module source and compiling the module
    3. removing previous kmod packages and installing the new ones

    However, since the kernel module is required to access some filesystems which are currently mounted, what is the next step? simply reboot to have the new module mounted?

    I also have, at some point, to enable the auto-import service, see https://unix.stackexchange.com/questions/338260/zfs-mounting-only-one-of-my-pools-on-boot

  • FarO
    FarO almost 5 years
    No ZFS 0.8.x packages are provided in the repo. ZFS however provides scripts to produce dkms, but somehow last time that was not enough to get ZFS after kernel update, so I thought about kmod packages that are faster. In this case, I guess I only need to remove old dkms package (2nd step), then install the newly compiled dkms package (last step)?
  • FarO
    FarO almost 5 years
    Still, what about kmod? so that your answer is complete and I can approve it.
  • earthmeLon
    earthmeLon almost 5 years
    I'm not sure I understand. When a new kernel is installed, this is detected and the modules are available for the new kernel. When you reboot into the new kernel, the ZFS module should load just fine and you should experience a normal boot. Is there something I am missing? Thanks
  • FarO
    FarO almost 5 years
    Some ZFS-related systemd services got automatically deactivated last time the kernel was updated, when I was using dkms modules. As result, dkms did not allow me to blindly update the kernel using "unattended-upgrades", therefore I tried kmod that looked simpler. Also, every time I have to manually interact with the kernel update process I download the latest ZFS sources, so if I cannot completely let it work by itself, I get no real advantage from dkms packages.
  • earthmeLon
    earthmeLon almost 5 years
    I've updated the answer to help explain fully automating. Again, you'll want to download latest vs some specific version, but you can tell dkms how to prepare and make. Does creating the ZFS dkms on your own prevent the issues with disabled systemd packages?
  • FarO
    FarO almost 5 years
    Hooking on POST_INSTALL seems a very good idea!