Chinaunix首页 | 论坛 | 博客
  • 博客访问: 69162
  • 博文数量: 15
  • 博客积分: 602
  • 博客等级:
  • 技术积分: 172
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-07 13:35
文章分类

全部博文(15)

文章存档

2011年(8)

2010年(2)

2009年(5)

分类:

2009-11-18 17:12:26

工作中写的脚本,先记下来:
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
fi


# 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
fi

# 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
fi


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
fi


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
fi

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
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
eof

echo "All finished"
sleep 3
exit 0
阅读(1762) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~