1. 程式人生 > >用qemu模擬i386的linux核心,用於核心學習

用qemu模擬i386的linux核心,用於核心學習

安裝
apt-get install qemu




下載http://kernel.org/  這裡也測試過4.5版本,但是無法啟動
wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.7.4.tar.bz2
tar xvf linux-3.7.4.tar.bz2
cd linux-3.7.4.tar.bz2




編譯核心


ls arch/x86/configs/
make i386_defconfig
make menuconfig
# compile whith the kernel parameter
# General Setup –> Initial RAM filesystem and RAM disk support
# Device Drivers –> Block Devices –> RAM block device support
make -j 2


用hello world 測試
 #include <stdio.h>
 #include <unistd.h>
 int  main(void)
 {
         while(1){
         printf("hello,world\n");
         usleep(1000000);
         }
         return 0;
 }


編譯
gcc -static -o init init.c




製作磁碟
dd if=/dev/zero of=ramdisk.img bs=1M count=4


格式化
mkfs.ext2 ramdisk.img


掛載磁碟
sudo mount -o loop ramdisk.img rootfs
sudo cp init rootfs 
sudo mkdir rootfs/dev
sudo mknod rootfs/dev/console c 5 1
sudo mknod rootfs/dev/ram b 1 0
sudo umount rootfs


啟動qemu,我的ubuntu系統是64位
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd ../qemu-i386/ramdisk.img -append "root=/dev/ram init=/init"




用busybox製作,注意哪個王好像無法下載,可以到國內一些網站下載,下面命令執行也要注意目錄位置,
所有make命令都是原始碼目錄進行,for i in {1..4} ; do sudo mknod dev/tty$i c 4 $i; done;在掛載目錄執行,其他的看平時了




wget http://www.busybox.net/downloads/busybox-1.21.0.tar.bz2
tar xvf busybox-1.21.0.tar.bz2
cd busybox-1.21.0
make menuconfig     # This creates a file called ".config"
# Busybox Settings  --->Build Options  --->[*] Build BusyBox as a static binary (no shared libs)
make -j 2           # This creates the "busybox" executable
sudo mount -o loop ramdisk.img rootfs # mount it again
sudo rm rootfs/init # delete the init program
sudo make CONFIG_PREFIX=Path-to/rootfs install
#make install        # or make CONFIG_PREFIX=/path/from/root install
for i in {1..4} ; do sudo mknod dev/tty$i c 4 $i; done;
# to fix the the /dev/tty? no found 
sudo umount rootfs


動命令
qemu-system-x86_64 -kernel arch/x86/boot/bzImage -initrd ../qemu-i386/ramdisk.img -append "root=/dev/ram init=/linuxrc"