Emulate Linux with QEMU and learn the Linux kernel

Source: Internet
Author: User

Article name: Simulating Linux with QEMU, learning the Linux kernel
Author: five_cent
Article Address: http://www.cnblogs.com/senix/archive/2013/02/21/2921221.html
Maintenance log: 2013-02-21 document creation
(Note: The article refers to from Http://www.linuxidc.com/Linux/2011-07/39373.htm, is a few additions and descriptions of this article.) The environment used in the article is Ubuntu 12.04, if you encounter a compilation problem, please refer to the error description, configure the dependent environment)

0. Preparation
All of our work works in the specified directory, using the following variables instead of the relevant directory.
$KERNEL Kernel working directory
$LINUX LINUX Kernel Source directory
$BUSYBOX BUSYBOX Source Directory


1. Compiling the kernel
(1) Download the appropriate kernel
To http://www.kernel.org/to download the appropriate kernel, I chose linux-2.6.32, the file name is linux-2.6.32.60.tar.bz2. (choose to use this version because you are beginners, it is best to use the lower version, to prevent the new version of the introduction of new modules resulting in various compilation problems)
Copy linux-2.6.32.60.tar.bz2 to $kernel and unpack the file with the following command

$tar-XF linux-2.6.32.60.tar.bz2

The extracted files are saved in the $kernel directory linux-2.6.32.60, in order to avoid differentiation, use $linux
(2) Compile kernel
Each kernel may have its own compilation criteria, and in order to avoid differentiation, please refer to $linux/documentation/howto. Here's my step

$make Help$make I386_defconfig$make

2. Install Qemu

(1) Install under Ubuntu

$sudo Apt-get Install Qemu

I use this command to install a simulator that is qemu-system-i386 and qemu-system-x86_64. Because the Linux kernel I compiled earlier was i386, I created a soft link.

$LN-S/usr/bin/qemu-system-i386/usr/bin/qemu

(2) Source code installation
To http://wiki.qemu.org/Download, download the appropriate QEMU source code, I downloaded the qemu-1.3.1.tar.bz2, copy to $kernel directory, input command decompression

$tar-XF qemu-1.3.1.tar.bz2

and then compile

$./configure$make$make Install


3. Compiling BusyBox
(1) Download BusyBox source code
To http://www.busybox.net/download the appropriate BusyBox source code, I downloaded is busybox-1.20.0.tar.bz2, copy to $kernel directory, input command decompression

$tar-XF busybox-1.20.0.tar.bz2

(2) Compiling BusyBox

$make Defconfig$make Menuconfig

Because Linux runs without a dynamic library, BusyBox must be compiled statically. Modify

Busybox Settings--->
Build Options--->
[*] Build BusyBox as a static binary (no shared libs)

$make $make Install

During the compilation process, you may encounter

INETD.C: (. text.prepare_socket_fd+0x8a): Undefined reference to ' Bindresvport '

$make Menuconfig

Remove unwanted functionality, other module compilation error Practices similar to
Networking Utilities--->
[] inetd

4. Simulating Linux with Qemu
(1) Writing INITRD startup scripts

$CD $BUSYBOX/_install# To create the required directory for the system runtime, where/proc is used to mount the proc system,/sys is used to mount the SYS system, and Dev is used to mdev the creation of the device node etc/init.d the directory where the BUSYBOX startup script is placed $mkdir proc SYS Dev etc etc/init.d
$vim $BUSYBOX/_install/etc/init.d/rcs

Input

#!/bin/sh# mount the proc file system to the/proc directory because many applications use the information in the/proc, and not mount causes various exceptions mount-t proc none/proc# to mount the SYS file system to the/sys directory. Because many applications use information in/sys, non-mounts can cause various exceptions mount-t Sysfs None/sys#mdev is a udev that comes with BusyBox, which automatically generates device nodes when used for system startup and hot-swapping or dynamic load drivers. If not added, you will need to manually mknod to mount the device node/sbin/mdev-s
$chmod +x $BUSYBOX/_install/etc/init.d/rcs

(Note: Why edit this file?) Because we will use BUSYBOX's init as the first process for our Linux boot, and BUSYBOX's init script is/etc/init.d/rcs, the path is declared in $busybox/init/init.c)

(2) Write the build initrd image script

$vim $KERNEL/build-initrd.sh

Input

#!/bin/sh
#定义变量KERNEL =$ (PWD) busybox=$ (find busybox*-maxdepth 0) linux=$ (find Linux *-maxdepth 0)
#通过cpio创建镜像cd $BUSYBOX/_installfind. | Cpio-o--format=newc > $KERNEL/rootfs.imgcd $KERNEL
#通过gzip创建zip镜像gzip-C rootfs.img > rootfs.img.gz
$chmod +x build-initrd.sh

(3) Writing a quick run script

$vim $KERNEL/run.sh
#!/bin/sh
#定义变量LINUX =$ (Find Linux *-maxdepth 0)
#启动qemuqemu-kernel $LINUX/arch/i386/boot/bzimage-initrd rootfs.img.gz-append "Root=/dev/ram rdinit=sbin/init noapic "

5. Other Instructions
image file
Vmlinux                     compiled most original kernel file, uncompressed
Zimage                       by MLI Nux the file
Bzimage big zimage after gzip compression. Zimage decompress the kernel to low-end memory (640K), Bzimage unzip the kernel to high-end memory (1M or more). If the kernel is relatively small, use zimage or bzimage, if the larger should be used bzimage.
Uimage                     U-boot dedicated image file, which is preceded by a zimage with a length of 0x40 tag 。
Vmlinuz                     is a copy of the Zimage/bzimage file or a pointer to Zimage/bzimage The link.
Initrd                        initial RAMDisk. A temporary root file system mounted during the boot process of the Linux system is mounted on/dev/ram, which is used to support the second phase of Linux boot process. It is a cpio file that is compressed using gzip.

Qemu
qemu-system-i386 QEMU Analog i386 instruction CPU simulator
qemu-system-x86_64 QEMU analog x86_64 instruction CPU simulator
Qemu-kernel parameters, using Bzimage as the Linux kernel
QEMU-INITRD parameter, specifying INITRD mirror
Qemu-append parameters, additional kernel boot parameters

Kernel boot parameters
root= which device to use as the root file system.
After the rdinit= kernel is loaded, the program that runs the path specified in INITRD to create the first process of Linux.
After the init= kernel is loaded, the program that runs the path specified in Initramfs to create the first process of Linux.
Noapic APIC, Advanced Programmable Interrupt controller. This is used to prevent Mp-bios bugs 8254 timer not connected from occurring.

Resources
1.QEMU official website http://www.qemu.org
2. Simply use QEMU to simulate the Linux operating environment http://www.linuxidc.com/Linux/2011-07/39373.htm
3.INITRD Kernel Description Document Http://lxr.linux.no/linux/Documentation/initrd.txt
4.INITRD and Initramfs http://blog.chinaunix.net/uid-25888519-id-3078218.html

Emulate Linux with QEMU and learn the Linux kernel

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.