yaobinwen / Linux-Lab

How to create a home lab to study Linux kernal and C/C++ libraries (and possibly everything else).
GNU General Public License v3.0
0 stars 0 forks source link

Linux Lab

Overview

This repository shows how to create a home lab to study Linux kernal and C/C++ libraries (and possibly everything else).

The home lab requires two machines:

Physical Machine

Follow the steps below to set up the physical machine:

Now you can move on to the section "Virtual Machine".

Virtual Machine (VM)

/lab is a shared folder between the physical machine and the virtual machine. Follow the steps below to build the code:

Source Code

Switching Kernels

If you want to switch back to another kernel:

Issues

VirtualBox shared folders (Unresolved yet)

After installing a custom built Linux kernel, the shared folders may not be mounted correctly and vagrant reload may print the following error:

VirtualBox: mount.vboxsf: mounting failed with the error: No such device

This should be because the newly installed kernel may not have the VirtualBox modules added as suggested by this answer:

modprobe -a vboxguest vboxsf vboxvideo

However, simply running the command above may not solve the problem because the modules vboxguest and vboxsf may not exist for the custom built kernel yet. If you look into /lib/modules, you'll see the old kernel modules (5.4.0-135-generic in my case):

vagrant@ywen-linux-lab:/lib/modules$ ll
total 16
drwxr-xr-x  4 root root 4096 Dec 11 18:51 ./
drwxr-xr-x 91 root root 4096 Dec 11 17:50 ../
drwxr-xr-x  3 root root 4096 Dec 11 18:51 5.10.0+/
drwxr-xr-x  5 root root 4096 Dec  2 13:14 5.4.0-135-generic/

And you can find the VirtualBox-related modules in the old kernel module directory:

vagrant@ywen-linux-lab:/lib/modules$ find ./5.4.0-135-generic/ -name "vb*" -type f
./5.4.0-135-generic/kernel/drivers/gpu/drm/vboxvideo/vboxvideo.ko
./5.4.0-135-generic/kernel/virtualbox-guest/vboxsf.ko
./5.4.0-135-generic/kernel/virtualbox-guest/vboxguest.ko

The module vboxvideo can also be found under the custom built kernel 5.10.0+, but the other two can't be found there.

One thing you think you can try (but will fail) is to create a symbolic link inside /lib/modules/5.10.0+ to point to the kernel modules that were built for the older kernel:

virtualbox-guest -> /lib/modules/5.4.0-135-generic/kernel/virtualbox-guest/

But this won't work and you will probably get the error of Exec format error:

vagrant@ywen-linux-lab:/lib/modules/5.10.0+/kernel$ sudo modprobe -a vboxguest vboxsf vboxvideo
modprobe: ERROR: could not insert 'vboxguest': Exec format error
modprobe: ERROR: could not insert 'vboxsf': Exec format error

I tried to mount VBoxGuestAdditions_7.0.4.iso inside the VM to install the guest addition but I got the following errors:

vagrant@ywen-linux-lab:/media/vbox-guest-addition$ sudo ./VBoxLinuxAdditions.run
Verifying archive integrity...  100%   MD5 checksums are OK. All good.
Uncompressing VirtualBox 7.0.4 Guest Additions for Linux  100%
VirtualBox Guest Additions installer
VBoxControl: error: Could not contact the host system.  Make sure that you are running this
VBoxControl: error: application inside a VirtualBox guest system, and that you have sufficient
VBoxControl: error: user permissions.
This system appears to have a version of the VirtualBox Guest Additions
already installed.  If it is part of the operating system and kept up-to-date,
there is most likely no need to replace it.  If it is not up-to-date, you
should get a notification when you start the system.  If you wish to replace
it with this version, please do not continue with this installation now, but
instead remove the current version first, following the instructions for the
operating system.

If your system simply has the remains of a version of the Additions you could
not remove you should probably continue now, and these will be removed during
installation.

Do you wish to continue? [yes or no]
yes
touch: cannot touch '/var/lib/VBoxGuestAdditions/skip-5.4.0-135-generic': No such file or directory
/opt/VBoxGuestAdditions-7.0.4/bin/VBoxClient: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory
/opt/VBoxGuestAdditions-7.0.4/bin/VBoxClient: error while loading shared libraries: libXt.so.6: cannot open shared object file: No such file or directory
VirtualBox Guest Additions: Starting.
VirtualBox Guest Additions: Setting up modules
VirtualBox Guest Additions: Building the VirtualBox Guest Additions kernel
modules.  This may take a while.
VirtualBox Guest Additions: To build modules for other installed kernels, run
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup <version>
VirtualBox Guest Additions: or
VirtualBox Guest Additions:   /sbin/rcvboxadd quicksetup all
VirtualBox Guest Additions: Kernel headers not found for target kernel 5.10.0+.
Please install them and execute
  /sbin/rcvboxadd setup
modprobe vboxguest failed
The log file /var/log/vboxadd-setup.log may contain further information.
vagrant@ywen-linux-lab:/media/vbox-guest-addition$

There are a few errors I need to understand and solve: