虚拟机Q&A

Q1:虚拟机运行一段时间后,各个版本的第一台虚拟机容量变大,增大容量可以接受

原因:不详

解决方法:用备份镜像替换当前镜像

1.1G Jun 24 17:07 win7_sp1_32_0.qcow2
1.5G Jun 24 17:07 win7_sp1_32_1.qcow2
1.2G Jun 24 17:07 win7_sp1_32_2.qcow2
651M Jun 24 17:07 win7_sp1_32_3.qcow2
3.6G Jun 24 17:08 win7_sp1_32_backup.qcow2
631M Jun 24 17:08 winxp_sp2_0.qcow2
1.1G Jun 24 17:08 winxp_sp2_1.qcow2
1.1G Jun 24 17:09 winxp_sp2_2.qcow2
1.1G Jun 24 17:09 winxp_sp2_3.qcow2
1.1G Jun 24 17:09 winxp_sp2_4.qcow2
1.1G Jun 24 17:09 winxp_sp2_5.qcow2
2.4G Jun 24 17:09 winxp_sp2_cn_backup.qcow2
350M Jun 24 17:10 winxp_sp3_0.qcow2
1.1G Jun 24 17:10 winxp_sp3_1.qcow2
1.1G Jun 24 17:10 winxp_sp3_2.qcow2
1.1G Jun 24 17:10 winxp_sp3_3.qcow2
1.1G Jun 24 17:10 winxp_sp3_4.qcow2
1.1G Jun 24 17:10 winxp_sp3_5.qcow2
2.4G Jun 24 17:11 winxp_sp3_cn_backup.qcow2

root 1.3G Jul  7 10:48 win7_sp1_32_0.qcow2
root 1.5G Jul  7 10:45 win7_sp1_32_1.qcow2
root 1.2G Jul  7 10:46 win7_sp1_32_2.qcow2
root 659M Jul  7 10:46 win7_sp1_32_3.qcow2
kvm  3.6G Jun 25 10:14 win7_sp1_32_backup.qcow2
root 1.1G Jul  7 15:06 winxp_sp2_0.qcow2
root 1.1G Jul  7 15:06 winxp_sp2_1.qcow2
root 1.1G Jul  7 15:06 winxp_sp2_2.qcow2
root 1.1G Jul  7 15:06 winxp_sp2_3.qcow2
root 1.1G Jul  7 15:06 winxp_sp2_4.qcow2
root 1.1G Jul  7 15:08 winxp_sp2_5.qcow2
kvm  2.4G Jun 25 10:15 winxp_sp2_cn_backup.qcow2
root 369M Jul  7 14:28 winxp_sp3_0.qcow2
root 1.1G Jul  7 14:27 winxp_sp3_1.qcow2
root 1.1G Jul  7 14:28 winxp_sp3_2.qcow2
root 1.1G Jul  7 10:49 winxp_sp3_3.qcow2
root 1.1G Jul  7 10:48 winxp_sp3_4.qcow2
root 1.1G Jul  7 10:47 winxp_sp3_5.qcow2
kvm  2.4G Jun 25 10:18 winxp_sp3_cn_backup.qcow2

Q2:Live snapshot revert times increases linearly with snapshot age

现象:The revert times keep increasing linearly with the age of the snapshot, going from tens of seconds to thousands. While the revert takes place the qemu process takes 100% of a core and there is no disk activity.

原因:kvm虚拟机创建的快照时间和当前的linux物理机时间间隔太大,如果linux时间修改回创建快照的时间,则snapshot-revert回复时间基本在5sec以内。 默认的clock选项为<clock offset='localtime'/>,猜测应该是guest会以高频率catchup host的时间,导致CPU占用为100%,且恢复快照时间很慢。

解决方法:

1、实时创建快照,保证创建快照时间和linux当前时间基本相近,同时删除前一快照

注:此方法修改后在虚拟机少于四台时效果较好,基本revert时间控制在5sec以内,如果大于4台kvm虚拟机,则revert时间还是会达到三四十秒,原因是高并发的I/O读写导致恢复时间较慢          

time1 = time.time()
self.vms[label].revertToSnapshot(snapshot, flags=0)
time2 = time.time()
cost_time = time2 - time1
log.debug("%s revertToSnapshot cost %s seconds." % (label, cost_time))
if cost_time > 10:
    xmlDesc = "<domainsnapshot/>"
    self.vms[label].snapshotCreateXML(xmlDesc)
    snapshot.delete()
    log.debug("%s snapshotCreateXML." % label)                    

2.更改xml内容

注意:更改xml内容最好是修改好domain.xml后再virsh define domain.xml,如果是已经建立内存快照,然后通过virsh edit domain修改xml,则下次恢复快照依旧不生效。原因是恢复快照会恢复xml文件的内容。

将<clock offset='localtime'/>改成

<clock offset='localtime'>
  <timer name='rtc' tickpolicy='catchup' track='guest'/>
</clock>

tickpolicy='catchup' 选项可有可无,主要就是让guest不要tarck host的时间,而track guest的时间

image

reference: https://bugs.launchpad.net/qemu/+bug/1505041

Q:虚拟机镜像变大的问题

需求:虚拟机恢复内存快照后,分析样本结束保持原有大小不变

解决方法:qemu-system-x86_64命令启动虚拟机时,带上-snapshot参数

-snapshot
    Write to temporary files instead of disk image files. In this case, the raw disk image you use is not written back.
    You can however force the write back by pressing C-a s.

版本信息:

root@polyhawk-50:/polydata/mnt/vmous# kvm --version
QEMU emulator version 2.0.0 (Debian 2.0.0+dfsg-2ubuntu1.15), Copyright (c) 2003-2008 Fabrice Bellard

root@polyhawk-50:/polydata/mnt/vmous# virsh version 
Compiled against library: libvirt 1.2.2
Using library: libvirt 1.2.2
Using API: QEMU 1.2.2
Running hypervisor: QEMU 2.0.0

root@polyhawk-50:/polydata/mnt/vmous# cat /etc/issue
Ubuntu 14.04.3 LTS \n \l

root@polyhawk-50:/polydata/mnt/vmous# uname -a
Linux polyhawk-50 3.13.0-32-generic #57-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

搜索出来主要有两类办法:一是修改xml文件,二是修改模拟器启动脚本

一:修改domain.xml

<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>

……

<qemu:commandline>
  <qemu:arg value='-snapshot'/>
</qemu:commandline>
</domain>

注:虚拟机中可以加入如下信息来优化虚拟机性能以及方便在不同的CPU架构之间进行虚拟机迁移

<qemu:commandline>
  <qemu:arg value='-cpu'/>
  <qemu:arg value='host,-hypervisor'/>
</qemu:commandline>
Instead of using "host", you can also choose a number of other CPU models from the list displayed with the "qemu-system-i386 -cpu help" command (SandyBridge, Haswell, etc).
x86             host  KVM processor with all supported host features (only available in KVM mode)

 

二:修改kvm脚本

查看domain.xml发现<emulator>/usr/bin/kvm-spice</emulator>,

root@polyhawk-50:/usr/bin# ls -l /usr/bin/kvm-spice
lrwxrwxrwx 1 root root 3 Jul 28 04:27 /usr/bin/kvm-spice –> kvm

root@polyhawk-50:/usr/bin# cat /usr/bin/kvm
#! /bin/sh
exec qemu-system-x86_64 -enable-kvm "$@"

直接修改kvm文件,最后加上-snapshot

此两种方法其实原理是相同的,但在上述环境中报出error

root@polyhawk-50:/polydata/mnt/vmous# virsh start winxp_sp2_test2
error: Failed to start domain winxp_sp2_test2
error: internal error: process exited while connecting to monitor: qemu-system-x86_64: -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1: Device needs media, but drive is empty
qemu-system-x86_64: -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1: Device initialization failed.
qemu-system-x86_64: -device ide-hd,bus=ide.0,unit=0,drive=drive-ide0-0-0,id=ide0-0-0,bootindex=1: Device 'ide-hd' could not be initialized

未解决,貌似是libvirt不支持,

image

最后还有一种解决方法,在domain.xml文件里加上<transient/>

<transient>- Indicates that changes to the device contents should be reverted automatically when
the guest virtual machine virtual machine exits. With some hypervisors, marking a disk transient
prevents the domain from participating in migration or snapshots.

sample:

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='writeback'/>
  <source file='/polydata/mnt/vmous/winxp_sp2_test2.qcow2'/>
  <target dev='hda' bus='ide'/>
  <transient/>
  <alias name='ide0-0-0'/>
  <address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>

同样是libvirt不支持,无奈……

reference:

http://bbs.linuxtone.org/forum.php?mod=viewthread&tid=19212&page=1&authorid=18819

http://serverfault.com/questions/111794/libvirt-and-qemu-kvm-snapshot-option

https://bugzilla.redhat.com/show_bug.cgi?id=832194

https://bugs.launchpad.net/qemu/+bug/1192780

https://libvirt.org/drvqemu.html

续:

加-snapshot参数报Device needs media, but drive is empty 错误是因为ubuntu的appamor阻挡了libvirt的一些行为,

为了确保Libvirt始终有必须的权限,必须禁用apparmor。方法如下:

1.  在编译libvirt的时候选择--without-apparmor 选项;

2.  执行下面的命令为libvirt禁用 apparmor:

$ ln -s /etc/apparmor.d/usr.sbin.libvirtd  /etc/apparmor.d/disable/

$ln -s /etc/apparmor.d/usr.lib.libvirt.virt-aa-helper  /etc/apparmor.d/disable/

$ apparmor_parser -R  /etc/apparmor.d/usr.sbin.libvirtd

$ apparmor_parser -R  /etc/apparmor.d/usr.lib.libvirt.virt-aa-helper

然后重启机器。

之后在xml文件中加入-snapshot选项即可,virsh destroy domain后暂存数据自动删除,但reboot数据却依然保留

冲突:xml文件中带入-snapshot参数导致建立虚拟机快照镜像大小(后备镜像方式)不增加,仍为193K,矛盾就是快照恢复失败,会boot启动,而不会瞬间恢复快照。即使已经建立不带-snapshot的快照,virsh snapshot-edit 修改快照信息增加-snapshot后,快照仍无法正常恢复,而是正常启动

 

Q3:新建虚拟机

环境:安装相应的库,apt-get install qemu-kvm libvirt-bin virtinst

1.创建img,格式为qcow2(支持压缩)

qemu-img create -f qcow2 /polydata/data/image/winxp_sp3_test.qcow2 8G

查看创建的img

qemu-img info /polydata/data/image/winxp_sp3_test.qcow2

2.光盘启动系统

virt-install --virt-type kvm --name winxp_sp3_test --ram 512 --os-type=windows --os-variant=winxp --disk path=/polydata/data/image/winxp_sp3_test.qcow2,format=qcow2,size=8,cache=none --cdrom=/polydata/polydev/iso/xp/zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_x14-80404.iso --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole

virt-install #man virt-install查看更多参数和用法

参数说明

--name winxp_sp3_test #虚拟机domain名称,domain是一个虚拟机的唯一标识

--ram 512 #内存512M

--virt-type kvm #hypervisor类型, kvm ,qemu, xen, kqemu

--hvm #full virtualization

--os-type=windows #操作系统类型 windows, linux

--os-variant=winxp #virt-install --os-variant list查看更多

--disk path=/polydata/data/image/winxp_sp3_test.qcow2,format=qcow2,size=8,cache=none

--cdrom=/polydata/polydev/iso/xp/zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_x14-80404.iso #启动光盘

--network bridge=br0 #桥接模式,网卡br0 桥接到tap0

--graphics vnc,listen=0.0.0.0 #因为是在服务器上安装,没有GUI,所以VNC远程桌面安装

--noautoconsole #不自动加载console

3. 远程连接桌面

查看VNC端口,然后用VNC客户端连接,注意端口保持一致

virsh vncdisplay winxp_sp3_test

4.安装系统

安装完成后打开服务,services.msc,找到windows defender和windows firewall,设置为disable并且关闭服务,关闭windows update,win7的ie打开设置下,安装python2.7.9,复制agent.pyw到客户机的startup文件夹,关闭自动播放,设置用户权限为Never Notify。

5.创建快照

snapshot-create winxp_sp3_test

 

多个快照共用一个backupfile

创建qcow2文件的时候,指定backing_file选项

qemu-img create -f qcow2 -o backing_file=winxp_sp3_backup.qcow2 winxp_sp3_0.qcow2

参考:http://smilejay.com/2012/08/qemu-img-details/

Q4:虚拟机迁移

