KVM学习笔记不断更新中

1.镜像格式
目前比较主流的虚拟化镜像格式 qcow2,raw,vmdk

1.raw格式

虚拟化默认的格式,也可以叫做裸格式,性能没得说,可以直接挂载,支持转换成其它格式的虚拟机镜像(如果其它格式需要转换,有时候还是需要它做为中间格式),空间使用
来看,这个很像磁盘,使用多少就是多少(du -h看到的大小就是使用大小),但如果你要把整块磁盘都拿走的话得全盘拿了(copy镜像的时候),会比较消耗网络带宽和I/O。接
下来还有个有趣的问题,如果那天你的硬盘用着用着不够用了,你咋办,在买一块盘。但raw格式的就比较犀利了,可以在原来的盘上追加空间:dd if=/dev/zero of=zeros.raw
bs=1024k count=4096(先创建4G的空间)cat foresight.img zeros.raw > new-foresight.img(追加到原有的镜像之后)。raw 不支持snapshot(快照);

2.qcow2格式

qcow2经过一代的优化,目前qcow2的性能上接近raw裸格式的性能
对于qcow2的格式,几点还是比较突出的,qcow2的snapshot,可以在镜像上做N多个快照:更小的存储空间,即使是不支持holes的文件系统也可以(这下du -h和ls -lh看到的就一样了)Copy-on-write support, where the image only represents changes made to an underlying diskimage(这个特性SUN ZFS表现的淋漓尽致)支持多个snapshot,对历史snapshot进行管理支持zlib的磁盘压缩支持AES的加密

3.vmdk 格式
VMware 3 & 4, or 6 image format, for exchanging images with that product VMware的格式,这个格式说的蛋疼一点就有点牛X,原本VMware就是做虚拟化起家,自己做了一个集群的VMDK的pool,做了自己的虚拟机镜像格式。又拉着一些公司搞了一个OVF的统一封包,从性能和功能上来说,vmdk应该算最出色的,由于vmdk结合了VMware的很多能力,目前来看,KVM和XEN使用这种格式的情况不是太多。但就VMware的Esxi来看,它的稳定性和各方面的能力还是可圈可点的
4.格式转化
raw转qcow2
此步骤使用qemu-img工具实现,如果机器上没有,可以通过rpm或yum进行安装,包名为qemu-img。
qemu-img是专门虚拟磁盘映像文件的qemu命令行工具。具体命令如下:
qemu-img convert -f raw centos.img -O qcow2 centos.qcow2
参数说明:convert 将磁盘文件转换为指定格式的文件
-f 指定需要转换文件的文件格式
-O 指定要转换的目标格式
转换完成后,将新生产一个目标映像文件,原文件仍保存。
VMDK转qcow2:
qemu-img convert -f vmdk -O qcow2 SLES11SP1-single.vmdk SLES11SP1-single.img


KMV命令学习
qemu-img
是QEMU的磁盘管理工具,在qemu-kvm源码编译后就会默认编译好qemu-img这个二进制文件。qemu-img也是QEMU/KVM使用过程中一个比较重要的工具
info 查看镜像的信息
qemu-img info youimg.img
create 创建镜像
qemu-img create -f raw -o size=4G youimg.img
check 检查镜像
qemu-img check youimg.img
convert 转化镜像的格式,(raw,qcow ……)
qemu-img convert -c -f fmt -O out_fmt -o options fname out_fname
-c:采用压缩,只有qcow和qcow2才支持
-f:源镜像的格式,它会自动检测,所以省略之
-O 目标镜像的格式
-o 其他选先
fname:源文件
out_fname:转化后的文件
qemu-img convert -c -O qcow2 src.raw dst.qcow2
snapshot 管理镜像的快照 只支出持 qcow2格式
qemu-img snapshot -l youimg.qcow2 查看快照
qemu-img snapshot -c booting youimg.qcow2 新建快照
qemu-img snapshot -a 1 youimg.qcow2 恢复快照
qemu-img snapshot -d 2 youimg.qcow 删除快照

rebase 在已有的镜像的基础上创建新的镜像
resize 增加或减小镜像大小 只支持raw格式
qemu-img resize imgname.raw +4GB

virsh
virsh --help 查看帮助

virsh shutdown domain 关闭虚拟机如果命令不生效需要在虚拟机上安装acpid 并且启动acpid 服务

virsh domid domain 查看虚拟机的标识符
virsh domname 26 和上一条命令是对应的
virsh domuuid domain 查看虚拟机的 UUID
virsh dominfo domain 查看虚拟机的信息


KVMxml文件解析
<domain type='kvm'>
<name>server</name>
<uuid>415704a2-be12-1bef-9e4d-7cf8dd3423c1</uuid>
<memory unit='KiB'>10485760</memory>
<currentMemory unit='KiB'>10485760</currentMemory>
<vcpu placement='static'>4</vcpu>
<os>
<type arch='x86_64' machine='rhel6.3.0'>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
<bootmenu enable='yes'/>
</os>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<clock offset='utc'/>
<on_poweroff>destroy</on_poweroff>
<on_reboot>restart</on_reboot>
<on_crash>restart</on_crash>
<devices>
<emulator>/usr/libexec/qemu-kvm</emulator>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/export/KVMTEST/bbz/bbz.img'/>
<target dev='vda' bus='virtio'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none'/>
<source file='/export/KVMTEST/bbz/export.img'/>
<target dev='vdb' bus='virtio'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<source file='/export/ios/CentOS-6.3-x86_64-bin-DVD1.iso'/>
<target dev='hdc' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='1' target='0' unit='0'/>
</disk>
<controller type='usb' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
</controller>
<controller type='ide' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
</controller>
<controller type='virtio-serial' index='0'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</controller>
<interface type='bridge'>
<mac address='52:54:00:9a:71:20'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</serial>
<console type='pty'>
<target type='serial' port='0'/>
</console>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='1'/>
</channel>
<input type='tablet' bus='usb'/>
<input type='mouse' bus='ps2'/>
<graphics type='spice' autoport='yes'/>
<video>
<model type='cirrus' vram='9216' heads='1'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>


虚拟机名
<name>server</name>

虚拟机ID
<uuid>415704a2-be12-1bef-9e4d-7cf8dd3423c1</uuid>
这个ID可以能过linux 命令 uuidgen 来取得

内存
<memory unit='KiB'>10485760</memory> 内存大小
<currentMemory unit='KiB'>10485760</currentMemory> 为虚拟机分配的内存大小
CPU
<vcpu placement='static'>4</vcpu> CPU核熟

Os
<os>
<type arch='x86_64' machine='rhel6.3.0'>hvm</type>
<boot dev='cdrom'/>
<boot dev='hd'/>
<bootmenu enable='yes'/>
</os>

KVM错误
错误信息

Error: viewer connection to hypervisor host got refused or disconnected!
这个错误跟vnc有关,折腾了一天没有搞定,网上有资料称把virt-manager 和python-virtinst删除从装可以搞定,但是我删除安装N次问题依然存在,没办法只好找替代的方法来解决了,在display 的 type 选项里选择spice 在宿主机上安装 相应的安装包
yum install spice-server spice-client spice-protocol
yum install cairo-spice ffmpeg-spice-libs ngspice pixman-spice spice-glib spice-gtk spice-gtk-python spice-gtk-tools spice-xpi

使用命令 spicec -h 127.0.0.1 -p 5900
这样就可以了,熟悉的服务器启动界面又出现了;

错误信息
PTY allocation request failed on channel 0
这个错误是在ssh 连接的时候报的跟KVM应该没有关系,处理方法
rm -rf /dev/ptmx
2.mknod /dev/ptmx c 5 2
3.chmod 666 /dev/ptmx
4.umount /dev/pts
5.rm -rf /dev/pts
6.mkdir /dev/pts
7.mount /dev/pts
重启一下如果还有问题 就把上面的命令加在rc.local 里吧

错误信息
新建文件后服务器如果通过virsh destroy 命令关机在启动后发现文件丢失,这是磁盘cache的事,如果拿用命令关机或在关机前使用sync 同步磁盘文件不会丢失,也可以在xml文件中把参数设成这样 cache='none‘ 但是经过我的测试XFS格式的分区无效,ext3 和ext4都没问题.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值