aws EC2二代镜像迁移阿里云ecs磁盘user_config.json生成脚本

脚本如下,请自行修改access_id和secret_key:

import os
import json



template_dict = {
    "access_id": "YOUR-ID",
    "secret_key": "YOUR-SECRET_KEY",
    "sts_token": "",
    "region_id": "",
    "image_name": "",
    "platform": "",
    "disks":[],
    "enable_disks" : True,
    "bandwidth_limit": 0
}
data_result = os.popen(' df -h | grep "/dev/nvme"').readlines()
#print(type(data_result))
#print(len(data_result))
disk_list = []
for line in data_result:
    line=str(line).split("\n")[0]
    line_list = line.split()
    disk_str=line_list[0]
    mount_str=line_list[5]
    disk_size_search = disk_str.split('p')[0]  ## use to search by fdisk -l for disk size
    #print(disk_size_search)
    searh_str = "fdisk -l | grep Disk | grep -v Disklabel | grep -v identifier | grep "+disk_size_search+ " | awk -F\":|,|G\" '{print $2}'"
    disk_size = int(float(os.popen(searh_str).readlines()[0].split("\n")[0].strip())+1)
    #disk_size = int(os.popen(searh_str).readlines())
    #print("disk size %s" % disk_size)
    #disk_size=int(line_list[1].split('G')[0])
    #print(disk_str,disk_size,mount_str)
    disk_list_item_dict = {}  ## use to save disk info items for disks list
    parts = []  ## use to save partion
    for i in range(1,10):
        disk_check_str='nvme'+str(i-1)
        if disk_check_str in disk_str:
            disk_list_item_dict['index']=i-1
            if disk_size < 20:
                disk_size = 20
            disk_list_item_dict["size"]=disk_size
            for j in range(1,10):
                part_check_str = disk_check_str +"n1p" +str(j)
                part_check_str_all_disk = disk_check_str +"n1"## all disk has only on partiton
                if disk_str.endswith(part_check_str_all_disk):
                    parts_item_dict = {}
                    parts_item_dict["part_index"] = 0
                    parts_item_dict["part_path"]=mount_str
                    if len(parts_item_dict) > 0:
                        parts.append(parts_item_dict)
                        break
                if part_check_str in disk_str:
                    parts_item_dict = {}
                    parts_item_dict["part_index"]=j-1
                    parts_item_dict["part_path"]=mount_str
                    if len(parts_item_dict) > 0:
                        parts.append(parts_item_dict)
        if len(parts) > 0:
            disk_list_item_dict["parts"] = parts
    if len(disk_list_item_dict) > 0:
        #print(disk_list_item_dict)
        disk_list.append(disk_list_item_dict)

if len(disk_list) > 0:
    template_dict["disks"] = disk_list

print(template_dict)
with open('/migrate_20200624/go2aliyun_client2.3.0b_linux_x86_64/user_config.json','w') as f:
    json.dump(template_dict,f)

请根据自己情况做适当修改和测试

上一篇:oracle查看所有表及字段


下一篇:asp中的删除文件