Chinaunix首页 | 论坛 | 博客
  • 博客访问: 78692
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 434
  • 用 户 组: 普通用户
  • 注册时间: 2017-03-23 09:31
个人简介

天行健,君子以自强不息;地势坤,君子以厚德载物

文章分类
文章存档

2018年(11)

2017年(30)

我的朋友

分类: LINUX

2018-01-06 18:24:51

Kernel + initrd(busybox制作,提供ext3文件系统模块) + ROOTFS(busybox制作)

initrd:仅需要提供内核访问真正的根文件系统所在设备需要的驱动,只包括存储设备和文件系统相关的模块
/etc/rc.d/rc.sysinit:初始化其他硬件的驱动程序

[root@localhost src]# tar xf linux-2.6.38.1.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src
[root@localhost src]# ls
debug  kernels  linux-2.6.38.1  linux-2.6.38.1.tar.gz  redhat
[root@localhost src]# ln -s linux-2.6.38.1 linux
[root@localhost src]# ls
debug  kernels  linux  linux-2.6.38.1  linux-2.6.38.1.tar.gz  redhat
[root@localhost src]# cd linux
[root@localhost linux]# make menuconfig  #编译内核#
[root@localhost linux]# make drivers/net/
[root@localhost linux]# make drivers/net/pcnet32.ko
[root@localhost linux]# modinfo drivers/net/pcnet32.ko
filename:       drivers/net/pcnet32.ko
license:        GPL
description:    Driver for PCnet32 and PCnetPCI based ethercards
author:         Thomas Bogendoerfer
srcversion:     ABBD126DB646898EFFEAA89
alias:          pci:v00001023d00002000sv*sd*bc02sc00i*
alias:          pci:v00001022d00002000sv*sd*bc*sc*i*
alias:          pci:v00001022d00002001sv*sd*bc*sc*i*
depends:        mii                         #依赖mii模块,需要安装#
vermagic:       2.6.38.1-1.mlinux SMP mod_unload modversions 686
parm:           debug:pcnet32 debug level (int)
parm:           max_interrupt_work:pcnet32 maximum events handled per interrupt (int)
parm:           rx_copybreak:pcnet32 copy breakpoint for copy-only-tiny-frames (int)
parm:           tx_start_pt:pcnet32 transmit start point (0-3) (int)
parm:           pcnet32vlb:pcnet32 Vesa local bus (VLB) support (0/1) (int)
parm:           options:pcnet32 initial option setting(s) (0-15) (array of int)
parm:           full_duplex:pcnet32 full duplex setting(s) (1) (array of int)
parm:           homepna:pcnet32 mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet (array of int)

[root@localhost linux]# make arch/x86

