Load offline image package: Online image offline is loaded as tar package, tar offline image package is loaded and tagged according to imageId

Step 1: Compress the offline image in the online environment:

Two files are required. The first is the script file image_offline_load.sh script, and the second is image_list.txt, which stores the image names that need to be offline by line.

./image_offline_load.sh save image_list.txt output.tar

Insert image description here

Step 2: Load the offline image in the offline environment and tag it:

Four files are required. The first is the compressed package output.tar generated in the first step. The second is the script file image_offline_load.sh script. The third is image_list.txt, which stores the image names that need to be offline by line. The fourth It is the imageid_list.txt generated in the first step to store the offline image ID.

./image_offline_load.sh load output.tar
./image_offline_load.sh tag image_list.txt imageid_list.txt

Insert image description here

The script file image_offline_load.sh is as follows, including save, load, and tag functions.

save <image_list_file> <output_tar_file> Save the image to a tar archive file"
load <input_tar_file> Load a tar archive file to the local"
tag <image_list_file> <imageid_list_file> tag local image"

#!/bin/bash

# 打印帮助信息
print_help() {
    
    
    echo "用法: $0 <操作> <参数1> [<参数2>]"
    echo "操作:"
    echo "  save <image_list_file> <output_tar_file>   保存镜像到 tar 归档文件"
    echo "  load <input_tar_file>                     加载 tar 归档文件到本地"
    echo "  tag <image_list_file>    <imageid_list_file>    对本地镜像打tag"
}

# 记录日志
log() {
    
    
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1"
}

# 参数数量检查
if [ "$#" -lt 2 ]; then
    log "错误:参数不足。"
    print_help
    exit 1
fi

# 参数1: save / load
operation="$1"

# 执行操作前的参数检查
if [ "$operation" == "save" ]; then
    if [ "$#" -ne 3 ]; then
        log "错误:'save' 操作需要两个参数。"
        print_help
        exit 1
    fi

    input_file="$2"
    output_tar="$3"

    # 检查文件是否存在
    if [ ! -f "$input_file" ]; then
        log "错误:文件 '$input_file' 不存在。"
        exit 1
    fi

    # 检查输出文件是否存在,如果存在先删除
    if [ -f "$output_tar" ]; then
        log "删除已存在的输出文件: $output_tar"
        rm "$output_tar"
    fi
   # 检查输出文件是否存在,如果存在先删除
    if [ -f "imageid_list.txt" ]; then
        log "删除已存在的镜像id文件: imageid_list.txt"
        rm "imageid_list.txt"
    fi
    while IFS= read -r line; do
        # 忽略空白行和以 '#' 开头的注释行
        if [ -z "$line" ] || [[ "$line" == \#* ]]; then
            continue
        fi
        image_name=$(echo "$line" | awk -F':' '{print $1}')
		image_tag=$(echo "$line" | awk -F':' '{print $2}')

		log "拉取镜像image_name: $image_name,image_tag:$image_tag"
       podman pull "$line"
        if [ $? -eq 0 ]; then
            log "拉取镜像[$line]成功"
        else
            log "拉取镜像[$line]失败,错误码: $?"
            exit $?
        fi
	image_tags=$(podman images | grep "$image_name" | awk '{print $2}')
	image_ids=$(podman images | grep "$image_name" | awk '{print $3}')
read -r -d '' -a image_tags_array <<< "$image_tags"
read -r -d '' -a image_ids_array <<< "$image_ids"
	images=()
	length=${
    
    #image_tags_array[@]}
for ((i=0; i<$length; i++))
do
    # 获取数组中的元素
    tag=${image_tags_array[i]}
    id=${image_ids_array[i]}
    log "tag: $tag
    id: $id"
    imageids+=("$id")
    if [ "$tag" == "$image_tag" ]; then
	        echo  $id >> imageid_list.txt
	        break
    	fi
    # 在这里插入处理标签和ID的代码
done

    cat imageid_list.txt
    done < "$input_file"

    log "保存镜像到 $output_tar"
    podman save -o "$output_tar" "${imageids[@]}"
    log "镜像已成功保存到 $output_tar。"

elif [ "$operation" == "load" ]; then
    if [ "$#" -ne 2 ]; then
        log "错误:'load' 操作需要一个参数。"
        print_help
        exit 1
    fi

    input_tar="$2"

    # 检查文件是否存在
    if [ ! -f "$input_tar" ]; then
        log "错误:文件 '$input_tar' 不存在。"
        exit 1
    fi

    # 获取当前 Podman 存放镜像的目录空间信息
    store_path=$(podman info --format '{
     
     {.Store.GraphRoot}}')
    available_space=$(df -k "$store_path" | awk 'NR==2 {print $4}')  # 获取可用空间,单位为 KB
 
    # 获取要加载的 tar 包大小
    tar_size=$(du -k "$input_tar" | awk '{print $1}')  # 获取 tar 包大小,单位为 KB
 
    # 检查是否有足够的空间容纳 tar 包
    if [ "$available_space" -lt "$tar_size" ]; then
        log "错误:存放 Podman 镜像的目录空间不足以容纳加载的 tar 包。"
        exit 1
    fi
 
    log "加载镜像: $input_tar"
    podman load -i "$input_tar"
    if [ $? -eq 0 ]; then
        log "加载镜像[$input_tar]成功"
    else
        log "加载镜像[$input_tar]失败,错误码: $?"
        exit $?
    fi
 
    log "镜像已成功加载。"



    #需要将镜像tag为
    #podman tag image.cestc.cn/iaas_pub/buildah:20231019 image.ccos.io/iaas_pub/buildah:20231019

elif [ "$operation" == "tag" ]; then
    if [ "$#" -ne 3 ]; then
        log "错误:'tag' 操作需要2个参数。"
        print_help
        exit 1
    fi
    image_list_file="$2"
    imageid_list_file="$3"
    while IFS= read -r line1 <&3 && IFS= read -r line2 <&4; do
    image=$(echo "$line1" | awk -F'/' '{print $3}')

    podman tag "$line2" image.ccos.io/iaas_pub/"$image"

done 3< "$image_list_file" 4< "$imageid_list_file"

else
    log "错误:无效的操作。"
    print_help
    exit 1
fi

Guess you like

Origin blog.csdn.net/qq_45808700/article/details/134864412