我们最终写好了一键制作Linux的脚本。这次真的是一键了,附带了常用的命令,以及欢迎界面,外加提示符等等。
明天加入网络模块,让其能够上网!
宿主机:RedHat Enterprise 5 (2.6.18)
目标机器:256内存,8G硬盘。
#!/bin/bash
#: Title: All_Mini_Linux.sh
#: Synopsis:
#: DateTime: 2011-08-04 12:33:02
#: Version: 0.2
#: Author: weiyan
#: Options:
#
#
read -p "The target disk: " DSK
if fdisk -l $DSK | grep "^/dev" &> /dev/null ; then
fdisk -l $DSK | grep "^/dev"
PARTS=`fdisk -l $DSK | grep "^/dev" | awk '{print $1}'`
for I in $PARTS; do
if mount | grep "$I" &> /dev/null; then
mount | grep "$I"
else
echo "$I is not mounted."
fi
done
fi
SPART() {
read -p "Warning !!!!! Next you do it will wipe your disk !!!! Continue(y/N)?" CHOICE
case $CHOICE in
y|Y)
for I in $PARTS; do
if mount | grep "$I" &> /dev/null; then
fuser -km $I
umount $I
fi
done
dd if=/dev/zero of=$DSK bs=512 count=1 &> /dev/null
echo '
n
p
1
+50M
n
p
2
+512M
n
p
3
+256M
t
3
82
w' | fdisk $DSK &> /dev/null
sleep 1
mke2fs -j ${DSK}1 &> /dev/null
mke2fs -j ${DSK}2 &> /dev/null
mkswap ${DSK}3 &> /dev/null
return 0
;;
n|N)
return 1
;;
*)
return 2
;;
esac
}
SPART
[ $? -eq 0 ] && echo "The Disk wipe success !!!!!!!" || echo "Failure"
## ==============2========================
function GUAZAI {
[ -d /mnt/boot ] || mkdir -p /mnt/boot
umount /mnt/boot &> /dev/null
mount $1 /mnt/boot
[ -d /mnt/sysroot ] || mkdir -p /mnt/boot
umount /mnt/sysroot &> /dev/null
mount $2 /mnt/sysroot
}
GUAZAI ${DSK}1 ${DSK}2
## =============3=========================
# firstblood ! rootfs $1 = /mnt/sysroot
function ROOTFS {
if [ -d $1 ];then
if mount | grep "$1" &> /dev/null ; then
cd $1
chmod 1777 tmp/
# create /etc/inittab
cat >>$1/etc/inittab<< EOF
id:3:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc.shutdown
l1:1:wait:/sbin/init -t1 S
l3:3:wait:/sbin/init -t1 3
#mingetty
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 /etc/rc.d/rc.shutdown
cat >>$1/etc/rc.d/rc.shutdown<< EOF
#!/bin/bash
sync
sleep 1
sync
sleep 1
umount /dev/sda1
umount /dev/sda2
exec /sbin/halt -p
EOF
# create rc.sysinit
cat >>$1/etc/rc.d/rc.sysinit<< EOF
#!/bin/bash
echo -e "\t\tWelcome to \033[031;1mLittle\033[0m Linux..."
mount -n -o remount,rw /
mount -n -a
# set HOSTNAME
[ -f /etc/sysconfig/network ] && source /etc/sysconfig/network
[ -z $HOSTNAME ] && HOSTNAME=localhost
echo $HOSTNAME > /proc/sys/kernel/hostname
EOF
# create /etc/sysconfig/network
cat >>$1/etc/sysconfig/network<< EOF
HOSTNAME=weiyan
EOF
# create /etc/sysctl.conf
cat >>$1/etc/sysctl.conf<< EOF
EOF
# create /etc/issue
cat >>$1/etc/issue<< EOF
Welcome to Little.Linux.org
Username : root Password : NiCai?
EOF
#create /root/.bash_profile
cat >>$1/root/.bash_profile<< EOF
PS1='[\u@\h \W]\
<pre class="cpp" name="code">EOF
chmod +x etc/rc.d/rc.shutdown
# create fstab
cat >>$1/etc/fstab<< EOF
/dev/sda2 /ext3defaults 0 0
/dev/sda1 /bootext3defaults 0 0
sysfs /sys sysfs defaults 0 0
proc/procprocdefaults 0 0
EOF
else
read -p "Error,the $1 still mount ! Contiune(y|N)?" MOT
case $MOT in
*)
echo "Exit......"
return 5
;;
esac
fi
fi
}
ROOTFS /mnt/sysroot
##====================4========================
# copy lib to your sys . $1= COMMAND $2 = /mnt/sysroot
function COPYLIB {
#copy command
if which $1 &> /dev/null ;then
AA=`which $1 | tail -1 | awk '{print $1}'`
cp "$AA" $2"$AA"
else
echo "$1 is not exist"
fi
#copy lib
LIBPATH=`echo $LIB | sed 's@\(.*\)/[^[:space:]]\{1,\}@\1@g'`
[ -d ${2}$LIBPATH ] || mkdir ${2}$LIBPATH
[ ! -e $2/$LIB ] && cp -f $LIB ${2}$LIBPATH
done
echo "$1 has cp done."
}
echo "=========================================================="
# zhijie jia ru ming ling
COPYLIB $CMDD /mnt/sysroot
done
#ln bash & sh !!!!!
ln -sv bash sh &> /dev/null
# copy lib/libnss*. with nsswitch
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/lib/
cp -d /usr/lib/libnss_compat.so /mnt/sysroot/usr/lib/
cp -d /usr/lib/libnss_files.so /mnt/sysroot/usr/lib/
cp -d /lib/libnss_files* /mnt/sysroot/lib/
cp -d /lib/libnss_compat* /mnt/sysroot/lib/
## copy login !
cd /mnt/sysroot/bin/
wget ftp://192.168.0.254/pub/login &> /dev/null
chmod +x login
cp /lib/libcrypt.so.1 /mnt/sysroot/lib/
cp /lib/libm.so.6 /mnt/sysroot/lib/
cd ~
## copy passwd .... nsswitch.conf
cp /etc/{passwd,shadow,group,gshadow} /mnt/sysroot/etc/
cp /etc/nsswitch.conf /mnt/sysroot/etc/
#########until xunhuan let user input ##########################
#
#echo -e "Please input COMMAND with \033[31mbash\033[0m and \033[31minit\033[0m !!!!!"
#read -p "COMMAND you want?(Quit for 'q') :" CMD1
#until [ $CMD1 == 'q' ]; do
# COPYLIB $CMD1 /mnt/sysroot
# read -p "Which COMMAND you want use ?(Quit for 'q') :" CMD1
#done
###############################################################
echo "=========================================================="
##==========================5===================================
# $1 = /mnt/sysroot
function YDFQ {
TMPDIR=`mktemp -d /tmp/little.XX`
cd $TMPDIR
zcat /boot/initrd-`uname -r`.img | cpio -id &> /dev/null ## zhankai bing songgei
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.*\) /.*@\1 sda2@" init
find . | cpio -H newc -o --quiet | gzip -9 > /mnt/boot/initrd.gz
cp /boot/vmlinuz-`uname -r` /mnt/boot/vmlinuz
}
YDFQ /mnt/sysroot
##==============================6================================
## $1 = /dev/sdc $2=/mnt/boot $3=kernel'name $4 initrd's name
function GRUB {
if [ `basename $2` == boot ];then
grub-install --root-directory=/mnt $1 &> /dev/null
cat >>$2/grub/grub.conf<< EOF
default=0
timeout=10
title Little Linux
root (hd0,0)
kernel /${3} ro root=/dev/sda2 quiet
initrd /${4}
EOF
else
echo "It's not boot !Exit & Please set it again !"
fi
}
GRUB $DSK /mnt/boot vmlinuz initrd.gz
sync
sync
sync
sync
sync
sleep 10
echo "Congratulation ! Your Mini Linux is install OK !"
当所有都运行完之后。
OK,万事搞定!