一、环境准备
 
yum groupinstall -y "Virtualization*"
yum install -y libguestfs-tools-c.x86_64
 
二、创建KVM虚拟机
 
#创建目录上传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
 
进入图形化界面安装。
三、修改KVM虚拟机配置
1、安装cloud-init工具
 
yum install -y cloud-utils-growpart cloud-init
 
2、修改网卡配置文件
 
cat /etc/sysconfig/network-scripts/ifcfg-eth0 

TYPE=Ethernet
BOOTPROTO=dhcp
DEVICE=eth0
ONBOOT=yes
 
3、编辑cloud.cfg文件
 
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
 
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
 
5、编辑grub文件
 
#添加内容
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
 
6、自定义云主机
关闭firewalld,selinux等等
7、关机
 
init 0
 
四、制作镜像
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" ...
 
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.
 
3、解决在ALLINONE openstack booting from hard disk
 
cat /etc/nova/nova.conf
[libvirt]
cpu_mode = none
virt_type = qemu
 
4、上网问题
 
创建路由连接,绑定浮动IP,添加/etc/resolv.conf 
  nameserver 8.8.8.8
 
5、SSH自动配置
 
vim /etc/ssh/sshd_config
#打开密码验证模式 【只要修改下面项内容并重启服务,该报错就处理成功了】
PasswordAuthentication yes 
#下面两项为打开公钥模式,按需求选择是否开启
RSAAuthentication yes   
PubkeyAuthentication yes   

#配置公钥记录文件 【可不管】
AuthorizedKeysFile     .ssh/authorized_keys   

# 是否运行root直登,按需求选择是否开启
PermitRootLogin yes

众所周知,创建OpenStack虚拟机时,提供了设置用户密码的功能,用户密码是通过云平台的metadata服务注入到虚拟机操作系统,密码注入是通过操作系统的cloud-init模块实现,关于cloud-init请参考:https://cloudinit.readthedocs.io/en/latest/
本文主要讲解关于用户密码注入的具体操作:
在虚拟机创建时,Linux官方镜像默认使用SSH-KEY的方式登录,如果我们想使用用户名与密码登录,需要在虚拟机创建时修改用户名与密码来保证虚拟机启动后可以使用用户名与密码登录。
CentOS 7 x8
Centos 7 x8
在创建虚拟机时,增加密码修改脚本如下:
启动云主机 -> 创建后 -> 定制脚本源(直接输入)->脚本数据
输入内容如下:
#!/bin/sh
passwd <<EOF
123456
123456
EOF
注:默认修改的用户密码即为虚拟机root用户密码。

若需通过SSH使用用户名与密码登录,即需要在创建虚拟机时不选择KEY,同时修改SSH服务配置文件才能允许root用户使用SSH登录:
启动云主机 -> 创建后 -> 定制脚本源(直接输入)->脚本数据
输入内容如下:
#!/bin/sh
passwd <<EOF
123456
123456
EOF
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
service sshd restart

此时虚拟机可以使用ROOT用户直接SSH登录。