kvm常用管理命令
查看虚拟机
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
kvm虚拟机开机启动
virsh autostart centos7
ll /etc/libvirt/qemu/autostart/
小项目:
把虚拟机磁盘迁移到/data目录并且启动
0.停止要迁移的虚拟机
virsh shutdown centos7
1.创建数据目录并移动磁盘文件
mkdir /data
mv /opt/centos7.raw /data
2.使用edit命令修改配置文件
virsh edit centos7
--------------------
<source file='/data/centos7.raw'/>
--------------------
3.启动虚拟机
virsh start centos7
virsh list
kvm连接方式
1.VNC
virsh vncdisplay centos7
2.SSH
3.console显示
#登录进需要开启console的虚拟机并添加参数
ssh 192.168.122.77
grubby --update-kernel=ALL --args="console=ttyS0,115200n8"
grep "115200" /boot/grub2/grub.cfg
reboot
virsh console centos7
#退出
ctrl + ]
kvm磁盘磁盘
1.虚拟机磁盘格式介绍
raw:不支持做快照,性能好
qcow2:支持快照,性能不如raw好
2.查看磁盘信息
qemu-img info centos7.raw
3.创建磁盘
qemu-img create -f qcow2 /data/centos7.qcow2 1G
4.查看磁盘信息
qemu-img info centos7.qcow2
5.调整磁盘容量: 只能加不能减
qemu-img resize /data/centos7.qcow2 1T
6.磁盘格式转换
操作步骤:
#1.将虚拟机关机
virsh destroy centos7
#2.转换磁盘格式
qemu-img convert -f raw -O qcow2 centos7.raw centos7.qcow2
#3.编辑配置文件修改为qcow2格式
virsh edit centos7
------------------------------------------
<driver name='qemu' type='qcow2'/>
<source file='/opt/centos7.qcow2'/>
------------------------------------------
#4.重新启动
virsh start centos7
virsh console centos7
kvm快照管理
1.查看快照
virsh snapshot-list centos7
2.创建快照
virsh snapshot-create-as centos7 snap1
virsh snapshot-list centos7
3.恢复快照
virsh snapshot-revert centos7 snap1
4.删除快照
virsh snapshot-delete centos7 snap1
kvm克隆虚拟机
1.完整克隆
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
2.链接克隆
#生成虚拟机磁盘文件
qemu-img create -f qcow2 -b centos7.qcow2 centos7-clone.qcow2
#查看磁盘信息
qemu-img info centos7-clone.qcow2
#生成虚拟机配置文件
virsh dumpxml centos7 > centos7-clone.xml
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 '/\<nam/s#centos7#centos7-clone#g' centos7-clone.xml
#导入配置文件
virsh define centos7-clone.xml
virsh list --all
#启动虚拟机
virsh start centos7-clone
kvm桥接网络
默认NAT模式
桥接模式
为了方便测试,可以在VM虚拟机上打开DHCP功能
1.创建桥接网卡
virsh iface-bridge eth0 br0
2.连接克隆新磁盘
cd /opt
qemu-img create -f qcow2 -b centos7.qcow2 bridge.qcow2
3.创建新虚拟机
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
4.登录虚拟机查看网卡信息
virsh console centos7-bridge
ip a
5.在其他主机ping测试
kvm虚拟机热添加磁盘
1.热添加硬盘
#创建磁盘
qemu-img create -f qcow2 centos7-add.qcow2 10G
#临时生效添加
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2
#虚拟机格式化并挂载
virsh console centos7
fdisk -l
mkfs.xfs /dev/vdb
mount /dev/vdb /mnt/
df -h
#永久生效添加
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2 --config
2.剥离磁盘
#临时剥离
virsh detach-disk centos7 vdb
#永久剥离
virsh detach-disk centos7 vdb
virsh detach-disk centos7 vdb --config
3.调整磁盘大小
#调整磁盘大小
qemu-img info /opt/centos7-add.qcow2
qemu-img resize /opt/centos7-add.qcow2 +10G
#添加到虚拟机并查看
virsh attach-disk centos7 /opt/centos7-add.qcow2 vdb --subdriver qcow2
virsh console centos7
fdisk -l /dev/vdb
mount /dev/vdb /mnt/
df -h|tail -1
#调整磁盘信息
xfs_growfs /dev/vdb
df -h|tail -1
kvm热添加网卡
#临时添加
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
virsh detach-interface centos7 bridge --config
kvm热添加内存
#创建虚拟机时直接添加最大内存参数
virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name centos7 --
memory 512,maxmemory=2048 --vcpus 1 --disk /opt/centos7.qcow2 --boot hd --network bridge=br0 --
graphics vnc,listen=0.0.0.0 --noautoconsole
#如果创建虚拟机的时候没有设置最大内存限制,执行如下操作添加配置
virsh destroy centos7
virsh setmaxmem centos7 4096M
virsh start centos7
virsh console centos7
free -h
#临时添加
virsh setmem centos7 2048M --live
virsh console centos7
free -h
#永久增大内存
virsh setmem centos7 2048M --config
virsh console centos7
free -h
KVM冷添加CPU
#编辑配置文件
virsh edit centos7
------------------------------------------------
<vcpu placement='static' current='2'>4</vcpu>
------------------------------------------------
#添加cpu核数
virsh setvcpus centos7 4 --live
#永久添加cpu核数
virsh setvcpus centos7 4 --config
网友评论