From 991d6a1f93d1d882dbea154752eed872a735ca0b Mon Sep 17 00:00:00 2001 From: mgb01105731 Date: Mon, 6 Dec 2021 14:34:18 +0800 Subject: [PATCH] Add migration to Anolis 7.7 ; add usage of centos2anolis.py in README.md modify remove redhat-lsb-core & redhat-lsb-submod-security --- README.md | 13 ++- centos2anolis.py | 271 +++++++++++++++++++++++++++++------------------ 2 files changed, 173 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index dbb2fde..e474d80 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This script is designed to automatically convert a CentOS instance to Anolis in- ## Supported versions -This script only supports switching CentOS Linux 8. It does not support CentOS Stream. +This script supports switching CentOS Linux 7 & 8. It does not support CentOS Stream. ## Before you switch @@ -20,6 +20,7 @@ Please ensure you have a **complete backup** of the system before you execute th ## Usage 1. Make sure you can run this script as root. +1. Make sure you have python3. 1. Either clone this repository or download [`centos2anolis.py`][2]. 1. Run `python3 centos2anolis.py -v * ` to start converting.`(* is the version of the anolis which you want)` @@ -44,11 +45,13 @@ Please ensure you have a **complete backup** of the system before you execute th The tool downloads packages from mirrors.openanolis.org by default, which could take a long time. This option intends to use mirrors.aliyun.com to speed up the process. -* `-v` The version of anolis8 +* `-v` The version of Anolis - The version of anolis8,like `8.2` or `8.4`.If you do not specify this parameter, the system prompts you to select. + The version of anolis , like `7.7` or `8.2` or `8.4`.If you do not specify this parameter, the system prompts you to select. - `Note that centos8.3/8.4 only supports migration to anolis8.4`. + * `Note that centos7.x only supports migration to anolis7.7`. + * `Note that centos8.0-8.2 supports migration to anolis8.2 or anolis8.4`. + * `Note that centos8.3/8.4 only supports migration to anolis8.4`. ## Support Open an issue on [bugs.openanolis.cn][1] for any bugs, questions and requirements. @@ -60,4 +63,4 @@ Copyright (c) 2021 OpenAnolis Community. See the LICENSE.txt file for details [1]: bugs.openanolis.cn -[2]: https://codeup.openanolis.cn/codeup/migration/centos2anolis/raw/master/centos2anolis.py +[2]: https://gitee.com/anolis/centos2anolis/raw/master/centos2anolis.py diff --git a/centos2anolis.py b/centos2anolis.py index bc7608f..3e4b12a 100644 --- a/centos2anolis.py +++ b/centos2anolis.py @@ -8,7 +8,6 @@ import os import subprocess import re -import dnf import socket import sys import shutil @@ -17,6 +16,18 @@ import platform yum_url="https://mirrors.openanolis.org/anolis/" github_url="http://codeup.openanolis.cn/codeup/migration/centos2anolis" +repostr_an7 = '''[an7_os] +name=AnolisOS-7 - OS +baseurl=http://mirrors.openanolis.org/anolis/7/os/$basearch/os +gpgcheck=0 +enabled=1 + +[an7_updates] +name=AnolisOS-7 - updates +baseurl=http://mirrors.openanolis.org/anolis/7/updates/$basearch/os +gpgcheck=0 +enabled=1 +''' repostr_an8 = '''[an8_baseos] name=AnolisOS-8 - BaseOS baseurl=http://mirrors.openanolis.org/anolis/8/BaseOS/$basearch/os @@ -51,8 +62,17 @@ libreport-centos libreport-plugin-mantisbt libreport-plugin-rhtsupport python3-s python-oauth sl-logos yum-rhn-plugin centos-indexhtml' reposdir ='' -anolislist = ['8.2','8.4'] - +anolislist7 = ['7.7'] +anolislist8 = ['8.2','8.4'] +try: + centosv = str(subprocess.check_output("cat /etc/centos-release | awk '{print $4}'", shell=True), 'utf-8')[:3] + print("centos version is " + centosv) + float(centosv) +except: + print("centos version not found or centos version is wrong type") + print("please check the file of centos-release") + sys.exit(1) +centosversion=int(float(centosv)) def check_pkg(pkg): if pkg.split('/')[0] == '': @@ -73,8 +93,13 @@ def check_pkg(pkg): def clean_and_exit(): global reposdir + global repostr_an7 global repostr_an8 - repostr_an8 = re.sub(r"/(8.4)/", "/8/", repostr_an8) + + if centosversion == 7: + repostr_an7 = re.sub(r"anolis/(.*)/", "anolis/7/", repostr_an7) + if centosversion == 8: + repostr_an8 = re.sub(r"anolis/(.*)/", "anolis/8/", repostr_an8) repo_path = os.path.join(reposdir, 'switch-to-anolis.repo') if os.path.exists(repo_path): @@ -98,7 +123,7 @@ def get_disk_info(string): def add_boot_option(): print("Current system is uefi, add boot option to boot manager.") - subprocess.run('which efibootmgr > /dev/null 2>&1 || dnf install -y efibootmgr', shell=True) + subprocess.run('which efibootmgr > /dev/null 2>&1 || yum install -y efibootmgr', shell=True) disk_name = subprocess.check_output('mount | grep /boot/efi | awk \'{print $1}\'', shell=True) disk_name = str(disk_name, 'utf-8') disk_name = disk_name.split('\n')[0] @@ -139,9 +164,10 @@ def change_repo_mirror(reposdir, prefix): def backup_repofiles(old_version): global yum_url + global centosversion repos = [] - if re.match('centos-release-8\.*|centos-linux-release-8\.*', old_version): + if re.match('centos-release-'+str(centosversion)+'\.*|centos-linux-release-'+str(centosversion)+'\.*', old_version): old_version_r = subprocess.check_output('rpm -qa centos*repos', shell=True) old_version_r = str(old_version_r, 'utf-8')[:-1] old_version = old_version +" "+old_version_r @@ -176,70 +202,93 @@ def backup_repofiles(old_version): os.remove(repo) return old_version - def check_version(version): + global repostr_an7 global repostr_an8 + global centosv - try: - centosv = str(subprocess.check_output("cat /etc/centos-release | awk '{print $4}'",shell=True),'utf-8')[:3] - print("centos version is " +centosv) - float(centosv) - except: - print("centos version not found or centos version is wrong type") - print("please check the file of centos-release") - sys.exit(1) + if float(centosv) == 7.0: + print("set 'setenforce 0' in centos 7.0") + subprocess.run('setenforce 0', shell=True) if not version: - print("You have to set the version of anolis8") + print("You have to set the version of Anolis") print("The following version are enabled") if float(centosv)>8.2: - print(' '.join(anolislist[1:])) + print(' '.join(anolislist8[1:])) + elif float(centosv)<8: + print(' '.join(anolislist7)) else: - print(' '.join(anolislist)) + print(' '.join(anolislist8)) version = str(input("Selected the version is : ")).strip() - if float(centosv) <=8.2 and version == "8.2" or version == "8": + if centosversion < 8 and version == "7.7": + print("You selected anolis7.7") + repostr_an7 = re.sub(r"/(7)/", "/7.7/", repostr_an7) + elif 8 <= float(centosv) <= 8.2 and version == "8.2": print("You selected anolis8.2") - elif version == "8.4": + repostr_an8 = re.sub(r"/(8)/", "/8.2/", repostr_an8) + elif centosversion >= 8 and version == "8.4": print("You selected anolis8.4") - repostr_an8 = re.sub(r"/(8)/","/8.4/",repostr_an8) + repostr_an8 = re.sub(r"/(8)/", "/8.4/", repostr_an8) else: - print("version "+ version +" not found") + print("version " + version + " not found, Please Check!") sys.exit(1) - elif version == "8.4": + elif centosversion < 8 and version == "7.7": + print("You selected anolis7.7") + repostr_an7 = re.sub(r"/(7)/", "/7.7/", repostr_an7) + elif 8 <= float(centosv) <= 8.2 and version == "8.2": + print("You selected anolis8.2") + repostr_an8 = re.sub(r"/(8)/", "/8.2/", repostr_an8) + elif centosversion >= 8 and version == "8.4": print("You selected anolis8.4") repostr_an8 = re.sub(r"/(8)/", "/8.4/", repostr_an8) - elif float(centosv) <=8.2 and version == "8.2" or version == "8": - print("You selected anolis8.2") else: - print("version " + version + " not found") + print("version " + version + " not found,Please Check!") print("The following version are enabled") if float(centosv)>8.2: - print(' '.join(anolislist[1:])) + print(' '.join(anolislist8[1:])) + elif float(centosv)<8: + print(' '.join(anolislist7)) else: - print(' '.join(anolislist)) + print(' '.join(anolislist8)) sys.exit(1) def process_special_pkgs(): print("swap centos-logos related packages with Anolis packages") - subprocess.run('rpm -q centos-logos-ipa && dnf swap -y centos-logos-ipa anolis-logos-ipa', shell=True) - subprocess.run('rpm -q centos-logos-httpd && dnf swap -y centos-logos-httpd anolis-logos-httpd', shell=True) + subprocess.run('rpm -q centos-logos-ipa && yum swap -y centos-logos-ipa anolis-logos-ipa', shell=True) + subprocess.run('rpm -q centos-logos-httpd && yum swap -y centos-logos-httpd anolis-logos-httpd', shell=True) print("redhat-lsb is replaced by system-lsb on Anolis") - subprocess.run('rpm -q redhat-lsb-core && dnf swap -y redhat-lsb-core system-lsb-core', shell=True) - subprocess.run('rpm -q redhat-lsb-submod-security && dnf swap -y redhat-lsb-submod-security system-lsb-submod-security',shell=True) + subprocess.run('rpm -q redhat-lsb-core && yum -y remove redhat-lsb-core system-lsb-core', shell=True) + subprocess.run('rpm -q redhat-lsb-submod-security && yum -y remove redhat-lsb-submod-security system-lsb-submod-security',shell=True) print("rhn related packages is not provided by Anolis") - subprocess.run('rpm -q rhn-client-tools && dnf -y remove rhn-client-tools python3-rhn-client-tools python3-rhnlib', shell=True) + subprocess.run('rpm -q rhn-client-tools && yum -y remove rhn-client-tools python3-rhn-client-tools python3-rhnlib', shell=True) print("subscription-manager related packages is not provided by Anolis") - subprocess.run('rpm -q subscription-manager && dnf -y remove subscription-manager', shell=True) + subprocess.run('rpm -q subscription-manager && yum -y remove subscription-manager', shell=True) print("python3-syspurpose is not provided by Anolis") - subprocess.run('rpm -q python3-syspurpose && dnf -y remove python3-syspurpose', shell=True) + subprocess.run('rpm -q python3-syspurpose && yum -y remove python3-syspurpose', shell=True) print("remove centos gpg-pubkey") subprocess.run('rpm -e $(rpm -q gpg-pubkey --qf "%{NAME}-%{VERSION}-%{RELEASE} %{PACKAGER}\\n" | grep CentOS | awk \'{print $1}\')', shell=True) +def yum_remove_pkg(): + # 7.0-7.1 + subprocess.run('rpm -q gnome-documents && yum -y remove gnome-documents', shell=True) + # 7.0-7.3 + # downgrade mythes-en & hyphen-en + subprocess.run("rpm -q mythes-en hyphen-en && yum -y downgrade mythes-en hyphen-en", shell=True) + subprocess.run("rpm -q cdparanoia-libs && yum -y downgrade cdparanoia-libs", shell=True) + subprocess.run("rpm -q libvirt-client && yum -y upgrade libvirt-client", shell=True) + subprocess.run("rpm -q gnome-packagekit* && yum -y upgrade gnome-packagekit*", shell=True) + subprocess.run("rpm -q libreoffice-langpack-en && yum -y upgrade libreoffice-langpack-en", shell=True) + subprocess.run("rpm -q unoconv && yum -y update unoconv", shell=True) + #7.0-7.1 remove centos-bookmarks + subprocess.run("rpm -q centos-bookmarks && yum -y remove centos-bookmarks", shell=True) + # remove kmod-kvdo + subprocess.run('rpm -q kmod-kvdo && yum -y remove kmod-kvdo', shell=True) def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, version=False): global reposdir @@ -255,12 +304,11 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers # check required packages print('Checking required packages') - for pkg in ['rpm','yum','curl']: + for pkg in ['rpm', 'yum', 'curl']: if not check_pkg(pkg): - print("Could not found "+pkg) + print("Could not found " + pkg) sys.exit(1) - - # display rpms info before conversion + if verify_all_rpms: print("Creating a list of RPMs installed before the switch") print("Verifying RPMs installed before the switch against RPM database") @@ -293,67 +341,64 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers if re.match('anolis-release', old_version): print("You are already using Anolis.") sys.exit(1) - + elif re.match('centos-linux-release', old_version): subver = old_version.split('-')[3] - + elif re.match('redhat-release|centos-release|sl-release', old_version): subver = old_version.split('-')[2] - + else: print("Your are using an unsupported distribution.") sys.exit(1) - if not re.match('8',subver): + if not re.match(str(centosversion),subver): print("You appear to be running an unsupported distribution.") sys.exit(1) - print("========= Checking: required python packages =========") - if not check_pkg('/usr/libexec/platform-python'): - print('/usr/libexec/platform-python not found.') - sys.exit(1) base_packages=['basesystem','initscripts','anolis-logos','plymouth','grub2','grubby'] print("========= Checking: yum lock ===========") if os.path.exists('/var/run/yum.pid'): with open('/var/run/yum.pid', 'r') as f: pid = f.read() - with open('/proc/'+pid+'/comm', 'r') as ff: + with open('/proc/' + pid + '/comm', 'r') as ff: comm = ff.read() - print('Another app is currently holding the yum lock: '+comm) - print('Running as pid: '+pid) + print('Another app is currently holding the yum lock: ' + comm) + print('Running as pid: ' + pid) print('Please kill it and run the tool again.') sys.exit(1) - # check dnf - print("========= Checking: dnf =========") - print("Identifying dnf modules that are enabled...") - enabled_modules = str( - subprocess.check_output("dnf module list --enabled | grep rhel | awk '{print $1}'", shell=True), - 'utf-8') - enabled_modules = enabled_modules.split('\n')[:-1] - unknown_mods=[] - if len(enabled_modules) > 0: - for mod in enabled_modules: - if re.fullmatch('container-tools|llvm-toolset', mod): - subprocess.run('dnf module reset -y '+mod, shell=True) - if not re.fullmatch('container-tools|go-toolset|jmc|llvm-toolset|rust-toolset|virt', mod): - unknown_mods.append(mod) - if len(unknown_mods) > 0: - print('This tool is unable to automatically switch module(s) ' \ - + ','.join(unknown_mods) \ - + ' from a CentOS \'rhel\' stream to an Anolis equivalent.'\ - ) - opt = input('Do you want to continue and resolve it manually? (Yes or No)\n' + \ - 'You may want select No to stop and raise an issue on ' \ - + github_url \ - + ' for advice. '\ - ) - if opt != 'Yes': - sys.exit(1) + if centosversion == 8: + # check dnf + print("========= Checking: dnf =========") + print("Identifying dnf modules that are enabled...") + enabled_modules = str( + subprocess.check_output("dnf module list --enabled | grep rhel | awk '{print $1}'", shell=True), + 'utf-8') + enabled_modules = enabled_modules.split('\n')[:-1] + unknown_mods = [] + if len(enabled_modules) > 0: + for mod in enabled_modules: + if re.fullmatch('container-tools|llvm-toolset', mod): + subprocess.run('dnf module reset -y ' + mod, shell=True) + if not re.fullmatch('container-tools|go-toolset|jmc|llvm-toolset|rust-toolset|virt', mod): + unknown_mods.append(mod) + if len(unknown_mods) > 0: + print('This tool is unable to automatically switch module(s) ' \ + + ','.join(unknown_mods) \ + + ' from a CentOS \'rhel\' stream to an Anolis equivalent.' \ + ) + opt = input('Do you want to continue and resolve it manually? (Yes or No)\n' + \ + 'You may want select No to stop and raise an issue on ' \ + + github_url \ + + ' for advice. ' \ + ) + if opt != 'Yes': + sys.exit(1) print("========= Finding your repository directory =========") - dir = dnf.Base().conf.get_reposdir + dir = '/etc/yum.repos.d/' if os.path.isdir(dir): reposdir = dir else: @@ -366,7 +411,11 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers repofile = os.path.join(reposdir, 'switch-to-anolis.repo') with open(repofile, 'w') as f: - f.write(repostr_an8) + if centosversion == 7: + repostr_an=repostr_an7 + else: + repostr_an=repostr_an8 + f.write(repostr_an) if accelerate: change_repo_mirror(reposdir, 'switch-to-anolis.repo') @@ -390,7 +439,7 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers content = re.sub(r"\n(bugtracker_url=)", r"\n#\1", content) with open('/etc/yum.conf', 'w') as f: f.write(content) - + print("Downloading anolis release package...") dst_release = ['anolis-release', 'anolis-repos', 'anolis-gpg-keys'] try: @@ -407,9 +456,12 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers clean_and_exit() print("Switching old release package with Anolis...") - dst_rpms = [s+'*.rpm' for s in dst_release] + dst_rpms = [s + '*.rpm' for s in dst_release] subprocess.run('rpm -i --force ' + ' '.join(dst_rpms) + ' --nodeps', shell=True) - subprocess.run('rpm -e --nodeps ' + old_version + ' centos-gpg-keys', shell=True) + if centosversion ==7: + subprocess.run('rpm -e --nodeps ' + old_version, shell=True) + else: + subprocess.run('rpm -e --nodeps ' + old_version + ' centos-gpg-keys', shell=True) os.remove(repofile) # switch completed @@ -417,9 +469,10 @@ def main(reinstall_all_rpms=False, verify_all_rpms=False, accelerate=False, vers change_repo_mirror(reposdir, 'AnolisOS-') print("Installing base packages for Anolis...") - cmd='yum shell -y < 0: - for mod in enabled_modules: - subprocess.run('dnf module reset -y '+mod, shell=True) - if re.fullmatch('container-tools|go-toolset|jmc|llvm-toolset|rust-toolset', mod): - subprocess.run('dnf module enable -y '+mod+':an8', shell=True) - elif mod =='virt': - subprocess.run('dnf module enable -y '+mod+':an', shell=True) - else: - print("Unsure how to transform module"+mod) - subprocess.run('dnf -y distro-sync', shell=True) - - try: - subprocess.check_call('dnf module list --enabled | grep satellite-5-client', shell=True) - print("Anolis does not provide satellite-5-client module, disable it.") - subprocess.run('dnf module disable -y satellite-5-client', shell=True) - except: - pass + if centosversion == 8: + if len(enabled_modules) > 0: + for mod in enabled_modules: + subprocess.run('dnf module reset -y '+mod, shell=True) + if re.fullmatch('container-tools|go-toolset|jmc|llvm-toolset|rust-toolset', mod): + subprocess.run('dnf module enable -y '+mod+':an8', shell=True) + elif mod =='virt': + subprocess.run('dnf module enable -y '+mod+':an', shell=True) + else: + print("Unsure how to transform module"+mod) + subprocess.run('dnf -y distro-sync', shell=True) + + try: + subprocess.check_call('dnf module list --enabled | grep satellite-5-client', shell=True) + print("Anolis does not provide satellite-5-client module, disable it.") + subprocess.run('dnf module disable -y satellite-5-client', shell=True) + except: + pass process_special_pkgs() @@ -490,10 +547,11 @@ EOF' os.remove('/var/cache/yum') elif os.path.isdir('/var/cache/yum'): shutil.rmtree('/var/cache/yum') - if os.path.isfile('/var/cache/dnf'): - os.remove('/var/cache/dnf') - elif os.path.isdir('/var/cache/dnf'): - shutil.rmtree('/var/cache/dnf') + if centosversion ==8: + if os.path.isfile('/var/cache/dnf'): + os.remove('/var/cache/dnf') + elif os.path.isdir('/var/cache/dnf'): + shutil.rmtree('/var/cache/dnf') if verify_all_rpms: print("Creating a list of RPMs installed after the switch") @@ -523,6 +581,7 @@ if __name__ == "__main__": parser.add_argument('-r', action='store_true', help='Reinstall all CentOS RPMs with Anolis RPMs (Note: This is not necessary for support)') parser.add_argument('-V', action='store_true', help='Verify RPM information before and after the switch') parser.add_argument('-s', action='store_true', help='Accelerate download') - parser.add_argument('-v', help='Choose the version of anolis8') + parser.add_argument('-v', help='Choose the version of Anolis') args = parser.parse_args() sys.exit(main(args.r, args.V, args.s, args.v)) + -- Gitee