Dealing with the kernel image and kernel source code in Sabayon Linux

Some Sabayon Linux users do not know the particulars of the Linux kernel installation or how to upgrade the kernel image. A recent post by a new user in the Sabayon Linux Forums illustrates this (translated from the German):

I’m just setting up my first sabayon box.

In the manual you should see which kernel is selected with:

eselect kernel list

But this is empty for me:

(not found)

When installing one or other packages I also already have a corresponding error message. What am I doing wrong?

The command ‘eselect kernel list‘ lists the versions of kernel source code currently installed (directories such as /usr/src/linux-4.8.17-sabayon/, /usr/src/linux-4.9.38-sabayon/ or whatever) and indicates to which kernel source code directory the symlink /usr/src/linux/ is currently pointing (see Kernel/Configuration – Set symlink in the Gentoo Linux Wiki).

My guess is that this user wanted to rebuild the kernel image. Anyway, the situation in Sabayon Linux he described occurs as a result of either not having installed the kernel source code or not having installed the version of kernel source code corresponding to the version of the installed kernel image.

Before I go into detail on how to do the above, it is essential to understand the basics of the Entropy package management system and how to maintain a sane installation in Sabayon Linux. You should regularly: a) update the local database containing a list of available packages and their versions; b) upgrade installed packages to their latest available version; c) check and fix dependencies and libraries; d) remove any old versions of installed packages:

root # equo update
root # equo upgrade
root # equo conf update
root # equo deptest
root # equo libtest
root # equo upgrade --purge
root # equo cleanup

Do not blindly enter the above commands if you do not know what they do; first read the explanations in the list of equo functions in the Sabayon Linux Wiki.

I have found that sometimes the command ‘equo upgrade --purge‘ does not report all the packages that have to be removed manually. I therefore repeat the command a few times until no more packages are listed for removal.

Right, now let’s look at why the aforementioned Sabayon Linux user was in trouble…

If you happen to have the directory /boot on a separate partition to / (root) and you specified in the file /etc/fstab that /boot should not be mounted automatically when the machine boots, do not forget to mount the boot partition first.

I would see the same message as the aforementioned Sabayon Linux user if the kernel image and source code were not set up correctly in my Sabayon Linux installation:

sabayon fitzcarraldo # eselect kernel list
Available kernel symlink targets:
  (none found)
sabayon fitzcarraldo #

Obviously a kernel image must be present, so first let’s check which version of the kernel image package is installed:

sabayon fitzcarraldo # equo search --installed linux-sabayon
╠  @@ Searching...
╠      @@ Package: sys-kernel/linux-sabayon-4.8.17 branch: 5, [__system__]
╠          Installed:     version: 4.8.17 ~ tag: NoTag ~ revision: 0
╠          Slot:          4.8
╠          Homepage:      https://github.com/Sabayon/kernel
╠          Description:   Official Sabayon Linux Standard
╠                         kernel image
╠          License:       GPL-2 freedist
╠   Keywords:  linux-sabayon
╠   Found:     1 entry
sabayon fitzcarraldo #

As you can see above, Version 4.8.17 of the kernel image package was installed in my case.

Note that the kernel source code is not installed by default in Sabayon Linux, so my guess is that the aforementioned user didn’t have the kernel source code installed. Here’s how to find out which version of the kernel source code package is installed, if any:

sabayon fitzcarraldo # equo search --installed sabayon-sources
╠  @@ Searching...
╠      @@ Package: sys-kernel/sabayon-sources-4.11.10 branch: 5, [__system__]
╠          Installed:     version: 4.11.10 ~ tag: NoTag ~ revision: 0
╠          Slot:          4.11
╠          Homepage:      https://github.com/Sabayon/kernel
╠          Description:   Official Sabayon Linux Standard
╠                         kernel sources
╠          License:       GPL-2 freedist
╠   Keywords:  sabayon-sources
╠   Found:     1 entry
sabayon fitzcarraldo #

As you can see above, in my case I had previously installed Version 4.11.10 of the kernel source code, and it didn’t correspond to the kernel image in use.

If you want to rebuild the kernel, you also need to have the linux kernel headers installed, and those are not installed by default in Sabayon Linux, so let’s check if that package is already installed while we’re at it:

sabayon fitzcarraldo # equo search --installed linux-headers
╠  @@ Searching...
╠      @@ Package: sys-kernel/linux-headers-4.4 branch: 5, [__system__]
╠          Installed:     version: 4.4 ~ tag: NoTag ~ revision: 0
╠          Slot:          0
╠          Homepage:      https://www.kernel.org/
╠                         https://www.gentoo.org/
╠          Description:   Linux system headers
╠          License:       GPL-2
╠   Keywords:  linux-headers
╠   Found:     1 entry
sabayon fitzcarraldo #

As you can see above, in my case I had previously installed Version 4.4 of the linux kernel headers. However, if you don’t already have the package installed, install it:

sabayon fitzcarraldo # equo install linux-headers
╠  @@ Calculating dependencies...
╠  ## [R] [sabayon-weekly] sys-kernel/linux-headers-4.4|0   [4.4|0]
╠  @@ Packages needing to be installed/updated/downgraded: 1
╠  @@ Packages needing to be removed: 0
╠  @@ Download size: 937.6kB
╠  @@ Freed disk space: 0.0b
╠  @@ You need at least: 1.9MB of free space
╠  ::: >>>  (1/1) 1 package
╠    ## Downloading: 1 package
╠    ## ( mirror #1 ) [sys-kernel:linux-headers-4.4.150ec398796671a9b475328e5ae3f180b9b096c3~0.tbz2] @ http://na.mirror.garr.it
╠   ## Aggregated download: 1 item
╠    # [1] na.mirror.garr.it => sys-kernel:linux-headers-4.4.150ec398796671a9b475328e5ae3f180b9b096c3~0.tbz2
╠    ## Checking package checksum...
╠       : [sys-kernel:linux-headers-4.4.150ec398796671a9b475328e5ae3f180b9b096c3~0.tbz2] GPG validated
╠       : SHA1 disabled
╠       : [sys-kernel:linux-headers-4.4.150ec398796671a9b475328e5ae3f180b9b096c3~0.tbz2] SHA256 validated
╠       : SHA512 disabled
╠    ## ( mirror #1 ) [sys-kernel:linux-headers-4.4.150ec398796671a9b475328e5ae3f180b9b096c3~0.tbz2] success @ http://na.mirror.garr.it
╠    ##  Aggregated transfer rate: 1.3MB/second
╠  +++ >>>  (1/1) sys-kernel/linux-headers-4.4
╠    ## Unpacking: sys-kernel:linux-headers-4.4.150ec398796671a9b475328e5ae3f180b9b096c3~0.tbz2
╠    ## Package phase: setup
╠    ## Package phase: preinstall
╠    ## Installing package: sys-kernel/linux-headers-4.4
╠    ## [Linux system headers]
╠    ## Updating installed packages repository: sys-kernel/linux-headers-4.4
╠    ## Cleaning previously installed application data.
╠    ## Package phase: postremove
╠    ## Package phase: postinstall
╠    ## Cleaning: sys-kernel/linux-headers-4.4
╠  @@ Installation complete.
╠  @@ No configuration files to update.
sabayon fitzcarraldo #

To make sure you have the latest versions of the packages sys-kernel/linux-sabayon (the kernel image) and sys-kernel/sabayon-sources (the kernel source code), install the packages as follows:

