2021-05-03 21:03:52 +00:00
#!/usr/bin/env bash
2021-04-25 17:08:19 +00:00
# label <label@rockylinux.org>
# Supports only CentOS 8.3
2021-05-02 12:15:39 +00:00
## Rocky is RC status. Using this script means you accept all risks of system instability.
2021-05-03 20:47:10 +00:00
(
# Pass everything to a subshell so the output can be piped to /var/log/centos2rocky.log
2021-05-06 19:02:40 +00:00
errcolor = $( tput setaf 1)
nocolor = $( tput op)
blue = $( tput setaf 4)
2021-05-04 04:18:33 +00:00
2021-04-25 17:08:19 +00:00
set -e
unset CDPATH
2021-05-03 20:47:10 +00:00
if [ [ " $( id -u) " -ne 0 ] ] ; then
2021-05-06 20:07:26 +00:00
printf '%s\n' " $errcolor " " You must run this script as root. $nocolor " \
" ${ errcolor } Either use sudo or 'su -c ${ 0 } ' $nocolor "
2021-04-25 17:08:19 +00:00
fi
2021-05-03 20:47:10 +00:00
if [ [ " $( wget 2>/dev/null || echo $? ) " = = 127 ] ] ; then
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Wget is not installed! Installing it... $nocolor "
2021-05-03 20:47:10 +00:00
dnf -y install wget
fi
2021-05-02 16:50:33 +00:00
2021-05-03 20:47:10 +00:00
if [ [ " $( curl 2>/dev/null || echo $? ) " = = 127 ] ] ; then
2021-05-06 20:07:26 +00:00
printf " ${ blue } Curl is not installed! Installing it... $nocolor "
2021-05-03 20:47:10 +00:00
dnf -y install curl libcurl
fi
export LANG = en_US.UTF-8
2021-05-03 18:26:13 +00:00
2021-04-25 17:08:19 +00:00
SUPPORTED_RELEASE = "8.3"
SUPPORTED_MAJOR = "8"
2021-05-02 01:06:53 +00:00
current_url = " https://dl.rockylinux.org/pub/rocky/ ${ SUPPORTED_RELEASE } /BaseOS/x86_64/os/Packages "
2021-04-25 17:08:19 +00:00
# These are packages that can be swapped safely over and will have more added over time.
packages_to_swap = (
centos-backgrounds \
centos-indexhtml \
2021-04-30 16:39:30 +00:00
centos-linux-repos \
2021-04-25 17:08:19 +00:00
centos-logos \
centos-gpg-keys \
centos-linux-release)
2021-05-02 01:06:53 +00:00
packages_that_exist = ( $( rpm -q --queryformat= "%{NAME}\n" " ${ packages_to_swap [@] } " | grep -v "not installed" ) )
2021-05-02 17:47:22 +00:00
release_to_install = ( $( curl -L -s ${ current_url } | awk -F '"' '/rocky-repos|rocky-gpg-keys|rocky-release/ {print $2}' ) )
2021-05-02 01:06:53 +00:00
2021-04-25 17:08:19 +00:00
# Release packages that are part of SIG's should be listed below when they are available.
#sigs_to_swap=()
# Defaults
2021-05-02 17:30:19 +00:00
list_enabled = ( " $( dnf repolist enabled | awk '!/repo/ {print $1}' ) " )
2021-04-25 17:08:19 +00:00
enabled_modules = ( " $( dnf module list --enabled | grep rhel | awk '{print $1}' ) " )
convert_info_dir = /root/convert
reinstall_all_rpms = false
verify_all_rpms = false
usage( ) {
2021-05-06 20:07:26 +00:00
printf '%s\n' \
" Usage: ${ 0 ##*/ } [OPTIONS] " \
'' \
'Options:' \
'-h displays this help' \
'-r Converts to rocky' \
'-V Verifies switch' \
'-R Reinstall all packages' \
' !! USE WITH CAUTION !!'
2021-04-25 17:08:19 +00:00
exit 1
} >& 2
exit_message( ) {
2021-05-06 20:07:26 +00:00
printf '%s\n' " $1 "
2021-05-03 20:47:10 +00:00
final_message
2021-04-25 17:08:19 +00:00
exit 1
} >& 2
final_message( ) {
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ errcolor } An error occurred while we were attempting to convert your system to Rocky Linux. Your system may be unstable. Script will now exit to prevent possible damage. $nocolor "
2021-05-03 20:47:10 +00:00
logmessage
}
logmessage( ) {
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } A log of this installation can be found at /var/log/centos2rocky.log $nocolor "
2021-04-25 17:08:19 +00:00
}
## The actual work
bin_hash( ) {
hash " $1 " >/dev/null 2>& 1
}
bin_check( ) {
if ! bin_hash " $1 " ; then
2021-05-03 20:47:10 +00:00
exit_message " ' ${ 1 } ' command not found. Please ensure you are running bash or that your PATH is set correctly. "
logmessage
2021-04-25 17:08:19 +00:00
fi
}
generate_rpm_info( ) {
mkdir /root/convert
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Creating a list of RPMs installed: $1 $nocolor "
2021-04-25 17:08:19 +00:00
rpm -qa --qf "%{NAME}|%{VERSION}|%{RELEASE}|%{INSTALLTIME}|%{VENDOR}|%{BUILDTIME}|%{BUILDHOST}|%{SOURCERPM}|%{LICENSE}|%{PACKAGER}\n" | sort > " ${ convert_info_dir } / $( hostname) -rpm-list- $1 .log "
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Verifying RPMs installed against RPM database: $1 $nocolor " ''
2021-04-25 17:08:19 +00:00
rpm -Va | sort -k3 > " ${ convert_info_dir } / $( hostname) -rpm-list-verified- $1 .log "
}
package_swaps( ) {
2021-05-02 01:06:53 +00:00
mkdir /root/release
pushd /root/release
for x in " ${ release_to_install [@] } " ; do
2021-05-06 20:07:26 +00:00
wget -q " ${ current_url } / ${ x } " || {
printf '%s\n' " ${ errcolor } failed to download ${ x } $nocolor " '' &&
logmessage
exit 20
}
2021-05-02 01:06:53 +00:00
done
# Remove packages we need to swap
rpm -e --nodeps " ${ packages_that_exist [@] } "
# Install our release
rpm -ihv " ${ release_to_install [@] } "
# Distrosync if the above succeeded
2021-05-04 02:33:47 +00:00
if [ [ $? -eq 0 ] ] ; then
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Removing dnf cache $nocolor "
2021-04-25 17:08:19 +00:00
rm -rf /var/cache/{ yum,dnf}
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Ensuring repos are enabled before the package swap $nocolor "
dnf config-manager --set-enabled ${ list_enabled [@] } || {
printf '%s\n' 'Repo name missing?'
exit 25
}
2021-04-25 17:08:19 +00:00
dnf distro-sync -y
else
2021-05-03 20:47:10 +00:00
exit_message "We failed to install the release package."
logmessage
2021-04-25 17:08:19 +00:00
fi
2021-05-03 20:47:10 +00:00
2021-05-02 01:06:53 +00:00
popd
2021-04-25 17:08:19 +00:00
}
sig_swaps( ) {
2021-05-03 20:47:10 +00:00
exit_message "Not Available"
2021-04-25 17:08:19 +00:00
}
module_check( ) {
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Finding our modules that are enabled $nocolor "
2021-04-25 17:08:19 +00:00
for module in " ${ enabled_modules [@] } " ; do
case ${ module } in
container-tools| go-toolset| jmc| llvm-toolset| rust-toolset| virt)
; ;
*)
unknown_modules += ( " ${ module } " )
; ;
esac
done
2021-05-04 02:33:47 +00:00
if [ [ ${# unknown_modules [@] } -gt 0 ] ] ; then
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ unknown_modules [@] } " \
" ${ blue } There are some modules that are unsure of how to handle. This normally shouldn't happen. Do you want to resolve this yourself (Yes) or continue (No)? $nocolor "
2021-04-25 17:08:19 +00:00
select yn in "Yes" "No" ; do
case $yn in
Yes)
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ errcolor } Unsure how to switch modules, so we are leaving. $nocolor "
2021-05-03 20:47:10 +00:00
logmessage
2021-04-25 17:08:19 +00:00
exit 1
; ;
No)
break
; ;
esac
done
fi
}
# This is just in case. There is a likelihood this will have to be done.
module_fix( ) {
for module in " ${ enabled_modules [@] } " ; do
2021-05-03 20:47:10 +00:00
dnf module reset -y " ${ module } "
2021-04-25 17:08:19 +00:00
case ${ module } in
container-tools| go-toolset| jmc| llvm-toolset| rust-toolset| virt)
2021-05-03 20:47:10 +00:00
dnf module install " ${ module } " -y
2021-04-25 17:08:19 +00:00
; ;
*)
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ errcolor } Unsure how to deal with the module presented. $nocolor "
2021-05-03 20:47:10 +00:00
logmessage
2021-04-25 17:08:19 +00:00
; ;
esac
# Final update
2021-05-03 20:47:10 +00:00
dnf update -y
2021-04-25 17:08:19 +00:00
done
}
2021-05-03 18:26:13 +00:00
2021-05-03 20:47:10 +00:00
## End actual work
2021-05-03 18:26:13 +00:00
2021-04-25 17:08:19 +00:00
while getopts "hrVR" option; do
case " $option " in
h)
usage
; ;
r)
reinstall_all_rpms = true
; ;
V)
verify_all_rpms = true
; ;
R)
reinstall_all_rpms = true
; ;
*)
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ errcolor } Invalid switch. $nocolor "
2021-04-25 17:08:19 +00:00
usage
; ;
esac
done
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Ensuring rpm, yum, and wget are here. $nocolor "
2021-05-02 01:06:53 +00:00
for pkg in rpm yum wget curl; do
2021-05-03 20:47:10 +00:00
bin_check " ${ pkg } "
2021-04-25 17:08:19 +00:00
done
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Ensuring your version of CentOS is supported $nocolor "
2021-04-25 17:08:19 +00:00
if ! old_release = $( rpm -q --whatprovides /etc/redhat-release) ; then
2021-05-03 20:47:10 +00:00
exit_message "You are not running a supported distribution."
logmessage
2021-04-25 17:08:19 +00:00
fi
if [ " $( echo " ${ old_release } " | wc -l) " -ne 1 ] ; then
2021-05-03 20:47:10 +00:00
exit_message "You seem to have package issues. More than one package provides redhat-release."
logmessage
2021-04-25 17:08:19 +00:00
fi
if ! grep ${ SUPPORTED_RELEASE } -q /etc/redhat-release; then
2021-05-03 20:47:10 +00:00
exit_message " ${ SUPPORTED_RELEASE } is only supported for conversion at this time. Stream is not supported. "
logmessage
2021-04-25 17:08:19 +00:00
fi
if " ${ verify_all_rpms } " ; then
2021-05-03 20:47:10 +00:00
generate_rpm_info begin
2021-04-25 17:08:19 +00:00
fi
case " ${ old_release } " in
centos-linux-release*) ; ;
rocky-release*)
2021-05-03 20:47:10 +00:00
exit_message "You are already running Rocky."
logmessage
2021-04-25 17:08:19 +00:00
; ;
*)
2021-05-03 20:47:10 +00:00
exit_message "You are running an unsupported distribution. Good bye."
logmessage
2021-04-25 17:08:19 +00:00
esac
# Check our modules before a swap
module_check
# Actually do the swap and distro-sync
2021-05-03 20:47:10 +00:00
package_swaps
2021-04-25 17:08:19 +00:00
# Fix up modules
module_fix
# Warning, this is potentially dangerous.
if " ${ reinstall_all_rpms } " ; then
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ errcolor } !! THIS MAY CAUSE ISSUES WITH YOUR SYSTEM !! $nocolor "
2021-04-25 17:08:19 +00:00
rpm_list = ( " $( rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE} %{VENDOR}\n" | grep CentOS | awk '{print $1}' ) " )
if [ [ -n " ${ rpm_list [*] } " ] ] ; then
2021-05-06 20:07:26 +00:00
printf '%s ' 'Reinstalling rpms:' " ${ rpm_list [@] } "
2021-05-03 20:47:10 +00:00
dnf reinstall " ${ rpm_list [@] } " -y
2021-04-25 17:08:19 +00:00
fi
non_rocky_rpm = ( " $( rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}|%{VENDOR}|%{PACKAGER}\n" | grep -iv Rocky) " )
if [ [ -n ${ non_rocky_rpm [*] } ] ] ; then
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } Non-Rocky packages are installed. This is generally not an issue. If you see centos packages, you may need to address them and file a bug report at https://bugs.rockylinux.org $nocolor "
2021-05-03 20:47:10 +00:00
printf '\t%s\n' " ${ non_rocky_rpm [@] } "
2021-04-25 17:08:19 +00:00
fi
fi
if " ${ verify_all_rpms } " ; then
2021-05-03 20:47:10 +00:00
generate_rpm_info finish
2021-05-06 20:07:26 +00:00
printf '%s\n' " ${ blue } You may review the following files: $nocolor "
2021-05-03 20:47:10 +00:00
find /root/convert -type f -name " $( hostname) -rpms-*.log "
2021-04-25 17:08:19 +00:00
fi
2021-05-04 04:18:33 +00:00
2021-05-06 20:07:26 +00:00
printf '\n\n\n'
2021-05-04 04:18:33 +00:00
cat /etc/issue | awk 'NR<=15'
2021-05-06 20:07:26 +00:00
printf '%s\n' " $blue " " Done, please reboot your system. $nocolor "
2021-05-03 20:47:10 +00:00
logmessage
) | tee /var/log/centos2rocky.log
# Pipe output to /var/log/centos2rocky.log