Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1172771
  • 博文数量: 150
  • 博客积分: 2739
  • 博客等级: 少校
  • 技术积分: 2392
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-07 12:28
文章分类

全部博文(150)

文章存档

2015年(2)

2014年(16)

2013年(10)

2012年(58)

2011年(64)

分类: Python/Ruby

2011-09-27 15:08:43

#!/bin/bash
#:Title: construction.sh
#:Path:/tmp/myscript/mklinux
#:Synopsis:
#:DateTime:2011-08-04 16:24:51
#:Version:Unknow
#:Author: redhat_hu
#:Option:None
read -p "please input the disk name:" DISKNAME
if fdisk -l $DISKNAME | grep "^$DISKNAME" &> /dev/null;then
        fdisk -l $DISKNAME | grep "^$DISKNAME"
        Adisk=`fdisk -l $DISKNAME | grep "^$DISKNAME" | awk '{print $1}'`
        for I in $Adisk ;do
        if mount | grep "$I" &> /dev/null;then
                mount | grep "$I"
        else
                echo "$I was not mounted."
        fi
        done
fi
function FRESH {
read -p "Continue(y/N)?" CHOISE
case $CHOISE in
y|Y)
for I in $Adisk;do
        if mount | grep "$I" >/dev/null;then
        fuser -km $I
        umount $I
        fi
done
dd if=/dev/zero of=$DISKNAME bs=512 count=1 &> /dev/null
echo '
n
p
1

+50m
n
p
2

+512m
n
p
3

+256m
t
3
82
w' |fdisk $DISKNAME &> /dev/null
partprobe $DISKNAME
sleep 1

mke2fs -j ${DISKNAME}1 &> /dev/null
mke2fs -j ${DISKNAME}2 &> /dev/null
mke2fs -j ${DISKNAME}3 &> /dev/null
return 0
;;
n|N)
return 1
;;
*)
return 2
;;
esac
}

function MOUNT {
        if find /mnt/boot &> /dev/null ;then
        umount /mnt/boot &> /dev/null
        mount $1 /mnt/boot
        else
        mkdir /mnt/boot
        umount /mnt/boot &> /dev/null
        mount $1 /mnt/boot
        fi
        if find /mnt/sysroot &> /dev/null ;then
        umount /mnt/sysroot &> /dev/null
        mount $2 /mnt/sysroot
        else
        mkdir /mnt/sysroot
        umount /mnt/sysroot &> /dev/null
        mount $2 /mnt/sysroot
        fi
}
function INIT {
if find [/mnt/sysroot &> /dev/null || mount|grep "/mnt/sysroot" &> /dev/null];then
        cd /mnt/sysroot
        mkdir -p  {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}
        chmod -R 1777 /mnt/sysroot/tmp
        touch /mnt/sysroot/etc/inittab

cat > /mnt/sysroot/etc/inittab <
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit        
l0:0:wait:/etc/rc.d/rc.shutdown

EOF
        touch /mnt/sysroot/etc/rc.d/rc.sysinit
cat > /mnt/sysroot/etc/rc.d/rc.sysinit << EOF
#!/bin/bash
echo -e "\t\tWelcome to \033[31;1mMyLinux\033[0m ..."
   
swapon -a
mount -n -o remount,rw /
mount -n -a
   
/bin/bash

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

        touch /mnt/sysroot/etc/fstab
cat > /mnt/sysroot/etc/fstab << EOF
/dev/sda2                /                      ext3    defaults        0 0
/dev/sda1               /boot                   ext3    defaults        0 0
/dev/sda3 swap swap defaults 0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

EOF

cat > /mnt/sysroot/etc/rc.d/rc.shutdown << EOF
#!/bin/bash
#
sync
sleep 2
sync
sleep 1
umount /dev/sda1
umount /dev/sda2

exec /sbin/halt -p
EOF
chmod +x /mnt/sysroot/etc/rc.d/rc.shutdown

else
        echo "error!"
fi
}


COPYCOMMAND() {
if which $B ;then
        COMMAND=`which $B | grep -o "/.*"`
        PTH=`which $B | grep -o "/.*" |sed "s@\(/[[:alpha:]]*\)/[[:alpha:]]*@\1@g"`
        if find /mnt/sysroot$PTH &> /dev/null;then
        cp $COMMAND /mnt/sysroot$PTH
        else
        mkdir -p /mnt/sysroot$PTH
        cp $COMMAND /mnt/sysroot$PTH
        fi
else
echo "wrong command"
fi

LIBS=`ldd $COMMAND | grep -o "/lib.*" | awk '{print $1}'`
for I in $LIBS;do
        [ ! -e /mnt/sysroot$I ] && cp $I /mnt/sysroot$I


done
}

function KERNEL {
TMPDIR=`mktemp -d /tmp/mylinux`

cd $TMPDIR
zcat /boot/initrd-`uname -r`.img | cpio -id

sed -i 's@echo Scanning and configuring dmraid supported devices@#&@g' init
sed -i 's@echo Scanning logical volumes@#&@g' init
sed -i 's@lvm vgscan --ignorelockingfailure@#&@g' init
sed -i 's@echo Activating logical volumes@#&@g' init
sed -i 's@lvm vgchange -ay --ignorelockingfailure  vol0@#&@g' init
sed -i 's@resume LABEL=SWAP-sda3@#&@g' init

sed -i "s@\(mkrootdev.* \)/.*@\1sda2@g" init
find . | cpio -H newc -o --quiet | gzip -9 > /mnt/boot/initrd.gz
cp /boot/vmlinuz-`uname -r` /mnt/boot/vmlinuz
}

function GRUB {
MOUNTPOINT=`basename $2`
until [ "$MOUNTPOINT" == "boot" ];do
        read -p "please modify the mountpoint." MOUNTPOINT
done

grub-install --root-directory=/mnt $1

cat > $2/grub/grub.conf << EOF
default=0
timeout=10
title mylinux
                root (hd0,0)
                kernel /$3 ro root=/dev/sda2
                initrd /$4
EOF

}
FRESH 
MOUNT ${DISKNAME}1 ${DISKNAME}2
INIT

for B in mingetty swapoff chmod sync sleep clear runlevel reboot sysctl swapon init bash mount umount halt poweroff shutdown ls vi cat; do
COPYCOMMAND
done 

#read -p "input the command,'c' for skip:" B
#until [ "$B" == "c" ];do

#COPYCOMMAND
#read -p "input the command,'c' for skip:" B
#done

KERNEL
GRUB $DISKNAME /mnt/boot vmlinuz initrd.gz
rm -rf $TMPDIR
sync
sync
sync

阅读(1081) | 评论(0) | 转发(0) |
0

上一篇:mysql自动备份的脚步

下一篇:贪吃蛇

给主人留下些什么吧!~~