sabayon fitzcarraldo # equo install linux-sabayon sabayon-sources
╠  @@ Calculating dependencies...
╠  ## [N] [sabayon-weekly] sys-kernel/linux-sabayon-4.11.10|0
╠  ## [R] [sabayon-weekly] sys-kernel/sabayon-sources-4.11.10|0   [4.11.10|0]
╠  @@ Packages needing to be installed/updated/downgraded: 2
╠  @@ Packages needing to be removed: 0
╠  @@ Download size: 198.9MB
╠  @@ Used disk space: 211.3MB
╠  @@ You need at least: 609.1MB of free space
╠  ::: >>>  (1/1) 2 packages
╠    ## Downloading: 2 packages
╠    ## ( mirror #1 ) [sys-kernel:linux-sabayon-4.11.10.1f22c24cc709872f47d62d3c9e44af879fe18888~0.tbz2] @ http://na.mirror.garr.it
╠    ## ( mirror #1 ) [sys-kernel:sabayon-sources-4.11.10.bd94789f59b1e0b33118e122a23a0164c7205b8a~0.tbz2] @ http://na.mirror.garr.it
╠   ## Aggregated download: 2 items
╠    # [1] na.mirror.garr.it => sys-kernel:linux-sabayon-4.11.10.1f22c24cc709872f47d62d3c9e44af879fe18888~0.tbz2
╠    # [2] na.mirror.garr.it => sys-kernel:sabayon-sources-4.11.10.bd94789f59b1e0b33118e122a23a0164c7205b8a~0.tbz2
╠    ## Checking package checksum...
╠       : [sys-kernel:linux-sabayon-4.11.10.1f22c24cc709872f47d62d3c9e44af879fe18888~0.tbz2] GPG validated
╠       : SHA1 disabled
╠       : [sys-kernel:linux-sabayon-4.11.10.1f22c24cc709872f47d62d3c9e44af879fe18888~0.tbz2] SHA256 validated
╠       : SHA512 disabled
╠    ## Checking package checksum...
╠       : [sys-kernel:sabayon-sources-4.11.10.bd94789f59b1e0b33118e122a23a0164c7205b8a~0.tbz2] GPG validated
╠       : SHA1 disabled
╠       : [sys-kernel:sabayon-sources-4.11.10.bd94789f59b1e0b33118e122a23a0164c7205b8a~0.tbz2] SHA256 validated
╠       : SHA512 disabled
╠    ## ( mirror #1 ) [sys-kernel:linux-sabayon-4.11.10.1f22c24cc709872f47d62d3c9e44af879fe18888~0.tbz2] success @ http://na.mirror.garr.it
╠    ## ( mirror #1 ) [sys-kernel:sabayon-sources-4.11.10.bd94789f59b1e0b33118e122a23a0164c7205b8a~0.tbz2] success @ http://na.mirror.garr.it
╠    ##  Aggregated transfer rate: 3.7MB/second
╠  +++ >>>  (1/2) sys-kernel/linux-sabayon-4.11.10
╠    ## Unpacking: sys-kernel:linux-sabayon-4.11.10.1f22c24cc709872f47d62d3c9e44af879fe18888~0.tbz2
╠    ## Package phase: setup
 * To avoid automounting and auto(un)installing with /boot,
 * just export the DONT_MOUNT_BOOT variable.

 * Your boot partition was detected as being mounted at /boot.
 * Files will be installed there for linux-sabayon to function correctly.
 * Preparing kernel and its modules
╠    ## Package phase: preinstall

 * Your boot partition was detected as being mounted at /boot.
 * Files will be installed there for linux-sabayon to function correctly.
╠    ## Installing package: sys-kernel/linux-sabayon-4.11.10
╠    ## [Official Sabayon Linux Standard kernel image]
╠    ## Updating installed packages repository: sys-kernel/linux-sabayon-4.11.10
╠    ## Package phase: postinstall
 * Removing extents option for ext4 drives from /etc/fstab
Generating grub configuration file ...
Found background: /boot/grub/default-splash.png
Found linux image: /boot/kernel-genkernel-x86_64-4.11.0-sabayon
Found initrd image: /boot/initramfs-genkernel-x86_64-4.11.0-sabayon
Found linux image: /boot/kernel-genkernel-x86_64-4.8.0-sabayon
Found initrd image: /boot/initramfs-genkernel-x86_64-4.8.0-sabayon
done

 * You are currently booting with kernel:
 * kernel-genkernel-x86_64-4.8.0-sabayon
 *
 * Use 'eselect bzimage' in order to switch between the available ones


 * If you are upgrading from a previous kernel, you may be interested
 * in the following document:
 *   - General upgrade guide: https://wiki.gentoo.org/wiki/Kernel/Upgrade

 * Updating module dependencies for 4.11.0-sabayon ...
depmod: WARNING: Ignored deprecated option -r                                                                                                                                                                                               [ ok ]
 * Please report kernel bugs at:
 * http://bugs.sabayon.org
 * The source code of this kernel is located at
 * =sys-kernel/sabayon-sources-4.11.10.
 * Sabayon Linux recommends that portage users install
 * sys-kernel/sabayon-sources-4.11.10 if you want
 * to build any packages that install kernel modules
 * (such as ati-drivers, nvidia-drivers, virtualbox, etc...).
╠    ## Cleaning: sys-kernel/linux-sabayon-4.11.10
╠  +++ >>>  (2/2) sys-kernel/sabayon-sources-4.11.10
╠    ## Unpacking: sys-kernel:sabayon-sources-4.11.10.bd94789f59b1e0b33118e122a23a0164c7205b8a~0.tbz2
╠    ## Package phase: setup
 * To avoid automounting and auto(un)installing with /boot,
 * just export the DONT_MOUNT_BOOT variable.

 * Your boot partition was detected as being mounted at /boot.
 * Files will be installed there for sabayon-sources to function correctly.
 * Preparing kernel and its modules
