bash自动化 定制属于自己的~Centos~

废话不多说直接上图

1、首先在宿主机创建目录,/mnt/boot /mnt/sysroot,格式化磁盘将其挂载

wKioL1M2YuSDWNZSAADyYlLXJRk849.jpg

1、相对应的函数:

mkdevice () {
    dd if=/dev/zero of=$1 bs=512 count=1 &> /dev/null
    [ $? -ne 0 ] && return 67
    echo '
        n
        p
        1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        +500M
        n
        p
        2
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        +9G
        w' | fdisk $1 &> /dev/null
    [ $? -ne 0 ] && return 68
    sync
    sleep 2
    mkfs.ext4 ${1}1 &> /dev/null
    PAPT1=$?
    mkfs.ext4 ${1}2 &> /dev/null
    PAPT2=$?
    [ $PAPT1 -ne 0 -o $PAPT2 -ne 0 ] && return 69
}
umountall () {
    for PAPT in `fdisk -l $1 | grep -o "${1}[0-9]\{1,\}"`; do
        fuser -km $PAPT &> /dev/null
        umount $PAPT &> /dev/null
    done
}

mountall () {
    [ -d $BOOT ] || mkdir -p $BOOT
    [ -d $SYSROOT ] || mkdir -p $SYSROOT
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    mount  ${1}1 $BOOT
    mount  ${1}2 $SYSROOT
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
}

2、安装grub程序,我们这里是安装到/mnt/boot

wKioL1M2Ym2xCvHoAAD8ruvEIwQ922.jpg

2、相对应的函数:

instgrub () {
    GRUBROOT=`dirname $BOOT`
    grub-install --root-directory=$GRUBROOT $1
}

3、创建系统所需的目录

wKioL1M2afeSEkbcAADyYlLXJRk804.jpg

3与其对应的函数

mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko  $SYSROOT/lib/modules/
cp  /boot/vmlinuz-2.6.32-431.el6.x86_64  $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e  "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro  root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}

4复制kernel 到目标主机

wKioL1M2YNGzgjZLAAD7hXPrkQU254.jpg0

4、与其对应的函数

mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko  $SYSROOT/lib/modules/
cp  /boot/vmlinuz-2.6.32-431.el6.x86_64  $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e  "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro  root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}

5从宿主机复制/boot/initramfs.img 到目标主机

wKioL1M2ZFOzBT2AAAEHRNgMVE4050.jpg

5、与其对应的函数

mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko  $SYSROOT/lib/modules/
cp  /boot/vmlinuz-2.6.32-431.el6.x86_64  $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e  "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro  root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}

6、创建grub.conf配置文件,是系统能够正常引导


wKiom1M2ZZ3jfdZ7AAEA4yqc5WQ130.jpg

6、与其对应的函数

mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko  $SYSROOT/lib/modules/
cp  /boot/vmlinuz-2.6.32-431.el6.x86_64  $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e  "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro  root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}

7、复制宿主机中的程序到目标主机

wKioL1M2Z2WS6CIWAAEfDMVw4vw113.jpg

7、与其对应的函数,需要cp的程序有:其实当中也有些不是那么重要的程序,不过在init中使用到的命令就必须提供,不然会导致内核恐慌的...其它命令可以随添加之!

bash  chmod  chown  cp  dd  echo  ls  mkdir  mount  ping  touch  umount  vi chkconfig  halt  ifconfig  init  insmod  reboot  rmmod  route


Bcp () {
    COMMAND=`which  --skip-alias $1`
    #echo COMMAND
    COMPATH=`dirname $COMMAND`
    #echo $COMPATH
    [ -d $SYSROOT$COMPATH ] || mkdir -p $SYSROOT$COMPATH
    [ -e $SYSROOT$COMMAND ]  || cp $COMMAND $SYSROOT$COMPATH
    for lib in `ldd $COMMAND | grep -o  "/.*lib[^[:space:]]*"` ; do
    LIBPATH=`dirname $lib`
    [ -d $SYSROOT$LIBPATH ] || mkdir -p $SYSROOT${LIBPATH}
    [ -e $SYSROOT$lib ] || cp $lib $SYSROOT${LIBPATH}
    done
}

8、复制宿主机的网卡模块到宿主机

wKioL1M2aFHiwFq-AAE4klIezWY617.jpg

8、与其对应的命令也在mkrootfs中

#!/bin/bash
#
##function fdisk
BOOT=/mnt/boot   #在宿主机中创建目标主机的工作目录
SYSROOT=/mnt/sysroot #在宿主机创建目标主机的工作目录
mkdevice () {
    dd if=/dev/zero of=$1 bs=512 count=1 &> /dev/null
    [ $? -ne 0 ] && return 67
    echo '
        n
        p
        1
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        +500M
        n
        p
        2
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        +9G
        w' | fdisk $1 &> /dev/null
    [ $? -ne 0 ] && return 68
    sync
    sleep 2
    mkfs.ext4 ${1}1 &> /dev/null
    PAPT1=$?
    mkfs.ext4 ${1}2 &> /dev/null
    PAPT2=$?
    [ $PAPT1 -ne 0 -o $PAPT2 -ne 0 ] && return 69
}
# 如上脚本格式化磁盘,将目标主机的磁盘拿到宿主机进行格式化
#umount for disk directory
umountall () {
    for PAPT in `fdisk -l $1 | grep -o "${1}[0-9]\{1,\}"`; do
        fuser -km $PAPT &> /dev/null
        umount $PAPT &> /dev/null
    done
}
#mount for disk directory # 如果此之前,在构建目标主机的目录中有数据一定先将其卸载
mountall () {
    [ -d $BOOT ] || mkdir -p $BOOT
    [ -d $SYSROOT ] || mkdir -p $SYSROOT
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
    mount  ${1}1 $BOOT
    mount  ${1}2 $SYSROOT
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
}
#grub install for directory
instgrub () {
    GRUBROOT=`dirname $BOOT`
    grub-install --root-directory=$GRUBROOT $1
}
# 安装能够引导系统的grub程序,如果没有改程序,系统将无法启动啦! 注意呀
#mkdir for directory and file system
mkrootfs () {
mkdir -p $SYSROOT/{proc,boot,sys,dev,tmp,lib,lib64,bin,sbin,root,home,opt,mnt,media,misc} &> /dev/null
mkdir -p $SYSROOT/usr{share/{man,doc},bin,sbin,local,src,lib,lib64} &> /dev/null
mkdir -p $SYSROOT/var/{log,run,lock,spool,tmp} &> /dev/null
mkdir -p $SYSROOT/lib/{modules,module} &> /dev/null
mkdir -p $SYSROOT/lib64/{modues,module} &> /dev/null
cp /lib/modules/2.6.32-431.el6.x86_64/kernel/drivers/net/e1000/e1000.ko  $SYSROOT/lib/modules/
cp  /boot/vmlinuz-2.6.32-431.el6.x86_64  $BOOT/vmlinuz
cp /boot/initramfs-2.6.32-431.el6.x86_64.img $BOOT/initramfs.img
echo -e  "default=0 \ntimeout=5 \ntitle MyCentos root (hd0,0) \nkernel /vmlinuz ro  root=/dev/sda2 quiet selinux=0 init=/sbin/init
initrd /initramfs.img " > $BOOT/grub/grub.conf
touch $SYSROOT/sbin/init && cat /root/init > $SYSROOT/sbin/init && chmod +x $SYSROOT/sbin/init
}
# 创建系统所需的目录
# 并将宿主机的内核程序复制到目标主机,有了内核,目标主机就所想披靡啦,不过要想系统能够正常启动还要将宿主机中的initramfs.img,要想拥有属于自己私人定制的系统initramfs.img是必须的.
Bcp () {
    COMMAND=`which  --skip-alias $1`
    #echo COMMAND
    COMPATH=`dirname $COMMAND`
    #echo $COMPATH
    [ -d $SYSROOT$COMPATH ] || mkdir -p $SYSROOT$COMPATH
    [ -e $SYSROOT$COMMAND ]  || cp $COMMAND $SYSROOT$COMPATH
    for lib in `ldd $COMMAND | grep -o  "/.*lib[^[:space:]]*"` ; do
    LIBPATH=`dirname $lib`
    [ -d $SYSROOT$LIBPATH ] || mkdir -p $SYSROOT${LIBPATH}
    [ -e $SYSROOT$lib ] || cp $lib $SYSROOT${LIBPATH}
    done
}
# 这个函数的作用是:原因:创建系统完成了,系统还需要些程序吧,不然内核太无聊了.... 这个函数的目的是能够将宿主机的应用程序复制到目标主机,以及这些程序所依赖的库呀,什么的。
main () {
    umountall $IDISK
    mkdevice $IDISK
    mountall $IDISK
    instgrub $IDISK
    mkrootfs
  while true; do
read -p "A CommanD : " CMD
case $CMD in
q|Q|quit|exit)
    echo "Finished."
    exit 0
