1、构造空镜像
qemu-img create -f qcow2 centos-7-guest.qcow2 10G
qcow2、raw都可以;还能相互转换。
2、创建虚拟机 根据指定的跟镜像
virt-install --virt-type kvm --name centos-7-guest --ram 1024 --disk centos-7-guest.qcow2,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --location=CentOS-7-x86_64-Minimal-2009.iso
virt-install 参数:
-n --name= 客户端虚拟机名称
-r --ram= 客户端虚拟机分配的内存
-u --uuid= 客户端UUID
--vcpus=2 客户端的vcpu个数
-v --hvm 全虚拟化
-p --paravirt 半虚拟化
-l --location=localdir 安装源
--vnc 使用vnc
-c --cdrom= 光驱 安装途径
-s --file-size= 使用磁盘映像的大小 单位为GB
-f --file= 作为磁盘映像使用的文件
--disk= 使用不同选项作为磁盘使用安装介质
3、查看kvm虚拟机
[root@controller guest]$ virsh list
Id Name State
----------------------------------------------------
1 instance-00000c0e running
2 centos-7-guest running
4、virt-manager
virt-maneger
进入虚拟机 dhcpclient 配置网络 修改虚拟机(自我定制,爱咋改咋改) 关闭虚拟机
5、清理
virt-sysprep -d centos-7-guest
[root@controller guest]$ virt-sysprep -d centos-7-guest
[ 0.0] Examining the guest ...
[ 3.4] Performing "abrt-data" ...
[ 3.4] Performing "backup-files" ...
[ 4.1] Performing "bash-history" ...
[ 4.1] Performing "blkid-tab" ...
[ 4.1] Performing "crash-data" ...
[ 4.1] Performing "cron-spool" ...
[ 4.1] Performing "dhcp-client-state" ...
[ 4.2] Performing "dhcp-server-state" ...
[ 4.2] Performing "dovecot-data" ...
[ 4.2] Performing "logfiles" ...
[ 4.2] Performing "machine-id" ...
[ 4.2] Performing "mail-spool" ...
[ 4.2] Performing "net-hostname" ...
[ 4.2] Performing "net-hwaddr" ...
[ 4.3] Performing "pacct-log" ...
[ 4.3] Performing "package-manager-cache" ...
[ 4.3] Performing "pam-data" ...
[ 4.3] Performing "passwd-backups" ...
[ 4.3] Performing "puppet-data-log" ...
[ 4.3] Performing "rh-subscription-manager" ...
[ 4.3] Performing "rhn-systemid" ...
[ 4.3] Performing "rpm-db" ...
[ 4.3] Performing "samba-db-log" ...
[ 4.3] Performing "script" ...
[ 4.3] Performing "smolt-uuid" ...
[ 4.4] Performing "ssh-hostkeys" ...
[ 4.4] Performing "ssh-userdir" ...
[ 4.4] Performing "sssd-db-log" ...
[ 4.4] Performing "tmp-files" ...
[ 4.4] Performing "udev-persistent-net" ...
[ 4.4] Performing "utmp" ...
[ 4.4] Performing "yum-uuid" ...
[ 4.4] Performing "customize" ...
[ 4.4] Setting a random seed
[ 4.4] Setting the machine ID in /etc/machine-id
[ 4.5] Performing "lvm-uuids" ...
6、压缩镜像文件
virt-sparsify --compress centos-7-guest.qcow2 centos-7-guest01.qcow2
7、上传镜像 open stack
glance image create --name centos7-guest --container-format bare --disk-format qcow2 --file centos-7-guest01.qcow2
注:在环境直接创建虚拟机:
virt-install --virt-type kvm --name centos-7-guest --ram 1024 --disk centos-7-guest01.qcow2,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --import