Kvm Cheatsheet
kvm 常用命令。
参考资料
常用命令
- virsh list 列出当前虚拟机列表,不包括未启动的
- virsh list –all 列出所有虚拟机,包括所有已经定义的虚拟机
- virsh destroy vm-name 关闭虚拟机
- virsh start vm-name 启动虚拟机
- virsh edit vm-name 编辑虚拟机xml文件
- virsh undefine vm-name 删除虚拟机
- virsh shutdown vm-name 停止虚拟机
- virsh reboot vm-name 重启虚拟机
- virsh autostart vm-name 虚拟机随宿主机启动
- virsh snapshot-create-as –domain {VM-NAME} –name “{SNAPSHOT-NAME}” # 镜像
- virsh snapshot-revert –domain freebsd –snapshotname 5Sep2016_S1
- virsh snapshot-delete –domain freebsd –snapshotname 5Sep2016_S2
- virsh attach-disk
/path/cd2.iso hdc –type cdrom # 换cdrom盘
安装windows
配置虚拟机,需要理解网络中的bridge
概念。
bridge配置成功之后,宿主机应该增加一个br0的网络接口,然后原来的eth0的ip地址就没了。Br0的地址配成当前宿主机的地址。
这样虚拟机在通过br0,就可以通过eth0连接到路由器上,然后虚拟机可以自己向路由器申请 IP 地址。
具体配置如下。
新建 br0 网卡
vi /etc/sysconfig/network-scripts/ifcfg-br0 DEVICE="br0" BOOTPROTO="static" IPADDR=172.17.1.73 NETMASK=255.255.255.0 GATEWAY=172.17.1.1 DNS1=202.106.0.20 ONBOOT=yes TYPE="Bridge" NM_CONTROLLED=no
调整原 Eth0 网卡
DEVICE="enp7s0" TYPE="Ethernet" BOOTPROTO="none" ONBOOT=yes NM_CONTROLLED=no BRIDGE=br0
重启网络服务
systemctl restart network brctl show bridge name bridge id STP enabled interfaces br0 8000.002590af7cb6 no eth0 vnet0 virbr0 8000.52540054bb56 yes virbr0-nic
网络配置好之后,就可以配置虚拟机了。
qemu-img create -f qcow2 win01.qcow2 30g
/root/download/virt-manager-1.5.1/virt-install -n win01 --vcpus=2 --ram=2048 --os-type=windows --os-variant=win7 -c /opt/iso/cn_windows_7_professional_x86_dvd_x15-65790.iso --disk path=/usr/share/virtio-win/virtio-win-0.1.141_amd64.vfd,device=floppy --disk path=/data/kvmimg/win01.qcow2,format=qcow2,bus=ide --graphics vnc,listen=0.0.0.0 --noautoconsole
qemu-img create -f qcow2 linux01.qcow2 30g
/root/download/virt-manager-1.5.1/virt-install -n linux01 --vcpus=2 --ram=2048 --os-type=linux --os-variant=rhel7 -c /opt/iso/CentOS-7-x86_64-DVD-1611.iso --disk path=/usr/share/virtio-win/virtio-win-0.1.141_amd64.vfd,device=floppy --disk path=/data/kvmimg/linux01.qcow2,format=qcow2,bus=virtio --graphics vnc,listen=0.0.0.0,port=5902 --noautoconsole
关于
os-variant
,运行osinfo-query os
可以获取可用的范围(https://raymii.org/s/articles/virt-install_introduction_and_copy_paste_distro_install_commands.html#os-variant)
要是linux,bus=virtio
装完之后,建议装vnc viewer for chrome
,远程连接虚拟机,非常方便。
快照
snapshot-create win01
这是创建快照
snapshot-create win01 –disk-only
这是创建外部快照 创建外部快照我是觉的也挺好的。下次再创建虚拟机,可以直接根据外部快照来创建新的虚拟机。 但是外部快照不支持直接 snapshot-revert 来恢复快照。得按照新建虚拟机的方式来还原。
克隆
未经实验验证
virsh shutdown linux01
virt-clone --connect=qemu:///system -o linux01 -n linux01_clone -f /data/kvmimg/linux01_clone.qcow2
FAQ
报错:
unsupported format character ‘��0xffffffef) at index 30
如果虚机创建过程中报编码错误,请把locale设成en_US
export LANG="en_US.utf8"
virsh shutdown win02
不能在Virsh中关闭虚拟机- 打开
gpedit.msc
修改组策略
Computer Configuration\Windows Settings\ Security Settings\Local Policies\Security Options\ Shutdown: Allow system to be shut down without having to log on
导入注册表。新建注册表文件并导入。
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows] "ShutdownWarningDialogTimeout"=dword:00000001
现在就可以关机了
virsh shutdown win02
对于win7还需要补充设置:控制面板 - 电源选项 -
- 打开
外部快照带来的各种问题操作
# 删除外部快照 virsh snapshot-delete win01 xxxx --metadata # 还原到快照之前 virsh undefine win01 qemu-img create ... --boot hd (重新用img创建一个快照,启动项设为hd)
调整机器配置 & 保存当前配置
virt edit win01 virt dumpxml win01 > current.xml
增加usb设备
yum install usbutils
lsusb
lsusb -v -s 008:009
找到usb
信息
idVendor 0x096e Feitian Technologies, Inc.
idProduct 0x0002
vi usbkey.xml
<hostdev mod='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x096e'/>
<product id='0x0002'/>
</source>
</hostdev>
virsh attach-device rhel6 --file usb_device.xml --config # next reboot
virsh attach-device rhel6 --file usb_device.xml # now
virsh attach-device rhel6 --file usb_device.xml --persistent # persistent
virsh detach-device rhel6 --file usb_device.xml