工作中写的脚本,先记下来:
init.sh:初始化工作,解压缩,预处理,如建立必须的文件夹,
mount必须的文件系统,chroot到新的环境,执行chroot中的脚本
#! /bin/sh
# Init the basic system
# error definition
ARGS=2
E_BAD_ARGS=100
E_NO_FILE=101
E_FORMAT=102
E_COMMAND=103
E_NO_DEVICE=104
# Check the args
if [ $# -ne $ARGS ]
then
echo "Usage: `basename $0` filename desdir"
exit $BAD_ARGS
# get the file name from the command line args
IMAGEFILE=$1
DES_DIR=$2
TAR_CMD=
# if the file not exists, then exit with 1
if [ ! -e $IMAGEFILE ]
then
echo "File not exists"
exit $E_NO_FILE
# set the TAR_CMD according to the file name
if [[ $IMAGEFILE = *\.tar\.gz ]] || [[ $IMAGEFILE = *\.tgz ]]
then
TAR_CMD="tar --numeric-owner -xvpzf $IMAGEFILE -C"
echo "gzip compression"
elif [[ $IMAGEFILE = *\.tar\.bz2 ]]
then
TAR_CMD="tar --numeric-owner -xvpjf $IMAGEFILE -C"
echo "bzip compression"
else
echo "not a tar ball, exit"
exit $E_FORMAT
echo "Begin decompress the source package, continue?(Y/N)"
read yes_no
if [ $yes_no == "Y" ] || [ $yes_no == "y" ]
then
echo "Before tar, we need mount the file system first"
mount LABEL=mips $DES_DIR
if [ $? != 0 ]
then
echo "Please check the device plugin or not"
exit $E_NO_DEVICE
fi
echo "Remove the old data in the file system, continue?(Y/N)"
yes_no="n"
read yes_no
if [ $yes_no == "Y" ] || [ $yes_no == "y" ]
then
echo "Removing..."
rm -rf $DES_DIR/*
fi
sync
sleep 3
echo "taring..."
$TAR_CMD $DES_DIR
if [ $? != 0 ]
then
echo "tar error, please check the input file"
exit $E_COMMAND
fi
else
echo "User canceled"
exit 0
sleep 3
echo "Finish descompression"
echo "Before chroot, we need create some necessary directories"
mkdir -v $DES_DIR/proc
mkdir -v $DES_DIR/mnt
mkdir -v $DES_DIR/home
mount LABEL=home $DES_DIR/home
if [ $? != 0 ]
then
echo "Please check the device plugin or not"
exit $E_NO_DEVICE
echo "Remove the old data in home and tmp diretory"
rm -rf $DES_DIR/home/*
rm -rf $DES_DIR/tmp/*
sync
sleep 3
echo "Disable the automic login"
sed -i "s/^AutomaticLoginEnable=true/AutomaticLoginEnable=false/g" $DES_DIR/etc/X11/gdm/custom.conf
echo "Now chroot the new env"
NEXTSHELL=update_repo.sh
cp $NEXTSHELL $DES_DIR
chroot $DES_DIR /bin/bash -c "sh $NEXTSHELL" || exit $?
rm $DES_DIR/$NEXTSHELL
umount $DES_DIR/home
umount $DES_DIR
echo "All finished"
exit 0
update_repo.sh:更新源,从源中获取必须的包,该包只是一个依赖关系
编辑新用户,fstab,设置自动登录,配置网络
#! /bin/sh
# This script runs on the chroot enviroment
echo "Add new repo"
urpmi.removemedia -a
urpmi.addmedia --distrib http://10.1.18.31/mandriva/Release/mips/
urpmi task-base
echo "Remove the old user gdium"
sed -i "/gdium/d" /etc/group
sed -i "/gdium/d" /etc/passwd
chmod 600 /etc/shadow
sed -i "/gdium/d" /etc/shadow
chmod 400 /etc/shadow
echo "Add new user cs2c"
useradd cs2c
echo "qwe123" | passwd cs2c --stdin
echo "Enable the automic login"
sed -i "s/^AutomaticLoginEnable=false/AutomaticLoginEnable=true/g" /etc/X11/gdm/custom.conf
echo "Set the automic login user"
sed -i "s/^AutomaticLogin=gdium$/AutomaticLogin=cs2c/g" /etc/X11/gdm/custom.conf
echo "Edit the sudoers file, give cs2c sudo privilege"
chmod 640 /etc/sudoers
sed -i "s/^gdium/cs2c/g" /etc/sudoers
chmod 440 /etc/sudoers
echo "Repair the network"
mv /etc/sysconfig/network-scripts/ifcfg-eth{1,0}
sed -i "s/^DEVICE=eth1/DEVICE=eth0/g" /etc/sysconfig/network-scripts/ifcfg-eth0
echo "Remove the fstab file"
rm /etc/fstab
echo "Create new fstab file"
cat >> /etc/fstab << eof
LABEL=mips / ext3 noatime 1 1
LABEL=home /home ext3 defaults 1 2
LABEL=swap swap swap defaults 0 0
none /tmp tmpfs defaults 0 0
none /proc proc defaults 0 0
echo "All finished"
sleep 3
exit 0
该贴由system转至本版2014-9-5 12:08:21