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

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

#!/bin/bash
#:Title:
#:Synopsis:
#:Data:
#:Version:1.0
#:Author:ro
#:Options:
#fdisk and fail the device
DISK(){                        //封装磁盘分区格式化的函数
read -p "choice a disk[/dev/sdb]:" CHOICE
NUM=`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
    done
fi
}
FAILDISK() {
    dd if=/dev/zero of=/dev/sdb bs=512 count=1
    sleep .1
    return 1
}
#create partions
CREATEDISK() {
echo '
n
p
1
+50M
n
p
2
+512M
n
p
3
+256M
t
3
82
w' | fdisk $CHOICE &>/dev/null
partprobe $CHOICE
sleep .1
mkfs.ext3  ${CHOICE}1 &>/dev/null
mkfs.ext3  ${CHOICE}2 &>/dev/null
mkswap -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
     fi
 else
 mkdir /mnt/$1
 mount $CHOICE$2 /mnt/$1
 fi
}
# make the root and make directories in sysboot //创建root目录和sysboot目录
ROOTFS(){                                   
cd /mnt/$1
mkdir {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 system
cd /mnt/$1/etc/rc.d
touch shutdown.sh
cat >>shutdown.sh<<EOF
#!/bin/bash
sync
sleep 2
sync
sync
umount /dev/sda1
umount /dev/sda2
exec /sbin/halt -p
EOF
chmod +x shutdown.sh
#the /sysroot/etc/inittab file and creat it
touch /mnt/$1/etc/inittab
cd /mnt/$1/etc
cat >>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 minilinux
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
EOF
#cp the INTERNET CARD module to the minilinux
cd /mnt/$1/lib
mkdir  -p modules
cd modules
cp /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 file
cd /mnt/$1/etc
touch  sysctl.conf
cat >>sysctl.conf<<EOF
net.ipv4.ip_forward = 0
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
EOF
#the /etc/rc.d/rc.sysinit file
cd /mnt/$1/etc/rc.d
touch rc.sysinit
cat >>rc.sysinit<<EOF
#!/bin/bash
echo -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 ]];then
 echo $"configuring kernel paremeters"
 /sbin/sysctl -p /etc/sysctl.conf
 echo $"....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.ko
 echo "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=RoQi
echo $HOSTNAME > /proc/sys/kernel/hostname
##########hostname for the systerm
mount -n -o remount,rw /
mount -n -a
EOF
chmod +x rc.sysinit
#the fstab file
cd /mnt/$1/etc/
touch fstab
echo "/dev/sda2      /         ext3    defaults        0 0" >fstab
echo "/dev/sda1      /boot     ext3    defaults        0 0" >>fstab
echo "sysfs          /sys      sysfs   defaults        0 0" >>fstab
echo "proc           /proc     proc    defaults        0 0" >>fstab
#the issue file to show the login infomation
cd /mnt/$1/etc/
touch issue
cat >>issue<<EOF
Red 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 file
mkdir -p /mnt/sysroot/proc/sys/kernel/
touch hostname
cat >>hostname<<EOF
Ro.minilinux.org
EOF
#the yum.conf files
mkdir -p  /mnt/$1/etc/yum.repos.d/
touch yum.repo
cat >>yum.repo<<EOF
[base]
name=Instructor Server Repository
baseurl=ftp://192.168.0.254/pub/Server
gpgcheck=0
[VT]
name=Instructor VT Repository
baseurl=ftp://192.168.0.254/pub/VT
gpgcheck=0
[Cluster]
name=Instructor Cluster Repository
baseurl=ftp://192.168.0.254/pub/Cluster
gpgcheck=0
[ClusterStorage]
name=Instructor ClusterStorage Repository
baseurl=ftp://192.168.0.254/pub/ClusterStorage
gpgcheck=0
EOF
cd  /mnt/$1/root/
touch .bash_profile
echo "PS1='[\u@\h \W]\$'">.bash_profile
}
#this function use to cp files from normal machine to the minilinux
CPLIB(){
#cp the "login" file
cp /tmp/myscript/login /mnt/sysroot/bin
chmod +x /mnt/sysroot/bin/login
#CP libnss files
 cp /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 files
cp -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/bin
BINCP(){
  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;do
 LDR=`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
  else
 echo "$1 is not exit."
  fi
}
#change the  initrd. file
INIT(){
TMPDIR=`mktemp -d /tmp/little.XX`
cd $TMPDIR
zcat /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/grub
touch grub.conf
cat >>grub.conf<<EOF
default=0
timeout=10
 title Welcome to Ro_Qi  minilinux
 root (hd0,0)
 kernel /vmlinuz-2.6.28.10-myminilinux  ro root=/dev/sda2
 initrd /initrd.gz
EOF
}
#the main function
HELPWORD(){
  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)
 DISK
 FAILDISK
 CREATEDISK
 MOUNT boot 1
 MOUNT sysroot 2
 ROOTFS sysroot
 CPLIB
 for 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/sysroot
 done
 ln -sv bash sh &>/dev/null
 INIT /mnt/boot
 rm -rf $TMPDIR
 GRUB
 sync
 sync
 sleep 2
    ;;
    d)
 DISK $OPTARG
 FAILDISK $OPTARG
 CREATEDISK $OPTARG
    ;;
    m)
 MOUNT boot 1
 MOUNT sysroot 2
    ;;
    r)
 ROOTFS sysroot
    ;;
    b)
 
 CPLIB
 for 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/sysroot
 done
 ln -sv bash sh &>/dev/null
    ;;
    k)
 NIT /mnt/boot
 rm -rf $TMPDIR
    ;;
    g)
 GRUB
 sync
 sync
 sleep 2
    ;;
    h)
 HELPWORD
    ;;
    *)
 HELPWORD
    ;;
  esac
done
#cp -d /lib/libcrypt-2.5.so /mnt/sysroot/lib/
#ln -s bash sh

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/21433.html