您的位置:首页 > 运维架构 > Linux

Linux最小根文件系统的建立,内核模块的编译,Qemu模拟测试最小系统

2014-04-19 23:41 701 查看
1. 利用busybox建立最小根文件系统

===============================================

(1.1) 下载和编译busybox

git clone git://git.busybox.net/busybox

cd busybox/

git tag

git checkout 1_22_1

make menuconfig (选择静态编译,并且去掉不需要的内容)

make

make install 

缺省的安装目录为_install/

(1.2) 拷贝busybox安装目录的内容到rootfs中

lipeng@localhost:~/busybox$ cp -rfP _install/* ~/rootfs/

(1.3) 补充必要的目录和文件(除了etc目录之外的部分)

删除 linuxrc -> bin/busybox

添加 init -> bin/busybox

并且建立一些必需的目录,补充后的目录结构为

lipeng@localhost:~/rootfs$ tree -d

.

├── bin

├── dev

├── etc

│   └── init.d

├── lib

│   └── modules

│       └── 2.6.32.61

├── proc

├── sbin

├── sys

└── usr

    ├── bin

    └── sbin

(1.4) 编辑etc/fstab

proc /proc proc defaults 0 0

sys /sys sysfs defaults 0 0

(1.5) 编辑etc/init.d/rcS

#!/bin/sh

mknod /dev/console c 5 1

echo  "INIT RCS  RUNNING"

mount -a

echo "After mount -a!!!!!!!!!!!!!!"

(1.6) 编辑etc/inittab

::sysinit:/etc/init.d/rcS

::respawn:-/bin/sh

::restart:/sbin/init

::ctrlaltdel:/sbin/reboot

::shutdown:/bin/umount -a -r

::shutdown:/sbin/swapoff -a

2. 编写模块,在最小根文件系统中插入内核,用Qemu进行简单实验

===============================================

(2.1) 写一个最简单的模块,例如目录为~/hello,生成hello.ko文件

lipeng@debian:~/hello$ cat main.c

#include <linux/init.h>

#include <linux/module.h>

MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)

{

  printk(KERN_EMERG "hello\n");

  return 0;

}

static void hello_exit(void)

{

  printk(KERN_EMERG "goodby\n");

}

module_init(hello_init);

module_exit(hello_exit);

lipeng@localhost:~/hello$ cat Makefile

KERNELDIR ?= ~/linux-2.6.32.61

obj-m := hello.o

hello-objs := main.o 

PWD := $(shell pwd)

all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

(2.2) 编译Linux Kernel

make -C ~/linux-2.6.32.61

(2.3) 编译模块

cd /home/lipeng/hello; make

(2.4)

拷贝模块到rootfs,并且修改rootfs的/etc/init.d/rcS来加载模块

cp /home/lipeng/hello/hello.ko /home/lipeng/rootfs/

lipeng@debian:~/trunk/code/rootfs$ cat etc/init.d/rcS 

#!/bin/sh

mknod /dev/console c 5 1

echo  "INIT RCS  RUNNING"

mount -a

echo "After mount -a!!!!!!!!!!!!!!"

insmod /hello.ko

(2.5)

制作根文件系统的initramfs

cd /home/lipeng/rootfs; find . | cpio -o -H newc | gzip > ~/initramfs.cpio.gz

(2.6)

用Qemu作实验,这里模拟CPU为4核,内存1600MB
qemu -kernel /home/lipeng/linux-2.6.32.61/arch/x86/boot/bzImage -initrd /home/lipeng/initramfs.cpio.gz -hda /dev/zero -m 1600M -smp 4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux qemu kernel