Tuesday, March 19, 2019

How to use Ubuntu kernel in Knoppix

. 1
   How I came up with this and why you may need it 1
   Outline of steps needed to install Ubuntu kernel in Knoppix. 1
   Preparing USB flash drive with Knoppix. 2
   Getting Ubuntu kernel images. 2
   Recreating modules.dep for Ubuntu kernel modules. 3
   Compiling out-of-tree cloop kernel module. 3
   Appending Ubuntu kernel modules to custom minirt.gz. 3
   Adding Ubuntu kernel to syslinux config. 4
Appendix A – Knoppix minirt init script outline. 4
Appendix B – Ubuntu kernel in Knoppix that is fully HDD-installed on reiserfs partition with grub. 5
Appendix C – how to get Ubuntu kernel source to embed needed kernel modules. 5

How to use Ubuntu kernel in Knoppix

 

How I came up with this and why you may need it

I am a Knoppix guy through and through and I was very disappointed that Knoppix 8.2 x86 with 4.16.5 kernel froze on my DEXP 310 Intel Baytrail tablet. I read bko109051 (which may give nightmares to the unprepared), added intel_idle.max_cstate=1, it prolonged working time a bit but eventually it froze all the same. I unpacked kernel source 4.16.5 which is preinstalled in Knoppix, tried backported Mika v3 patch and rebuilt kernel using make-kpkg. It worked a little longer but still froze. Googling a little more led me to Linuxium project, where I found respun Ubuntu 16.04.4 x64 ISO. I tried to boot it and the miracle happened – it was working pretty stable on my Dexp Bay Trail tablet! So I downloaded latest official LUbuntu with HWE lubuntu-18.04.2-desktop-i386.iso and it worked stable as well.
I copied Ubuntu kernel and modules to Knoppix and tried to boot it but no dice – most drivers that are built-in in Knoppix are compiled as modules in Ubuntu. After 2 days of brutal Knoppix hacking/troubleshooting I emerged victorious.
You may want to use Ubuntu kernel in Knoppix because:
  • Stock Knoppix kernel freezes on your system but Ubuntu somehow works and you don’t want to fully switch to Ubuntu
  • You want latest kernel with latest drivers from Ubuntu HWE
To use the following method stock Knoppix kernel should work at least for some time sufficient to install Ubuntu kernel/module packages and to compile cloop kernel module (described later). On my tablet it worked longer in text mode boot with ‘knoppix intel_idle.max_cstate=1  2’. If Knoppix freezes at once even in text mode boot and you don’t have enough time to do anything described below, you may want to boot Knoppix USB flash at another PC or under Vmware/Virtualbox (may connect USB flash drive to VM and boot it from KNOPPIX/bootonly.iso if it does not support booting from USB).

 

Outline of steps needed to install Ubuntu kernel in Knoppix

  1. Get Ubuntu kernel image/module/header packages
  2. Prepare USB flash drive with Knoppix 8.2.
  3.  Boot Knoppix USB flash drive and install Ubuntu kernel packages
  4. Recreate modules.dep for Ubuntu kernel modules to make sure that graphics modules can be found by modprobe during boot to init graphics console (or else screen might remain blank on UEFI systems)
  5. Compile out-of-tree cloop kernel module using Ubuntu kernel headers
  6. Append Ubuntu kernel modules usb-storage,  nls_iso8859-1.ko, isofs, reiserfs, cloop, aufs to custom minirt_ub.gz (they are needed to access USB drive, mount FAT partition, mount and unify KNOPPIX and KNOPPIX1 compressed filesystems)
  7. Add Ubuntu kernel with custom minirt_ub.gz to syslinux.cfg (for legacy BIOS systems) or syslnx32.cfg/syslnx64.cfg (for UEFI systems)

 

Preparing USB flash drive with Knoppix

Download Knoppix ISO, boot it and install it to USB flash drive using flash-knoppix (dump of ISO to flash drive with dd in Linux or rufus in Windows is not tested by me as it has strange partition layout but YMMV). Resulting USB drive must have:
  •  first FAT KNOPPIX partition with boot files and compressed filesystem files
  • second reiserfs KNOPPIX-DATA partition which will be extended to fill all available space on USB flash drive

 

Getting Ubuntu kernel images

To get .deb files for Ubuntu kernel you may
  •  repack installed packages from running Ubuntu (if you have Ubuntu ISO and have no internet connection)
  • download prebuilt packages (look up package filenames in /var/lib/apt/lists).
The following examples are for 4.18.0-15-generic kernel which is in lubuntu 18.04.2.
To repack/recreate debs from installed packages boot Ubuntu and use

sudo dpkg-repack {package name}

Package name
Unpacked Size
Comment
linux-headers-4.18.0-15

