其他分享
首页 > 其他分享> > OpenStack 创建自定义的QCOW2格式镜像

OpenStack 创建自定义的QCOW2格式镜像

作者:互联网

一、安装KVM虚拟机

1.1 虚拟机安装虚拟化软件包

注意:虚拟机指的是CentOS7.8

#挂载光盘
[root@cloudcs ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@cloudcs ~]# ls /mnt/
CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL

#配置本地YUM源
[root@cloudcs ~]# mkdir /tmp/repo
[root@cloudcs ~]# mv /etc/yum.repos.d/*.repo /tmp/repo/
[root@cloudcs ~]# vim /etc/yum.repos.d/local.repo
[root@cloudcs ~]# cat /etc/yum.repos.d/local.repo 
[local_repo]
name = local
baseurl = file:///mnt/
gpgcheck = 0

#安装虚拟化软件包及虚拟机镜像管理工具
[root@cloudcs ~]# yum groupinstall -y "Virtualization*"
[root@cloudcs ~]# yum install -y libguestfs-tools-c.x86_64

1.2 创建KVM虚拟机

注意:虚拟机通过命令ifconfig可看到一张虚拟网卡vritbr0,该虚拟网卡为kvm虚拟机default默认的NAT网络类型网卡(192.168.122.1)

#创建目录上传ISO镜像
[root@cloudcs ~]# mkdir -p /soft/disk

#将CentOS7.4上传到 /soft 目录下
[root@cloudcs ~]# ls /soft/
CentOS-7-x86_64-DVD-1708.iso  disk

#创建kvm虚拟机使用的磁盘文件
[root@cloudcs ~]# qemu-img create -f qcow2 /soft/disk/centos7.qcow2 15G
Formatting '/soft/disk/centos7.qcow2', fmt=qcow2 size=16106127360 encryption=off cluster_size=65536 lazy_refcounts=off 

#创建kvm虚拟机
virt-install \
--name centos \
--disk path=/soft/disk/centos7.qcow2 \
--vcpus 1 \
--memory 2048 \
--location /soft/CentOS-7-x86_64-DVD-1708.iso \
--network network=default

1.3 安装KVM虚拟机

1.3.1 语言选择

1.3.2 自动分区

1.3.3 网络及主机名

注意:当开启网络后,必须可以通过 DHCP 自动获取到 IP 地址,如下图

1.3.4 配置 ROOT 密码

默认采用最小化 minimal 安装

1.3.5 重启并测试网络

重启kvm虚拟机,并以root身份登录,测试kvm虚拟机 能否连通外网

二、配置 KVM 虚拟机

2.1 修改KVM虚拟机网络配置文件

注意:修改的是kvm虚拟机,不是虚拟机,概念分不清?参考第一部分 约定

