kvm学习小计

1.Kvm安装

  安装方法两种方式,一种是直接下载源码编译安装,一种是直接使用yum install kvm/apt-get install kvm,这部分就不详细说明了,我使用的是第二种方法,待后

 续需要研究源码的时候再下载源码,当前没有此部分的需求

 

2.Kvm镜像创建

 使用一下命令可以创建一个指定类型和指定大小的空镜像,镜像文件类型参加下面说明,不同类型的文件具有不同的效果,此处我没有一一尝试,直接使用系统推荐的默认类型qcow2,因为qcow2具有copy and write属性方便拷贝使用。

Image types

QEMU supports several image types. The "native" and most flexible type is qcow2, which supports copy on write, encryption, compression, and VM snapshots.

QEMU currently can use these image types or formats:

raw 
    (default) the raw format is a plain binary image of the disc image, and is very portable. On filesystems that support sparse files, images in this format only use the space actually used by the data recorded in them.
cloop 
    Compressed Loop format, mainly used for reading Knoppix and similar live CD image formats
cow 
    copy-on-write format, supported for historical reasons only and not available to QEMU on Windows
qcow 
    the old QEMU copy-on-write format, supported for historical reasons and superseded by qcow2
qcow2 
    QEMU copy-on-write format with a range of special features, including the ability to take multiple snapshots, smaller images on filesystems that don't support sparse files, optional AES encryption, and optional zlib compression
vmdk 
    VMware 3 & 4, or 6 image format, for exchanging images with that product
vdi 
    VirtualBox 1.1 compatible image format, for exchanging images with VirtualBox. 

 

qemu-img create -f qcow2 wi7.img 10G

 

从IOS安装系统

下面有个部分可能和网上其他的不同,网上很多资料都现实使用qemu但是我这里qemu显示无法使用,kvm相当于qemu的别名了,这部分具体的原因暂时就不去深究了,后续文章中再进行补充

以下两种方式是通过iso镜像或者光驱来向指定的镜像安装系统

kvm -m 1024 -hda win7.img -cdrom win7sp2.iso -boot d
kvm -m 1024 -hda winxp.img -cdrom /dev/cdrom -boot d

 

3.Kvm镜像克隆(创建)

 这部分说是创建其实就是克隆,把已经安装好的系统克隆出一个可用镜像,后续创建可以直接使用该镜像进行创建

  qcow2格式支持copy and write 我们可以使用qemu-img命令把已经安装配置好的系统创建成镜像后续可以直接使用该镜像进行虚拟机的创建方便快捷

  qemu-img create -f qcow2 -o backing_file=win7.img test01.img

创建后我们目录下就会多出来一个test01.img,就可以直接使用命令来启动机器

kvm -m 1024 -hda test01.img

 

4.Kvm远程连接

  启动kvm镜像的时候可以使用monitor监听socket,监听之后可以通过socket连接到monitor进行操作,例如:查看状态、关机等

  下面提供简单示例

kvm -m 1024 -hda test01.img -monitor unix:/home/guowei/working/kvm/kvmsocket,server,nowait

 

socat - UNIX-CONNECT:/home/guowei/working/kvm/kvmsocket
(qemu) system_powerdown //关机
//info可以查看信息
(qemu) info name  //名称  
(qemu) info vnc  //vnc信息
 

 

 5.Kvm Vnc连接

//创建镜像指定vnc server信息
kvm -smp 16 -m 1024 -hda test01.img -monitor unix:/home/guowei/working/kvm/kvmsocket,server,nowait -name guowei -uuid 5a35a426-f7ce-11dd-abd2-0017f227cfc7 -nographic -vnc 127.0.0.1:1

 

//使用socat连接monitor,同时可以使用sendkey命令发送ctrl-alt-delete快捷键,使用set_password vnc 设置vnc连接密码

socat - UNIX-CONNECT:/home/guowei/working/kvm/kvmsocket
sendkey ctrl-alt-delete

#设置密码
set_password vnc 1111111

 

6.使用vncview连接虚拟机

  使用vncviewer可以远程连接虚拟机同步屏幕

vncviewer 127.0.0.1:5901

 

7. kvm虚拟主机主机名称和密码修改

 可以使用 CloudBaseInit来操作虚拟主机的主机名称和密码,CloudBaseInit密码注入是通过客户端下载服务器的meta_data.json数据文件进行对比注入的,第一次注入之后会在注册 表中更新HKEY_LOCAL_MACHINE/SOFTWARE/Cloudbase Sloutions/Cloudbase-Init/***/Plugins/SetUserPasswordPlugin值为1,表示下次不再更新,我们处理的时候需要保持SetUserPasswordPlugin始终为0,即每次启动都要检测密码是否需要修改。为了实现功能就需要修改 CloudBaseInit的脚本,增加特殊判断不更新密码重置状态。

CloudBaseInit有关于初始化的所有脚本在对应的安装目录下的/Cloudbase-Init/Python27/Lib /site-packages/cloudbaseinit目录下,我们需要修改的就是该目录下的init.py,对比修改内容如下:

68,73c68
<                 #notice:Execture SetUserPasswordPlugin and do not change status
<                 #Check Password everytime when service start
<                 if plugin_name == "SetUserPasswordPlugin":
<                    LOG.info('Execture SetUserPasswordPlugin  plugin  do not update status and continue')
<                 else:
<                   self._set_plugin_status(osutils, instance_id, plugin_name,
---
>                 self._set_plugin_status(osutils, instance_id, plugin_name,


以上增加了对SetUserPasswordPlugin插件的特殊判断,有一点需要注意,增加内 容的前面空白地方需要使用空格不能使用tab,如果是空格的话服务可以正常启动,如果是tab的话服务就无法启动报启动失败的错误,具体原因 不太清楚,这部分可以当作一个潜规则来处理。创建虚拟机的时候可以直接使用附件里面的init.py进行替换,对于多个虚拟机 修改密码的问题后续再进行研究 

 

meta_data.json数据使用如下内容

{     
	"admin_pass": "Testi123456",
	"uuid": "d8e02d56-2648-49a3-bf97-6be8f1204f38",     
	"availability_zone": "nova",     
	"hostname": "testhostname",    
	"launch_index": 0,     
	"meta": {         
		"priority": "low",         
		"role": "webserver"    
	},     
	"public_keys": {         
		"mykey": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDYVEprvtYJXVOBN0XNKVVRNCRX6BlnNbI+USLGais1sUWPwtSg7z9K9vhbYAPUZcq8c/s5S9dg5vTHbsiyPCIDOKyeHba4MUJq8Oh5b2i71/3BISpyxTBH/uZDHdslW2a+SrPDCeuMMoss9NFhBdKtDkdG9zyi0ibmCP6yMdEX8Q== Generated by Nova\n"    
	}
} 

 

参考:

https://en.wikibooks.org/wiki/QEMU/Images

http://www.nico.schottelius.org/blog/control-and-shutdown-qemu-kvm-vm-via-unix-socket/

http://www.nico.schottelius.org/blog/tunneling-qemu-kvm-unix-socket-via-ssh/

 https://cloudinit.readthedocs.org/en/latest/

http://www.cloudbase.it/cloud-init-for-windows-instances/

http://cloudinit.readthedocs.org/en/latest/index.html

https://blueprints.launchpad.net/cloud-init/+spec/set-image-root-password

 

//web vnc

https://github.com/InstantWebP2P/peer-vnc

posted @ 2015-06-16 08:20  davygeek  阅读(720)  评论(0编辑  收藏  举报