设置-处理器-虚拟化
yum install libvirt virt-install qemu-kvm -y
systemctl start libvirtd.service
systemctl status libvirtd.service
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --memory 1024 --vcpus 1 --disk /opt/centos7.raw,format=raw,size=10 --cdrom /opt/CentOS-7-x86_64-DVD-1810.iso --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
安装VNC工具连接
virsh list
virsh list --all
virsh start centos7
virsh reboot centos7
virsh shutdown centos7
virsh destroy centos7
virsh dumpxml centos7
virsh dumpxml centos7 > centos7.xml
virsh destroy centos7
virsh undefine centos7
virsh define centos7.xml
virsh domrename panghu shouhu
virsh suspend centos7
virsh resume centos7
virsh autostart centos7
ll /etc/libvirt/qemu/autostart/
把虚拟机磁盘迁移到/data目录并且启动
virsh shutdown centos7
mkdir /data
mv /opt/centos7.raw /data
virsh edit centos7
--------------------
virsh start centos7
virsh list
virsh vncdisplay centos7
ssh 192.168.122.77
登录进需要开启console的虚拟机并添加参数
ssh 192.168.122.77
grubby --update-kernel=ALL --args=“console=ttyS0,115200n8”
grep “115200” /boot/grub2/grub.cfg
reboot
virsh destroy centos7
virsh start centos7
virsh console centos7
退出
ctrl + ]
raw:不支持做快照,性能好
qcow2:支持快照,性能不如raw好
qemu-img info centos7.raw
qemu-img create -f qcow2 /data/centos7.qcow2 1G
qemu-img info centos7.qcow2
qemu-img resize /data/centos7.qcow2 1T
virsh destroy centos7
qemu-img convert -f raw -O qcow2 centos7.raw centos7.qcow2
virsh edit centos7
------------------------------------------
virsh start centos7
virsh console centos7
virsh snapshot-list centos7
virsh snapshot-create-as centos7 snap1
virsh snapshot-list centos7
virsh snapshot-revert centos7 snap1
virsh snapshot-delete centos7 snap1
virsh snapshot-list centos7
virsh shutdown centos7
virt-clone --auto-clone -o centos7 -n centos7-backup
virsh list --all
virsh dumpxml web-blog-backup |grep “qcow2”
virsh snapshot-list centos7
qemu-img create -f qcow2 -b centos7.qcow2 centos7-clone.qcow2
qemu-img info centos7-clone.qcow2
virsh dumpxml centos7 > centos7-clone.xml virsh define centos7-clone.xml virsh start centos7-clone virsh iface-bridge eth0 br0 cd /opt virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7-bridge --memory 1024 --vcpus 1 --disk /opt/bridge.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole virsh console centos7-bridge ping xxxx qemu-img create -f qcow2 centos7-add.qcow2 10G virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2 virsh console centos7 virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2 virsh detach-disk centos7 vdb virsh detach-disk centos7 vdb qemu-img info /opt/centos7-add.qcow2 virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2 xfs_growfs /dev/vdb virsh attach-interface centos7 --type bridge --mac 52:54:00:b1:b5:8a --source br0 --model virtio detachinterface virsh attach-interface centos7 --type bridge --mac 52:54:00:b1:b5:8a --source br0 --model virtio detachinterface --config virsh detach-interface centos7 bridge virsh detach-interface centos7 bridge virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 – virsh destroy centos7 virsh setmem centos7 2048M --live virsh setmem centos7 2048M --config virsh edit centos7 virsh setvcpus centos7 4 --live setvcpus centos7 4 --config
sed -i ‘/uuid/d’ centos7-clone.xml
sed -i ‘/mac address/d’ centos7-clone.xml
sed -i ‘s#centos7.qcow2#centos7-clone.qcow2#g’ centos7-clone.xml
sed -i ‘/2.3 导入配置文件
virsh list --all2.4 启动虚拟机
第8章 KVM桥接网络
1.创建桥接网卡
2.连接克隆新磁盘
qemu-img create -f qcow2 -b centos7.qcow2 bridge.qcow23.创建新虚拟机
4.登录虚拟机查看网卡信息
ip a5.在其他主机ping测试
第9章 KVM虚拟机热添加磁盘
1.热添加硬盘
1.1 创建磁盘
1.2 临时生效添加
1.3 虚拟机格式化并挂载
fdisk -l
mkfs.xfs /dev/vdb
mount /dev/vdb /mnt/
df -h1.4 永久生效添加
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2 --config2.剥离磁盘
2.1 临时剥离
2.2 永久剥离
virsh detach-disk centos7 vdb --config3.调整磁盘大小
3.1 调整磁盘大小
qemu-img resize /opt/centos7-add.qcow2 +10G3.2 添加到虚拟机并查看
virsh console centos7
fdisk -l /dev/vdb
mount /dev/vdb /mnt/
df -h|tail -13.3 调整磁盘信息
df -h|tail -1第10章 KVM热添加网卡
1.临时添加
2.永久生效
3,临时剥离
4.永久剥离
virsh detach-interface centos7 bridge --config第11章 KVM热添加内存
1.创建虚拟机时直接添加最大内存参数
memory 512,maxmemory=2048 --vcpus 1 --disk /opt/centos7.qcow2 --boot hd --network bridge=br0 –
graphics vnc,listen=0.0.0.0 --noautoconsole2.如果创建虚拟机的时候没有设置最大内存限制,执行如下操作添加配置
virsh setmaxmem centos7 4096M
virsh start centos7
virsh console centos7
free -h3.临时添加
virsh console centos7
free -h4.永久增大内存
virsh console centos7
free -h第12章 KVM冷添加CPU
1.编辑配置文件
------------------------------------------------
------------------------------------------------2.添加cpu核数
3.永久添加cpu核数