01-centos离线升级至almalinux

  • 官网链接

  • 官方代码调整:

    1. vi repositories/system_upgrade/common/actors/targetuserspacecreator/libraries/userspacegen.py
      with mounting.BindMount(source=userspace_dir, target=os.path.join(context.base_dir, install_root_dir.lstrip('/'))):
      _restore_persistent_package_cache(userspace_dir)
      if not is_nogpgcheck_set():
      _import_gpg_keys(context, install_root_dir, target_major_version)

      repos_opt = [['--enablerepo', repo] for repo in enabled_repos]
      repos_opt = list(itertools.chain(repos_opt))
      cmd = ['dnf', 'install', '-y']
      if is_nogpgcheck_set():
      cmd.append('--nogpgcheck')
      cmd += [
      '--setopt=module_platform_id=platform:el{}'.format(target_major_version),
      '--setopt=keepcache=1',
      '--releasever', api.current_actor().configuration.version.target,
      '--installroot', install_root_dir,
      '--disablerepo', '
      ',
      '--enablerepo', UPGRADE_REPO_NAME
      ] + repos_opt + packages

    2. repositories/system_upgrade/common/libraries/dnfplugin.py
      def _prepare_transaction(used_repos, target_userspace_info, binds=()):
      """ Creates the transaction environment needed for the target userspace DNF execution """
      target_repoids = set([UPGRADE_REPO_NAME])
      try:
      dest = '/var/lib/leapp/el8userspace' + UPGRADE_DIR
      shutil.copytree(UPGRADE_DIR, dest)
      except Exception as e:
      print(e)
      with mounting.NspawnActions(base_dir=target_userspace_info.path, binds=binds) as context:
      yield context, list(target_repoids), target_userspace_info

  • 离线升级脚本

    #!/bin/bash

    alma8_upgraded_option() {
    rpm -qa | grep el7 | xargs rpm -e --nodeps &>/dev/null
    rpm -qa | grep elevate | xargs rpm -e --nodeps &>/dev/null
    rpm -qa | grep leapp | xargs rpm -e --nodeps &>/dev/null
    rm -fr /root/tmp_leapp_py3
    dnf clean all
    rpm -q gpg-pubkey --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n' | awk '{print $1}' | xargs rpm -e
    sed -i '/^exclude=python2-leapp/ s/^/#/' /etc/yum.conf
    sed -i '/^exclude=python2-leapp/ s/^/#/' /etc/dnf/dnf.conf
    }

    install_leapp() {
    # leapp软件下载
    leapp_install=cat ${LOGS_FILE_PATH} | grep "${VERSION_TO_UPGRADE}_leapp_install=true"
    if [ -z "leapp_install" ];then yum localinstall -y {PACKAGES_DIR}/elevate-release-latest-{ELEVATE_VERSION}.noarch.rpm find /etc/yum.repos.d/ ! -name "local.repo" -type f | xargs rm -f yum localinstall -y {PACKAGES_DIR}/leapp/*.rpm
    if [ ? -eq 0 ]; then echo "{VERSION_TO_UPGRADE}_leapp_install=true" >> ${LOGS_FILE_PATH}
    fi
    fi
    }

    reboot_confirm() {
    reboot_option=cat ${LOGS_FILE_PATH} | grep "${VERSION_TO_UPGRADE}_${1}_reboot=true"
    if [ -z "reboot_option" ];then read -p "Please confirm whether to restart the machine[y/n]:" option case option in
    y)
    echo "Start reboot"
    echo "{VERSION_TO_UPGRADE}_{1}_reboot=true" >> ${LOGS_FILE_PATH}
    reboot
    ;;
    n)
    echo "Cancel reboot"
    ;;
    *)
    echo "Please input [y/n]"
    ;;
    esac
    fi
    }

    generate_local_repo (){
    case {VERSION_TO_UPGRADE} in centos7_to_alma8) cat < /etc/yum.repos.d/local.repo [{PREPARE_REPO_NAME}]
    name={PREPARE_REPO_NAME} Repository baseurl=file://{PREPARE_DIR}
    enabled=1
    gpgcheck=0

    [{UPGRADE_REPO_NAME}] name={UPGRADE_REPO_NAME} Repository
    baseurl=file://${UPGRADE_DIR}
    enabled=1
    gpgcheck=0
    EOF
    ;;

    复制代码
      alma8_to_alma9)

    cat <<EOF > /etc/yum.repos.d/local.repo
    [{UPGRADE_REPO_NAME}] name={UPGRADE_REPO_NAME} Repository
    baseurl=file://${UPGRADE_DIR}
    enabled=1
    gpgcheck=0
    EOF
    ;;
    *)
    exit 0
    ;;
    esac
    }

    当前机器的版本

    CURRENT_OS_VERSION=cat /etc/os-release | grep PRETTY_NAME= | tr '[:upper:]' '[:lower:]'

    if [[ "CURRENT_OS_VERSION" == *"centos"* ]];then VERSION_TO_UPGRADE=centos7_to_alma8 ELEVATE_VERSION=el7 elif [[ "CURRENT_OS_VERSION" == "almalinux 8" ]];then
    VERSION_TO_UPGRADE=alma8_to_alma9
    ELEVATE_VERSION=el8
    fi

    BASE_DIR=(cd "(dirname "0")";pwd) LOGS_DIR={BASE_DIR}/logs
    LOGS_FILE_PATH={LOGS_DIR}/history PACKAGES_DIR={BASE_DIR}/{VERSION_TO_UPGRADE} PREPARE_REPO_NAME=prepare UPGRADE_REPO_NAME=upgrade PREPARE_DIR={PACKAGES_DIR}/{PREPARE_REPO_NAME} UPGRADE_DIR={PACKAGES_DIR}/${UPGRADE_REPO_NAME}

    配置本地仓库

    if [ ! -d "{PACKAGES_DIR}/yum.repos.d" ]; then mv /etc/yum.repos.d {PACKAGES_DIR}/
    mkdir -p /etc/yum.repos.d
    fi

    generate_local_repo

    解压rpm包

    mkdir -p {PACKAGES_DIR} if [ -f "{VERSION_TO_UPGRADE}.tar.gz" ];then
    echo "Start tar xf {BASE_DIR}/{VERSION_TO_UPGRADE}.tar.gz"
    rm -rf {PACKAGES_DIR}/* tar xf {BASE_DIR}/{VERSION_TO_UPGRADE}.tar.gz -C {PACKAGES_DIR}
    else
    echo "please check ${VERSION_TO_UPGRADE}.tar.gz exists?"
    exit 0
    fi

    mkdir -p {LOGS_DIR} touch {LOGS_FILE_PATH}

    升级前处理

    case {VERSION_TO_UPGRADE} in centos7_to_alma8) # 软件升级 yum_upgrade=`cat {LOGS_FILE_PATH} | grep "{VERSION_TO_UPGRADE}_yum_upgrade=true"` if [ -z "yum_upgrade" ];then
    yum upgrade -y --disablerepo=* --enablerepo={PREPARE_REPO_NAME} if [ ? -eq 0 ]; then
    echo "{VERSION_TO_UPGRADE}_yum_upgrade=true" >> {LOGS_FILE_PATH}
    fi
    fi
    # 重启
    reboot_confirm yum_upgrade
    # 安装leapp
    install_leapp
    # 生成 answerfile
    cat <<EOF > /var/log/leapp/answerfile
    [remove_pam_pkcs11_module_check]
    confirm = True
    EOF
    # 卸载内核模块pata_acpi
    rmmod pata_acpi
    # 修改sshd_config,允许root密码登录
    if ! grep -q "^PermitRootLogin yes" /etc/ssh/sshd_config; then
    sed -i '/^PermitRootLogin/ s/^/#/' /etc/ssh/sshd_config
    echo PermitRootLogin yes | tee -a /etc/ssh/sshd_config
    fi
    ;;

    alma8_to_alma9)
    # 升级前处理
    alma8_upgraded_option
    # 安装leapp
    install_leapp
    # 生成 answerfile
    cat <<EOF > /var/log/leapp/answerfile
    [check_vdo]
    confirm = True
    EOF
    # 修改sshd_config,禁止root密码登录
    if grep -q "^PermitRootLogin yes" /etc/ssh/sshd_config; then
    sed -i '/^PermitRootLogin/ s/^/#/' /etc/ssh/sshd_config
    echo PermitRootLogin no | tee -a /etc/ssh/sshd_config
    fi
    # 防火墙设置
    sed -i "s/^AllowZoneDrifting=.*/AllowZoneDrifting=no/" /etc/firewalld/firewalld.conf
    ;;
    *)
    exit 0
    ;;
    esac

    拷贝执行目录到目标路径

    echo UPGRADE_REPO_NAME={UPGRADE_REPO_NAME} > {PACKAGES_DIR}/{VERSION_TO_UPGRADE//_/-}-leapp-repository/leapp_upgrade.env echo UPGRADE_DIR={UPGRADE_DIR} >> {PACKAGES_DIR}/{VERSION_TO_UPGRADE///-}-leapp-repository/leapp_upgrade.env
    rm -rf /usr/share/leapp-repository
    cp -r {PACKAGES_DIR}/{VERSION_TO_UPGRADE//
    /-}-leapp-repository /usr/share/leapp-repository

    执行升级

    leapp upgrade

相关推荐
hujun861012 小时前
Ubuntu安装无线网卡
linux
Johny_Zhao12 小时前
CentOS Stream 8 高可用 Kuboard 部署方案
linux·网络·python·网络安全·docker·信息安全·kubernetes·云计算·shell·yum源·系统运维·kuboard
卖猪肉的痴汉14 小时前
1.1 Linux 编译FFmpeg 4.4.1
linux·ffmpeg
十五年专注C++开发14 小时前
Qt .pro配置gcc相关命令(三):-W1、-L、-rpath和-rpath-link
linux·运维·c++·qt·cmake·跨平台编译
qq_6285157614 小时前
Centos与RockLinux设置静态ip
linux·运维·centos
程序猿小D15 小时前
第27节 Node.js Buffer
linux·开发语言·vscode·node.js·c#·编辑器·vim
沧浪之水1201013716 小时前
linux常用命令
linux·运维·服务器
梦会实现16 小时前
无外接物理显示器的Ubuntu系统的远程桌面连接(升级版)
linux·运维·ubuntu·计算机外设
暗离子跃迁16 小时前
达梦数据库单机部署dmhs同步复制(dm8->kafka)
linux·运维·数据库·分布式·学习·kafka·达梦数据库
北城笑笑17 小时前
Server 11 ,⭐通过脚本在全新 Ubuntu 系统中安装 Nginx 环境,安装到指定目录( 脚本安装Nginx )
linux·运维·前端·nginx·ubuntu