目录
创建kvm虚拟机
创建目录上传ISO镜像
将 CentOS 7.4 上传到 /soft 目录下
创建 kvm虚机 使用的磁盘文件
配置kvm虚拟机
修改 KVM 虚机网络配置文件
安装 cloud-init 软件包
编辑 cloud.cfg 文件
编辑 network 文件
编辑 grub 文件
关闭kvm虚拟机
创建 QCOW2 镜像
重置并清理镜像
创建并压缩镜像
测试镜像启动
挂载光盘
[root@lucky ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@lucky ~]# ls /mnt
CentOS_BuildTag GPL LiveOS RPM-GPG-KEY-CentOS-7
EFI images Packages RPM-GPG-KEY-CentOS-Testing-7
EULA isolinux repodata TRANS.TBL
配置本地源
[root@lucky ~]# cd /etc/yum.repos.d/
[root@lucky yum.repos.d]# mkdir bak
[root@lucky yum.repos.d]# mv *.repo bak
[root@lucky yum.repos.d]# vim 111.repo
[root@lucky yum.repos.d]# cat 111.repo
[baseurl]
name = baseurl
baseurl = file:///mnt
gpgcheck = 0
安装虚拟化软件包及虚拟机镜像管理工具
[root@lucky ~]# yum groupinstall -y "Virtualization*"
[root@lucky ~]# yum install -y libguestfs-tools-c.x86_64
创建kvm虚拟机
创建目录上传ISO镜像
[root@lucky ~]# mkdir -p /soft/disk
将 CentOS 7.4 上传到 /soft 目录下
[root@lucky ~]# ls /soft
CentOS-7-x86_64-DVD-1810.iso disk
创建 kvm虚机 使用的磁盘文件
[[root@lucky ~]# 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 虚机
[root@lucky ~]# virt-install \
> --name centos \
> --disk path=/soft/disk/centos7.qcow2 \
> --vcpus 1 \
> --memory 2048 \
> --location /soft/CentOS-7-x86_64-DVD-1810.iso \
> --network network=default
配置kvm虚拟机
修改 KVM 虚机网络配置文件
[root@lucky ~]# cd /etc/sysconfig/network-scripts/
[root@lucky network-scripts]# cat ifcfg-eth0
TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
NAME="eth0"
DEVICE="eth0"
ONBOOT="yes"
安装 cloud-init 软件包
[root@lucky ~]# yum install -y cloud-utils-growpart cloud-init
编辑 cloud.cfg 文件
编辑 /etc/cloud/cloud.cfg 配置文件,在 cloud_init_modules 下添加以下内容
- resolv-conf
编辑 network 文件
配置文件,使其包含以下内容,避免访问 EC2 元数据服务时出现问题。
root@lucky ~]# vi /etc/sysconfig/network
[root@lucky ~]# cat /etc/sysconfig/network
# Created by anaconda
NOZEROCONF=yes
编辑 grub 文件
配置文件,使其包含以下引导选项内容,为了确保控制台的信息出现在 dashboard 的日志标签页中,以及 nova console-log 日志的输出内容中。
[root@lucky ~]# 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="crashkernel=auto 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@lucky ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-957.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-22867537d636425fb339f9411e7e4959
Found initrd image: /boot/initramfs-0-rescue-22867537d636425fb339f9411e7e4959.img
done
关闭kvm虚拟机
[root@lucky ~]# init 0
创建 QCOW2 镜像
注意:以下步骤在 虚机 中操作,不是 kvm 虚机,
重置并清理镜像
root@lucky ~]# virsh list --all
Id Name State
----------------------------------------------------
- centos shut off
[root@lucky ~]# virt-sysprep -d centos
[ 0.0] Examining the guest ...
[ 31.9] Performing "abrt-data" ...
[ 31.9] Performing "backup-files" ...
[ 33.5] Performing "bash-history" ...
[ 33.5] Performing "blkid-tab" ...
[ 33.6] Performing "crash-data" ...
[ 33.6] Performing "cron-spool" ...
[ 33.6] Performing "dhcp-client-state" ...
[ 33.6] Performing "dhcp-server-state" ...
[ 33.6] Performing "dovecot-data" ...
[ 33.6] Performing "logfiles" ...
[ 33.7] Performing "machine-id" ...
[ 33.7] Performing "mail-spool" ...
[ 33.7] Performing "net-hostname" ...
[ 33.8] Performing "net-hwaddr" ...
[ 33.8] Performing "pacct-log" ...
[ 33.8] Performing "package-manager-cache" ...
[ 33.9] Performing "pam-data" ...
[ 33.9] Performing "passwd-backups" ...
[ 33.9] Performing "puppet-data-log" ...
[ 33.9] Performing "rh-subscription-manager" ...
[ 33.9] Performing "rhn-systemid" ...
[ 33.9] Performing "rpm-db" ...
[ 33.9] Performing "samba-db-log" ...
[ 33.9] Performing "script" ...
[ 33.9] Performing "smolt-uuid" ...
[ 33.9] Performing "ssh-hostkeys" ...
[ 33.9] Performing "ssh-userdir" ...
[ 33.9] Performing "sssd-db-log" ...
[ 34.0] Performing "tmp-files" ...
[ 34.0] Performing "udev-persistent-net" ...
[ 34.0] Performing "utmp" ...
[ 34.0] Performing "yum-uuid" ...
[ 34.0] Performing "customize" ...
[ 34.0] Setting a random seed
[ 34.0] Setting the machine ID in /etc/machine-id
[ 34.1] Performing "lvm-uuids" ...
创建并压缩镜像
查找 centos 实例对应的磁盘文件
[root@lucky ~]# virsh domblklist centos
Target Source
------------------------------------------------
vda /soft/disk/centos7.qcow2
hda -
创建并清理压缩镜像
[root@lucky ~]# 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.3] Fill free space in /dev/centos/root with zero
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
[ 89.1] Clearing Linux swap on /dev/centos/swap
[ 91.1] Fill free space in /dev/sda1 with zero
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ --:--
[ 97.2] Copy to destination and make sparse
[ 204.0] Sparsify operation completed with no errors.
virt-sparsify: Before deleting the old disk, carefully check that the
target disk boots and works correctly.
测试镜像启动
注册镜像,将镜像 centos-cloud.qcow2 注册到 OpenStack 平台
创建云主机类型(规格)
然后直接发放云主机并且使用刚创建好的镜像