Location>code7788 >text

Kernel] QEMU-based Linux kernel compilation and installation

Popularity:583 ℃/2024-10-18 16:36:23

catalogs
  • Installing a virtual machine system
  • shared directory (computing)
  • Compile the kernel
  • Uninstalling the kernel
  • bibliography

This article documents the process of compiling and installing a Linux kernel in a QEMU environment while doing research on storage systems.

Installing a virtual machine system

Previously inSetting up and using an environment to test ZNS with RocksDB + ZenFS The steps for a graphical installation with VNC were given, but here are the steps for a terminal-only installation.

# Download the Ubuntu image
wget /24.04.1/ubuntu-24.04.

# Create a disk image of any size
qemu-img create -f qcow2 u24s.qcow2 80G

# Mount the ubuntu image on a cdrom and boot.
# -enable-kvm to enable KVM virtualization
# -boot once=d to boot from cdrom only once
# -nographic to disable the GUI
qemu-system-x86_64 -m 8G -smp 4 -enable-kvm -nographic -hda u24s.qcow2 \
        -cdrom ubuntu-24.04. -boot once=d

按 e 进入编辑模式

Then in the grub menu presse Enter Edit Mode

新增 console=ttyS0

Then on the vmlinuz line, addconsole=ttyS0Afterctrl+x Just start it up.

Once installed, subsequent startup commands can be simplified

qemu-system-x86_64 -m 8G -smp 4 -enable-kvm -nographic -hda u24s.qcow2

However, the grub menu will not be displayed during boot, so you need to change the grub configuration.

sudo vim /etc/default/grub

# Modify the following three configuration items
# GRUB_TIMEOUT_STYLE=hidden
GRUB_TIMEOUT=3
GRUB_TERMINAL=console

sudo update-grub
sudo poweroff

If you want to log on to the VM via ssh, you can add a port forwarding to the startup parameter

qemu-system-x86_64 -m 8G -smp 4 -enable-kvm -nographic -hda u24s.qcow2 \
        -net nic,model=virtio -net user,hostfwd=tcp::6666-:22

After that you can log on to the VM via ssh on the physical machine

ssh -p 6666 [user]@localhost

shared directory (computing)

To speed up kernel compilation, you can compile the kernel on the physical machine and then transfer the compiled kernel files to the virtual machine with the help of a shared directory

# Create a shared directory on the physical machine
mkdir -p xxx/share

# Mount the shared directory when starting the VM
qemu-system-x86_64 -m 8G -smp 4 -enable-kvm -nographic -hda u24s.qcow2 \
        -fsdev local,path=xxx/share,id=share_dir,security_model=none \
        -device virtio-9p-pci,fsdev=share_dir,mount_tag=hostshare \
        -net nic,model=virtio -net user,hostfwd=tcp::6666-:22

If an error is reported, it is likely that theqemu unsupported9pThe code needs to be compiled from the source codeqemuinconfigure adding--enable-virtfs Options are available

After that mount the shared directory in the virtual machine

# Mount the shared directory in the virtual machine
sudo mkdir -p /mnt/share
sudo mount -t 9p -o trans=virtio hostshare /mnt/share/ -oversion=

If an error is reported, it is likely that the kernel of the virtual machine does not support the9pThe kernel needs to be compiled, is to turn on the following kernel configuration options:

CONFIG_NET_9P=y
CONFIG_NET_9P_VIRTIO=y
CONFIG_NET_9P_DEBUG=y (Optional)
CONFIG_9P_FS=y
CONFIG_9P_FS_POSIX_ACL=y
CONFIG_PCI=y
CONFIG_VIRTIO_PCI=y

CONFIG_PCI=y
CONFIG_VIRTIO_PCI=y
CONFIG_PCI_HOST_GENERIC=y (only needed for the QEMU Arm 'virt' board)

Compile the kernel

Preparing the environment on a physical machine

# Build tools, lexical syntax analysis library
sudo apt install build-essential bison flex
# If you are missing openssl header files, install the related library
sudo apt install libssl-dev
# To configure the build options using the make menuconfig graphical interface, you need to install the ncurses environment:
sudo apt install libncurses5-dev

# Download the kernel source code and unpack it.
wget /pub/linux/kernel//linux-5.
wget /pub/linux/kernel//linux-5. tar xvf linux-5.
mv linux-5. xxx/share/

Getting the kernel configuration inside a virtual machine

sudo mount -t 9p -o trans=virtio hostshare /mnt/share/ -oversion=

cd /mnt/share/linux-5.
sudo make oldconfig

Compiling the kernel on a physical machine

# Resolve make Error issues
sudo scripts/config --set-str SYSTEM_TRUSTED_KEYS ""
sudo scripts/config --set-str SYSTEM_REVOCATION_KEYS ""

# Compile the kernel and modules, -j24 means compile with 24 threads, you can adjust it according to your CPU cores and memory size
sudo make -j24

Installing the kernel in a virtual machine

# Remove debugging information to fix oversize issues
sudo make INSTALL_MOD_STRIP=1 modules_install
sudo make install
sudo poweroff

Uninstalling the kernel

There may be bugs during development that require uninstalling the problematic kernel in the virtual machine

# Delete the directory named after the kernel's version number in the /lib/modules/ directory
sudo rm -rf /lib/modules/5.+/

# (Optional) Remove unneeded kernel source code from the /usr/src/linux/ directory.
# sudo rm -rf /usr/src/linux-headers-5.

# Remove the kernel and kernel image files from the /boot directory.
sudo rm /boot/*5.*

# Change grub's configuration file to remove the list of unneeded kernel boots
sudo update-grub2

bibliography

  • 【QEMU】Invocation
  • 【QEMU】Invocation
  • 【ask ubuntu】No rule to make target 'debian/'
  • CSDN] Using 9p virtio in qemu, support for shared directories in host and guest.
  • CSDN] Solving the Linux Compilation Kernel Module () Over-sizing Problem

Author of this article: ywang_wnlo
Link to this article: /posts/5fce01ae/
Copyright: All posts on this blog, unless otherwise stated, use theBY-NC-SA License Agreement. Reprinted with permission