Setting up the workspace
Firstly, you'll have to decide whether to run your test Linux system as a native system (on the bare metal) or as a guest OS. We'll cover the factors that will help you decide. Next, we (briefly) cover the installation of some software (the guest additions) for a case where you use a guest Linux OS, followed by the required software packages to install.
Running Linux as a native or guest OS
Ideally, you should run a recent Linux distribution (Ubuntu, Fedora, and so on) on native hardware. We tend to use Ubuntu 20.04 LTS in this book as the primary system to experiment upon. The more powerful your system – in terms of RAM, processing power, and disk space – the better! Of course, as we shall be debugging at the level of the kernel, crashes and even data loss (the chances of the latter are small, but nevertheless...) can occur; hence, the system should be a test one with no valuable data on it.
If running Linux on native hardware – on the bare metal, as it were – isn't feasible for you, then a practical and convenient alternative is to install and use the Linux distribution as a guest OS, a VM. It's important to install a recent Linux distribution.
Running a Linux guest as a VM is certainly feasible but (there's always a but isn't there?!), it will almost certainly feel a lot slower than running Linux natively. Still, if you must run a Linux guest, it certainly works. It goes without saying that the more powerful your host system, the better the experience. There's also an arguable advantage to running your test system as a guest OS: even if it does crash (please do expect that to happen, especially with the deliberate (de)bugging we'll do in this book!), you don't even need to reboot the hardware; merely reset the hypervisor software running the guest (typically Oracle VirtualBox).
Alternate Hardware – Using Raspberry Pi (and Other) ARM-Based Systems
Though we specified that you can run a recent Linux distro either as a native system or as a guest VM, the assumption was that it's an x86_64 system. While that suffices, to get more out of the experience of this book (and simply to have more fun), I highly recommend you also try out the sample code and run the (buggy) test cases on alternate architectures. With many, if not most, modern embedded Linux systems being ARM-based (on both 32-bit ARM and 64-bit AArch64 processors), the Raspberry Pi hardware is extremely popular, relatively cheap, and has tremendous community support, making it an ideal test bed. I do use it every now and then within this book, in the chapters that follow; I'd recommend you do the same!
All the details – installation, setup, and so on – are amply covered in the well-documented Raspberry Pi documentation pages here: https://www.raspberrypi.org/documentation/.
Ditto for another popular embedded prototyping board - TI's BeagleBone Black (affectionately, the BBB). This site is a good place to get started with the BBB: https://beagleboard.org/black.
Running Linux as a guest OS
If you do decide to run Linux as an x86_64 guest, I'd recommend using Oracle VirtualBox 6.x (or the latest stable version) as a comprehensive and powerful all-in-one GUI hypervisor application appropriate for a desktop PC or laptop. Other virtualization software, such as VMware Workstation or QEMU, should also be fine. All of these are freely available and open source. It's just that the code for this book has been tested on Oracle VirtualBox 6.1. Oracle VirtualBox is considered Open Source Software (OSS) and is licensed under the GPL v2 (the same as the Linux kernel). You can download it from https://www.virtualbox.org/wiki/Downloads. Its documentation can be found here: https://www.virtualbox.org/wiki/End-user_documentation.
The host system should be either MS Windows 10 or later (of course, even Windows 7 will work), a recent Linux distribution (for example, Ubuntu or Fedora), or macOS.
The guest (or native) Linux distribution can be any sufficiently recent one. For the purpose of following along with the material and examples presented in this book, I'd recommend installing Ubuntu 20.04 LTS. This is what I primarily use for the book.
How can you quickly check which Linux distribution is currently installed and running?
On Debian/Ubuntu, the lsb_release –a
command should do the trick; for example, on my guest Linux:
$ lsb_release –a 2> /dev/null Distributor ID: Ubuntu Description:    Ubuntu 20.04.2 LTS Release:        20.04 Codename:       focal $
How can you check if the Linux currently running is on native hardware or is a guest VM (or a container)? There are many ways to do so. The script virt-what
is one (we will be installing it). Other commands include hostnamectl(1)
, dmidecode(8)
(on x86), systemd-detect-virt(1)
(if systemd is the initialization framework), lshw(1)
(x86, IA-64, PPC), raw ways via dmesg(1)
(grepping for Hypervisor detected
), and via /proc/cpuinfo
.
In this book, I shall prefer to focus on setting up what is key from a kernel debug perspective; hence, we won't discuss the in-depth details of installing a guest VM (typically on a Windows host running Oracle VirtualBox) here. If you require some help, please refer to the many links to tutorials on precisely this within the Further reading section of this chapter. (FYI, these installation details, and a lot more, are amply covered in my previous book Linux Kernel Programming, Chapter 1, Kernel Workspace Setup).
Tip – Using Prebuilt VirtualBox Images
The OSBoxes project allows you to freely download and use prebuilt VirtualBox (as well as VMware) images for popular Linux distributions. See their site here: https://www.osboxes.org/virtualbox-images/.
In our case, you can download a prebuilt x86_64 Ubuntu 20.04.3 (as well as others) Linux image here: https://www.osboxes.org/ubuntu/. It comes with the guest additions preinstalled! The default username/password is osboxes/osboxes.org
.
(Of course, for more advanced readers, you'll realize it's really up to you. Running an as-light-as-possible custom Linux system on a Qemu (emulated) standard PC is a choice as well.)
Note that if your Linux system is installed natively on the hardware platform, you're using an OSBoxes Linux distro with the VirtualBox guest additions preinstalled, or you're using a Qemu-emulated PC, simply skip the next section.
Installing the Oracle VirtualBox guest additions
The guest additions are essentially software (para-virtualization accelerators) that quite dramatically enhance the performance, as well as the look and feel, of the experience of running a guest OS on the host system; hence, it's important to have it installed. (Besides acceleration, the guest additions provide conveniences such as the ability to nicely scale the GUI window and share facilities such as folders, the clipboard, and to drag and drop between the host and the guest.)
Before doing this though, please ensure you have already installed the guest VM (as mentioned previously). Also, the first time you log in, the system will likely prompt you to update and possibly restart; please do so. Then, follow along:
- Log in to your Linux guest VM (I'm using the login name
letsdebug
; guess why!) and first run the following commands within a Terminal window (on a shell):sudo apt update sudo apt upgrade sudo apt install build-essential dkms linux-headers-$(uname –r) ssh -y
(Ensure you run each of the preceding commands on one line.)
- Install the Oracle VirtualBox Guest Additions now. Refer to How to Install VirtualBox Guest Additions in Ubuntu: https://www.tecmint.com/install-virtualbox-guest-additions-in-ubuntu/.
- On Oracle VirtualBox, to ensure that you have access to any shared folders you might have set up, you need to set the guest account to belong to the
vboxsf
group; you can do so like this (once done, you'll need to log in again, or sometimes even reboot, to have this take effect):sudo usermod -G vboxsf -a ${USER}
The commands (step 1), after updating, have us install the build-essential
package along with a couple of others. This ensures that the compiler (gcc
), make
, and other essential build utility programs are installed so that the Oracle VirtualBox Guest Additions can be properly installed straight after (in step 2).
Installing required software packages
To install the required software packages, perform the following steps (do note that, here, we assume the Linux distribution is our preferred one, Ubuntu 20.04 LTS):
- Within your Linux system (be it a native one or a guest OS), first do the following:
sudo apt update
Now, to install the remaining required packages for the kernel build, run the following command in a single line:
sudo apt install bison flex libncurses5-dev ncurses-dev xz-utils libssl-dev libelf-dev util-linux tar -y
(The -y
option switch has apt
assume a yes answer to all prompts; careful though, this could be dangerous in other circumstances.)
- To install the packages required for work we'll do in other parts of this book, run the following command in a single line:
sudo apt install bc bpfcc-tools bsdmainutils clang cmake cppcheck cscope curl \ dwarves exuberant-ctags fakeroot flawfinder git gnome-system-monitor gnuplot \ hwloc indent kernelshark libnuma-dev libjson-c-dev linux-tools-$(uname -r) \ net-tools numactl openjdk-16-jre openssh-server perf-tools-unstable psmisc \ python3-distutils rt-tests smem sparse stress sysfsutils tldr-py trace-cmd \ tree tuna virt-what -y
A point to mention: all the packages mentioned above aren't strictly required in order for you to work on this book; some are those we encounter just once or twice.
Tip – A Script to Auto-Install Required Packages
To make the (immediately above-mentioned) package install task simpler, you can make use of a simple bash script that's part of the GitHub repository for this book: pkg_install4ubuntu_lkd.sh
. It's been tested on an x86_64 OSBoxes Ubuntu 20.04.3 LTS VM (running on Oracle VirtualBox 6.1).
FYI, to check which packages are taking up the most space, install the wajig
package and run this: sudo wajig large
.
Great; now that the packages are installed, let's proceed with understanding the next portion of our workspace setup – the need for two kernels!