How to Setup QEMU For The Custom Kernel, Kernel Programming
Hello guys, welcome back to my blog. In this article, I will discuss how to setup QEMU for the custom kernel, Linux kernel programming, step-by-step procedure to stepup QEMU on Linux, etc.
If you have any electrical, electronics, and computer science doubts, then ask questions. You can also catch me on Instagram – CS Electrical & Electronics.
Also, read:
- Roadmap To Become A PCB Design Engineer, Skills, Tools, Salary, Scope
- 1000+ Electronics Projects For Engineers, Diploma, MTech Students
- What Is Interrupt, Interrupts Handling, Interrupt Service Routine
How to Setup QEMU For The Custom Kernel
QEMU is utilized to emulate machines and several furnished instructions and needs either the KVM kernel or KQEMU modules and the host operating system to give a virtual machine environment. It is typically utilized to run Windows and DOS applications on Linux computers.
QEMU is a virtualization technology emulator that enables you to run operating systems and Linux distributions easily on your current system without the necessity to install them or burn their ISO files. It is similar to VMware or VirtualBox.
You need to have any one of these three things on your system, such as,
- WSL
- Vbox with Ubuntu
- Double boot Ubuntu
Once you have this, now let’s see how to setup QEMU of the custom kernel.
Setup Clean Workspace
- Choose a clean directory for all the work,
- You may choose a directory like workspace/eworkspace/kworkspace/ebuildws/ews or with any sensible name under the home directory
- Don’t use Desktop, Downloads, Documents, Music, Videos, Pictures, etc, which are meant for another purpose.
- Avoid spaces or special symbols in path names
- Under this workspace keep different sub directories for downloaded packages, extracted sources to build, configuration files, examples, etc.
Setup Qemu
Install Qemu, a full system emulator for the ARM target architecture.
sudo apt install qemu-system-arm
qemu-system-arm -version
qemu-system-arm -M ?
qemu-system-aarch64 -version
Rootfs
Download core-image-minimal-qemuarm.ext4 from
http://downloads.yoctoproject.org/releases/yocto/yocto-2.5/machines/qemu/qemuarm/
Rename core-image-minimal-qemuarm.ext4 as rootfs.img
Align the size of rootfs
e2fsck -f rootfs.img
resize2fs rootfs.img 16M
Toolchain
Install linaro toolchain from ubuntu package manager.
sudo apt install gcc-arm-linux-gnueabi
We’ll go for soft float for now, due rootfs compatability
Alternatively, download latest pre-built linaro toolchain from as per host architecture
From https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabi/, say v7.5.0
First Boot
Collect prebuilt zImage, vexpress-v2p-ca9.dtb.
Ensure rootfs.img is also in same location
Emulate using Qemu – sdcard approach
qemu-system-arm -M vexpress-a9 -m 1024 -serial stdio -kernel zImage -dtb vexpress-v2p-ca9.dtb -sd rootfs.img -append "console=ttyAMA0 root=/dev/mmcblk0 rw"
Emulate using Qemu – initrd approach
qemu-system-arm -M vexpress-a9 -m 1024 -serial stdio -kernel zImage -dtb vexpress-v2p-ca9.dtb -initrd rootfs.img -append "console=ttyAMA0 root=/dev/mmcblk0 rw"
Post Boot Steps
uname -r
uname -v
cat /proc/cpuinfo
free -m
mount
dmesg
Obtaining Kernel Source
Download any recent LTS version of kernel source
Let’s go with 4.14.x for now, for better compatibility with Qemu
wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.202.tar.xz
tar –xvf linux-4.14.202.tar.xz
Or you can checkout kernel source from git.kernel.org, and switch to desired branch
git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
cd linux
git checkout tags/v4.14 –b v4.14
Let’s call extracted content (or) checked out content as KSRC
Obtain Configuration File
Locate default config available in KSRC/arch/arm/configs, we’ll refer vexpress_defconfig for Versatil Express target being used for Qemu emulation
Or collect any well tested configuration file as base config
make ARCH=arm mrproper
make ARCH=arm vexpress_defconfig
(or)
# copy custom config file as .config under KSRC
Please note that mrproper will remove built files, including the configuration. So run this only for any new build.
Customize Kernel Configuration
Run menuconfig for further customization
Resolve any host dependencies at this stage, e.g. libncurses5-dev, flex, bison etc.
make ARCH=arm menuconfig
Let’s do these minimal changes for now
General Setup -> Local Version -> "-custom“
Device Drivers -> Block Devices ->Â
Enable RAM Block device support
Increase default RAM disk size to suitable limit, say 65536
Enable the block layer
Support for large (2TB+)
Customization – menuconfig
Run menuconfig for further customization
Build kernel image
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- zImage -j <n>
Build Device Tree Binaries
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- dtbs firmware
Build dynamic modules (can skip for now)
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- modules_install INSTALL_MOD_PATH=<tempdir> # or mount point of target rootfs
Booting the new kernel
Collect built outcome to a temporary location
# switch to KSRC
cp $KSRC/arch/arm/boot/zImage <path-of-temp-boot-dir>
cp $KSRC/arch/arm/boot/dts/*.dtb <path-of-temp-boot-dir>
Ensure rootfs.img is also in same location
Emulate using Qemu
qemu-system-arm -M vexpress-a9 -m 1024 -serial stdio -kernel zImage -dtb vexpress-v2p-ca9.dtb -rootfs.img -append "console=ttyAMA0 root=/dev/mmcblk0 rw"
Test the Built outcome
Post Boot Checks:-
# In Target
uname –r
uname –v
# In Host
ls –lh $KSRC/arch/arm/boot/zImage
ls –lh $KSRC/vmlinux
Offline Method – cscope
sudo apt install cscope
# switch to KSRC
make cscope
cscope
This was about “”. I hope this article may help you all a lot. Thank you for reading.
Also, read:
- 100+ C Programming Projects With Source Code, Coding Projects Ideas
- 1000+ Interview Questions On Java, Java Interview Questions, Freshers
- App Developers, Skills, Job Profiles, Scope, Companies, Salary
- Applications Of Artificial Intelligence (AI) In Renewable Energy
- Applications Of Artificial Intelligence, AI Applications, What Is AI
- Applications Of Data Structures And Algorithms In The Real World
- Array Operations In Data Structure And Algorithms Using C Programming
- Artificial Intelligence Scope, Companies, Salary, Roles, Jobs