假设要迁移winxp_sp3_test到另一台机器,按一下步骤执行(务必在虚拟机running状态下操作

1. 导出winxp_sp3_test的虚拟机及快照配置

virsh dumpxml --migratable winxp_sp3_test > winxp_sp3_test.xml

virsh snapshot-current winxp_sp3_test > winxp_sp3_test_snapshot.xml

2. 把winxp_sp3_test.qcow2和步骤1中导出的xml文件都复制到目标机器上,并检查文件的uuid,mac是否重复,按需修改,如果修改IP,需要在目标机器上重新创建快照

3. 在目标机器上导入配置

定义domain

virsh define winxp_sp3_test.xml

定义快照

virsh snapshot-create winxp_sp3_test winxp_sp3_test_snapshot.xml --redefine –current

在目标机器上执行snapshot-info winxp_sp3_test --current检查是否成功

Q:安装虚拟机时磁盘空间不足,只有1M
image
创建镜像命令:qemu-img create -f qcow2 /polydata/image/winxp_sp2_backup1.qcow2 10G
安装命令:virt-install --virt-type kvm --name winxp_sp2_backup1 --ram 512 --os-type=windows --os-variant=winxp --disk path=/polydata/image/winxp_sp2_backup1.qcow2,cache=writeback --network bridge=br0 --cdrom=/tmp/WinLite.iso --graphics vnc,listen=0.0.0.0 --noautoconsole
原因是安装虚拟机的命令行中没有指定磁盘格式,改为如下命令即可
virt-install --virt-type kvm --name winxp_sp2_backup1 --ram 512 --os-type=windows --os-variant=winxp --disk path=/polydata/image/winxp_sp2_backup1.qcow2,format=qcow2,cache=writeback --network bridge=br0 --cdrom=/tmp/WinLite.iso --graphics vnc,listen=0.0.0.0 --noautoconsole

Q:删除虚拟机内部的文件,镜像在宿主机上的磁盘空间能否缩小?
打算用跑着的vm做出模板。  磁盘已经删除的比较小了。
但是image还是占用很大的空间。如何是image磁盘占用小一些?

答:使用virt-sparsify命令能压缩掉一些虚拟机文件系统的空洞,但是虚拟机系统 和宿主机系统是两回事情。如果是精简模式,比如qcow2,使用的时候是用多少占多少。
但是一旦占用了,技术虚拟机内部文件删除了,空间是没有办法缩小的。
另外介绍一个比较巧妙的办法:
1 使用dd命令将虚拟机未使用的空间用0填满:
dd id=/dev/zero of=/data
然后 rm /data
关闭虚拟机
2 使用qemu-img转换镜像文件
qemu-img convert -O qcow2 vm.qcow2 vm.new.qcow2

Q:使用qemu-system-x86_64启动虚拟机时报Could not initialize SDL(No available video device) - exiting
未指定vga参数, -vga [std|cirrus|vmware|qxl|xenfb|tcx|cg3|none]
qemu-system-x86_64 winxp_sp3_test.qcow2 –vga std -vnc 0.0.0.0:0

Q:多块磁盘启动
加上-hda/-hdb参数,另外可以指定cpu,mem等参数
qemu-system-x86_64 -hda Base_OS_From_25.86-disk1.img -hdb Base_OS_From_25.86-disk2.img -vga std -vnc 0.0.0.0:0 -m 32768 -smp cpus=12 -cpu host

Q: tcp远程连接libvirt

TCP方式:
qemu+tcp://example.com/system
例如:qemu+tcp://172.16.0.15/system,服务端只需要做简单配置即可:
vim /etc/libvirt/libvirtd.conf:
listen_tls = 0 #禁用tls登录
listen_tcp = 1   #启用tcp方式登录
tcp_port = "16509" #tcp端口16509
listen_addr = "0.0.0.0"
auth_tcp = "none"    #TCP不使用认证
max_clients = 1024   #最大总的连接客户数1024
min_workers = 50     #libvirtd启动时,初始的工作线程数目
max_workers = 512 #同上,最大数目
max_requests = 1000 #最大同时支持的RPC调用,必须大于等于max_workers
max_client_requests = 1000 #每个客户端支持的最大连接数
 
同时修改libvirt-bin的配置文件:
vim /etc/default/libvirt-bin:
# Start libvirtd to handle qemu/kvm:
start_libvirtd="yes"
# options passed to libvirtd, add "-l" to listen on tcp
# 默认加载/etc/libvirt/libvirtd.conf,如果需要替换则加上 --config [conf_path]
libvirtd_opts="-d -l"
 
做完以上修改后,执行service libvirt-bin restart即可。 netstat -anpt | grep 16509就能看到libvirtd监听在TCP 16509端口。

posted on 2015-07-07 16:06  月流霜  阅读(832)  评论(0编辑  收藏  举报