制作tar系统的shell脚本--My tools-deargodzw-ChinaUnix博客_VMware, Unix及操作系统讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  VMware, Unix及操作系统讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2901 | 回复: 0   主题: 制作tar系统的shell脚本--My tools-deargodzw-ChinaUnix博客        下一篇 
    本主题由 Administrator 于 2014-9-5 12:08:22 移动
xuefeng
注册用户
等级:上士
经验:315
发帖:69
精华:0
注册:2011-8-17
状态:离线
发送短消息息给xuefeng 加好友    发送短消息息给xuefeng 发消息
发表于: IP:您无权察看 2014-9-3 18:44:15 | [全部帖] [楼主帖] 楼主

    工作中写的脚本,先记下来:

    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



赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论