69.6 Mb 
main kernel headers – needed to compile cloop module
linux-headers-4.18.0-15-generic
7.5 Mb
additional headers symlink and Module.symvers – needed to compile cloop module
linux-image-4.18.0-15-generic
7.9 Mb  kernel image
cannot be repacked with dpkg-repack – do not install this package in Knoppix as it depends on Ubuntu base package, will be marked broken and will give trouble with synaptic, instead unpack it manually or get kernel from Ubuntu ISO – vmlinuz file in casper dir, rename it vmlinuz-4.18.0-15-generic and put to /boot/syslinux dir on Knoppix USB FAT partition
linux-modules-4.18.0-15-generic

41.3 Mb 
base modules, has aufs.ko
linux-modules-extra-4.18.0-15-generic

116 Mb
extra modules, has graphics modules and also reiserfs.ko

If you want to download prebuilt packages find urls for them in  /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_bionic-updates_main_binary-i386_Packages
(Far in Windows may open squashfs file on Ubuntu ISO)
e.g.
Package: linux-image-4.18.0-15-generic
Architecture: i386
Version: 4.18.0-15.16~18.04.1
Filename: pool/main/l/linux-hwe/linux-image-4.18.0-15-generic_4.18.0-15.16~18.04.1_i386.deb

 

Recreating modules.dep for Ubuntu kernel modules

Now that all kernel modules are installed run 
sudo depmod 4.18.0-15-generic

to recreate modules.dep. It is needed if modules.dep was truncated (may happen if installed from dpkg-repacked packages) and missing lines with dependencies for graphics modules (DRM compiled as module in ubuntu). If you don’t do it and modules.dep does not have dependencies for needed graphics modules, Knoppix may boot Ubuntu kernel and may work but kernel DRM will have problems with graphics init during boot as modprobe in /etc/init.d/knoppix-autoconfig will fail to load nvidia / i915 modules, for example:
  • On my old Athlon 64 PC with Nvidia card (needs nouveau kernel module) Knoppix starts Ubuntu kernel and works OK, but uses text console and framebuffer Xorg graphics is not accelerated e.g. no compiz;
  • On my Dexp Intel Baytrail UEFI tablet (needs i915 kernel module) it cannot init graphics console at all - no Knoppix big ASCIIart banner with kernel/CPU info below and it cannot exit the UEFI black screen.

 

Compiling out-of-tree cloop kernel module

Knoppix needs cloop and aufs kernel modules which are not part of vanilla kernel. aufs module is already in ubuntu kernel (fs/aufs/aufs.ko), so you need to compile only missing cloop module.
To build out-of-tree cloop module you need:
  • Ubuntu kernel headers (full kernel source is not needed)
  • cloop.c and cloop.h from Knoppix kernel source (it is in /usr/src, unpack to /tmp and files are in drivers/block dir)
  • Makefile (Check and modify KERNEL_DIR in Makefile to match Ubuntu kernel version):
#!/usr/bin/make
MACHINE=$(shell uname -m)
ifndef KERNEL_DIR
# If building under running Ubuntu kernel
#KERNEL_DIR:=/lib/modules/`uname -r`/build
# If building under running Knoppix kernel
KERNEL_DIR:=/lib/modules/4.18.0-15-generic/build
endif
MODULE:=cloop.ko
module: $(MODULE)
# For Kernel >= 2.6, we now use the "recommended" way to build kernel modules
obj-m := cloop.o
$(MODULE): cloop.c cloop.h
       @echo "Building Kernel module"
       $(MAKE) modules -C $(KERNEL_DIR) M=$(CURDIR)

To compile cloop.ko kernel module run
make module

 

Appending Ubuntu kernel modules to custom minirt.gz

Copy the following kernel modules to temporary modules dir. Ubuntu default initrd (casper/initrd in Ubuntu ISO) has many kernel modules in /lib/modules/4.18.0-15-generic but Knoppix’s minirt.gz init script will not find them – it will load modules only from modules dir.
Kernel module
Subdir where to get it (root modules dir is /lib/modules/4.18.0-15-generic)
Purpose
usb-storage.ko 
kernel/drivers/usb/storage
for USB flash drive access
nls_iso8859-1.ko
kernel/fs/nls
for VFAT mount, this iocharset is default in Ubuntu kernel (see CONFIG_FAT_DEFAULT_IOCHARSET in kernel .config file), Knoppix uses UTF8 by default
isofs.ko                  
kernel/fs/isofs
iso9660 support for KNOPPIX and KNOPPIX1 cloop mount
reiserfs.ko
kernel/fs/reiserfs
default FS in Knoppix, module is in linux-modules-extra .deb package
aufs.ko
fs/aufs
for filesystems unification.
Can also be loaded from KNOPPIX/modules dir on Knoppix USB drive FAT partition
cloop.ko
where you just compiled it
for compressed ISO filesystems mount.
Can also be loaded from KNOPPIX/modules dir on Knoppix USB drive FAT partition