;;
*)
 ! which $CMD &> /dev/null && echo "Wrong Command." && continue
    Bcp $CMD
    ;;
esac
done
}
# 这个是主函数,main() 作用就是调用这个脚本中的其它函数啦...
read -p "A disk : " IDISK      # 然用户选择块磁盘000
if ! fdisk -l | grep "^Disk $IDISK" &> /dev/null; then
    echo "$IDISK is not a disk."
    exit 5
fi
read -p "Destroy all date on $IDISK, contine (y/n) : " ANSWER
case $ANSWER in
y)
main
;;
n)
exit
;;
*)
echo "Wrong option."
;;
esac
# 语句判断啦,如果选择y就进行下去,如果选项N就退出脚本啦,以此类推啦!

cat/root/init> $SYSROOT/sbin/init&& chmod+x $SYSROOT/sbin/init

/root/init的内容是:

#!/bin/bash
#
echo -e "\tWelcome to \033[34m MyCentos\033[0m Linux "
mount -n -t proc porc /proc
mount -n -t sysfs sysfs /sys
insmod /lib/modules/e1000.ko
[ $? -eq 0 ] && echo -e "Load e1000 module succeeded                    [ \033[32mOK\033[0m ]"
ifconfig lo 127.0.0.1/8
ifconfig eth0 172.16.251.111/16
mount -n -o remount,rw /dev/sda2 /
/bin/bash

这里另外提供cp命令的脚本.想用什么命令就cp什么命令!你懂的

#!/bin/bash
                #copy order and it's libs of depend
                noorder(){
                        if ! which $cmd &> /dev/null;then
                                echo "No such command!"
                                return 3
                        fi
                        orderpath=`which --skip-alias $cmd`
                }
                cporder(){
                        [ -d /mnt/sysroot`dirname $orderpath` ] || mkdir -p /mnt/sysroot`dirname $orderpath`
                        [ -f /mnt/sysroot$orderpath ] || cp -r $orderpath /mnt/sysroot$orderpath
                }
                cplib(){
                        for lib in `ldd $orderpath | grep -o "/[^[:space:]]\{1,\}"`;do
                                [ -d /mnt/sysroot`dirname $lib` ] || mkdir -p /mnt/sysroot`dirname $lib`
                                [ -f /mnt/sysroot`basename $lib` ] || cp -r $lib /mnt/sysroot$lib
                        done
                }
                while true;do
                        read -p "Please enter a command:" cmd
                        if [[ "$cmd" = "q" || "$cmd" = "Q" ]];then
                                echo "You choice quit,now quit!"
                                exit 0
                        else
                                noorder
                                [ $? == 3 ] && continue
                                cporder
                                cplib
                        fi
                done


你可能感兴趣的:(return,null,count,格式化磁盘)