废话不多说,直接上脚本
#!/bin/bash
#function:CentOS7配置本地yum仓库
#author:墨染-2022-6-22
if [ $USER != root ];then
echo "当前不是root用户,请切换至root用户再次运行脚本"
exit
fi
df -h | grep "/dev/sr0" &> /dev/null
if [ $? != 0 ];then
echo "请先到虚拟机挂载光盘,再运行脚本"
exit
fi
echo "开始备份源yum文件"
sleep 1.5
mkdir /etc/yum.repos.d/backup &>/dev/null
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup &>/dev/null
mkdir /mnt/cdrom
mount /dev/sr0 /mnt/cdrom
echo "/dev/sr0 /mnt/cdrom iso9660 defaults 0 0" >> /etc/fstab
mount -a
cat>>/etc/yum.repos.d/localhost_yum.repo << EOF
[local]
name=localhost_yum
baseurl=file:///mnt/cdrom
gpgcheck=0
enable=1
EOF
echo "yum仓库配置成功"
echo "正在清理原来的yum文件" && yum clean all
echo "正在重新搭建yum仓库" && yum makecache &>/dev/null
echo "执行完成,请查看yum文件数量" && yum repolist | grep repolist