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

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

#include

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值