1、安装grub
[root@localhost ~]# mount /dev/hdb1 /mnt/boot
[root@localhost ~]# mount /dev/hdb2 /mnt/sysroot/
[root@localhost ~]# grub-install --root-directory=/mnt /dev/hdb
Installation finished. No error reported.
This is the contents of the device map /mnt/boot/grub/device.map.
Check if this is correct or not. If any of the lines is incorrect,
fix it and re-run the script `grub-install'.

(fd0)   /dev/fd0
(hd0)   /dev/hda
(hd1)   /dev/hdb
(hd2)   /dev/sda
[root@localhost ~]# ls /mnt/boot/
grub  initrd.gz  lost+found  vmlinuz
[root@localhost ~]# cp /boot/vmlinuz-2.6.18-308.el5 /mnt/boot/vmlinuz
[root@localhost ~]# sync
[root@localhost ~]#tar xf busybox-1.20.2.tar.bz2
[root@localhost ~]# cd busybox-1.20.2/
[root@localhost busybox-1.20.2]# cd include/
[root@localhost include]# mkdir mtd
[root@localhost busybox-1.20.2]# cp /root/linux-4.14.8/include/uapi/mtd/ubi-user.h include/mtd
[root@localhost busybox-1.20.2]# make menuconfig    #本次不做修改#
[root@localhost busybox-1.20.2]# make install
[root@localhost ~]# mkdir /tmp/initrd
[root@localhost ~]# cd busybox-1.20.2/
[root@localhost busybox-1.20.2]# cp _install/* /tmp/initrd/ -a
[root@localhost initrd]# mkdir proc sys mnt/sysroot dev tmp lib/modules etc -pv
[root@localhost initrd]# rm linuxrc
rm:是否删除 符号链接 “linuxrc”? y
[root@localhost initrd]# mknod dev/console c 5 1
[root@localhost initrd]# mknod dev/null c 1 3
[root@localhost initrd]# ls dev/ -l
总计 8
crw-r--r-- 1 root root 5, 1 12-24 21:27 console
crw-r--r-- 1 root root 1, 3 12-24 21:28 null

[root@localhost initrd]# vim init
    mount -t proc proc /proc   #挂载/proc 文件系统#
    mount -t sysfs sysfs /sys  #挂载/sys文件系统#

    insmod /lib/modules/jbd.ko
    insmod /lib/modules/ext3.ko

    mdev -s  #探测额外硬件#

    mount -t ext3 /dev/hda2 /mnt/sysroot  #挂载根文件系统#

    exec switch_root /mnt/sysroot /sbin/init #切换虚根到真正的根文件系统上#
[root@localhost initrd]# chmod +x init
[root@localhost initrd]# modinfo jbd
filename:       /lib/modules/2.6.18-308.el5/kernel/fs/jbd/jbd.ko
license:        GPL
srcversion:     11842879E04FE2392B988CC
depends:        
vermagic:       2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
module_sig:    883f3504f2326dcdc995cc0b59af121112a9509d1d376cb3f07503612b0236e17947cc804b2e6909e3d31e223c8ae8dc3083a2d932d5329bef38115a
[root@localhost initrd]# modinfo ext3
filename:       /lib/modules/2.6.18-308.el5/kernel/fs/ext3/ext3.ko
license:        GPL
description:    Second Extended Filesystem with journaling extensions
author:         Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others
srcversion:     26DC008FC415305C5F65313
depends:        jbd
vermagic:       2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
module_sig:    883f3504f2326dddc995cc0b59af121112b66609f56f4d643dcbbde3384fc7aee2342f5b331f631b09d15ace08361a0e2dfa6ec1ec46718b122bde923
[root@localhost initrd]# cp /lib/modules/2.6.18-308.el5/kernel/fs/jbd/jbd.ko lib/modules/
[root@localhost initrd]# cp /lib/modules/2.6.18-308.el5/kernel/fs/ext3/ext3.ko lib/modules/

[root@localhost initrd]# find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz
[root@localhost initrd]# vim /mnt/boot/grub/grub.conf
default=0
timeout=3
title MageEdu Linux (2.6.18)
        root (hd0,0)
        kernel /vmlinuz ro root=/dev/hda2
        initrd /initrd.gz
[root@localhost ~]# cp busybox-1.20.2/_install/* /mnt/sysroot/ -a
[root@localhost ~]# cd /mnt/sysroot/
[root@localhost sysroot]# ls
bin  linuxrc  sbin  usr
[root@localhost sysroot]# rm linuxrc
rm:是否删除 符号链接 “linuxrc”? y
[root@localhost sysroot]# mkdir boot root etc/rc.d/init.d var/{log,lock,run} proc sys dev lib/modules tmp home -pv
[root@localhost sysroot]# mknod dev/console c 5 1
[root@localhost sysroot]# mknod dev/null c 1 3
[root@localhost sysroot]# vim etc/inittab
::sysinit:/etc/rc.d/rc.sysinit
console::respawn:-/bin/sh
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
[root@localhost sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/sh
echo -e "\tWelcome to  \033[31mMageEdu\033[0m Linux"

echo "mount proc and sys ...."
mount -t proc proc /proc
mount -t sysfs sysfs /sys

echo -e "Remounting the root filesystem ...[  \033[32mOK\033[0m  ]"
mount -t ext3 -o remount,rw /dev/hda2 /

echo "Detect and export hardward information...."
mdev -s

echo -e "Mounting the filesystem ...[  \033[32mOK\033[0m  ]"
mount -a
[root@localhost sysroot]# chmod +x etc/rc.d/rc.sysinit

[root@localhost sysroot]# vim etc/fstab
sysfs           /sys            sysfs   defaults 0 0
proc            /proc           proc    defaults 0 0
/dev/hda1       /boot           ext3    defaults 0 0
/dev/hda2       /               ext3    defaults 1 1

[root@localhost ~]# ./bincopy.sh   
Your command: bash    #将bash移植到/mnt/sysroot#
/bin/bash
copy lib /lib/libtermcap.so.2 finished.
copy lib /lib/libdl.so.2 finished.
copy lib /lib/libc.so.6 finished.
copy lib /lib/ld-linux.so.2 finished.
copy /bin/bash finished.
Continue: q
[root@localhost ~]# chroot /mnt/sysroot/
bash-3.2# exit
exit

添加登录用户:
[root@localhost ~]# grep -E "^root:" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]# grep -E "^root:" /etc/passwd > /mnt/sysroot/etc/passwd
[root@localhost ~]# grep -E "^root:" /etc/shadow > /mnt/sysroot/etc/shadow
[root@localhost ~]# grep -E "^root:" /etc/group > /mnt/sysroot/etc/group
[root@localhost ~]# ls /mnt/sysroot/etc/
fstab  group  inittab  passwd  rc.d  shadow
[root@localhost ~]# vim /mnt/sysroot/etc/passwd   #查看内容,注意脚本是/bin/bash,因busybox是/bin/sh,所以需要移植/bin/bash#
root:x:0:0:root:/root:/bin/bash      


[root@localhost ~]# vim /mnt/sysroot/etc/inittab
::sysinit:/etc/rc.d/rc.sysinit
::respawn:/sbin/getty 9600 tty1
::respawn:/sbin/getty 9600 tty2
::ctrlaltdel:/sbin/reboot
::shutdown:/bin/umount -a -r
[root@localhost ~]# vim /mnt/sysroot/etc/hostname  #定义主机名#
HOSTNAME=tiny.magedu.co


#!/bin/sh
echo -e "\tWelcome to  \033[31mMageEdu\033[0m Linux"

echo "Set the hostname..."
[ -f /etc/hostname ] && . /etc/hostname
[ -z "$HOSTNAME" -o "$HOSTNAME" == '(none)' ] && HOSTNAME=localhost
hostname $HOSTNAME
加载HOSTNAME变量到系统上:
[root@localhost ~]# vim /mnt/sysroot/etc/rc.d/rc.sysinit
echo "mount proc and sys ...."
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  /

echo -e "Remounting the root filesystem ...[  \033[32mOK\033[0m  ]"
mount -t ext3 -o remount,rw /dev/hda2 /

echo "Detect and export hardward information...."
mdev -s

echo -e "Mounting the filesystem ...[  \033[32mOK\033[0m  ]"
mount -a

添加开机后显示的抬头信息(/etc/issue):
[root@localhost ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 5.8 (Tikanga)
Kernel \r on an \m

[root@localhost ~]# cp /etc/issue /mnt/sysroot/etc/

装载网卡驱动:
[root@localhost sysroot]# modinfo mii
filename:       /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko
license:        GPL
description:    MII hardware support library
author:         Jeff Garzik
srcversion:     16DCEDEE4B5629C222C352D
depends:        
vermagic:       2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
module_sig:    883f3504f23273dc995cc0b59af12111211c809f6f79961e4a84bc133b97d3d32bb217be64af3d1c09d11dbf04e53225fd698ce3def9448666b2a5a31
[root@localhost sysroot]# modinfo pcnet32
filename:       /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko
license:        GPL
description:    Driver for PCnet32 and PCnetPCI based ethercards
author:         Thomas Bogendoerfer
srcversion:     F81443556AAE169CBF80F55
alias:          pci:v00001023d00002000sv*sd*bc02sc00i*
alias:          pci:v00001022d00002000sv*sd*bc*sc*i*
alias:          pci:v00001022d00002001sv*sd*bc*sc*i*
depends:        mii
vermagic:       2.6.18-308.el5 SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1
parm:           debug:pcnet32 debug level (int)
parm:           max_interrupt_work:pcnet32 maximum events handled per interrupt (int)
parm:           rx_copybreak:pcnet32 copy breakpoint for copy-only-tiny-frames (int)
parm:           tx_start_pt:pcnet32 transmit start point (0-3) (int)
parm:           pcnet32vlb:pcnet32 Vesa local bus (VLB) support (0/1) (int)
parm:           options:pcnet32 initial option setting(s) (0-15) (array of int)
parm:           full_duplex:pcnet32 full duplex setting(s) (1) (array of int)
parm:           homepna:pcnet32 mode for 79C978 cards (1 for HomePNA, 0 for Ethernet, default Ethernet (array of int)
module_sig:    883f3504f2326fedc995cc0b59af121112c9240a0888050bfd72bf9d5ba34fa2b2d35ad9c599dc2b509f6fc0ce174c811a747cc9292d8a4d837983e5b14
[root@localhost sysroot]# cp /lib/modules/2.6.18-308.el5/kernel/drivers/net/mii.ko lib/modules/
[root@localhost sysroot]# cp  /lib/modules/2.6.18-308.el5/kernel/drivers/net/pcnet32.ko lib/modules/
[root@localhost sysroot]# sync

[root@localhost sysroot]# vim etc/rc.d/rc.sysinit
#!/bin/sh
echo -e "\tWelcome to  \033[31mMageEdu\033[0m Linux"

echo "Set the hostname..."
[ -f /etc/hostname ] && . /etc/hostname
[ -z "$HOSTNAME" -o "$HOSTNAME" == '(none)' ] && HOSTNAME=localhost
hostname $HOSTNAME

echo "mount proc and sys ...."
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  /

echo -e "Remounting the root filesystem ...[  \033[32mOK\033[0m  ]"
mount -t ext3 -o remount,rw /dev/hda2 /

echo "Detect and export hardward information...."
mdev -s

echo -e "Mounting the filesystem ...[  \033[32mOK\033[0m  ]"
mount -a

echo "Load ethernet card module..."
insmod /lib/modules/mii.ko
insmod /lib/modules/pcnet32.ko



实现日志信息记录:
[root@localhost ~]# cd /mnt/sysroot/
[root@localhost sysroot]# vim etc/rc.d/rc.sysinit

#!/bin/sh
echo -e "\tWelcome to  \033[31mMageEdu\033[0m Linux"

echo "Set the hostname..."
[ -f /etc/hostname ] && . /etc/hostname
[ -z "$HOSTNAME" -o "$HOSTNAME" == '(none)' ] && HOSTNAME=localhost
hostname $HOSTNAME

echo "mount proc and sys ...."
mount -t proc proc /proc
mount -t sysfs sysfs /sys
mount -o  remount,rw  /

echo -e "Remounting the root filesystem ...[  \033[32mOK\033[0m  ]"
mount -t ext3 -o remount,rw /dev/hda2 /

echo "Detect and export hardward information...."
mdev -s

echo -e "Mounting the filesystem ...[  \033[32mOK\033[0m  ]"
mount -a

echo "Start syslog...."    #启动syslog服务#
syslogd
klogd

echo "Load ethernet card module..."
insmod /lib/modules/mii.ko
insmod /lib/modules/pcnet32.ko

[root@localhost sysroot]# touch var/log/secure
[root@localhost sysroot]# chmod 600 var/log/secure    #设置访问权限,只允许管理员访问#

[root@localhost sysroot]# vim etc/syslog.conf   #添加syslog.conf配置文件#
*.info;auth.none                /var/log/messages    #为安全起见,auth的所有级别的日志信息不记录在messages#

auth.*                          /var/log/secure    #auth的所有级别的日志信息在secure下#


[root@localhost sysroot]# ls var/           #var/下需要有log目录#
lock  log  run
[root@localhost sysroot]# sync              #将修改的信息同步到系统中#
阅读(1162) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~