Kali Linux Long Term Usage Part 2: Systemd, Partitions, and Swap Space
I am using Kali Linux on a regular basis inside a virtual machine (VirtualBox) and I have maintained my image for a couple of years.
Over the last months and years, the booting time got worse and worse, until it reached the following startup time:
|
|
In the first part of the series, we covered some optimizations with regard to disk usage. In this part, we want to determine why the system takes so much time to start up, even though it has sufficient hardware resources.
Journalctl to the Rescue
A good start is the journalctl
command, which will print log entries from the systemd journal.
The systemd journal logs detailed startup events, including timeouts and failures.
Since we are interested in the boot process, we can look for potential errors and timeouts with the following command, which shows only messages for the current boot:
|
|
The output can be overwhelming, so we refine our search by looking at higher priority events only:
|
|
- -p 3: Shows priority level 3 (errors) and above, often highlighting startup failures.
|
|
This is already a promising hint: systemd waits for a certain disk or device. What is going on here?
Systemd Timeouts
First of all, we note that it takes over 90 seconds for systemd to time out (note the timestamps from above, which start at 17:26:00, whereas the systemd error message shows up only at 17:27:35). Let us examine the timeout values configured for systemd:
|
|
Since no timeout value is configured, systemd’s built-in default is 90 seconds (1min 30s), as per the systemd-system.conf man page.
The DefaultTimeoutStartSec configuration in systemd sets the default time limit that systemd will wait for a service to start before considering it failed.
Let us configure now a shorter timeout value for both DefaultDeviceTimeoutSec, which configures the default timeout for waiting for devices, and DefaultTimeoutStartSec. Note that this will not fix the underlying problem, but maybe it will reduce the boot loading time a bit.
To make it more uniform, we apply the same value (10 seconds) to the DefaultTimeoutStopSec setting as well:
|
|
After rebooting, we check the results again:
|
|
Success! We reduced our system startup time from 2 minutes 16 seconds to 1 minute 3 seconds, a reduction of more than 50%.
The Swap Problem
Let us now check /etc/fstab
for configured disk devices:
|
|
Here we see also the UUID of the device that systemd is waiting for:
UUID=8191879a-f03c-4f6c-9d59-c217cfca09ab
This disk is supposed to serve as a swap partition. We continue to inspect the available disks and disk IDs:
|
|
The UUID of the swap partition changed, but why? As it turns out, when you resize your disk, for example with GParted, the UUIDs change (see https://techlabs.blog/categories/debian-linux/expand-linux-virtual-machine-disk-partition-using-gparted).
Thus, the static references in /etc/fstab
will not update and you have to adjust them manually.
This is described in detail on https://linux-blog.anracom.com/2020/11/15/long-boot-time-of-kali-linux-after-swap-partition-changes-swap-settings-for-the-initramfs/.
Thus, we have to make sure each time after resizing a disk that has swap partitions to update at least the following files:
/etc/fstab
/etc/initramfs-tools/conf.d/resume
(this file is used when resuming from hibernation, and it can cause boot delays if we no longer have a swap partition, see https://www.linuxuprising.com/2018/08/how-to-use-swap-file-instead-of-swap.html)/etc/default/grub
(for Grub2 configuration)
Back to our situation: Since we had originally configured a swap partition, but the swap partition UUID changed and it was not updated in /etc/fstab
we currently do not have any swap space at all!
|
|
Due to the lack of swap space, programs could fail or crash when they run out of memory. In our case it was not noticeable due to the large amount of available RAM. But there can be several problems:
- Programs may not run fully or crash when running of memory.
- The system might crash completely when running out of physical RAM.
- There may be a segmentation fault when writing to a page when there are no physical pages.
In modern Linux kernel versions swap files are virtually as fast as swap partitions. Of course, the swap file should be contiguous to provide the same speed as a partition. When using swap files instead of swap partitions, we also mitigate the problem of changing disk UUIDs.
First, let us create a swap file and use it in /etc/fstab
. With a RAM size of 16 GB, we set the size of the swap file to 4 GB (see https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/managing_storage_devices/getting-started-with-swap_managing-storage-devices#overview-of-swap-space_getting-started-with-swap):
|
|
We can immediately verify that our system has swap space again:
|
|
Secondly, we comment out the line in /etc/fstab
with the swap partition:
|
|
After rebooting, we can notice another improvement in the system’s startup time:
|
|
Thirdly, we boot with a GParted ISO image, remove the swap partition entirely and reize our main partition /dev/sda1
:
Finally, we finish with a startup time of roughly 47 seconds. This is still a lot and we will tackle the underlying problems in the next blog post:
|
|