╠    ## Package phase: preinstall
╠    ## Installing package: sys-kernel/sabayon-sources-4.11.10
╠    ## [Official Sabayon Linux Standard kernel sources]
╠    ## Updating installed packages repository: sys-kernel/sabayon-sources-4.11.10
╠    ## Package phase: preremove
╠    ## Cleaning previously installed application data.
╠    ## Package phase: postremove
╠    ## Package phase: postinstall

 * If you are upgrading from a previous kernel, you may be interested
 * in the following document:
 *   - General upgrade guide: https://wiki.gentoo.org/wiki/Kernel/Upgrade

╠    ## Cleaning: sys-kernel/sabayon-sources-4.11.10
╠  @@ Installation complete.
╠  @@ No configuration files to update.
sabayon fitzcarraldo #

Now upgrade the OS installation to the latest kernel image and reboot the machine so that the new kernel image is loaded:

sabayon fitzcarraldo # kernel-switcher switch linux-sabayon-4.11.10
sabayon fitzcarraldo # reboot

Check that the new kernel version is running:

sabayon fitzcarraldo # uname -a
Linux sabayon.local 4.11.0-sabayon #1 SMP Sat Jul 15 09:33:23 UTC 2017 x86_64 Intel(R) Pentium(R) CPU G2030 @ 3.00GHz GenuineIntel GNU/Linux
sabayon fitzcarraldo #

The minor in the version number reported by the uname command does not necessarily correspond to the minor in the version number of the kernel image package. This is not an error: see Misunderstandings about the kernel in the Sabayon Wiki regarding this apparent discrepancy.

The symlink to the kernel source code will now exist:

sabayon fitzcarraldo # eselect kernel list
Available kernel symlink targets:
  [1]   linux-4.11.0-sabayon *
sabayon fitzcarraldo #

If everything works correctly, you can uninstall the earlier version(s) of the kernel image package:

sabayon fitzcarraldo # equo search --installed linux-sabayon
sabayon fitzcarraldo # equo remove linux-sabayon-4.8.17

In my case an earlier version of the kernel source code package was not installed, but if one had been then I would have uninstalled that package too:

sabayon fitzcarraldo # equo search --installed sabayon-sources
sabayon fitzcarraldo # equo remove sabayon-sources-4.8.17

You can also remove old kernel image files manually from the /boot directory if they were not removed automatically:

sabayon fitzcarraldo # ls /boot
sabayon fitzcarraldo # rm -i /boot/*genkernel-x86_64-4.8.0-sabayon

Regenerate the file grub.cfg so that the GRUB 2 menu at boot only lists the kernel images actually present in the /boot directory:

sabayon fitzcarraldo # grub-mkconfig -o /boot/grub/grub.cfg

If you’re running Sabayon Linux inside a VirtualBox virtual machine, you’ll also need to install in the guest installation the correct version of the VirtualBox Guest Additions for the new version of the kernel you just installed in the guest installation. Uninstall the old version if it has not already been uninstalled automatically, then search for the version that corresponds to the kernel you installed in the guest installation:

sabayon fitzcarraldo # equo remove virtualbox-guest-additions
sabayon fitzcarraldo # equo search virtualbox-guest-additions
sabayon fitzcarraldo # equo install virtualbox-guest-additions-5.1.22#4.11.0-sabayon

xdotool comes to the rescue

In a previous post I explained how I implemented a method for adding my current location and the local time to my e-mail signature wherever I happen to be in the World, irrespective of the time on the laptop’s hardware clock and system clock. In that post I described how I created a keyboard shortcut using the Linux application AutoKey. Unfortunately AutoKey has not been updated for several years and no longer works properly in KDE Plasma 5 on my laptops. Therefore I decided to replace it with a KDE keyboard shortcut, and this is to explain how I did it.

