脚本自动创建一个小于10M的Linux系统

自动创建一个小于10M的Linux系统,脚本使用说明:需要在宿主机上装两个硬盘,,在测试的时候,只需要把第二块硬盘放到新的虚拟机中,然后启动就行了。

#!/bin/bash#:Title:#:Synopsis:#:Data:#:Version:1.0#:Author:ro#:Options:#fdisk and fail the deviceDISK(){ //封装磁盘分区格式化的函数read -p “choice a disk[/dev/sdb]:” CHOICENUM=`fdisk -l | grep $CHOICE |wc -l &>/dev/null`if [ “$NUM” -gt ‘0’ ];then fdisk -l $CHOICE MOUNT=`mount -l | grep “^$CHOICE” &>/dev/null` MOUNTDISK=`echo $MOUNT` for I in $MOUNTDISK ;do if mount | grep “$I” &>/dev/null ;then mount | grep “$I” else echo “$I is not mounted.” fi donefi}FAILDISK() { dd if=/dev/zero of=/dev/sdb bs=512 count=1 sleep .1 return 1} #create partions CREATEDISK() {echo ‘np1+50Mnp2+512Mnp3+256Mt382w’ | fdisk $CHOICE &>/dev/nullpartprobe $CHOICEsleep .1mkfs.ext3 ${CHOICE}1 &>/dev/nullmkfs.ext3 ${CHOICE}2 &>/dev/nullmkswap -f ${CHOICE}3 &>/dev/null#mkfs}#mount the fdisk //挂载磁盘分区MOUNT(){if [ -d /mnt/$1 ];then if mount | grep “/mnt/$1”;then umount /mnt/$1 mount $CHOICE$2 /mnt/$1 else mount $CHOICE$2 /mnt/$1 fielsemkdir /mnt/$1mount $CHOICE$2 /mnt/$1fi}# make the root and make directories in sysboot //创建root目录和sysboot目录ROOTFS(){ cd /mnt/$1mkdir {boot,proc,sys,dev,home,root,etc/{rc.d,sysconfig,init.d},bin,sbin,lib,usr/{bin,sbin,lib,include},var/{log,run},tmp,mnt,opt,media} -pv &>/dev/null //创建基本的目录chmod 777 tmp#the script for init 0 to shutdown the systemcd /mnt/$1/etc/rc.dtouch shutdown.shcat >>shutdown.sh<<EOF#!/bin/bashsyncsleep 2syncsyncumount /dev/sda1umount /dev/sda2exec /sbin/halt -pEOFchmod +x shutdown.sh#the /sysroot/etc/inittab file and creat ittouch /mnt/$1/etc/inittabcd /mnt/$1/etccat >>inittab<<EOF#default level.id:3:initdefault:#—>rc.sysinit.si::sysinit:/etc/rc.d/rc.sysinit #—–>init 0 to shutdown. l0:0:wait:/etc/rc.d/shutdown.sh#—–>single user mode.l1:1:wait:/sbin/init -t1 S#—–>the terminals of the minilinux1:2345:respawn:/sbin/mingetty tty12:2345:respawn:/sbin/mingetty tty2EOF#cp the INTERNET CARD module to the minilinuxcd /mnt/$1/libmkdir -p modulescd modulescp /lib/modules/2.6.18-164.el5/kernel/drivers/net/mii.ko ./cp /lib/modules/2.6.18-164.el5/kernel/drivers/net/pcnet32.ko ./#the /etc/sysctl.conf filecd /mnt/$1/etctouch sysctl.confcat >>sysctl.conf<<EOFnet.ipv4.ip_forward = 0net.ipv4.ip_forward = 1net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 4294967295kernel.shmall = 268435456EOF#the /etc/rc.d/rc.sysinit file cd /mnt/$1/etc/rc.dtouch rc.sysinitcat >>rc.sysinit<<EOF#!/bin/bashecho -e “\t\tWelcome to \033[31;1mLittle\033[0m Linux…”##the network modules an set IP address to the minilinux##if [[ -x /sbin/sysctl && -r /etc/syctl.conf ]];thenecho $”configuring kernel paremeters”/sbin/sysctl -p /etc/sysctl.confecho $”….kernel parameters done.”fi[ -e /lib/modules/mii.ko ] && /sbin/insmod /lib/modules/mii.ko[ -e /lib/modules/pcnet32.ko ] && /sbin/insmod /lib/modules/pcnet32.koecho “load the eth cart drivers ok..”echo “set ip “/sbin/ifconfig lo 127.0.0.1/8/sbin/ifconfig eth0 192.168.0.133/24##the network modules an set IP address to the minilinux#############The hostname for the minilinux[ -f /etc/sysconfig/network ] && source /etc/sysconfig/network[ -z $HOSTNAME ] && HOSTNAME=RoQiecho $HOSTNAME > /proc/sys/kernel/hostname##########hostname for the systermmount -n -o remount,rw /mount -n -aEOFchmod +x rc.sysinit#the fstab filecd /mnt/$1/etc/touch fstabecho “/dev/sda2 / ext3 defaults 0 0” >fstabecho “/dev/sda1 /boot ext3 defaults 0 0” >>fstabecho “sysfs /sys sysfs defaults 0 0” >>fstabecho “proc /proc proc defaults 0 0” >>fstab#the issue file to show the login infomationcd /mnt/$1/etc/touch issuecat >>issue<<EOFRed Hat Enterprise Linux Server release 5.4 (Tikanga)Kernel \r on an \m**************************welcome to Ro’s minilinux**login:root;passwd: RedHat**************************EOF##the /proc/sys/kernel filemkdir -p /mnt/sysroot/proc/sys/kernel/touch hostnamecat >>hostname<<EOFRo.minilinux.orgEOF#the yum.conf files mkdir -p /mnt/$1/etc/yum.repos.d/touch yum.repocat >>yum.repo<<EOF[base]name=Instructor Server Repositorybaseurl=ftp://192.168.0.254/pub/Servergpgcheck=0[VT]name=Instructor VT Repositorybaseurl=ftp://192.168.0.254/pub/VTgpgcheck=0[Cluster]name=Instructor Cluster Repositorybaseurl=ftp://192.168.0.254/pub/Clustergpgcheck=0[ClusterStorage]name=Instructor ClusterStorage Repositorybaseurl=ftp://192.168.0.254/pub/ClusterStoragegpgcheck=0EOFcd /mnt/$1/root/touch .bash_profileecho “PS1='[\u@\h \W]\$'”>.bash_profile}#this function use to cp files from normal machine to the minilinuxCPLIB(){#cp the “login” file cp /tmp/myscript/login /mnt/sysroot/binchmod +x /mnt/sysroot/bin/login#CP libnss filescp /usr/lib/libnss3.so /mnt/sysroot/usr/lib/cp /usr/lib/libnssckbi.so /mnt/sysroot/usr/lib/cp /usr/lib/libnssutil3.so /mnt/sysroot/usr/bin/cp -d /usr/lib/libnss_files.so /mnt/sysroot/usr/lib/cp -d /usr/lib/libnss_compat.so /mnt/sysroot/usr/lib/cp -d /lib/libnss_files* /mnt/sysroot/lib/cp -d /lib/libnss_compat* /mnt/sysroot/lib/#cp login filescp -d /lib/libcrypt.so.1 /mnt/sysroot/lib/cp -d /lib/libcrypt-2.5.so /mnt/sysroot/lib/cp /lib/libm.so.6 /mnt/sysroot/lib/#cp -d /lib/libcrypt-2.5.so /mnt/sysroot/lib/#cp user and passwd files cp /etc/passwd /mnt/sysroot/etc/cp /etc/shadow /mnt/sysroot/etc/cp /etc/group /mnt/sysroot/etc/cp /etc/gshadow /mnt/sysroot/etc/#cp nsswitch.conf file cp /etc/nsswitch.conf /mnt/sysroot/etc/}#the script use to cp the /bin to /mnt/sysroot/binBINCP(){ if which $1 &>/dev/null;then DR=`which $1 | grep -o “/.*” | sed -n ‘s/\(.*\)\/.*/\1/p’` FR=`which $1 | grep -o “/.*”` [ -d ${2}$DR ] || mkdir -pv ${2}$DR &> /dev/null [ -e ${2}$FR ]|| cp $FR ${2}$DR LIB=`ldd $FR | sed -n ‘s/.*\/lib\/\(.*\)/\1/gp’ | awk ‘{print $1}’` for I in $LIB;doLDR=`ldd $FR | grep -o ‘/.*’ | awk ‘{print $1}’ | sed -n ‘s@\(/.*\)/'”$I”‘@\1@gp’`[ -d ${2}$LDR ] || mkdir -pv ${2}$LDR &> /dev/null[ ! -e ${2}”$LDR”/”$LIB” ] && cp -f “$LDR”/”$I” ${2}$LDR done elseecho “$1 is not exit.” fi}#change the initrd. fileINIT(){TMPDIR=`mktemp -d /tmp/little.XX`cd $TMPDIRzcat /boot/initrd-2.6.18-164.el5.img | cpio -id sed -i ‘s/echo Scanning and configuring dmraid supported devices/#&/g’ $TMPDIR/init sed -i ‘s/echo Scanning logical volumes/#&/g’ $TMPDIR/init sed -i ‘s/lvm vgscan –ignorelockingfailure/#&/g’ $TMPDIR/init sed -i ‘s/echo Activating logical volumes/#&/g’ $TMPDIR/init sed -i ‘s/lvm vgchange -ay –ignorelockingfailure vol0/#&/g’ $TMPDIR/init sed -i ‘s/resume LABEL=SWAP-sda3/#&/g’ $TMPDIR/init sed -i ‘s@mkrootdev -t ext3 -o defaults,ro /dev/vol0/root/@mkrootdev -t ext3 -o defaults,ro sda2@g’ $TMPDIR/init find . | cpio -H newc -o –quiet | gzip -9 > $1/initrd.gz# cp /boot/vmlinuz-2.6.18-164.el5 $1/vmlinuz cp /boot/vmlinuz-2.6.28.10-myminilinux /mnt/boot/vmlinuz-2.6.28.10-myminilinux}###the grub file GRUB(){# install grub grub-install –root-directory=/mnt $CHOICE &>/dev/null#the context of grub file cd /mnt/boot/grubtouch grub.confcat >>grub.conf<<EOFdefault=0timeout=10title Welcome to Ro_Qi minilinuxroot (hd0,0)kernel /vmlinuz-2.6.28.10-myminilinux ro root=/dev/sda2initrd /initrd.gzEOF}#the main functionHELPWORD(){ echo “./mknewlinux.sh -a:make a new linux.” echo “./mknewlinux.sh -d DEVICE_NAME:make a new fdisk.” echo “./mknewlinux.sh -m:mount the filesystem.” echo “./mknewlinux.sh -r:Initialize the rootfs.” echo “./mknewlinux.sh -b:copy the lib and command.” echo “./mknewlinux.sh -k:edit the interd file.” echo “./mknewlinux.sh -g:make grub.conf.” echo “./mknewlinux.sh -h:help files.”}while getopts “ad:mrbkgh” OPTS;do case $OPTS in a)DISKFAILDISKCREATEDISKMOUNT boot 1MOUNT sysroot 2ROOTFS sysrootCPLIBfor I in ls ifconfig ping vim useradd userdel usermod yum lsmod modprobe touch insmod mkdir mingetty hostname mv rm vi runlevel sync halt chmod swapoff swapon sysctl umount cp cat init bash ;do BINCP $I /mnt/sysrootdoneln -sv bash sh &>/dev/nullINIT /mnt/bootrm -rf $TMPDIRGRUB syncsyncsleep 2 ;; d)DISK $OPTARGFAILDISK $OPTARGCREATEDISK $OPTARG ;; m)MOUNT boot 1MOUNT sysroot 2 ;; r)ROOTFS sysroot ;; b)CPLIBfor I in pwd ls ifconfig ping useradd userdel usermod yum lsmod modprobe touch insmod mkdir mingetty hostname mv rm vi runlevel sync halt chmod swapoff swapon sysctl umount cp cat init bash ;do BINCP $I /mnt/sysrootdoneln -sv bash sh &>/dev/null ;; k)NIT /mnt/bootrm -rf $TMPDIR ;; g)GRUB syncsyncsleep 2 ;; h)HELPWORD ;; *)HELPWORD ;; esacdone#cp -d /lib/libcrypt-2.5.so /mnt/sysroot/lib/#ln -s bash sh

而开始追寻他内心世界的真正财富

脚本自动创建一个小于10M的Linux系统

相关文章:

你感兴趣的文章:

标签云: