zoukankan      html  css  js  c++  java
  • linux系统qcow2镜像的制作

    一.简单说明

    PS: 最近有客户需要在云平台上部署我们的产品,好久没有做过qcow2镜像了,这里还是把制作流程记录下来。
    QCOW2镜像格式是Qemu支持的磁盘镜像格式之一。它可以使用一个文件来表示一个固定大小的块设备。与Raw镜像格式相比,QCOW2具有如下优点:

    • 更小的文件大小,即便不支持holes(稀疏文件)的文件系统同样适用
    • 支持写时拷贝(COW, Copy-on-write),QCOW2镜像只反映底层磁盘镜像所做的修改
    • 支持快照,QCOW2镜像可以包含镜像历史的多重快照
    • 支持基于zlib的数据压缩
    • 支持AES加密

    二.制作Qcow2镜像环境部署

    2.1 基础ISO

    这里,我们安装centos7.5环境即可。

    2.2软件安装:

    [root@qcow2-build ~]# yum install qemu-kvm qemu-img virt-manager libvirt libvirt-python python-virtinst libvirt-client virt-install virt-viewer
    

    启用libvirtd服务与设置开机启动

    [root@qcow2-build ~]# systemctl start libvirtd
    [root@qcow2-build ~]# systemctl enable libvirtd
    

    查看kvm是否安装

    [root@qcow2-build ~]# lsmod |grep kvm
    kvm_intel             188688  14 
    kvm                   636969  1 kvm_intel
    irqbypass              13503  1 kvm
    

    2.3 配置网络

    # 修改网卡的配置文件如下
    [root@qcow2-build network-scripts]# cat ifcfg-enp1s0 
    TYPE=Ethernet
    BRIDGE=br0
    BOOTPROTO=none
    NAME=enp1s0
    DEVICE=enp1s0
    ZONE=public
    # 创建br0,配置如下
    [root@qcow2-build network-scripts]# cat ifcfg-br0 
    TYPE=Bridge
    BOOTPROTO=static
    NAME=br0
    DEVICE=br0
    ONBOOT=yes
    IPADDR=172.16.30.22
    NETMASK=255.255.255.0
    GATEWAY=172.16.30.254
    DNS1=8.8.8.8
    ZONE=public
    

    三.制作qcow2镜像

    3.1 编写制作qcow2镜像的python脚本

    [root@qcow2-build ~]# cat create_qcow2_by_iso.py
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @Author         : yuhaohao
    # @Email          : yuhaohaozz@163.com
    # @Time           : 2020/06/09 9:20
    # @Version        : 1.0
    
    import sys
    import subprocess
    import random
    import string
    import time
    
    isofile=str(sys.argv[1])
    
    try:
        hostinfo = {}
        hostinfo['name'] = 
        subprocess.check_output("echo %s|sed -r 's/(.*yuhaohao_)(.*)(_x86.*)/\2/'" % isofile, shell=True).split('
    ')[0]
        hostinfo['nic'] = '00:00:00:00:00:' + str(sys.argv[2])
        # 定义VNC的端口如5928
        hostinfo['vncport'] = '59' + str(sys.argv[2])
        # QCOW2镜像的名称
        hostinfo['disk'] = hostinfo['name'] + '.qcow2'
        # 内存配置4G
        hostinfo['ram'] = 4096
        # CPU核数
        hostinfo['vcpus'] = 2
        # 系统磁盘大小
        hostinfo['disksize'] = '290G'
        hostinfo['isofile'] = isofile
        hostinfo['vncpassword'] = ''.join(random.sample(string.ascii_letters + string.digits, 12))
    except Exception as e:
        print('we have exception:' + e)
        exit(2)
    print(hostinfo)
    
    
    def create_vm():
        # QCOW2镜像存放在/home/yuhaohao目录
        subprocess.call('qemu-img create -f qcow2 /home/yuhaohao/{disk} {disksize}'.format(**hostinfo), shell=True)
        subprocess.call('virt-install --virt-type kvm 
    --name {name} 
    --os-variant rhel7 
    --ram {ram} 
    --cdrom={isofile} 
    -m {nic} 
    --autostart 
    --vcpus {vcpus}  
    --disk=/kvm/{disk} 
    --graphics vnc,listen=0.0.0.0,port={vncport},keymap=en-us 
    --network bridge=br0 
    --noautoconsole'.format(**hostinfo), shell=True)
    
    
    if __name__ == '__main__':
        create_vm()
        print('Your vnc port is {vncport}'.format(**hostinfo))
    

    3.2 制作qcow2镜像

    这里我们基于现有的kubernetes集群封装的ISO镜像制作qcow2格式的镜像:

    • 制作qcow2镜像
    [root@qcow2-build ~]# chmod -R 777 create_qcow2_by_iso.py
    [root@qcow2-build ~]# ./create_qcow2_by_iso.py oss_1.6.0.0_x86_64_20200608.iso 29
    

    这里通过vnc viewer采用http://${IP}:5929连接查看安装进度,安装完成后,系统会自动关机。

    • 修改配置
    [root@qcow2-build ~]# virsh start oss_1.6.0.0_x86_64_20200608.iso
    # 修改/etc/default/grub
    [root@oss ~]# vi /etc/default/grub
    # 将biosdevname=1 改为了net.ifnames=0 biosdevname=0
    GRUB_CMDLINE_LINUX="console=ttyS0,115200n8 console=tty0 net.ifnames=0 biosdevname=0 crashkernel=auto rd.luks.uuid=luks-13dadde4b-das3-43b0-d8fe-ed322a62c6b8
    [root@oss ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
    DEVICE=eth0
    BOOTPROTO=dhcp
    ONBOOT=yes
    ......
    [root@oss ~]# systemctl restart network 
    # 重新配置grub2
    [root@oss ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
    # 关机
    [root@oss ~]# shutdown -h 0
    
    • 获取qcow2镜像
    [root@qcow2-build ~]# ls /home/yuhaohao/oss_1.6.0.0_x86_64_20200608.iso.qcow2 -alh
    -rw-r--r-- 1 qemu qemu 197K Jun  9 15:27 /home/yuhaohao/oss_1.6.0.0_x86_64_20200608.iso.qcow2
    
  • 相关阅读:
    【HDOJ】2267 How Many People Can Survive
    【HDOJ】2268 How To Use The Car
    【HDOJ】2266 How Many Equations Can You Find
    【POJ】2278 DNA Sequence
    【ZOJ】3430 Detect the Virus
    【HDOJ】2896 病毒侵袭
    求奇数的乘积
    平方和与立方和
    求数列的和
    水仙花数
  • 原文地址:https://www.cnblogs.com/yuhaohao/p/13071612.html
Copyright © 2011-2022 走看看