First create a custom shortcut in KDE:

  1. ‘System Settings’ > ‘Shortcuts’ > ‘Custom Shortcuts’
  2. ‘Edit’ > ‘New’ > ‘Global Shortcut’ > ‘Command/URL’, and name the New Action ‘Insert current time’
  3. On the Comment pane for ‘Insert current time’, add the comment ‘Insert current time at specified location’ (without the quotes)
  4. On the Trigger pane, configure the shortcut to be Ctrl+Alt+Space
  5. On the Action pane, enter the Command/URL as ‘/home/fitzcarraldo/timezone_signature_GeoNames.sh‘ (without the quotes)
  6. Click ‘Apply’

Next modify the Bash script timezone_signature_GeoNames.sh so that it contains the following (obviously change the username and path to suit):

#!/bin/bash

place=$(kdialog --title "Current Location" --inputbox "Enter your location:")

placetime=$(perl /home/fitzcarraldo/now1.pl $place)

# xdotool does not output a space in a string, so we have to extract each field from the string
# and print each field individually, separated by a space character.

city=$(echo $placetime | awk -F "|" '{print $1}')
country=$(echo $placetime | awk -F "|" '{print $2}' | sed 's/[)(]//g')
region=$(echo $placetime | awk -F "|" '{print $4}')

datetime=$(/usr/bin/zdump $region | awk -F " " '{print $2" "$3" "$4" "$5" "$6" "$7}')
dayofweek=$(echo $datetime | awk -F " " '{print $1}')
month=$(echo $datetime | awk -F " " '{print $2}')
day=$(echo $datetime | awk -F " " '{print $3}')
time=$(echo $datetime | awk -F " " '{print $4}')
year=$(echo $datetime | awk -F " " '{print $5}')
timezone=$(echo $datetime | awk -F " " '{print $6}')

activewindow=$(xdotool getactivewindow)

xdotool type --window $activewindow "Sent from:"
for oneword in $city; do
    xdotool key --window $activewindow space
    sleep 0.1s
    xdotool type --window $activewindow --delay 100 $oneword
done
xdotool key --window $activewindow comma
for oneword in $country; do
    xdotool key --window $activewindow space
    sleep 0.1s
    xdotool type --window $activewindow --delay 100 $oneword
done
xdotool key --window $activewindow Return
xdotool type --window $activewindow "Local time now: "
xdotool type --window $activewindow $dayofweek
xdotool type --window $activewindow " "
xdotool type --window $activewindow $month
xdotool type --window $activewindow " "
xdotool type --window $activewindow $day
xdotool type --window $activewindow " "
xdotool type --window $activewindow $time
xdotool type --window $activewindow " "
xdotool type --window $activewindow $year
xdotool type --window $activewindow " "
if [ ${timezone:0:1} = "-" ]; then
    timezone="UTC-"${timezone#*-}
elif [ ${timezone:0:1} = "+" ]; then
    timezone="UTC+"${timezone#*+}
fi
xdotool type --window $activewindow $timezone
xdotool type --window $activewindow " "
xdotool key --window $activewindow Return
xdotool key --window $activewindow Return
echo

The Perl script now1.pl is listed in my my earlier post. Notice that the script timezone_signature_GeoNames.sh in my earlier post was much simpler. This was because the AutoKey shortcut took care of sending the text to the currently active window. Without AutoKey, I now had to do this myself in the script timezone_signature_GeoNames.sh, and the command xdotool came to the rescue. The developer explains what xdotool does as follows:

This tool lets you simulate keyboard input and mouse activity, move and resize windows, etc. It does this using X11’s XTEST extension and other Xlib functions.

Additionally, you can search for windows and move, resize, hide, and modify window properties like the title. If your window manager supports it, you can use xdotool to switch desktops, move windows between desktops, and change the number of desktops.

So I installed xdotool via the Gentoo package manager:

# emerge xdotool
# eix xdotool
[I] x11-misc/xdotool
     Available versions:  3.20150503.1-r1^t ~3.20160805.1^t {examples}
     Installed versions:  3.20150503.1-r1^t(22:51:30 02/04/17)(-examples)
     Homepage:            http://www.semicomplete.com/projects/xdotool/
     Description:         Simulate keyboard input and mouse activity, move and resize windows

Anyway, my Bash script using xdotool works a treat with Thunderbird (and KWrite, LibreOffice Writer, etc.). I used to experience a problem with certain characters, for example a colon was printed as a semi-colon (see the xdotool bug report xdotool writes the wrong case #121), but that no longer happens in my current KDE Plasma 5 installation:

Sent from: Galeão International Airport, Brazil
Local time now: Thu Jul 6 15:11:40 2017 UTC-03

What a useful tool xdotool is!