一、编译内核相关命令

1、重装initrd文件命令:

mkinitrdcreates initial ramdisk p_w_picpaths for preloading modules

格式:mkinitrd  initrd文件路径 内核版本号,如:mkinitrd  /boot/initrd-`uname -r`.img  `uname -r`

2、I/O处理命令

        a、命令格式说明2

${parameter#*word}

${parameter##*word}

              The word is expanded to produce a pattern just as in pathname expansion.  If the pattern matches the beginning of the value of

              parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern  (the  ?..?.

              case)  or  the  longest  matching pattern (the ?..#?..case) deleted.  If parameter is @ or *, the pattern removal operation is

              applied to each positional parameter in turn, and the expansion is the resultant list.  If parameter is an array variable sub-

              scripted  with  @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the

              resultant list.

 

${parameter%word*}

${parameter%%word*}

              The word is expanded to produce a pattern just as in pathname expansion.  If the pattern matches a  trailing  portion  of  the

              expanded  value  of  parameter, then the result of the expansion is the expanded value of parameter with the shortest matching

              pattern (the ?..?..case) or the longest matching pattern (the ?..%?..case) deleted.  If parameter  is  @  or  *,  the  pattern

              removal  operation  is applied to each positional parameter in turn, and the expansion is the resultant list.  If parameter is

              an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in  turn,  and

              the expansion is the resultant list.

        b、命令格式

            # FILE=/usr/local/src

            # echo ${FILE#*/}: usr/local/src

            # echo ${FILE##*/}: src

            # ${FILE%/*}: /usr/local

            # ${FILE%%/*}:

二、编译系统内核、grub和文件系统到新的磁盘

1、在原有CentOS5.9中添加一块IDE硬盘分2个磁盘并格式化为ext3文件系统

2、挂载分区

mkdir /mnt/boot

mkdir /mnt/sysroot

mount /dev/hda1 /mnt/boot

moutn /dev/hda2 /mnt/sysroot

3、创建grub

grub-install --root-directory=/mnt /dev/hda

4、复制系统内核到/mnt/boot下cp /boot/vmlinuz-2.6.18-348.el5 /mnt/boot/vmlinuz

5、重装initrd文件:mkinitrd  /boot/initrd-`uname -r`.img  `uname -r`

6、重新编译根文件系统文件

cp /boot/initrd-2.6.18-348.el5.img /root

mv initrd-2.6.18-348.el5.img initrd-2.6.18-348.el5.img.gz

gzip -d initrd-2.6.18-348.el5.img.gz 

mkdir test

cd test

cpio -id < ../initrd-2.6.18-348.el5.img或zcat /boot/initrd-2.6.18-348.el5.img | cpio -id

vim init(修改其中一行为mkrootdev -t ext3 -o defaults,ro /dev/hda2注释掉#resume LABEL=SWAP-sda5)

7、封装新的根文件系统

find . | cpio -H newc --quiet -o | gzip -9 > /mnt/boot/initrd.gz

8、编辑新的grub文件vim /mnt/boot/grub/grub.conf

default=0

timeout=5

title Test Linux (Magedu Team)

root (hd0,0)

kernel /vmlinuz

initrd /initrd.gz

9、创建根文件系统中必要的文件

cd /mnt/sysroot

mkdir -pv proc sys dev etc/rc.d lib bin sbin boot home var/log usr/{bin,sbin} root tmp

10、复制文件系统的bash环境

cp /sbin/init /mnt/sysroot/sbin/

cp /bin/bash /mnt/sysroot/bin

11、复制系统共享库文件

ldd /sbin/init

cp /lib/libsepol.so.1 /mnt/sysroot/lib

cp /lib/libselinux.so.1 /mnt/sysroot/lib

cp /lib/libc.so.6 /mnt/sysroot/lib

cp /lib/libdl.so.2 /mnt/sysroot/lib

ldd /bin/bash

cp /lib/libtermcap.so.2 /mnt/sysroot/lib

12、编辑inittab为linux初始化文件系统时init初始化程序用到的配置文件

vim /mnt/sysroot/etc/inittab

id:3:initdefault:

si::sysinit:/etc/rc.d/rc.sysinit

chmod +x /mnt/sysroot/etc/inittab

13、编辑vim /mnt/sysroot/etc/rc.d/rc.sysinit

#!/bin/bash

#

echo -e "\tWelcome to \033[31mMageEdu Team\033[0m Linux."

/bin/bash

chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit

14、编辑脚本用于复制二进制程序及其依赖的库文件的脚本可复制所有要用到的系统命令

#!/bin/bash

#

DEST=/mnt/sysroot

libcp() {

  LIBPATH=${1%/*}

  [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH

  [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished."

}

 

bincp() {

  CMDPATH=${1%/*}

  [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH

  [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH

 

  for LIB in  `ldd $1 | grep -o "/.*lib\(64\)\{0,1\}/[^[:space:]]\{1,\}"`; do

    libcp $LIB

  done

}

 

read -p "Your command: " CMD

until [ $CMD == 'q' ]; do

   ! which $CMD &> /dev/null && echo "Wrong command" && read -p "Input again:" CMD && continue

  COMMAND=` which $CMD | grep -v "^alias" | grep -o "[^[:space:]]\{1,\}"`

  bincp $COMMAND

  echo "copy $COMMAND finished."

  read -p "Continue: " CMD

done

15、复制系统的网卡模块

mkdir /mnt/sysroot/lib/modules

cp /lib/modules/2.6.18-348.el5/kernel/drivers/net/mii.ko /mnt/sysroot/lib/modules

cp /lib/modules/2.6.18-348.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/lib/modules

16、编辑/mnt/sysroot/etc/inittab文件加载网卡模块添加

insmod /lib/modules/mii.ko

insmod /lib/modules/pcnet32.ko

ifconfig eth0 172.16.100.13/16

ifconfig lo 127.0.0.1/8

/bin/bash

17、切入到定制的系统内核进行测试chroot /mnt/sysroot

18、***原有宿主机的第一块磁盘保留第二块磁盘启动系统测试效果。