#!/bin/bash
# =============================================
# Author: <qin>
# Create date: <2021-11-26>for redhat\Centos\openEuler,当前系统的ISO文件直接放在/opt/tool/目录下
# Update date: <2024-11-04>/dev/cdrom存在时优先使用之
# Description: <自动挂载光驱或本地ISO并配置本地yum源>
# 获取 GPGKEYFile 、 ISO 文件名时可能受 $OsDistribution 影响不正确,或许需要人工编辑/etc/yum.repos.d/*.repo文件
# =============================================
#获取系统发行版本
function getOsDistribution(){
if [[ "$OSTYPE"="linux-gnu" ]]; then
if [ -f /etc/neokylin-release ]; then
OsDistribution="neokylin" #--中标麒麟
elif [ -f /etc/kylin-release ]; then
OsDistribution="kylin" #--kylin兼容版
elif [ -f /etc/centos-release ]; then
OsDistribution="CentOS"
elif [ -f /etc/redhat-release ]; then
OsDistribution="Redhat"
elif [ -f /etc/openEuler-release ]; then
OsDistribution="openEuler"
elif [ -f /etc/SuSE-release ]; then
OsDistribution="Suse"
elif [ -f /etc/arch-release ]; then
OsDistribution="Arch"
elif [ -f /etc/mandrake-release ]; then
echo "Mandrake"
elif [ -f /etc/debian_version ]; then
OsDistribution="Ubuntu/Debian"
else
OsDistribution="Unknown Linux distribution"
fi
elif [[ "$OSTYPE"="darwin"* ]]; then
OsDistribution="Mac OS (Darwin)"
elif [[ "$OSTYPE"="freebsd"* ]]; then
OsDistribution="FreeBSD"
else
OsDistribution="Unknown operating system"
fi
}
getOsDistribution
echo $OsDistribution
#1.获取GPGKEYFile、ISO文件名
if [ "$OsDistribution" = "Unknown Linux distribution" ]
then
echo "OsDistribution is not get!"
exit
else
GPGKEYFile=$(ls -t /etc/pki/rpm-gpg/RPM-GPG-KEY-${OsDistribution}* | sed -n 1p)
ISOFile=$(ls -t /opt/tool/${OsDistribution}*.iso | sed -n 1p)
echo -e " OS:\033[33m${OsDistribution}\033[0m"
echo -e "GPGKEYFile:\033[33m${GPGKEYFile}\033[0m"
echo -e " ISOFile:\033[33m${ISOFile}\033[0m"
fi
#2.挂载cdrom|ISO
mkdir -p /mnt/${OsDistribution}/
if [ ! -d "/mnt/${OsDistribution}/Packages" ]; then
if [ -b "/dev/cdrom" ]; then
mount /dev/sr0 /mnt/${OsDistribution}/
else
mount ${ISOFile} /mnt/${OsDistribution}/
fi
fi
#3.备份repo
if [ ! -d "/etc/yum.repos.d/bak" ]; then
cd /etc/yum.repos.d/
mkdir bak
mv *.repo ./bak/
fi
#4.配置repo
if [ ! -f "/etc/yum.repos.d/${OsDistribution}-media.repo" ]; then
cat >/etc/yum.repos.d/${OsDistribution}-media.repo<<EOF
[mnt]
name=${OsDistribution}
baseurl=file:///mnt/${OsDistribution}/
enabled=1
gpgcheck=1
gpgkey=file://${GPGKEYFile}
EOF
fi
#5.刷新yum配置
yum clean all
yum makecache
自动挂载光驱或本地ISO并配置本地yum源
最新推荐文章于 2025-05-03 12:54:49 发布