安装: yum install libvirt virt-install qemu-kvm -y

介绍: libvirt服务:管理kvm虚拟机的生命周期 virt-install工具:创建安装虚拟机 qemu-kvm工具:使用qemu-img为虚拟机提供硬盘

新创建一个虚拟机:

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 
--memory 1024 --vcpus 1 --disk /opt/centos2.raw,format=raw,size=10 
--cdrom /opt/CentOS-7-x86_64-DVD-1708.iso 
--network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

创建虚拟磁盘

qemu-img  create  test.raw  10G
qemu-img create -f qcow2 test.qcow2 10G
查看虚拟磁盘信息
 qemu-img info test.raw

调整虚拟磁盘容量大小
qemu-img resize test.raw +5G

磁盘格式转换
qemu-img convert -f raw -O qcow2  test.raw oldboy.qcow2

创建快照
virsh snapshot-create centos7

查看快照
virsh snapshot-list centos7

还原快照
virsh snapshot-revert centos7 --snapshotname 1516574134

删除快照
virsh snapshot-delete centos7 --snapshotname 1516636570

完整克隆 实现方法: virt-clone -o web01 --auto-clone

链接克隆

1:克隆虚拟磁盘文件 cp centos7.qcow2 web03.qcow2 2:生成新的虚拟机配置文件 name修改 uuid删掉 disk路径/opt/web03.qcow2 mac地址删除 3:测试启动

virsh define  web01.xml

qemu-img create -f qcow2 -b cetnos7.qcow2 web04.qcow2

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web02 --memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

创建桥接网络 1: virsh iface-bridge eth0 br0

基于桥接网络创建虚拟机 2: virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 1024 --vcpus 1 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

1:

virsh edit centos7

    <interface type='bridge'>
      <mac address='52:54:00:55:aa:fa'/>
      <source bridge='br0’/>

2:修改虚拟机ip地址 /etc/sysconfig/network-scripts/ifcfg-eth0


创建硬盘 qemu-img create -f qcow2 centos7-add01.qcow2 5G

热添加硬盘 virsh attach-disk web01 /opt/centos7-add01.qcow2 vdb --live --cache=none --subdriver=qcow2

添加网卡

virsh attach-interface web04 --type bridge  --model  virtio --source br0

virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web01 --memory 1024 --vcpus 1,maxvcpus=4 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

热添加cpu

virsh setvcpus web04 --count=2


virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 512,maxmemory=2048 --vcpus=1,maxvcpus=2 --disk /opt/web04.qcow2,format=qcow2,size=10 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0  --noautoconsole

热添加内存 virsh setmem web04 1G