我的工作情况
- 很多机器是在内网的,不能连外网源使用dnf下载软件包
- 内网机器有些可以搭建源有些不可以
需求
如果内网有需求则需要找了rpm包在发到内网安装,但这样可能出现依赖缺失,但可能有些环境下不给搭建源所以想了一个办法
目前解决流程
- 本地虚拟机安装一个最小化的openeuler系统,并且配置外网源然后打快照
- 使用yumdownloader命令下载软件包和依赖然后制作压缩包
- 发送到内网安装
目前将这些都写成脚本来使用
示例
下载
sh down.sh vim
安装
脚本
#!/bin/bash
scripts_name=$(basename $0)
GREEN='\033[0;32m'
NC='\033[0m'
RED='\033[0;31m'
if [[ $(id -u) -ne 0 ]]; then
echo -e "${RED} Run ${scripts_name} need root ====${NC}"
exit 1
fi
if ! command -v tar &> /dev/null; then
echo -e "${RED}tar command not found. Please install tar to proceed.${NC}"
echo -e "${RED}Example: dnf install -y tar ${NC}"
exit 1
fi
if [ $# != 1 ]; then
echo -e "${RED}Example: ./${scripts_name} vim ====${NC}"
exit 1
fi
rpm_name=$1
if [ -f /etc/openEuler-latest ];then
os=$(cat /etc/openEuler-latest | grep openeulerversion | awk -F "=" '{print $2}')
else
echo -e "${RED}Should input os version ${NC}"
while true; do
read -p "Enter OS version: " os
if [ -n "$os" ]; then
break
else
echo -e "${RED}Input cannot be empty. Please enter a valid OS version.${NC}"
fi
done
fi
echo -e "${GREEN}Downloader ${rpm_name} ${NC}"
yumdownloader --destdir=/root/${os}-${rpm_name} --resolve ${rpm_name}
echo -e "${GREEN}Create install-rpms.sh files in /root/${os}-${rpm_name}${NC}"
target_file="/root/${os}-${rpm_name}/install-rpms.sh"
cat << 'EOF' > "$target_file"
#!/bin/bash
packages=$(ls *.rpm | sed 's/\(.*\)/"\1"/' | tr '\n' ' ')
eval "packages=($packages)"
echo "Number of packages defined: ${#packages[@]}"
to_install=()
for package in "${packages[@]}"; do
if ! rpm -q "$(basename "$package" .rpm)" >/dev/null 2>&1; then
to_install+=("$package")
fi
done
echo "Number of packages to be installed: ${#to_install[@]}"
if [ ${#to_install[@]} -gt 0 ]; then
rpm -Uvh "${to_install[@]}"
else
echo "No packages to install."
fi
EOF
chmod +x /root/${os}-${rpm_name}/install-rpms.sh
echo -e "${GREEN}Compress /root/${os}-${rpm_name}${NC}"
tar -czvf ${os}-${rpm_name}.tar.gz -C /root ${os}-${rpm_name}
echo -e "${GREEN}${scripts_name} completion ${NC}"
说明
install-rpms.sh的作用是检测本机是否已经安装过离线的rpm包,如果安装过了就排查,因为很多时候会出现下面这个场景,如果加–force就显的很不优雅(有点偏执)
如果使用这个脚本就会如下所示