基于kvm虚拟机创建openstack qcow2磁盘镜像

发布时间 2023-12-06 11:28:48作者: caocong

前提知识

KVM做单机管理虚拟机,Openstack集群管理虚拟机

 

使用工具

virt-manager  虚拟机管理器(Virtual Machine Manager)

 

目标

基于 Kylin-Server-V10-SP3-General-Release-2303-X86_64.iso

创建 qcow2格式的openstack 磁盘镜像

 

qcow2镜像制作

使用环境

       IP: 192.168.62.96  root/123456

CPU:     Hygon C86 5380 16-core

OS: ky10.x86_64

1、      创建一个空的 qcow2磁盘

qemu-img create -f qcow2 kylin10-2303-x86.qcow2 20G

kylin10-2303-x86.qcow2: 磁盘名称

20G: 磁盘容量大小, 实际大小会根据所需空间自动变化

 

2、      创建虚拟机

选择镜像 Kylin-Server-V10-SP3-General-Release-2303-X86_64.iso

选择磁盘 qcow2 kylin10-2303-x86.qcow2

按自己的需求创建好虚拟机

 

3、      cloud-init安装(重要)

cloud-init官方文档
官方配置参数参考
阿里云官方文档 安装cloud-init

 

yum install cloud-init -y

rpm -qa |grep cloud-ini
cloud-init-19.4-6.p05.ky10.noarch

cloud-init -v
/usr/bin/cloud-init 19.4

cloud-init init –local
Cloud-init v. 19.4 running 'init-local' at Wed, 06 Dec 2023 02:28:44 +0000. Up 330.55 seconds.

#设置开机自启动
systemctl enable cloud-init-local.service cloud-init.service cloud-config.service cloud-final.service

 

4、      安装相应的包(重要)

# ACPI服务是控制重启和关闭实例
yum -y install acpid
systemctl enable acpid

#
安装cloud-utils-growpart 允许分区调整
yum -y install cloud-utils-growpart

# CentOS
系统必须要禁用默认zeroconf路由,以便精确访问OpenStack数据源
echo "NOZEROCONF=yes" >> /etc/sysconfig/network

 

5、 配置Cloud-Init工具(重要)

[root@kylin10-tmpl ~]# cat /etc/cloud/cloud.cfg
# The top level settings are used as module

# and system configuration.

 

# A set of users which may be applied and/or used by various modules

# when a 'default' entry is found it will reference the 'default_user'

# from the distro configuration specified below

users:

   - default

 

# If this is set, 'root' will not be able to ssh in and they

# will get a message to login instead as the default $user

disable_root: false

 

mount_default_fields: [~, ~, 'auto', 'defaults,nofail', '0', '2']

resize_rootfs_tmp: /dev

ssh_pwauth:   1

 

# This will cause the set+update hostname module to not operate (if true)

preserve_hostname: false

 

# Example datasource config

# datasource:

#    Ec2:

#      metadata_urls: [ 'blah.com' ]

#      timeout: 5 # (defaults to 50 seconds)

#      max_wait: 10 # (defaults to 120 seconds)

 

# The modules that run in the 'init' stage

cloud_init_modules:

 - migrator

 - seed_random

 - bootcmd

 - write-files

 - growpart

 - resizefs

 - disk_setup

 - mounts

# - set_hostname

# - update_hostname

 - update_etc_hosts

 - ca-certs

 - rsyslog

 - users-groups

 - ssh

 

# The modules that run in the 'config' stage

cloud_config_modules:

 - ssh-import-id

 - locale

 - set-passwords

 - spacewalk

 - yum-add-repo

 - ntp

 - timezone

 - disable-ec2-metadata

 - runcmd

 

# The modules that run in the 'final' stage

cloud_final_modules:

 - package-update-upgrade-install

 - puppet

 - chef

 - mcollective

 - salt-minion

 - rightscale_userdata

 - scripts-vendor

 - 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 and/or distro specific settings

# (not accessible to handlers/transforms)

system_info:

   # This will affect which distro class gets used

   distro: kylin

   # Default user name + that default users groups (if added/used)

   default_user:

     name: cloud-user

     lock_passwd: True

     gecos: kylin Cloud User

     groups: [wheel, adm, systemd-journal]

     sudo: ["ALL=(ALL) NOPASSWD:ALL"]

     shell: /bin/bash

   # Other config here will be given to the distro class and/or path classes

   paths:

      cloud_dir: /var/lib/cloud/

      templates_dir: /etc/cloud/templates/

   ssh_svcname: sshd

chpasswd:

  list: |

    root:123456

  expire: False

 

#自动扩容vda2分区

growpart:

  mode: auto

  devices: [/dev/vda2]

  ignore_growroot_disabled: false

 

##利用runcmd 扩容lvm

runcmd:

  - [pvresize,/dev/vda2]

  - [lvextend,-l,+100%FREE,/dev/mapper/klas-root]

  - [xfs_growfs,/dev/mapper/klas-root]

 

manage_resolv_conf: true

resolv_conf:

  nameservers: ['192.168.1.15','223.5.5.5']

  searchdomains:

    - localdomain

  domain: localdomain

  options:

    rotate: true

    timeout: 1

  

 

 

说明: 自动扩容磁盘

devices: [/dev/vda2] 选择对应的 vda2
- [pvresize,/dev/vda2] 选择对应的 vda2

使用 blkid 可以看到 根磁盘/dev/mapper/klas-root使用的 LVM /dev/sda2

[root@kylin10-tmpl ~]# blkid
/dev/mapper/klas-swap: UUID="8600826a-c5f3-46f3-a9aa-ce1acf98856d" TYPE="swap"
/dev/mapper/klas-root: UUID="3dae7c4a-7369-4cfc-8290-0332054d2c7a" BLOCK_SIZE="512" TYPE="xfs"
/dev/sda2: UUID="XShTtI-9C6g-ulXR-cww2-QguR-dx4r-D2CnDp" TYPE="LVM2_member" PARTUUID="31f9dfa8-02"
/dev/sda1: UUID="a1ee5d36-df4d-40b6-83e7-d9912c6ad409" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="31f9dfa8-01"

 

6、      检查Cloud-Init配置

[root@kylin10-tmpl ~]# cloud-init init --local
Cloud-init v. 19.4 running 'init-local' at Wed, 06 Dec 2023 02:48:50 +0000. Up 1536.22 seconds.

 

7、      清理日志和历史记录

cd /usr/lib/python3.7/site-packages/cloudinit/sources/
#
执行如下命令,删除__init__.pyc文件和优化编译后的__init__.pyo文件
rm -rf init.pyc
rm -rf init.pyo

#
执行如下命令,清理日志信息。
rm -rf /var/lib/cloud/*
rm -rf /var/log/cloud-init*

history -c
shutdown -h now

上传镜像到openstack, 创建虚拟机验证