#!/bin/bash
#Author:Haodong Zhang
#Date:11/09/2021
#Function:打包系统镜像为docker image


VERSION=$(nkvers | sed -n '10p' | awk -F "/" '{print $3}')
clear
#${FUNCNAME[1]}表示调用该函数的函数
#$LINENO表示当前代码行号
Log(){
local log_level=$1
local log_info=$2
local line=$3
local script_name=$(basename $0)

case ${log_level} in
"INFO")
echo -e "\033[32m$(date "+%Y-%m-%d %T.%N") [INFO]: ${log_info}\033[0m";;
"WARN")
echo -e "\033[33m$(date "+%Y+%m+%d %T.%N") [WARN]: ${log_info}\033[0m";;
"ERROR")
echo -e "\033[31m$(date "+%Y-%m-%d %T.%N") [ERROR ${script_name} ${FUNCNAME[1]}:$line]: ${log_info}\033[0m";;

*)
echo -e "${@}"
;;
esac
}

function base_image {
#部署yum源
[ ! -d /etc/yum.repos.d/bak ] && mkdir /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
mount /dev/sr0 /mnt/ 1>/dev/null 2>&1
yum-config-manager --add-repo=file:///mnt 1>/dev/null 2>&1 && echo "gpgcheck=0" >>/etc/yum.repos.d/mnt.repo
#拷贝环境变量,安装基础包
[ ! -d /v10-sp1-x86-server-$VERSION ] && mkdir /v10-sp1-x86-server-$VERSION
Log INFO "系统正在为docker image安装基础包,大小约800M,请等待......"
yum -y --installroot=/v10-sp1-x86-server-$VERSION install yum net-tools vim iproute iputils 1>/dev/null 2>&1
cp /etc/skel/.bash* /v10-sp1-x86-server-$VERSION/root && echo >/v10-sp1-x86-server-$VERSION/root/.bash_history
Log INFO "安装完毕,正在进行打包镜像......"
cd /v10-sp1-x86-server-$VERSION && tar -zcvpf /root/v10-sp1-x86-server-$VERSION.tar --exclude=proc --exclude=sys --exclude=run --exclude=boot . 1>/dev/null 2>&1
Log INFO "镜像制作完毕,存取路径是/root/v10-sp1-x86-server-$VERSION.tar"
}

base_image

执行效果:

shell脚本制作docker镜像 制作国产麒麟系统docker镜像_bash