# 修改 kvm 虚机网卡配置文件,使其只包括以下 4 行内容
[root@local ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 
[root@local ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 

TYPE=Ethernet
BOOTPROTO=dhcp
DEVICE=eth0
ONBOOT=yes

2.2 安装 cloud-init 软件包

根据 1.3.5 小节测试,kvm 虚机可正常连通外网,此时直接在线安装软件包即可。目的是为了可以获取到客户自定义元数据信息,如主机名及root密码等。

[root@local ~]# yum install -y cloud-utils-growpart cloud-init

#输出

Loaded plugins: fastestmirror
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
(1/4): extras/7/x86_64/primary_db                          | 247 kB   00:00     
(2/4): base/7/x86_64/group_gz                              | 153 kB   00:00     
(3/4): updates/7/x86_64/primary_db                         |  16 MB   00:31     
(4/4): base/7/x86_64/primary_db                            | 6.1 MB   00:36     
Determining fastest mirrors
 * base: mirrors.cn99.com
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package cloud-init.x86_64 0:19.4-7.el7.centos.6 will be installed
--> Processing Dependency: python-six for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: python-setuptools for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: python-requests for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: python-prettytable for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: python-jsonpatch for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: python-jinja2 for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: pyserial for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: policycoreutils-python for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: net-tools for package: cloud-init-19.4-7.el7.centos.6.x86_64
--> Processing Dependency: PyYAML for package: cloud-init-19.4-7.el7.centos.6.x86_64
---> Package cloud-utils-growpart.noarch 0:0.29-5.el7 will be installed
--> Running transaction check
---> Package PyYAML.x86_64 0:3.10-11.el7 will be installed
--> Processing Dependency: libyaml-0.so.2()(64bit) for package: PyYAML-3.10-11.el7.x86_64
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
---> Package policycoreutils-python.x86_64 0:2.5-34.el7 will be installed
--> Processing Dependency: policycoreutils = 2.5-34.el7 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: setools-libs >= 3.3.8-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libsemanage-python >= 2.5-14 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: audit-libs-python >= 2.1.3-4 for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: python-IPy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.4)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1(VERS_1.2)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libcgroup for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4(VERS_4.0)(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: checkpolicy for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libqpol.so.1()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
--> Processing Dependency: libapol.so.4()(64bit) for package: policycoreutils-python-2.5-34.el7.x86_64
---> Package pyserial.noarch 0:2.6-6.el7 will be installed
---> Package python-jinja2.noarch 0:2.7.2-4.el7 will be installed
--> Processing Dependency: python-babel >= 0.8 for package: python-jinja2-2.7.2-4.el7.noarch
--> Processing Dependency: python-markupsafe for package: python-jinja2-2.7.2-4.el7.noarch
---> Package python-jsonpatch.noarch 0:1.2-4.el7 will be installed
--> Processing Dependency: python-jsonpointer for package: python-jsonpatch-1.2-4.el7.noarch
---> Package python-prettytable.noarch 0:0.7.2-3.el7 will be installed
---> Package python-requests.noarch 0:2.6.0-10.el7 will be installed
--> Processing Dependency: python-urllib3 >= 1.10.2-1 for package: python-requests-2.6.0-10.el7.noarch
--> Processing Dependency: python-chardet >= 2.2.1-1 for package: python-requests-2.6.0-10.el7.noarch
---> Package python-setuptools.noarch 0:0.9.8-7.el7 will be installed
--> Processing Dependency: python-backports-ssl_match_hostname for package: python-setuptools-0.9.8-7.el7.noarch
---> Package python-six.noarch 0:1.9.0-2.el7 will be installed
--> Running transaction check
---> Package audit-libs-python.x86_64 0:2.8.5-4.el7 will be installed
--> Processing Dependency: audit-libs(x86-64) = 2.8.5-4.el7 for package: audit-libs-python-2.8.5-4.el7.x86_64
---> Package checkpolicy.x86_64 0:2.5-8.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
---> Package libsemanage-python.x86_64 0:2.5-14.el7 will be installed
--> Processing Dependency: libsemanage = 2.5-14.el7 for package: libsemanage-python-2.5-14.el7.x86_64
---> Package libyaml.x86_64 0:0.1.4-11.el7_0 will be installed
---> Package policycoreutils.x86_64 0:2.5-17.1.el7 will be updated
---> Package policycoreutils.x86_64 0:2.5-34.el7 will be an update
--> Processing Dependency: libsepol >= 2.5-10 for package: policycoreutils-2.5-34.el7.x86_64
--> Processing Dependency: libselinux-utils >= 2.5-14 for package: policycoreutils-2.5-34.el7.x86_64
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package python-babel.noarch 0:0.9.6-8.el7 will be installed
---> Package python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7 will be installed
--> Processing Dependency: python-ipaddress for package: python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch
--> Processing Dependency: python-backports for package: python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch
---> Package python-chardet.noarch 0:2.2.1-3.el7 will be installed
---> Package python-jsonpointer.noarch 0:1.9-2.el7 will be installed
---> Package python-markupsafe.x86_64 0:0.11-10.el7 will be installed
---> Package python-urllib3.noarch 0:1.10.2-7.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Processing Dependency: libselinux >= 2.5-14.1 for package: setools-libs-3.3.8-4.el7.x86_64
--> Running transaction check
---> Package audit-libs.x86_64 0:2.7.6-3.el7 will be updated
--> Processing Dependency: audit-libs(x86-64) = 2.7.6-3.el7 for package: audit-2.7.6-3.el7.x86_64
---> Package audit-libs.x86_64 0:2.8.5-4.el7 will be an update
---> Package libselinux.x86_64 0:2.5-11.el7 will be updated
--> Processing Dependency: libselinux(x86-64) = 2.5-11.el7 for package: libselinux-python-2.5-11.el7.x86_64
---> Package libselinux.x86_64 0:2.5-15.el7 will be an update
---> Package libselinux-utils.x86_64 0:2.5-11.el7 will be updated
---> Package libselinux-utils.x86_64 0:2.5-15.el7 will be an update
---> Package libsemanage.x86_64 0:2.5-8.el7 will be updated
---> Package libsemanage.x86_64 0:2.5-14.el7 will be an update
---> Package libsepol.x86_64 0:2.5-6.el7 will be updated
---> Package libsepol.x86_64 0:2.5-10.el7 will be an update
---> Package python-backports.x86_64 0:1.0-8.el7 will be installed
---> Package python-ipaddress.noarch 0:1.0.16-2.el7 will be installed
--> Running transaction check
---> Package audit.x86_64 0:2.7.6-3.el7 will be updated
---> Package audit.x86_64 0:2.8.5-4.el7 will be an update
---> Package libselinux-python.x86_64 0:2.5-11.el7 will be updated
---> Package libselinux-python.x86_64 0:2.5-15.el7 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                          Arch   Version                  Repository
                                                                           Size
================================================================================
Installing:
 cloud-init                       x86_64 19.4-7.el7.centos.6      updates 933 k
 cloud-utils-growpart             noarch 0.29-5.el7               base     27 k
Installing for dependencies:
 PyYAML                           x86_64 3.10-11.el7              base    153 k
 audit-libs-python                x86_64 2.8.5-4.el7              base     76 k
 checkpolicy                      x86_64 2.5-8.el7                base    295 k
 libcgroup                        x86_64 0.41-21.el7              base     66 k
 libsemanage-python               x86_64 2.5-14.el7               base    113 k
 libyaml                          x86_64 0.1.4-11.el7_0           base     55 k
 net-tools                        x86_64 2.0-0.25.20131004git.el7 base    306 k
 policycoreutils-python           x86_64 2.5-34.el7               base    457 k
 pyserial                         noarch 2.6-6.el7                base    124 k
 python-IPy                       noarch 0.75-6.el7               base     32 k
 python-babel                     noarch 0.9.6-8.el7              base    1.4 M
 python-backports                 x86_64 1.0-8.el7                base    5.8 k
 python-backports-ssl_match_hostname
                                  noarch 3.5.0.1-1.el7            base     13 k
 python-chardet                   noarch 2.2.1-3.el7              base    227 k
 python-ipaddress                 noarch 1.0.16-2.el7             base     34 k
 python-jinja2                    noarch 2.7.2-4.el7              base    519 k
 python-jsonpatch                 noarch 1.2-4.el7                base     15 k
 python-jsonpointer               noarch 1.9-2.el7                base     13 k
 python-markupsafe                x86_64 0.11-10.el7              base     25 k
 python-prettytable               noarch 0.7.2-3.el7              base     37 k
 python-requests                  noarch 2.6.0-10.el7             base     95 k
 python-setuptools                noarch 0.9.8-7.el7              base    397 k
 python-six                       noarch 1.9.0-2.el7              base     29 k
 python-urllib3                   noarch 1.10.2-7.el7             base    103 k
 setools-libs                     x86_64 3.3.8-4.el7              base    620 k
Updating for dependencies:
 audit                            x86_64 2.8.5-4.el7              base    256 k
 audit-libs                       x86_64 2.8.5-4.el7              base    102 k
 libselinux                       x86_64 2.5-15.el7               base    162 k
 libselinux-python                x86_64 2.5-15.el7               base    236 k
 libselinux-utils                 x86_64 2.5-15.el7               base    151 k
 libsemanage                      x86_64 2.5-14.el7               base    151 k
 libsepol                         x86_64 2.5-10.el7               base    297 k
 policycoreutils                  x86_64 2.5-34.el7               base    917 k

Transaction Summary
================================================================================
Install  2 Packages (+25 Dependent packages)
Upgrade             (  8 Dependent packages)

Total download size: 8.2 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/base/packages/PyYAML-3.10-11.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for PyYAML-3.10-11.el7.x86_64.rpm is not installed
(1/35): PyYAML-3.10-11.el7.x86_64.rpm                      | 153 kB   00:00     
(2/35): audit-libs-python-2.8.5-4.el7.x86_64.rpm           |  76 kB   00:00     
(3/35): cloud-utils-growpart-0.29-5.el7.noarch.rpm         |  27 kB   00:00     
(4/35): audit-2.8.5-4.el7.x86_64.rpm                       | 256 kB   00:00     
(5/35): audit-libs-2.8.5-4.el7.x86_64.rpm                  | 102 kB   00:00     
(6/35): libcgroup-0.41-21.el7.x86_64.rpm                   |  66 kB   00:00     
(7/35): libselinux-utils-2.5-15.el7.x86_64.rpm             | 151 kB   00:00     
(8/35): libselinux-python-2.5-15.el7.x86_64.rpm            | 236 kB   00:00     
(9/35): libselinux-2.5-15.el7.x86_64.rpm                   | 162 kB   00:00     
(10/35): libsemanage-2.5-14.el7.x86_64.rpm                 | 151 kB   00:00     
(11/35): checkpolicy-2.5-8.el7.x86_64.rpm                  | 295 kB   00:00     
(12/35): libyaml-0.1.4-11.el7_0.x86_64.rpm                 |  55 kB   00:00     
(13/35): libsemanage-python-2.5-14.el7.x86_64.rpm          | 113 kB   00:00     
(14/35): libsepol-2.5-10.el7.x86_64.rpm                    | 297 kB   00:00     
(15/35): net-tools-2.0-0.25.20131004git.el7.x86_64.rpm     | 306 kB   00:00     
Public key for cloud-init-19.4-7.el7.centos.6.x86_64.rpm is not installed03 ETA 
(16/35): cloud-init-19.4-7.el7.centos.6.x86_64.rpm         | 933 kB   00:01     
(17/35): policycoreutils-python-2.5-34.el7.x86_64.rpm      | 457 kB   00:00     
(18/35): python-IPy-0.75-6.el7.noarch.rpm                  |  32 kB   00:00     
(19/35): python-backports-1.0-8.el7.x86_64.rpm             | 5.8 kB   00:00     
(20/35): policycoreutils-2.5-34.el7.x86_64.rpm             | 917 kB   00:00     
(21/35): pyserial-2.6-6.el7.noarch.rpm                     | 124 kB   00:00     
(22/35): python-backports-ssl_match_hostname-3.5.0.1-1.el7 |  13 kB   00:00     
(23/35): python-ipaddress-1.0.16-2.el7.noarch.rpm          |  34 kB   00:00     
(24/35): python-jsonpatch-1.2-4.el7.noarch.rpm             |  15 kB   00:00     
(25/35): python-jsonpointer-1.9-2.el7.noarch.rpm           |  13 kB   00:00     
(26/35): python-prettytable-0.7.2-3.el7.noarch.rpm         |  37 kB   00:00     
(27/35): python-markupsafe-0.11-10.el7.x86_64.rpm          |  25 kB   00:00     
(28/35): python-requests-2.6.0-10.el7.noarch.rpm           |  95 kB   00:00     
(29/35): python-chardet-2.2.1-3.el7.noarch.rpm             | 227 kB   00:00     
(30/35): python-six-1.9.0-2.el7.noarch.rpm                 |  29 kB   00:00     
(31/35): python-urllib3-1.10.2-7.el7.noarch.rpm            | 103 kB   00:00     
(32/35): python-jinja2-2.7.2-4.el7.noarch.rpm              | 519 kB   00:00     
(33/35): setools-libs-3.3.8-4.el7.x86_64.rpm               | 620 kB   00:00     
(34/35): python-setuptools-0.9.8-7.el7.noarch.rpm          | 397 kB   00:00     
(35/35): python-babel-0.9.6-8.el7.noarch.rpm               | 1.4 MB   00:02     
--------------------------------------------------------------------------------
Total                                              2.0 MB/s | 8.2 MB  00:04     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-4.1708.el7.centos.x86_64 (@anaconda)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : libsepol-2.5-10.el7.x86_64                                  1/43 
  Updating   : libselinux-2.5-15.el7.x86_64                                2/43 
  Updating   : audit-libs-2.8.5-4.el7.x86_64                               3/43 
  Updating   : libsemanage-2.5-14.el7.x86_64                               4/43 
  Updating   : libselinux-python-2.5-15.el7.x86_64                         5/43 
  Installing : python-ipaddress-1.0.16-2.el7.noarch                        6/43 
  Installing : python-six-1.9.0-2.el7.noarch                               7/43 
  Installing : libsemanage-python-2.5-14.el7.x86_64                        8/43 
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                        9/43 
  Installing : net-tools-2.0-0.25.20131004git.el7.x86_64                  10/43 
  Updating   : libselinux-utils-2.5-15.el7.x86_64                         11/43 
  Updating   : policycoreutils-2.5-34.el7.x86_64                          12/43 
  Installing : setools-libs-3.3.8-4.el7.x86_64                            13/43 
  Installing : libcgroup-0.41-21.el7.x86_64                               14/43 
  Installing : python-prettytable-0.7.2-3.el7.noarch                      15/43 
  Installing : libyaml-0.1.4-11.el7_0.x86_64                              16/43 
  Installing : PyYAML-3.10-11.el7.x86_64                                  17/43 
  Installing : python-chardet-2.2.1-3.el7.noarch                          18/43 
  Installing : python-jsonpointer-1.9-2.el7.noarch                        19/43 
  Installing : python-jsonpatch-1.2-4.el7.noarch                          20/43 
  Installing : python-backports-1.0-8.el7.x86_64                          21/43 
  Installing : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch   22/43 
  Installing : python-setuptools-0.9.8-7.el7.noarch                       23/43 
  Installing : python-urllib3-1.10.2-7.el7.noarch                         24/43 
  Installing : python-requests-2.6.0-10.el7.noarch                        25/43 
  Installing : python-babel-0.9.6-8.el7.noarch                            26/43 
  Installing : python-markupsafe-0.11-10.el7.x86_64                       27/43 
  Installing : python-jinja2-2.7.2-4.el7.noarch                           28/43 
  Installing : python-IPy-0.75-6.el7.noarch                               29/43 
  Installing : checkpolicy-2.5-8.el7.x86_64                               30/43 
  Installing : policycoreutils-python-2.5-34.el7.x86_64                   31/43 
  Installing : pyserial-2.6-6.el7.noarch                                  32/43 
  Installing : cloud-init-19.4-7.el7.centos.6.x86_64                      33/43 
  Updating   : audit-2.8.5-4.el7.x86_64                                   34/43 
  Installing : cloud-utils-growpart-0.29-5.el7.noarch                     35/43 
  Cleanup    : policycoreutils-2.5-17.1.el7.x86_64                        36/43 
  Cleanup    : libsemanage-2.5-8.el7.x86_64                               37/43 
  Cleanup    : libselinux-utils-2.5-11.el7.x86_64                         38/43 
  Cleanup    : audit-2.7.6-3.el7.x86_64                                   39/43 
  Cleanup    : libselinux-python-2.5-11.el7.x86_64                        40/43 
  Cleanup    : libselinux-2.5-11.el7.x86_64                               41/43 
  Cleanup    : libsepol-2.5-6.el7.x86_64                                  42/43 
  Cleanup    : audit-libs-2.7.6-3.el7.x86_64                              43/43 
  Verifying  : python-backports-ssl_match_hostname-3.5.0.1-1.el7.noarch    1/43 
  Verifying  : libselinux-2.5-15.el7.x86_64                                2/43 
  Verifying  : libselinux-python-2.5-15.el7.x86_64                         3/43 
  Verifying  : python-setuptools-0.9.8-7.el7.noarch                        4/43 
  Verifying  : pyserial-2.6-6.el7.noarch                                   5/43 
  Verifying  : python-urllib3-1.10.2-7.el7.noarch                          6/43 
  Verifying  : audit-libs-2.8.5-4.el7.x86_64                               7/43 
  Verifying  : checkpolicy-2.5-8.el7.x86_64                                8/43 
  Verifying  : policycoreutils-2.5-34.el7.x86_64                           9/43 
  Verifying  : libsepol-2.5-10.el7.x86_64                                 10/43 
  Verifying  : net-tools-2.0-0.25.20131004git.el7.x86_64                  11/43 
  Verifying  : python-IPy-0.75-6.el7.noarch                               12/43 
  Verifying  : python-jinja2-2.7.2-4.el7.noarch                           13/43 
  Verifying  : libselinux-utils-2.5-15.el7.x86_64                         14/43 
  Verifying  : policycoreutils-python-2.5-34.el7.x86_64                   15/43 
  Verifying  : python-six-1.9.0-2.el7.noarch                              16/43 
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                            17/43 
  Verifying  : python-markupsafe-0.11-10.el7.x86_64                       18/43 
  Verifying  : audit-2.8.5-4.el7.x86_64                                   19/43 
  Verifying  : python-babel-0.9.6-8.el7.noarch                            20/43 
  Verifying  : libsemanage-python-2.5-14.el7.x86_64                       21/43 
  Verifying  : libsemanage-2.5-14.el7.x86_64                              22/43 
  Verifying  : python-backports-1.0-8.el7.x86_64                          23/43 
  Verifying  : cloud-init-19.4-7.el7.centos.6.x86_64                      24/43 
  Verifying  : python-jsonpointer-1.9-2.el7.noarch                        25/43 
  Verifying  : python-jsonpatch-1.2-4.el7.noarch                          26/43 
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                       27/43 
  Verifying  : python-chardet-2.2.1-3.el7.noarch                          28/43 
  Verifying  : libyaml-0.1.4-11.el7_0.x86_64                              29/43 
  Verifying  : python-prettytable-0.7.2-3.el7.noarch                      30/43 
  Verifying  : python-requests-2.6.0-10.el7.noarch                        31/43 
  Verifying  : python-ipaddress-1.0.16-2.el7.noarch                       32/43 
  Verifying  : PyYAML-3.10-11.el7.x86_64                                  33/43 
  Verifying  : cloud-utils-growpart-0.29-5.el7.noarch                     34/43 
  Verifying  : libcgroup-0.41-21.el7.x86_64                               35/43 
  Verifying  : libselinux-utils-2.5-11.el7.x86_64                         36/43 
  Verifying  : libselinux-2.5-11.el7.x86_64                               37/43 
  Verifying  : libsemanage-2.5-8.el7.x86_64                               38/43 
  Verifying  : audit-2.7.6-3.el7.x86_64                                   39/43 
  Verifying  : policycoreutils-2.5-17.1.el7.x86_64                        40/43 
  Verifying  : libsepol-2.5-6.el7.x86_64                                  41/43 
  Verifying  : audit-libs-2.7.6-3.el7.x86_64                              42/43 
  Verifying  : libselinux-python-2.5-11.el7.x86_64                        43/43 

Installed:
  cloud-init.x86_64 0:19.4-7.el7.centos.6                                       
  cloud-utils-growpart.noarch 0:0.29-5.el7                                      

Dependency Installed:
  PyYAML.x86_64 0:3.10-11.el7                                                   
  audit-libs-python.x86_64 0:2.8.5-4.el7                                        
  checkpolicy.x86_64 0:2.5-8.el7                                                
  libcgroup.x86_64 0:0.41-21.el7                                                
  libsemanage-python.x86_64 0:2.5-14.el7                                        
  libyaml.x86_64 0:0.1.4-11.el7_0                                               
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                                   
  policycoreutils-python.x86_64 0:2.5-34.el7                                    
  pyserial.noarch 0:2.6-6.el7                                                   
  python-IPy.noarch 0:0.75-6.el7                                                
  python-babel.noarch 0:0.9.6-8.el7                                             
  python-backports.x86_64 0:1.0-8.el7                                           
  python-backports-ssl_match_hostname.noarch 0:3.5.0.1-1.el7                    
  python-chardet.noarch 0:2.2.1-3.el7                                           
  python-ipaddress.noarch 0:1.0.16-2.el7                                        
  python-jinja2.noarch 0:2.7.2-4.el7                                            
  python-jsonpatch.noarch 0:1.2-4.el7                                           
  python-jsonpointer.noarch 0:1.9-2.el7                                         
  python-markupsafe.x86_64 0:0.11-10.el7                                        
  python-prettytable.noarch 0:0.7.2-3.el7                                       
  python-requests.noarch 0:2.6.0-10.el7                                         
  python-setuptools.noarch 0:0.9.8-7.el7                                        
  python-six.noarch 0:1.9.0-2.el7                                               
  python-urllib3.noarch 0:1.10.2-7.el7                                          
  setools-libs.x86_64 0:3.3.8-4.el7                                             

Dependency Updated:
  audit.x86_64 0:2.8.5-4.el7             audit-libs.x86_64 0:2.8.5-4.el7        
  libselinux.x86_64 0:2.5-15.el7         libselinux-python.x86_64 0:2.5-15.el7  
  libselinux-utils.x86_64 0:2.5-15.el7   libsemanage.x86_64 0:2.5-14.el7        
  libsepol.x86_64 0:2.5-10.el7           policycoreutils.x86_64 0:2.5-34.el7    

Complete!

2.3 编辑 cloud.cfg 文件

编辑 /etc/cloud/cloud.cfg 配置文件,在 cloud_init_modules 下添加以下内容

#添加内容
 - resolv-conf

#最终配置文件效果
[root@local ~]# vi /etc/cloud/cloud.cfg
[root@local ~]# cat /etc/cloud/cloud.cfg
users:
 - default

disable_root: 1
ssh_pwauth:   0

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   1
ssh_genkeytypes:  ~
syslog_fix_perms: ~
disable_vmware_customization: false

cloud_init_modules:
 - disk_setup
 - migrator
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - rsyslog
 - users-groups
 - ssh
 - resolv-conf

cloud_config_modules:
 - mounts
 - locale
 - set-passwords
 - rh_subscription
 - yum-add-repo
 - package-update-upgrade-install
 - timezone
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd

cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

system_info:
  default_user:
    name: centos
    lock_passwd: true
    gecos: Cloud User
    groups: [adm, systemd-journal]
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    shell: /bin/bash
  distro: rhel
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

# vim:syntax=yaml

2.4 编辑 network 文件

编辑 /etc/sysconfig/network 配置文件,使其包含以下内容,避免访问 EC2 元数据服务时出现问题。

#添加内容
NOZEROCONF=yes

#最终配置文件效果
[root@local ~]# vi /etc/sysconfig/network
[root@local ~]# cat /etc/sysconfig/network
#Created by anaconda
NOZEROCONF=yes

2.5 编辑 grub 文件

编辑 /etc/default/grub 配置文件,使其包含以下引导选项内容,为了确保控制台的信息出现在 dashboard 的日志标签页中,以及 nova console-log 日志的输出内容中。

#添加内容
GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"

#最终配置文件效果
[root@local ~]# vi /etc/default/grub
[root@local ~]# cat /etc/default/grub 

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="rd.lvm.lv=centos/root rd.lvm.lv=centos/swap rhgb quiet"
GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"
GRUB_DISABLE_RECOVERY="true"

#执行命令使参数生效
[root@local ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-c515a8f65bf14e138d99b2c451179327
Found initrd image: /boot/initramfs-0-rescue-c515a8f65bf14e138d99b2c451179327.img
done

2.6 关闭 KVM 虚拟机

[root@local ~]# init 0

三、创建 QCOW2 镜像

注意:以下步骤在虚拟机中操作,不是kvm虚拟机,概念分不清?参考第一部分 约定

3.1 重置并清理镜像

[root@cloudcs ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos                         shut off

[root@cloudcs ~]# virt-sysprep -d centos

#输出
[   0.0] Examining the guest ...
[  25.4] Performing "abrt-data" ...
[  25.4] Performing "backup-files" ...
[  26.3] Performing "bash-history" ...
[  26.4] Performing "blkid-tab" ...
[  26.4] Performing "crash-data" ...
[  26.4] Performing "cron-spool" ...
[  26.4] Performing "dhcp-client-state" ...
[  26.4] Performing "dhcp-server-state" ...
[  26.4] Performing "dovecot-data" ...
[  26.4] Performing "logfiles" ...
[  26.5] Performing "machine-id" ...
[  26.5] Performing "mail-spool" ...
[  26.5] Performing "net-hostname" ...
[  26.5] Performing "net-hwaddr" ...
[  26.5] Performing "pacct-log" ...
[  26.5] Performing "package-manager-cache" ...
[  26.5] Performing "pam-data" ...
[  26.5] Performing "passwd-backups" ...
[  26.5] Performing "puppet-data-log" ...
[  26.6] Performing "rh-subscription-manager" ...
[  26.6] Performing "rhn-systemid" ...
[  26.6] Performing "rpm-db" ...
[  26.6] Performing "samba-db-log" ...
[  26.6] Performing "script" ...
[  26.6] Performing "smolt-uuid" ...
[  26.6] Performing "ssh-hostkeys" ...
[  26.6] Performing "ssh-userdir" ...
[  26.6] Performing "sssd-db-log" ...
[  26.6] Performing "tmp-files" ...
[  26.6] Performing "udev-persistent-net" ...
[  26.6] Performing "utmp" ...
[  26.6] Performing "yum-uuid" ...
[  26.6] Performing "customize" ...
[  26.6] Setting a random seed
[  26.6] Setting the machine ID in /etc/machine-id
[  26.7] Performing "lvm-uuids" ...

3.2 创建并压缩镜像

#查找 centos 实例对应的磁盘文件
[root@cloudcs ~]# virsh domblklist centos
Target     Source
------------------------------------------------
vda        /soft/disk/centos7.qcow2
hda        -

#创建并清理压缩镜像
[root@cloudcs ~]# virt-sparsify --compress /soft/disk/centos7.qcow2 /tmp/centos-cloud.qcow2

#输出
[   0.0] Create overlay file in /tmp to protect source disk
[   0.1] Examine source disk
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  13.6] Fill free space in /dev/centos/root with zero
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[  31.5] Clearing Linux swap on /dev/centos/swap
[  33.3] Fill free space in /dev/sda1 with zero
[  40.9] Copy to destination and make sparse
[ 101.4] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the 
target disk boots and works correctly.

四、测试镜像启动

4.1 注册镜像

将镜像 centos-cloud.qcow2 注册到 OpenStack 平台,以下为图形化注册演示,命令行注册请参考博文 G020-OP-INS-RHEL-02 RedHat OpenStack 发放云主机(命令行)

4.2 创建云主机类型(规格)

4.3 发放云主机测试

注意:网络及路由请参考 G020 创建,务必保证路由及网关存在,否则无法获取到客户自定义主机名




观察启动流程及尝试登录,测试镜像正确无误

标签:x86,自定义,python,QCOW2,noarch,64,OpenStack,el7,2.5
来源: https://www.cnblogs.com/kongshuo/p/16648489.html