# centos添加本地yum源

  1. 上传ISO文件至任意路径

  2. 上传脚本LocalYum.sh至任意路径

# cat LocalYum.sh
#!/bin/bash
function useFun()
{
	echo "use example: "
	echo "./local_yum.sh isoFilePath"
}

#文件颜色设置
# 恢复文本颜色
C="\033[0m"
# 红色
R="\033[1;31m"


# mount centos_iso
centos_iso=$1

if [ "$centos_iso" == "" ] 
then
	echo "isoFilePath is null. exit!"
	useFun
	exit 1	
fi

isFullPath=$( echo $centos_iso |  grep "^/" )
if [ "$isFullPath" == "" ] 
then
	echo "error: the param $centos_iso is not full path. 镜像文件路径请输入为绝对路径 "
	echo "exit!"
	exit 1
fi

if [ ! -f $centos_iso ]
then
	echo "err: isofile($centos_iso) is not exist."	
	exit 1
fi

mnt_path="/mnt/CentOS-ISO"
echo "isoPath=$centos_iso, mnt_path=$mnt_path"

if [ ! -d "$mnt_path" ]
then
	 mkdir -p $mnt_path
fi

isMounted=$( df -h | grep "$mnt_path" )
if [ "$isMounted" != "" ] 
then
	umount $mnt_path
fi

mount $centos_iso  -o  loop  $mnt_path
if [ "$?" != 0 ]
then
	echo -e "$R-----------------------------------------Error------------------------------------------------ $C"
	echo -e "$R ERROR: [ mount $centos_iso  -o  loop  $mnt_path ] failed! $C"
	echo -e "$R---------------------------------------------------------------------------------------------- $C"
	exit -1
fi
	
#================================================ set yum ================================================
if [ -f /etc/yum.repos.d/CentOS-Base.repo ]
then
	mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
	if [ "$?" != 0 ]
	then
		echo -e "$R -----------------------------------------Error----------------------------------------------- $C"
		echo -e "$R ERROR: [ mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak ] failed! $C"
		echo -e "$R --------------------------------------------------------------------------------------------- $C"
		exit -1
	fi
fi

sed -i 's_^baseurl=.*$_baseurl=file://'$mnt_path'_' /etc/yum.repos.d/CentOS-Media.repo
if [ "$?" != 0 ]
then
	echo -e "$R ----------------------------------------------Error------------------------------------------------------ $C"
	echo -e "$R  ERROR: [ modify baseurl value of /etc/yum.repos.d/CentOS-Media.repo ] failed!  mnt_path=$mnt_path $C"
	echo -e "$R --------------------------------------------------------------------------------------------------------- $C"
	exit -1
fi

#============================= yum设置完成后,将镜像文件挂载项保存在/etc/fstab ===============================
#先删除原有挂载项
sed -i 's#'$mnt_path'#EXCLUSIVE#;/EXCLUSIVE/d' /etc/fstab
sed -i 's#'$centos_iso'#EXCLUSIVE#;/EXCLUSIVE/d' /etc/fstab

#在文件尾部重新追加挂载目录
sed -i '$a '$centos_iso'   '$mnt_path'   iso9660   loop   0 0' /etc/fstab
if [ "$?" != 0 ]
then
	echo -e "$R -----------------------------------------Error----------------------------------------------- $C"
	echo -e "$R ERROR: [ add $centos_iso $mnt_path  to /etc/fstab ] failed! $C"
	echo -e "$R --------------------------------------------------------------------------------------------- $C"
	exit -1
fi

sed -i 's_^gpgcheck=.*$_gpgcheck=0_' /etc/yum.repos.d/CentOS-Media.repo

sed -i 's_^enabled=.*$_enabled=1_' /etc/yum.repos.d/CentOS-Media.repo

echo "mount iso, yum set finish!"
exit 0

  1. 执行脚本
bash local_yum.sh ${iso_path}
posted @ 2021-03-31 09:24  那就这样吧~  阅读(91)  评论(0编辑  收藏  举报