#!/bin/bash # Copyright 2005-17 Steadfast Networks, LLC # # This script will update your package repository configuration to use # mirror.steadfast.net instead of the official mirrors contained in the # default configuration. # # Currently supported OSes: # * CentOS 6.x, 7.x # * Fedora 24+ # * Debian Wheezy (7), Jessie (8), Stretch (9) DATE=`date +%s`; echo Mirrorizing... # Detect the Linux Distribution by RPM or Deb Package Version if [ -e "/usr/bin/rpmquery" ] ; then # RPM-based Linux RELEASE=`rpmquery fedora-release redhat-release centos-release | grep -v 'not installed'` OS=`echo ${RELEASE} | awk -F- "{ print \\$1; }"`; VER=`echo ${RELEASE} | awk -F- "{ print \\$3; }"`; if [[ ${OS} == "" ]] ; then OS='rpm-based-unknown' fi elif [ -e "/usr/bin/dpkg-query" ] ; then # Deb-based Linux RELEASE=`cat /etc/debian_version` DISTRO=`cat /etc/issue | awk "{ print \\$1; }"` if [[ ${DISTRO} == "Debian" && ${RELEASE} != "" ]] ; then VER=`echo ${RELEASE} | awk -F. "{ if ( \\$2 == \"\" ) { \\$2 = 0; } print \\$1\".\"\\$2; }"` OS='debian'; else OS='deb-based-unknown' fi fi # If no match is found, OS is completely unrecognized if [[ ${OS} == "" ]] ; then OS='unknown' fi # Determine CPU Architecture ARCH=`uname -i 2> /dev/null`; # Fallback to more specific architecture if -i is unusable if [[ ${ARCH} == "unknown" || ${ARCH} == "" ]]; then ARCH=`uname -m 2> /dev/null`; fi if [[ ${OS} == "centos" ]] ; then echo " * This is CentOS ${VER} (${ARCH})..."; # Make sure this installation's architecture is available on this mirror if [[ ${ARCH} != 'i386' && ${ARCH} != 'x86_64' ]] ; then echo " * Error: Unsupported Platform: ${ARCH}"; exit fi GPG=http://mirror.steadfast.net/centos/RPM-GPG-KEY-CentOS-${VER} echo " - Importing GPG Key..."; rpm --import ${GPG} echo " - Setting mirrors to mirror.steadfast.net..." if [[ (( ${VER} -ge 6 )) ]]; then cp -pR /etc/yum.repos.d /etc/yum.repos.d.mirrorize.${DATE} sed -i "s/mirror.centos.org/mirror.steadfast.net/" /etc/yum.repos.d/CentOS-*.repo echo " - Switching off mirror rotation..."; sed -i "s/^mirrorlist=/#mirrorlist=/" /etc/yum.repos.d/CentOS-*.repo sed -i "s/^#baseurl=/baseurl=/" /etc/yum.repos.d/CentOS-*.repo else echo " * Error: Unsupported Version: ${VER}"; fi elif [[ ${OS} == "fedora" ]] ; then # Make sure this installation's architecture is available on this mirror if [[ ${ARCH} != 'i386' && ${ARCH} != 'x86_64' ]] ; then echo " * Error: Unsupported Platform: ${ARCH}"; exit fi echo " * This is Fedora ${VER} (${ARCH})..."; if [ ${VER} -ge 24 ]; then echo " - Importing GPG Key(s)..."; rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-${ARCH} echo " - Setting mirrors to mirror.steadfast.net..."; cp -pR /etc/yum.repos.d /etc/yum.repos.d.mirrorize.${DATE} sed -i "s%download.fedoraproject.org/pub/fedora/linux/%mirror.steadfast.net/fedora/%" /etc/yum.repos.d/fedora*.repo echo " - Switching off mirror rotation..."; sed -i "s/^mirrorlist=/#mirrorlist=/" /etc/yum.repos.d/fedora*.repo sed -i "s/^#baseurl=/baseurl=/" /etc/yum.repos.d/fedora*.repo else echo " * Error: Unsupported Version: ${VER}"; fi elif [[ ${OS} == "debian" ]] ; then case ${VER} in 7.*) REPO="wheezy"; ;; 8.*) REPO="jessie"; ;; 9.*) REPO="stretch"; ;; esac echo " * This is Debian ${VER} [${REPO}] (${ARCH})..."; # Make sure this installation's architecture is available on this mirror if [[ ${ARCH} != 'i686' && ${ARCH} != 'amd64' && ${ARCH} != 'x86_64' ]] ; then echo " * Error: Unsupported Platform: ${ARCH}"; exit fi if [[ ${REPO} != "" ]] ; then if grep -e '^[^#].*mirror.steadfast.net' /etc/apt/sources.list > /dev/null; then DONE=`grep -ve "^#\|mirror\.steadfast\.net\|mirror\.nozonenet\.com\|^$" /etc/apt/sources.list | grep -e "security\.debian\.org\|/debian\s"`; else DONE='no' fi if [[ ${DONE} != "" ]] ; then echo " * Backing up sources.list..." mv /etc/apt/sources.list /etc/apt/sources.list.mirrorize.${DATE} echo " * Reinstating non-standard (manual) repositories..." touch /etc/apt/sources.list echo "# Added by Steadfast Networks Mirrorize (from old sources.list)" >> /etc/apt/sources.list; grep -ve "^\#\|deb cdrom:\|security\.debian\.org\|debian\.org/debian/\s\|mirror\.steadfast\.net\|mirror\.nozonenet\.com\|^\s*$" /etc/apt/sources.list.mirrorize.${DATE} >> /etc/apt/sources.list || echo "# Nothing" >> /etc/apt/sources.list; echo " * Adding ${REPO} main, non-free, contrib at mirror.steadfast.net to list..."; echo >> /etc/apt/sources.list echo "# Added By Steadfast Networks Mirrorize" >> /etc/apt/sources.list; echo "deb http://mirror.steadfast.net/debian ${REPO} main non-free contrib" >> /etc/apt/sources.list; echo "deb-src http://mirror.steadfast.net/debian ${REPO} main non-free contrib" >> /etc/apt/sources.list; echo "deb http://mirror.steadfast.net/debian ${REPO}-updates main non-free contrib" >> /etc/apt/sources.list; echo "deb-src http://mirror.steadfast.net/debian ${REPO}-updates main non-free contrib" >> /etc/apt/sources.list; echo "deb http://mirror.steadfast.net/debian-security ${REPO}/updates main non-free contrib" >> /etc/apt/sources.list; echo "deb-src http://mirror.steadfast.net/debian-security ${REPO}/updates main non-free contrib" >> /etc/apt/sources.list; echo " * Running apt-get update..."; apt-get update 2>&1 >/dev/null if [[ $? != 0 ]]; then echo "* Error: Failed to run apt-get update... reverting sources.list!"; mv /etc/apt/sources.list /etc/apt/sources.list.mirrorize.${DATE}.failed mv /etc/apt/sources.list.mirrorize.${DATE} /etc/apt/sources.list fi else echo " * Debian mirror is already set correctly!"; fi else echo " * Error: Unsupported Version: ${VER}"; fi else echo " * This is ${OS} ${VER} (${ARCH})..."; echo " * Error: Unsupported OS: ${OS}"; fi