01-centos离线升级至almalinux

发布于:2025-05-16 ⋅ 阅读:(16) ⋅ 点赞:(0)
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 <<EOF > /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


网站公告

今日签到

点亮在社区的每一天
去签到