Skip to content

Instantly share code, notes, and snippets.

@scottlinux
Forked from hvindin/convertCentOS7toRHEL7.sh
Last active January 28, 2021 18:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save scottlinux/891548a4c2aaeacd28d50ac92ebd2a77 to your computer and use it in GitHub Desktop.
Save scottlinux/891548a4c2aaeacd28d50ac92ebd2a77 to your computer and use it in GitHub Desktop.
Convert CentOS to RHEL | Thanks https://gist.github.com/hvindin
#!/bin/sh
showHelp() {
cat <<EOF
This script requires 2 or 3 arguments exactly.
"${0}" <redhat username> <redhat password> [<redhat release>]
examples:
"${0}" user1 passw0rd! 7Server
"${0}" user2 pAssw@rd
By default the omission of the release version will default to 7Server being set.
EOF
}
# Assign arguments to variables
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
showHelp
elif [ $# -eq 3 ]; then
REDHAT_USERNAME="${1}"
REDHAT_PASSWORD="${2}"
REDHAT_RELEASE="${3}"
elif [ $# -eq 2 ]; then
REDHAT_USERNAME="${1}"
REDHAT_PASSWORD="${2}"
REDHAT_RELEASE="7Server"
fi
# linode base image lacks this for later dependencies
yum install usermode -y
# remove centos package
rpm -e --nodeps centos-release
# create tmp directory to store rpms
mkdir /tmp/convert
# Download all the packages needed to subscribe to RHEL repos as well as some of their commonly
# missing dependencies
curl -Ss "http://hcv.link/2d1Rxty" -L | tar xJC /tmp/convert
# Install all of those packages
# This could probably be a yum install -y /tmp/convert/*.rpm but I'd rather have the
# list of packages visible in case I forget what is included.
yum install -y \
/tmp/convert/libnl-1.1.4-3.el7.x86_64.rpm \
/tmp/convert/libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm \
/tmp/convert/m2crypto-0.21.1-17.el7.x86_64.rpm \
/tmp/convert/pygobject2-2.28.6-11.el7.x86_64.rpm \
/tmp/convert/pyOpenSSL-0.13.1-3.el7.x86_64.rpm \
/tmp/convert/python-dateutil-1.5-7.el7.noarch.rpm \
/tmp/convert/python-dmidecode-3.10.13-11.el7.x86_64.rpm \
/tmp/convert/python-ethtool-0.8-5.el7.x86_64.rpm \
/tmp/convert/python-gudev-147.2-7.el7.x86_64.rpm \
/tmp/convert/python-hwdata-1.7.3-4.el7.noarch.rpm \
/tmp/convert/python-rhsm-1.15.4-5.el7.x86_64.rpm \
/tmp/convert/redhat-release-server-7.2-9.el7.x86_64.rpm \
/tmp/convert/rhn-check-2.0.2-6.el7.noarch.rpm \
/tmp/convert/rhn-client-tools-2.0.2-6.el7.noarch.rpm \
/tmp/convert/rhnlib-2.5.65-2.el7.noarch.rpm \
/tmp/convert/rhnsd-5.0.13-5.el7.x86_64.rpm \
/tmp/convert/rhn-setup-2.0.2-6.el7.noarch.rpm \
/tmp/convert/subscription-manager-1.15.9-15.el7.x86_64.rpm \
/tmp/convert/yum-rhn-plugin-2.0.1-5.el7.noarch.rpm
# Subscribe to the redhat repositories.
subscription-manager register --username=${REDHAT_USERNAME} --password=${REDHAT_PASSWORD} --release=${REDHAT_RELEASE} --auto-attach
# Disable all the repos incase we accidentally attached Atomic RHEL or Developer tools repos that we don't actually want
subscription-manager repos --disable="*"
# Enable the bare minimum of repositories
# This can't be included in the previous step because --disable="*" seems to clobber any --enable arguments.
subscription-manager repos --enable=rhel-7-server-rpms --enable=rhel-7-server-optional-rpms --enable=rhel-7-server-extras-rpms
# Deal with system logos...
rpm -e --nodeps centos-logos
yum install -y redhat-logos
# distro-sync to get all of the packages either upgraded downgraded or completely removed as required.
yum distro-sync -y
# Clean up the temporary "convert" files but keep a tar of them just in case we need it.
# Also keep a backup of this script.
pushd /tmp/convert
tar czvf /root/conversion-rpms.tar.gz libnl-1.1.4-3.el7.x86_64.rpm libxml2-python-2.9.1-6.el7_2.3.x86_64.rpm m2crypto-0.21.1-17.el7.x86_64.rpm pygobject2-2.28.6-11.el7.x86_64.rpm pyOpenSSL-0.13.1-3.el7.x86_64.rpm python-dateutil-1.5-7.el7.noarch.rpm python-dmidecode-3.10.13-11.el7.x86_64.rpm python-ethtool-0.8-5.el7.x86_64.rpm python-gudev-147.2-7.el7.x86_64.rpm python-hwdata-1.7.3-4.el7.noarch.rpm python-rhsm-1.15.4-5.el7.x86_64.rpm redhat-release-server-7.2-9.el7.x86_64.rpm rhn-check-2.0.2-6.el7.noarch.rpm rhn-client-tools-2.0.2-6.el7.noarch.rpm rhnlib-2.5.65-2.el7.noarch.rpm rhnsd-5.0.13-5.el7.x86_64.rpm rhn-setup-2.0.2-6.el7.noarch.rpm subscription-manager-1.15.9-15.el7.x86_64.rpm yum-rhn-plugin-2.0.1-5.el7.noarch.rpm "${0}"
popd
rm -rf /tmp/convert
# Reinstall all the CentOS packages to make sure that we have the RHEL Repo versions.
# They should be the same most of the time but why not be a little extra careful.
yum reinstall $(yum list installed | awk '{print$1}' | grep -v -E '^(@|[0-9])') -y
# Reboot and hopefully the machine comes back up alive.
systemctl reboot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment