diff --git a/README.md b/README.md index 4dc103f5d6545239f0bd7eb7f13fc2c0eda46973..ab110f625551407f2d04dbf3b8abd2a73a9041f2 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,14 @@ 该选项使用mirrors.aliyun.com加速下载。 + +* `-l` 离线迁移 + + 在无法接入到mirrors.openanolis.org 或 mirrors.aliyun.com时,使用本地源迁移系统。 + + 在运行工具之前,编辑Anolis仓库的repo文件:/etc/yum.repos.d/switch-to-anolis.repo + + ## 问题反馈 任何问题,需求或者疑问,都可以在[bugzilla.openanolis.cn][1]上提交。 diff --git a/centos2anolis.py b/centos2anolis.py index f35b9c38e6774b23bc2fa5e0a86a206bf6c43249..5729bbe5bb6742acef851b5f31b8834d266f12c6 100644 --- a/centos2anolis.py +++ b/centos2anolis.py @@ -16,8 +16,8 @@ import urllib.request import json import logging -yum_url="https://mirrors.openanolis.org/anolis/" -github_url="https://gitee.com/anolis/centos2anolis" +yum_url = "https://mirrors.openanolis.org/anolis/" +github_url = "https://gitee.com/anolis/centos2anolis" repostr_an7 = '''[an7_os] name=AnolisOS-7 - OS baseurl=http://mirrors.openanolis.org/anolis/7/os/$basearch/os @@ -63,16 +63,17 @@ centos-release-xen-412 centos-release-xen-46 centos-release-xen-48 centos-releas libreport-centos libreport-plugin-mantisbt libreport-plugin-rhtsupport python3-syspurpose \ python-oauth sl-logos yum-rhn-plugin centos-indexhtml' -reposdir ='' -anolislist7 = ['7.7','7.9'] -anolislist8 = ['8.2','8.4','8.5'] +reposdir = '' +anolislist7 = ['7.7', '7.9'] +anolislist8 = ['8.2', '8.4', '8.5'] centosversion = 0 centosv = "" internal_ecs = False dst_release = ['anolis-release', 'anolis-repos', 'anolis-gpg-keys'] old_release_pkg = "" -def get_current_version(continue_after_sync = False): + +def get_current_version(continue_after_sync=False): global centosversion global centosv @@ -82,17 +83,19 @@ def get_current_version(continue_after_sync = False): log_it(logging.INFO, "Continue after sync.") crtversion = "anolis" try: - centosv = str(subprocess.check_output("cat /etc/"+ crtversion +"-release | awk '{print $4}'", shell=True), 'utf-8')[:3] + centosv = str(subprocess.check_output("cat /etc/" + crtversion + "-release | awk '{print $4}'", shell=True), + 'utf-8')[:3] log_it(logging.INFO, "Version is " + centosv) float(centosv) except Exception as e: log_it(logging.ERROR, "Version not found, Please Check!!") sys.exit(1) - centosversion=int(float(centosv)) + centosversion = int(float(centosv)) if centosversion == 0: log_it(logging.ERROR, "Get current version failed.") sys.exit(1) + def check_pkg(pkg): if pkg.split('/')[0] == '': if os.path.exists(pkg): @@ -110,6 +113,7 @@ def check_pkg(pkg): return True return False + def clean_and_exit(): global repostr_an7 global repostr_an8 @@ -124,20 +128,22 @@ def clean_and_exit(): os.remove(repo_path) sys.exit(1) + def get_disk_info(string): dev_name = "" part_name = "" length = len(string) - for c in range(length-1, -1, -1): + for c in range(length - 1, -1, -1): if not string[c].isdigit(): if string.find('nvme') != -1: dev_name = string[0:c] - part_num = string[c+1:length] + part_num = string[c + 1:length] else: - dev_name = string[0:c+1] - part_num = string[c+1:length] + dev_name = string[0:c + 1] + part_num = string[c + 1:length] break - return dev_name,part_num + return dev_name, part_num + def add_boot_option(): log_it(logging.INFO, "Current system is uefi, add boot option to boot manager.") @@ -145,12 +151,12 @@ def add_boot_option(): 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] - dev_name,part_num = get_disk_info(disk_name) + dev_name, part_num = get_disk_info(disk_name) if dev_name == "" or part_num == "": log_it(logging.INFO, "Parse /boot/efi disk info failed, update boot loader failed.") return - cmd="" + cmd = "" arch = platform.machine() if arch == "x86_64": cmd = 'efibootmgr -c -d ' + dev_name + ' -p ' + part_num + ' -l "/EFI/anolis/shimx64.efi" -L "Anolis OS"' @@ -161,31 +167,35 @@ def add_boot_option(): except: log_it(logging.INFO, "Use efibootmgr update boot loader failed, please update boot loader manually.") + def change_repo_mirror(reposdir, prefix): mirror_openanolis1 = "mirrors.openanolis.org" mirror_openanolis2 = "mirrors.openanolis.cn" - mirror_aliyun="mirrors.aliyun.com" + mirror_aliyun = "mirrors.aliyun.com" log_it(logging.INFO, "Accelerating downloads...") anolis_repos = [] files = os.listdir(reposdir) for f in files: - repo_path=os.path.join(reposdir, f) + repo_path = os.path.join(reposdir, f) if os.path.isfile(repo_path): if re.match(prefix, f): - #replace + # replace with open(repo_path, 'r') as fr: content = fr.read() - content = content.replace(mirror_openanolis1, mirror_aliyun).replace(mirror_openanolis2, mirror_aliyun) + content = content.replace(mirror_openanolis1, mirror_aliyun).replace(mirror_openanolis2, + mirror_aliyun) with open(repo_path, 'w') as fw: fw.writelines(content) + def backup_repofiles(): global old_release_pkg log_it(logging.INFO, "Backing up and removing old repository files...") repos = [] - if re.match('centos-release-' + str(centosversion) + '\.*|centos-linux-release-' + str(centosversion) + '\.*', old_release_pkg): + if re.match('centos-release-' + str(centosversion) + '\.*|centos-linux-release-' + str(centosversion) + '\.*', + old_release_pkg): old_version_r = subprocess.check_output('rpm -qa centos*repos', shell=True) old_version_r = str(old_version_r, 'utf-8')[:-1] old_release_pkg = old_release_pkg + " " + old_version_r @@ -197,7 +207,7 @@ def backup_repofiles(): repos = str(repos, 'utf-8').split('\n')[:-1] num_centos_repos = subprocess.check_output('rpm -qa "centos-release-*" | wc -l', shell=True) - if int(str(num_centos_repos,'utf-8')[0]) > 0: + if int(str(num_centos_repos, 'utf-8')[0]) > 0: addtional_repos = subprocess.check_output('rpm -qla "centos-release-*"', shell=True) addtional_repos = str(addtional_repos, 'utf-8') if addtional_repos != '': @@ -207,18 +217,19 @@ def backup_repofiles(): repos.append(r) backup_comment = '# This is a yum repository file that was disabled by\n' \ - + '# ' + __file__ + ', a script to convert CentOS to Anolis OS.\n' \ - + '# Please see ' + yum_url + ' for more information.\n\n' + + '# ' + __file__ + ', a script to convert CentOS to Anolis OS.\n' \ + + '# Please see ' + yum_url + ' for more information.\n\n' for repo in repos: if not os.path.isfile(repo): continue with open(repo, 'r') as fsrc: content = fsrc.read() - with open(repo+'.disabled','w') as fdst: - fdst.write(repo + '\n' + backup_comment+content) + with open(repo + '.disabled', 'w') as fdst: + fdst.write(repo + '\n' + backup_comment + content) os.remove(repo) + def check_version(version): global repostr_an7 global repostr_an8 @@ -265,6 +276,7 @@ def check_version(version): log_it(logging.ERROR, "CentOS version is not supported to switch.") sys.exit(1) + def process_special_pkgs(): log_it(logging.INFO, "Swap centos-logos related packages with Anolis OS packages") subprocess.run('rpm -q centos-logos-ipa && yum swap -y centos-logos-ipa anolis-logos-ipa', shell=True) @@ -272,14 +284,17 @@ def process_special_pkgs(): if centosversion == 7: subprocess.run('rpm -q redhat-lsb-core && yum -y remove redhat-lsb-core', shell=True) - subprocess.run('rpm -q redhat-lsb-submod-security && yum -y remove redhat-lsb-submod-security',shell=True) + subprocess.run('rpm -q redhat-lsb-submod-security && yum -y remove redhat-lsb-submod-security', shell=True) else: log_it(logging.INFO, "Package redhat-lsb is replaced by system-lsb on Anolis OS") subprocess.run('rpm -q redhat-lsb-core && yum swap -y redhat-lsb-core system-lsb-core', shell=True) - subprocess.run('rpm -q redhat-lsb-submod-security && yum swap -y redhat-lsb-submod-security system-lsb-submod-security',shell=True) + subprocess.run( + 'rpm -q redhat-lsb-submod-security && yum swap -y redhat-lsb-submod-security system-lsb-submod-security', + shell=True) log_it(logging.INFO, "Packages related to rhn are not provided by Anolis OS") - subprocess.run('rpm -q rhn-client-tools && yum -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) log_it(logging.INFO, "Packages related to subscription are not provided by Anolis OS") subprocess.run('rpm -q subscription-manager && yum -y remove subscription-manager', shell=True) @@ -288,7 +303,10 @@ def process_special_pkgs(): subprocess.run('rpm -q python3-syspurpose && yum -y remove python3-syspurpose', shell=True) log_it(logging.INFO, "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) + subprocess.run( + 'rpm -e $(rpm -q gpg-pubkey --qf "%{NAME}-%{VERSION}-%{RELEASE} %{PACKAGER}\\n" | grep CentOS | awk \'{print $1}\')', + shell=True) + def process_pkgs_for_c7(): if centosversion != 7: @@ -304,7 +322,7 @@ def process_pkgs_for_c7(): 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 + # 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) @@ -312,19 +330,21 @@ def process_pkgs_for_c7(): subprocess.run("rpm -q libreport-plugin-rhtsupport && yum -y remove libreport-plugin-rhtsupport", shell=True) subprocess.run("rpm -q libreport-centos && yum -y remove libreport-centos", shell=True) subprocess.run("rpm -q libreport-plugin-mantisbt && yum -y remove libreport-plugin-mantisbt", shell=True) - subprocess.run("rpm -q libreport-rhel-anaconda-bugzilla && yum -y remove libreport-rhel-anaconda-bugzilla", shell=True) + subprocess.run("rpm -q libreport-rhel-anaconda-bugzilla && yum -y remove libreport-rhel-anaconda-bugzilla", + shell=True) subprocess.run('yum -y downgrade systemd systemd-libs systemd-python systemd-sysv libgudev1', shell=True) + def get_version_from_rpm(string): length = len(string) release_pos = -1 version = "" - for c in range(length-1, -1, -1): + for c in range(length - 1, -1, -1): if string[c] == '-': if release_pos == -1: release_pos = c else: - version = string[c+1:release_pos] + version = string[c + 1:release_pos] if version != "": break @@ -333,6 +353,7 @@ def get_version_from_rpm(string): return version + def check_mysql(version): log_it(logging.INFO, 'Checking mysql version') mysql_install = False @@ -365,6 +386,7 @@ def check_mysql(version): log_it(logging.INFO, "Install mysql version is higher than anolis mysql version, cannot migrate to anolis.") sys.exit(1) + def process_yum_utils_install(internal_ecs): log_it(logging.INFO, "Process yum-utils install") yum_utils_install = False @@ -403,10 +425,12 @@ enabled=1 if not check_pkg('yumdownloader'): clean_and_exit() + def log_it(level=logging.INFO, msg=""): print(msg) logging.log(level, msg) + def set_log_info(): logging.basicConfig( filename='/var/log/centos2anolis.log', @@ -415,6 +439,7 @@ def set_log_info(): datefmt="%Y-%m-%d %H:%M:%S" ) + def check_user(): log_it(logging.INFO, "Checking if the tool is executed by root user") # check if the script is executed by root user @@ -422,6 +447,7 @@ def check_user(): log_it(logging.ERROR, "Please run the tool as root user.") sys.exit(1) + def check_required_pkgs(): # check required packages log_it(logging.INFO, 'Checking required packages') @@ -430,9 +456,12 @@ def check_required_pkgs(): log_it(logging.ERROR, "Could not found " + pkg) sys.exit(1) + def check_i686_pkgs(): log_it(logging.INFO, 'Checking i686 packages') - i686_pkgs = subprocess.check_output('rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{VENDOR}\n" | grep CentOS | grep i686 | awk \'{print $1}\'', shell=True) + i686_pkgs = subprocess.check_output( + 'rpm -qa --qf "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} %{VENDOR}\n" | grep CentOS | grep i686 | awk \'{print $1}\'', + shell=True) i686_pkgs = str(i686_pkgs, 'utf-8') if len(i686_pkgs) != 0: log_it(logging.WARNING, "Anolis OS does not provide i686 packages, please remove them before migration.") @@ -440,6 +469,7 @@ def check_i686_pkgs(): log_it(logging.WARNING, i686_pkgs) sys.exit(1) + def verify_before(verify_all_rpms): if verify_all_rpms: log_it(logging.INFO, "Creating a list of RPMs installed before the switch") @@ -447,14 +477,16 @@ def verify_before(verify_all_rpms): out1 = subprocess.check_output('rpm -qa --qf \ "%{NAME}|%{VERSION}|%{RELEASE}|%{INSTALLTIME}|%{VENDOR}|%{BUILDTIME}|%{BUILDHOST}|%{SOURCERPM}|%{LICENSE}|%{PACKAGER}\\n" \ | sort > "/var/tmp/$(hostname)-rpms-list-before.log"', shell=True) - out2 = subprocess.check_output('rpm -Va | sort -k3 > "/var/tmp/$(hostname)-rpms-verified-before.log"',shell=True) + out2 = subprocess.check_output('rpm -Va | sort -k3 > "/var/tmp/$(hostname)-rpms-verified-before.log"', + shell=True) files = os.listdir('/var/tmp/') hostname = socket.gethostname() log_it(logging.INFO, "Review the output of following files:") for f in files: - if re.match(hostname+'-rpms-(.*)\.log', f): + if re.match(hostname + '-rpms-(.*)\.log', f): log_it(logging.INFO, f) + def check_distribution(): global old_release_pkg @@ -467,7 +499,8 @@ def check_distribution(): log_it(logging.ERROR, "You appear to be running an unsupported distribution.") sys.exit(1) if len(old_version) > 1: - log_it(logging.ERROR,"Could not determine your distribution because multiple packages are providing redhat-release:") + log_it(logging.ERROR, + "Could not determine your distribution because multiple packages are providing redhat-release:") log_it(logging.ERROR, '\n'.join(old_version)) sys.exit(1) @@ -487,10 +520,11 @@ def check_distribution(): log_it(logging.ERROR, "Your are using an unsupported distribution.") sys.exit(1) - if not re.match(str(centosversion),subver): + if not re.match(str(centosversion), subver): log_it(logging.ERROR, "You appear to be running an unsupported distribution.") sys.exit(1) + def check_yum_lock(): log_it(logging.INFO, "Checking: yum lock") if os.path.exists('/var/run/yum.pid'): @@ -503,6 +537,7 @@ def check_yum_lock(): log_it(logging.ERROR, 'Please kill it and run the tool again.') sys.exit(1) + def check_dnf_modules(): if centosversion != 8: return @@ -529,16 +564,17 @@ def check_dnf_modules(): unknown_mods.append(mod) if len(unknown_mods) > 0: log_it(logging.WARNING, 'This tool is unable to automatically switch module(s) ' \ - + ','.join(unknown_mods) \ - + ' from a CentOS \'rhel\' stream to an Anolis OS equivalent.' \ - ) + + ','.join(unknown_mods) \ + + ' from a CentOS \'rhel\' stream to an Anolis OS 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) + sys.exit(1) + def check_network(): global internal_ecs @@ -554,11 +590,13 @@ def check_network(): log_it(logging.ERROR, err) pass + def check_yumdownloader(): log_it(logging.INFO, "Looking for yumdownloader") if not check_pkg('yumdownloader'): process_yum_utils_install(internal_ecs) + def check_repodir(): global reposdir @@ -574,6 +612,7 @@ def check_repodir(): log_it(logging.ERROR, "Could not locate your repository directory.") sys.exit(1) + def process_third_repos(): log_it(logging.INFO, "Processing contentdir in repo files...") subprocess.run("sed -i \'s/\$contentdir/centos/g\' /etc/yum.repos.d/*.repo", shell=True) @@ -582,20 +621,28 @@ def process_third_repos(): subprocess.run("mkdir /etc/yum.repos.d/migration-bak", shell=True) subprocess.run("mv $(find /etc/yum.repos.d/*.repo) /etc/yum.repos.d/migration-bak", shell=True) + def write_switch_repo(accelerate): repofile = os.path.join(reposdir, 'switch-to-anolis.repo') with open(repofile, 'w') as f: if centosversion == 7: - repostr_an=repostr_an7 + repostr_an = repostr_an7 else: - repostr_an=repostr_an8 + repostr_an = repostr_an8 f.write(repostr_an) if accelerate: change_repo_mirror(reposdir, 'switch-to-anolis.repo') if internal_ecs: - subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', shell=True) + subprocess.run( + 'sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', + shell=True) + subprocess.run( + 'sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', + shell=True) + subprocess.run( + 'sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/switch-to-anolis.repo', + shell=True) + def modify_yum_conf(): log_it(logging.INFO, "Removing CentOS-specific yum configuration from /etc/yum.conf ...") @@ -608,10 +655,11 @@ def modify_yum_conf(): with open('/etc/yum.conf', 'w') as f: f.write(content) + def download_anolis_release(): log_it(logging.INFO, "Downloading Anolis OS release package...") try: - stat = subprocess.check_output("yumdownloader "+' '.join(dst_release), shell=True) + stat = subprocess.check_output("yumdownloader " + ' '.join(dst_release), shell=True) except Exception: log_it(logging.ERROR, "Could not download the following packages from " + yum_url) log_it(logging.ERROR, '\n'.join(dst_release)) @@ -619,11 +667,12 @@ def download_anolis_release(): log_it(logging.ERROR, "Are you behind a proxy? If so, make sure the 'http_proxy' environmen") log_it(logging.ERROR, "variable is set with your proxy address.") log_it(logging.ERROR, "An error occurred while attempting to switch this system to Anolis OS" + \ - "and it may be in an unstable/unbootable state. To avoid further issues, " +\ - "the script has terminated.") + "and it may be in an unstable/unbootable state. To avoid further issues, " + \ + "the script has terminated.") clean_and_exit() -def switch_release_pkgs(): + +def switch_release_pkgs(local_flag): log_it(logging.INFO, "Switching old release package with Anolis OS...") dst_rpms = [s + '*.rpm' for s in dst_release] subprocess.run('rpm -i --force ' + ' '.join(dst_rpms) + ' --nodeps', shell=True) @@ -631,19 +680,27 @@ def switch_release_pkgs(): subprocess.run('rpm -e --nodeps ' + old_release_pkg, shell=True) else: subprocess.run('rpm -e --nodeps ' + old_release_pkg + ' centos-gpg-keys', shell=True) - os.remove(os.path.join(reposdir, 'switch-to-anolis.repo')) + if not local_flag: + os.remove(os.path.join(reposdir, 'switch-to-anolis.repo')) + else: + local_disabled_release_repo() + def optimise_repofile(accelerate): if accelerate: change_repo_mirror(reposdir, 'AnolisOS-') if internal_ecs: - subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) - subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', shell=True) + subprocess.run('sed -i \'s/mirrors.openanolis.cn/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', + shell=True) + subprocess.run('sed -i \'s/mirrors.openanolis.org/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', + shell=True) + subprocess.run('sed -i \'s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g\' /etc/yum.repos.d/*.repo', + shell=True) + def install_base_pkgs(): - base_packages=['basesystem','initscripts','anolis-logos','plymouth','grub2','grubby'] + base_packages = ['basesystem', 'initscripts', 'anolis-logos', 'plymouth', 'grub2', 'grubby'] log_it(logging.INFO, "Installing base packages for Anolis OS...") subprocess.run('yum clean all', shell=True) @@ -658,6 +715,7 @@ EOF' log_it(logging.INFO, "Updating initrd...") subprocess.run('/usr/libexec/plymouth/plymouth-update-initrd') + def process_enabled_modules(): if centosversion != 8: return @@ -674,13 +732,13 @@ def process_enabled_modules(): if len(enabled_modules) > 0: for mod in enabled_modules: - subprocess.run('dnf module reset -y '+mod, shell=True) + 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) + subprocess.run('dnf module enable -y ' + mod + ':an8', shell=True) + elif mod == 'virt': + subprocess.run('dnf module enable -y ' + mod + ':an', shell=True) else: - log_it(logging.WARNING, "Unsure how to transform module"+mod) + log_it(logging.WARNING, "Unsure how to transform module" + mod) subprocess.run('dnf -y distro-sync', shell=True) try: @@ -690,18 +748,20 @@ def process_enabled_modules(): except: pass + def remove_yum_cache(): log_it(logging.INFO, "Removing yum cache") if os.path.isfile('/var/cache/yum'): os.remove('/var/cache/yum') elif os.path.isdir('/var/cache/yum'): shutil.rmtree('/var/cache/yum') - if centosversion ==8: + 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') + def verify_after(verify_all_rpms=False): if verify_all_rpms: log_it(logging.INFO, "Creating a list of RPMs installed after the switch") @@ -709,14 +769,16 @@ def verify_after(verify_all_rpms=False): out1 = subprocess.check_output('rpm -qa --qf \ "%{NAME}|%{VERSION}|%{RELEASE}|%{INSTALLTIME}|%{VENDOR}|%{BUILDTIME}|%{BUILDHOST}|%{SOURCERPM}|%{LICENSE}|%{PACKAGER}\\n" \ | sort > "/var/tmp/$(hostname)-rpms-list-after.log"', shell=True) - out2 = subprocess.check_output('rpm -Va | sort -k3 > "/var/tmp/$(hostname)-rpms-verified-after.log"',shell=True) + out2 = subprocess.check_output('rpm -Va | sort -k3 > "/var/tmp/$(hostname)-rpms-verified-after.log"', + shell=True) files = os.listdir('/var/tmp/') hostname = socket.gethostname() log_it(logging.INFO, "Review the output of following files:") for f in files: - if re.match(hostname+'-rpms-(.*)\.log', f): + if re.match(hostname + '-rpms-(.*)\.log', f): log_it(logging.INFO, f) + def update_grub_cfg(): log_it(logging.INFO, "Sync successfully, update grub.cfg.") if os.path.isdir('/sys/firmware/efi'): @@ -732,15 +794,18 @@ def update_grub_cfg(): except: pass + def resume_third_repos(): log_it(logging.INFO, "Resume 3rd-part repository files...") subprocess.run("mv /etc/yum.repos.d/migration-bak/* /etc/yum.repos.d", shell=True) subprocess.run("rm -rf /etc/yum.repos.d/migration-bak", shell=True) + def reset_yum_cache(): subprocess.run("yum clean all", shell=True) subprocess.run("yum makecache", shell=True) + def do_migration(): try: subprocess.check_call('yum update libdnf -y', shell=True) @@ -748,13 +813,49 @@ def do_migration(): except: log_it(logging.ERROR, "Could not automatically sync with Anolis OS repositories.\n\ Check the output of 'yum distro-sync' to manually resolve the issue.") - log_it(logging.ERROR, "After you resolve the issue, Please use 'python3 centos2anolis.py -c ' to continue migration") + log_it(logging.ERROR, + "After you resolve the issue, Please use 'python3 centos2anolis.py -c ' to continue migration") sys.exit(1) -def main(verify_all_rpms=False, accelerate=False, version=False, continue_after_sync=False): +def local_disabled_release_repo(): + path = '/etc/yum.repos.d' + if os.path.exists(path): + file_list = os.listdir(path) + for file in file_list: + fpath = os.path.join(path, file) + if os.path.isdir(fpath): + continue + else: + if re.fullmatch('switch-to-anolis.repo', file, re.IGNORECASE): + continue + elif not re.search('repo$', file, re.IGNORECASE): + continue + with open(fpath, 'r') as fdst: + allrepo = fdst.read() + fdst.close() + with open(fpath + '.disabled', 'w+') as fdst: + fdst.write( + '#This is a yum repository file that was disabled . \n' + allrepo) + fdst.close() + os.remove(fpath) + + +def check_local_repo(localrepo): + if not localrepo: + return + repopath = '/etc/yum.repos.d/switch-to-anolis.repo' + if os.path.exists(repopath): + return + else: + log_it(logging.ERROR, "Please write the file of the anolis repository: /etc/yum.repos.d/switch-to-anolis.repo") + sys.exit(1) + + +def main(verify_all_rpms=False, accelerate=False, version=False, continue_after_sync=False, local_repo=False): set_log_info() log_it(logging.INFO, "====== Start ======") check_user() + check_local_repo(local_repo) get_current_version(continue_after_sync) if not continue_after_sync: @@ -769,12 +870,13 @@ def main(verify_all_rpms=False, accelerate=False, version=False, continue_after_ check_yumdownloader() check_mysql(version) check_repodir() - backup_repofiles() - process_third_repos() - write_switch_repo(accelerate) + if not local_repo: + backup_repofiles() + process_third_repos() + write_switch_repo(accelerate) modify_yum_conf() download_anolis_release() - switch_release_pkgs() + switch_release_pkgs(local_repo) optimise_repofile(accelerate) install_base_pkgs() process_pkgs_for_c7() @@ -791,12 +893,14 @@ def main(verify_all_rpms=False, accelerate=False, version=False, continue_after_ log_it(logging.INFO, "Switch complete. Anolis OS recommends rebooting this system.") + if __name__ == "__main__": parser = argparse.ArgumentParser() 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 Anolis OS') parser.add_argument('-c', action='store_true', help='Continue to migration after distro-sync') + parser.add_argument('-l', action='store_true', help='Select the local repo file') args = parser.parse_args() - sys.exit(main(args.V, args.s, args.v, args.c)) + sys.exit(main(args.V, args.s, args.v, args.c, args.l)) diff --git a/centos2anolis.spec b/centos2anolis.spec index aeb285be0e4c51695e877cc454f7a729a36bdf0e..1d30e19afbd61520e481b220e8611930408b0193 100644 --- a/centos2anolis.spec +++ b/centos2anolis.spec @@ -1,4 +1,4 @@ -%define anolis_release 4 +%define anolis_release 5 Name: centos2anolis Version: 0.1 @@ -39,6 +39,9 @@ install -m 755 %{SOURCE0} %{buildroot}/%{_sbindir} %changelog +* Thu Apr 14 2022 XueZhixin - 0.1-5 +- Add offline migration + * Fri Mar 25 2022 mgb0110571 - 0.1-4 - fix module virt