So we need to create gzipped cpio archive with listed above modules and append it to custom minirt_ub.gz:
  •  mkdir modules    -  create it anywhere e.g. in /tmp, but it must be named modules
  • Copy modules from table above to newly created temporary modules subdir
  •  Create gzipped cpio archive that will be appended to custom minirt.gz (run command from dir where you created modules subdir)

    ls -1 ./modules | sed 's/^/\.\/modules\//' | cpio -H newc -o | gzip > ramdisk_modules.gz
  • Copy ramdisk_modules.gz  to /boot/syslinux dir on Knoppix USB FAT partition (it will be mounted under /mnt-system if you are running Knoppix)
  • copy knoppix's default minirt.gz as custom minirt_ub.gz in /boot/syslinux dir and append ramdisk_modules.gz to it:
    cat minirt.gz ramdisk_modules.gz > minirt_ub.gz

 

Adding Ubuntu kernel to syslinux config

Must use updated minirt for ubuntu kernel boot in syslinux.cfg (for legacy BIOS systems) or syslnx32.cfg/syslnx64.cfg (for UEFI systems):
LABEL knoppix_ub
KERNEL vmlinuz-4.18.0-15-generic
APPEND lang=en apm=power-off initrd=minirt_ub.gz nomce hpsa.hpsa_allow_any=1 loglevel=1

If you did everything correctly you may reboot, press TAB at syslinux prompt and type knoppix_ub to boot Ubuntu kernel! If everything is working you may set knoppix_ub to boot by default.

 

Appendix A – Knoppix minirt init script outline

To debug init script add 'debug' to kernel start options (may not be visible on UEFI systems)
---------------------------------------------------------------------------------
Knoppix init:
   init script has many utility functions at start of script
   # Main
    ASCIIart banner
    load_modules - loads /modules/*.ko /modules/scsi/*.ko that are in ramdisk
    Tries NFS
    # Print kernel info
    # Print CPU info
    mounts ramdisk
    Looks for knoppix in local drives
      if [ -z "$FOUND_KNOPPIX" -a -z "$TSCLIENT" ]; then
     (not NFS)
     (no root=/dev/... kernel option specified e.g. Knoppix is not fully installed on HDD)
     if /sys/bus/usb/drivers/usb-storage
       tries findknoppix for local partitions (it mounts them under /mnt-system)
       Will print red message (NOTFOUND var) if no partition with knoppix and/or if usb-storage module not loaded

   checkbootparam "debug" && debugshell "Past mounting /mnt-system."
   ...
   if mountknoppix; then # compressed
    mountknoppix checks cloop and aufs
     grep -q cloop /proc/devices     || insmod /mnt-system/"$knoppix_dir"/modules/cloop.ko preload=32 || return 2
     grep -q aufs  /proc/filesystems || insmod /mnt-system/"$knoppix_dir"/modules/aufs.ko || return 3

 

Appendix B – Ubuntu kernel in Knoppix that is fully HDD-installed on reiserfs partition with grub

·        Install ubuntu kernel packages
·        take casper/initrd from ubuntu iso/flash (39 mb, it has all needed modules including reiserfs)
·        add to /boot/grub/menu.lst

 title Ubuntu Kernel
 root (hd0,1)
 kernel /boot/vmlinuz-4.18.0-15-generic root=/dev/sda2 rw
 initrd /boot/initrd     <<<<----  this is casper/initrd from ubuntu iso/flash

 

Appendix C – how to get Ubuntu kernel source to embed needed kernel modules

Ubuntu kernel versioning is in mapping table  http://people.canonical.com/~kernel/info/kernel-version-map.html. So linux-image-4.18.0-15-generic (4.18.0-15.16~18.04.1) corresponds to 4.18.20 mainline kernel (https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.18.20.tar.xz).

Ubuntu kernel source differs from corresponding mainline kernel so you need to get Ubuntu kernel source package - use apt-get source <linux-image….> in running Ubuntu or find source package name in /var/lib/apt/lists e.g. http://archive.ubuntu.com/ubuntu/pool/main/l/linux-hwe/linux-source-4.18.0_4.18.0-15.16~18.04.1_all.deb and download it manually.

To rebuild Knoppix-like monolithic kernel with cloop and other needed modules:
·        Copy cloop.c and cloop.h to drivers/block in kernel source and add cloop.o in that dir’s Makefile
obj-$(CONFIG_BLK_DEV_LOOP) += loop.o cloop.o
·        Configure all modules listed in tables above as built-in
·        Rebuild kernel as per https://wiki.ubuntu.com/Kernel/BuildYourOwnKernel at Ubuntu Wiki


No comments:

Post a Comment