5 KVM 常用命令
1 | https://blog.51cto.com/14306186/2512440?source=dra |
1-1. 创建目录 /home/aaa/bbb/ccc
1 | mkdir -p /home/aaa/bbb/ccc |
1-2. /home目录下同时建 iso,images 目录
1 | mkdir -p /home/{iso,images} |
2-1.启动libvirt
1 | systemctl start libvirtd.service |
2-2.停止libvirt
1 | systemctl stop libvirtd.service |
2-3.重启libvirt
1 | systemctl restart libvirtd.service |
2-4. 设置开机启动libvirt
1 | systemctl enable libvirtd.service |
2-5. 关闭开机启动libvirt
1 | systemctl disable libvirtd.service |
3-1.启动网卡
1 | systemctl start network.service |
3-2.停止网卡
1 | systemctl stop network.service |
3-3.重启网卡
1 | systemctl restart network.service |
3-4. 设置开机启动网卡
1 | systemctl enable network.service |
3-5. 关闭开机启动网卡
1 | systemctl disable network.service |
4-1. 只显示已经启动的虚拟机
1 | virsh list |
4-2. 显示所有的虚拟机[包含已经启动,也包含没有启动的]
1 | virsh list --all |
5-1. 启动虚拟机kvm12
1 | virsh start kvm12 |
5-2. 关闭虚拟机kvm12
1 | virsh shutdown kvm12 |
5-3. 重启虚拟机kvm12
1 | virsh reboot kvm12 |
5-4. 强制断电kvm12 [对于running的虚拟机使用]
1 | virsh destroy kvm12 |
5-5. 删除虚拟机kvm12 [对于shutdown的虚拟机使用]
1 | virsh undefine kvm12 |
5-6. 修改虚拟机配置文件kvm12
1 | virsh edit kvm12 |
6-1. 显示虚拟机的当前配置文件kvm12
1 | virsh dumpxml kvm12 |
6-2. 备份虚拟机kvm12
1 | virsh dumpxml kvm12 > /root/kvm12.xml |
7-1. 更改虚拟机名称kvm12 [改成kvm112]
1 | virsh domrename kvm12 kvm112 |
7-2. 查看虚拟机的详细信息kvm12
1 | virsh dominfo kvm12 |
7-3. 查看网卡配置信息kvm12
1 | virsh domiflist kvm12 |
7-4. 查看该虚拟机的磁盘位置kvm12
1 | virsh domblklist kvm12 |
8-1. 设置虚拟机为开机自启kvm12
1 | virsh autostart kvm12 |
8-2. 取消开机自启kvm12
1 | virsh autostart --disable kvm12 |
9-1. 查看kvm12对应的VNC连接端口号;默认第一个端口号为:5900(用“:0”表示)
1 | virsh vncdisplay kvm12 |
10-1. 创建.qcow2格式的虚拟机磁盘/home/images/kvm55.qcow2 5G
1 | qemu-img create -f qcow2 /home/images/kvm55.qcow2 5G |
10-2. 创建.raw格式的虚拟机磁盘/home/images/kvm56.raw 5G
1 | qemu-img create -f raw /home/images/kvm56.raw 5G |
10-3. 将kvm56.raw磁盘格式raw转换为qcow2 [/home/images/kvm56.raw /home/images/kvm56.qcow2]
1 | qemu-img convert -f raw -O qcow2 /home/images/kvm56.raw /home/images/kvm56.qcow2 |
10-4. 查看虚拟机的磁盘信息/home/images/kvm12.qcow2
1 | qemu-img info /home/images/kvm12.qcow2 |
11-1. 自动克隆虚拟机kvm11 克隆 kvm12
1 | virt-clone --auto-clone -o kvm11 -n kvm12 |
11-2. 手动克隆kvm11 克隆 kvm12
1 | virt-clone -o kvm11 -n kvm12 -f /home/images/kvm12.qcow2 |
12-1. 查看虚拟机kvm12的IP
1 2 | virsh dumpxml kvm12 | grep mac //得到一个mac地址:52:54:00:2e:d2:75 arp -a | grep 52:54:00:2e:d2:75 |
13-1. 修改kvm12 vnc连接端口为5912
1 | virsh edit kvm12 //或 vi /etc/libvirt/qemu/kvm12.xml |
1 | <graphics type='vnc' port='5912' autoport='no' listen='0.0.0.0'> //这行原本为 port='-1' autoport='yes' |
14-1. 检查当前的网络设置
1 | virsh net-list --all |
14-2. 检查当前的网络接口
1 | ifconfig |
15-1. 查看虚拟机的xml文件
1 | ll /etc/libvirt/qemu/ |
15-2. 查看磁盘文件目录查看
1 | ll /home/images/ |
16-1. 查看xml配置的磁盘文件位置
1 | awk -F "'" '/<source file=/{print $2}' /etc/libvirt/qemu/kvm12.xml //<source file='/home/images/kvm12.qcow2'/> |
16-2. 虚拟机配置文件中设定的mac地址
1 2 | awk -F "'" '/<mac address=/{print $2}' /etc/libvirt/qemu/kvm12.xml //<mac address='52:54:00:2e:d2:75'/> awk -F "'" '/<mac address=/{print $2}' /etc/libvirt/qemu/kvm11.xml |
17-1. 虚拟机监控
1 | virt-top |
18-1. 快照 – 创建快照kvm12
1 | virsh snapshot-create kvm12 |
18-2. 快照 – 创建快照[“自定义名称”]kvm12 [kvm12-snapshot]
1 | virsh snapshot-create-as kvm12 kvm12-snapshot |
18-3. 快照 – 查看虚拟机相关的快照kvm12
1 | virsh snapshot-list kvm12 |
18-4. 快照 – 删除快照kvm12[kvm12-snapshot]
1 | virsh snapshot-delete kvm12 kvm12-snapshot |
18-5. 快照 – 启动快照kvm12[kvm12-snapshot]
1 | virsh snapshot-revert kvm12 kvm12-snapshot |