本片文章介绍在宿主机机上面通过bash脚本实现自动化构建Min Linux,从而了解linux系统的启动过程,当系统无法启动时可以有效定位出现错误的原因,在阅读本片文章之前建议先阅读这篇文章: http://urchin.blog.51cto.com/4356076/788394
BASH 脚本代码如下:
首先您需要在宿主机上面添加一块磁盘IDE或者SCSI,它们分别被linux系统识别为hd和sd,并根据您的实际情况修改脚本中的磁盘设备参数,我的实验环境是在Vmware workstation上实现而且宿主机(RedHat 5.4 OS)上有两块SCSI的磁盘,新添加的磁盘设备是SDB
主代码
- #!/bin/bash
- # Author: urchin,
- # Version:1.0.0
- # Date: 20120309
- # Mail:shouwei19870814@163.com
- ## clear the MBR co
- dd if=/dev/zero of=/dev/sdb bs=512 count=1
- ## create partitions
- echo '
- n
- p
- 1
- +100M
- n
- p
- 2
- +128M
- t
- 2
- 82
- n
- p
- 3
- +4G
- w' | fdisk /dev/sdb >/dev/null 2>&1
- ## kernel reread partition table
- partprobe /dev/sdb
- partprobe /dev/sdb
- sleep 3
- ## format partitions
- mkfs -j /dev/sdb1 >/dev/null 2>&1
- mkswap /dev/sdb2 >/dev/null 2>&1
- mkfs -j /dev/sdb3 >/dev/null 2>&1
- ## create diretory
- mkdir -pv /mnt/{boot,sysroot}
- ## mount partitions
- mount /dev/sdb1 /mnt/boot
- mount /dev/sdb3 /mnt/sysroot
- ## install grub
- grub-install --root-directory=/mnt /dev/sdb
- cd /mnt/sysroot
- mkdir -pv ./{bin,sbin,usr/lib,lib/modules,etc/{rc.d,sysconfig},tmp,mnt,media,var/run,opt,dev,home,root,proc,sys,srv}
- cd /mnt/boot
- ## copy kernel
- cp /boot/vmlinuz-2.6.18-164.el5 vmlinuz
- ## modify and order initrd
- mkdir -pv /tmp/linux
- cd /tmp/linux
- zcat /boot/initrd-2.6.18-164.el5.img | cpio -id
- sed -i "s@/dev/Vol0/LogVol00@/dev/sda3@g" /tmp/linux/init
- find . | cpio -o -H newc --quiet | gzip -9 > /mnt/boot/initrd
- cd /mnt/boot/grub && rm -rf /tmp/linux
- ## create grub.conf
- cat > grub.conf << EOF
- default=0
- timeout=3
- title Min_linux
- root (hd0,0)
- kernel /vmlinuz ro root=/dev/sda3
- initrd /initrd
- EOF
- ## order issue file
- cat > /mnt/sysroot/etc/issue << EOF
- Welcome to Min_linux
- the password of root is redhat
- EOF
- ## copy modules to init Ethernet
- cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/mii.ko /mnt/sysroot/lib/modules
- cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/pcnet32.ko /mnt/sysroot/lib/modules
- ## create inittab
- cat > /mnt/sysroot/etc/inittab <<EOF
- id:3:initdefault:
- si::sysinit:/etc/rc.d/rc.sysinit
- l0:0:wait:/etc/rc.d/rc.sysdone
- l1:1:wait:/sbin/init -t1 S
- 1:2345:respawn:/sbin/mingetty tty1
- 2:2345:respawn:/sbin/mingetty tty2
- 3:2345:respawn:/sbin/mingetty tty3
- 4:2345:respawn:/sbin/mingetty tty4
- 5:2345:respawn:/sbin/mingetty tty5
- 6:2345:respawn:/sbin/mingetty tty6
- EOF
- ## create link for sh
- cd /mnt/sysroot/bin
- ln -s bash sh
- ## creating rc.sysinit and giving the executive right to it
- cat > /mnt/sysroot/etc/rc.d/rc.sysinit << EOF
- #!/bin/bash
- echo -e "Welcome to My Min_linux"
- [ -r /etc/sysconfig/network ] && source /etc/sysconfig/network
- [ -z $HOSTNAME ] && /bin/hostname localhost || /bin/hostname $HOSTNAME
- echo " insert pcnet32 module."
- /sbin/insmod /lib/modules/mii.ko
- /sbin/insmod /lib/modules/pcnet32.ko
- echon " mount filesystme"
- /bin/mount -n -o remount,rw /dev/sda3
- echo "mount the extra fielsystem"
- mount -n -a
- echo " activing swap filesystem."
- swapon /dev/sda2
- echo "seting loopback"
- ifconfig lo 127.0.0.1/8
- echo "set the kernel parameters."
- /sbin/sysctl -p
- EOF
- chmod +x /mnt/sysroot/etc/rc.d/rc.sysinit
- ## creating shutdown file and give the executive right to it
- cat > /mnt/sysroot/etc/rc.d/rc.sysdone << EOF
- #!/bin/bash
- /bin/sync
- /bin/sleep 3
- /bin/sync
- exec /sbin/halt -p
- EOF
- ## give the executive right to it
- chmod +x /mnt/sysroot/etc/rc.d/rc.sysdone
- ## copy login file
- cp /root/login /mnt/sysroot/bin
- ## create /etc/{passwd,group,shadow}
- head -1 /etc/passwd > /mnt/sysroot/etc/passwd
- head -1 /etc/group > /mnt/sysroot/etc/group
- head -1 /etc/shadow > /mnt/sysroot/etc/shadow
- ## change /etc/shadow's right
- chmod 400 /mnt/sysroot/etc/shadow
- ## copy libraries needed by Authentication
- cd /usr/lib
- cp -a libnss_files.so libnss3.so libnssckbi.so libnss_dns.so libnssutil3.so libnss_compat.so libnss_db.so /mnt/sysroot/usr/lib
- cd /lib
- cp -a libnss_compat* libnss_dns* libnss_db* libnss_files* /mnt/sysroot/lib/
- ## order cdrom facility
- cd /dev
- cp -a cdrom /mnt/sysroot/dev
- cp -a cdrom-hdc /mnt/sysroot/dev
- cp -a hdc /mnt/sysroot/dev
- ## change the right of /mnt/sysroot/root
- chmod go=--- /mnt/sysroot/root -R
- ## copy vim's configration file
- cp /etc/vimrc /mnt/sysroot/etc
- ## create /mnt/sysroot/etc/nsswitch.conf
- cat > /mnt/sysroot/etc/nsswitch.conf << EOF
- passwd: files
- shadow: files
- group: files
- hosts: files dns
- EOF
- ## create fstab
- cat >> /mnt/sysroot/etc/fstab << EOF
- /dev/sda2 swap swap defaults 0 0
- /dev/sda3 / ext3 defaults 0 0
- /dev/sda1 /boot ext3 defaults 0 0
- proc /proc proc defaults 0 0
- devpts /devpts devpts defaults 0 0
- sysfs /sys sysfs defaults 0 0
- tmpfs /dev/shm tmpfs defaults 0 0
- EOF
- ## create /etc/sysconfig/network file
- cat > /mnt/sysroot/etc/sysconfig/network << EOF
- NETWORKING=yes
- HOSTNAME=www.urchin.org
- EOF
- ## create /etc/sysctl.conf
- cat > /mnt/sysroot/etc/sysctl.conf << EOF
- net.ipv4.ip_forward = 1
- EOF
- ## create /etc/profile
- cat > /mnt/sysroot/etc/profile << EOF
- # /etc/profile
- PS1='[\u@\h \W]\$'
- PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
- export PS1 PATH
- EOF
- ## create /etc/bashrc
- cat > /mnt/sysroot/etc/bashrc << EOF
- # /etc/bashrc
- if [ $UID -gt 99 ] && [ `id -gn` -eq `id -un` ]; then
- umask 002
- else
- umask 022
- fi
- EOF
- ## copy applications needed by min_linux
- exec /root/bcp.sh
复制应用程序以及对应程序运行所需的库文件
- #!/bin/bash
- #Author:urchin
- #Version:1.0.0
- #Date:20120309
- #E-mail:shouwei19870814@163.com
- ##define function to copy applications and libraries needed by applications
- function BCP {
- TARGET=/mnt/sysroot
- COMMAND=`which $1 | grep -o "/.*"`
- CMDPATH=${COMMAND%/*}
- [ -d $TARGET$CMDPATH ] || mkdir -p $TARGET$CMDPATH
- [ -e $TARGET$COMMAND ] || cp $COMMAND $TARGET$CMDPATH
- for LIBFILE in `ldd $COMMAND | grep -o "/.*lib[^[:space:]]*"`; do
- LIBPATH=${LIBFILE%/*}
- [ -d $TARGET$LIBPATH ] || mkdir -p $TARGET$LIBPATH
- [ -e $TARGET$LIBFILE ] || cp $LIBFILE $TARGET$LIBPATH
- done
- }
- #
- for I in {du,df,dd,kill,fdisk,mkfs,e2fsck,tune2fs,clear,sysctl, \
- chmod,chgrp,chown,ftp,swapoff,swapon,free,init,bash,sync,grep,cat, \
- less,halt,hostname,autoconf,umount,automake,gcc,vim,vi,sleep,yum, \
- ls,insmod,ping,ifconfig,reboot,mount,mingetty,passwd,route,uname, \
- mkdir,ps,tree,rm,cp,mv,netstat,id,tar,ln,touch}; do
- BCP $I
- done
当您在扩展您的Min linux时需要用到其他的应用程序及其程序所需的库文件时,您可以使用一下的脚本复制程序及其所需的库文件
- #!/bin/bash
- #Author:urchin
- #Version:1.0.0
- #Date:20120309
- #E-mail:shouwei19870814@163.com
- function BCP {
- TARGET=/mnt/sysroot
- COMMAND=`which $1 | grep -o "/.*"`
- CMDPATH=${COMMAND%/*}
- [ -d $TARGET$CMDPATH ] || mkdir -p $TARGET$CMDPATH
- [ -e $TARGET$COMMAND ] || cp $COMMAND $TARGET$CMDPATH
- for LIBFILE in `ldd $COMMAND | grep -o "/.*lib[^[:space:]]*"`; do
- LIBPATH=${LIBFILE%/*}
- [ -d $TARGET$LIBPATH ] || mkdir -p $TARGET$LIBPATH
- [ -e $TARGET$LIBFILE ] || cp $LIBFILE $TARGET$LIBPATH
- done
- }
- while true; do
- read -p "A Command: " MYCMD
- case $MYCMD in
- q|Q)
- echo "Quit..."
- exit 0
- ;;
- *)
- ! which $MYCMD &> /dev/null && echo "Wrong command..." && continue
- BCP $MYCMD
- ;;
- esac
- done
注:请确保各个脚本有执行权限,特别是复制application及其运行所需的Libraries的脚本有执行权限,否则会出现错误
转载于:https://blog.51cto.com/urchin/828167