用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
 #include
 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"

你可能感兴趣的:(用qemu模拟i386的linux内核,用于内核学习)