Preparing a minimal x86 linux system for Qemu

The fast way only !

Downloads :

wget https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.0.3.tar.xz
wget https://busybox.net/downloads/busybox-1.23.2.tar.bz2
tar xf linux-4.0.3.tar.xz
tar xf busybox-1.23.2.tar.bz2

Build x86 busybox :

 mkdir -pv build/busybox-x86
 cd busybox-1.23.2
 make O=../build/busybox-x86 defconfig
 make O=../build/busybox-x86 menuconfig
 (BusyboxSettings->Build Options-> Build BB as a static binary = ON)
 cd ../build/busybox-x86
 make
 make install # will install in the _install dir
 cd .. # in build dir
 mkdir -p initramfs-x86
 cd initramfs-x86
 mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}}
 cp -a ../busybox-x86/_install/* .
 echo '#!/bin/sh' > init
 echo "mount -t proc none /proc" >> init
 echo "mount -t sysfs none /sys" >> init
 echo "echo -e \"\nBoot took \$(cut -d' ' -f1 /proc/uptime) seconds\n\"" >> init
 echo "exec /bin/sh" >> init
 chmod +x init
 find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs-busybox-x86.cpio.gz

Build x86 Linux :

 cd linux-4.0.3/
 make O=../build/linux-x86-basic x86_64_defconfig
 make O=../build/linux-x86-basic kvmconfig
 make O=../build/linux-x86-basic -j4

Run it x86 system with Qemu :

qemu-system-x86_64 -kernel linux-x86-basic/arch/x86_64/boot/bzImage -initrd initramfs-busybox-x86.cpio.gz -nographic -append "console=ttyS0"

Debug :

If init is not found at boot, you can check it is in the initramfs file :

$ zcat initramfs-busybox-x86.cpio.gz | cpio -t| grep init
init
sbin/init
5111 blocks

Build ARMv5 Linux :

For ARM, ensure you have a the compilation toolchain in your PATH.

 cd linux-4.0.3/
 export CROSS_COMPILE=arm-unknown-linux-gnueabi-
 export ARCH=arm
 make O=../build/linux-armv5-basic versatile_defconfig
 make O=../build/linux-armv5-basic -j4

Build ARMv5 busybox :

 
 export CROSS_COMPILE=arm-unknown-linux-gnueabi-
 export ARCH=arm
 mkdir -pv build/busybox-armv5
 cd busybox-1.23.2
 make O=../build/busybox-armv5 defconfig
 make O=../build/busybox-armv5 menuconfig
 (BusyboxSettings->Build Options-> Build BB as a static binary = ON)
 cd ../build/busybox-armv5
 make
 make install # will install in the _install dir
 cd .. # in build dir
 mkdir -p initramfs-armv5
 cd initramfs-armv5
 mkdir -pv {bin,sbin,etc,proc,sys,usr/{bin,sbin}}
 cp -a ../busybox-armv5/_install/* .
 echo '#!/bin/sh' > init
 echo "mount -t proc none /proc" >> init
 echo "mount -t sysfs none /sys" >> init
 echo "echo -e \"\nBoot took \$(cut -d' ' -f1 /proc/uptime) seconds\n\"" >> init
 echo "exec /bin/sh" >> init
 chmod +x init
 find . -print0 | cpio --null -ov --format=newc | gzip -9 > ../initramfs-busybox-armv5.cpio.gz

Run it x86 system with Qemu :

qemu-system-arm  -M versatilepb  -kernel linux-armv5-basic/arch/arm/boot/zImage -initrd initramfs-busybox-armv5.cpio.gz -nographic -append "console=ttyAMA0,115200"

Debug:

Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(1,0)

You probably forget the initrd or your initramfs is wrong.

Failed to execute /init (error -8)
input: AT Raw Set 2 keyboard as /devices/fpga:06/serio0/input/input0
Starting init: /sbin/init exists but couldn't execute it (error -8)
Starting init: /bin/sh exists but couldn't execute it (error -8)
Kernel panic - not syncing: No working init found.  Try passing init= option to kernel. See Linux Documentation/init.txt for guidance.

Your initramfs is wrong.

source :

I mostly reproduce the proposition from this website, it also proposes more explanation and a way to speed-up the kernel boot : http://mgalgs.github.io/2015/05/16/how-to-build-a-custom-linux-kernel-for-qemu-2015-edition.html

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:

Logo WordPress.com

Vous commentez à l'aide de votre compte WordPress.com. Déconnexion / Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion / Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion / Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion / Changer )

Connexion à %s