Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9250586
  • 博文数量: 1669
  • 博客积分: 16831
  • 博客等级: 上将
  • 技术积分: 12594
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-25 07:23
个人简介

柔中带刚,刚中带柔,淫荡中富含柔和,刚猛中荡漾风骚,无坚不摧,无孔不入!

文章分类

全部博文(1669)

文章存档

2023年(4)

2022年(1)

2021年(10)

2020年(24)

2019年(4)

2018年(19)

2017年(66)

2016年(60)

2015年(49)

2014年(201)

2013年(221)

2012年(638)

2011年(372)

分类: 虚拟化

2012-07-17 11:26:18

kvm virsh console  

2011-08-15 23:13:17|  分类: 虚拟化+云计 |  标签: |字号 

总结

1.客户机支持 串口登陆---   virsh console rhel6

方法一:(在客户机添加)

1.1   /etc/grub.conf 中添加


serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1 
terminal --timeout=5 serial console                               


title Red Hat Enterprise Linux (2.6.32-71.el6.x86_64)
        root (hd0,0)
#        kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=UUID=807c23e0-693b-46bb-9485-7db5d665e303 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=ja_JP.UTF-8 KEYBOARDTYPE=pc KEYTABLE=jp106 crashkernel=auto rhgb quiet
        kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=UUID=807c23e0-693b-46bb-9485-7db5d665e303 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=ja_JP.UTF-8 KEYBOARDTYPE=pc KEYTABLE=jp106 crashkernel=auto console=ttyS0,115200n8 console=tty0
        initrd /initramfs-2.6.32-71.el6.x86_64.img

1.2修改 /etc/securetty 到最后添加 ttyS0


 方可登陆:

virsh console rhel6

--------------------------

方法二:安装直接支持:---增加内核参数


virt-install --name testvm \
--ram 1024 \
--vcpus 2
\
--os-type linux \
--os-variant rhel6.1 \
--disk path=/vmrepo/disk/testvm.img,size=12 \
 
--location ftp://192.168.2.1/rpm/rhel6/ \
--network bridge
=br1 \
–extra-args “console=tty0 console=ttyS0,115200n8″

--------------------------------------------------------

kvm使用的是qemu虚拟机程序。qemu有2种方式支持远程运行。

1.字符模式,就是把串口关联在当前的控制台,这样在安装的时候,安装程序的内核参数增加一个console=ttyS0

就可以让安装程序的控制台使用串口,也就是kvm运行的终端。

2.vnc模式 就是远程机器连接到本机的某个vnc端口,作为kvm的显示器,vnc模式灵活性比较大,我们采用这种模式。

先生成一个5G虚拟磁盘。

qemu-img create /home/kvm.img -f qcow2 10G

从debian.org下载 安装盘 netinst.iso

启动安装过程


kvm -vnc :0 -no-acpi -hda /home/kvm.img -cdrom /mnt/os/netinst.iso -boot d


然后,通过vncview或者rdesktop连接远程的192.168.xx.xx:0,

进行正常的安装过程。

(不需要安装GNOME等桌面包和vnc4server包,在另外一台计算机上用vncviewer连接安装系统)

按照以上方法在虚拟机安装XP同样可行



安装结束从新启动kvm,去掉光驱 不启动图形界面 后台运行。


kvm -vnc :0 -daemonize -hda /home/kvm.img -no-acpi -redir tcp:3389::3389 \
-M pc -m 256 -soundhw es1370 -local...



在KVM中安装了一个RHEL6.1,启动virsh命令想通过console命令连接至客户机的终端,结果输入命令后终端卡在这个状态:


virsh # console test
Connected to domain test
Escape character is ^]


敲任何键都没反应,求助万能的Google。原来想通过console连接客户机终端还要在客户机上做相应设置,大致如下:

添加ttyS0:

echo “ttyS0″ >> /etc/securetty

在/etc/grub.conf文件中为内核添加参数:

console=ttyS0

在/etc/inittab中添加agetty:

S0:12345:respawn:/sbin/agetty ttyS0 115200

重启客户机:

reboot

此时,再次尝试console命令可以正常使用:

virsh # console test Connected to domain test Escape character is ^] Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64 station32.loveky.info login:  
以前玩Xen,如果用半虚在安装的时候可以直接在主机的shell中显示guest的状况,也可以做相关操作。
KVM也有,通过 virsh console 链接到guest,但是结果是这样:

再怎么打也没反应。
后来研究了一下,写下来。

下面的操作都在 虚拟机上做:
修改 /boot/grub/menu.lst 在 kernel 行最后添加 console=ttyS0,115200
修改 /etc/inittab 添加 co:2345:respawn:/sbin/mingetty ttyS0 115200 vt100
修改  /etc/securetty 到最后添加 ttyS0

重启就好了,可以看见从进入内核引导后的全部过程。
说一下原理
修改 menu.1st 让内核吧输出输出至 ttyS0
在inittab里加一个ttyS0在系统启动时会生成一个ttyS0来接收内核的数据
还有在 securetty里面要有不然也出不来。

最后发现 virsh console有了整个启动过程,kvm连上去反而没有了,只有启动到内核之前的数据,不爽。
后来发现在 menu.1st 的kernel 这行加一个 console=tty0 就可以恢复默认的kvm输出。
不过这2个console是有优先级的,最后一个console优先级最高。
下面的截图是 左面是ttyS0  右边是tty0

vnc是tty0 数据明显比ttyS0多,启动数据全部在tty0。不过启动完成后都能操作,也没啥区别。
上面是实际用下来的感觉,没有查过相关文档。文档牛人可以绕道或留言。。。

 
 
 1  为KVM客户机添加virsh console支持

在KVM中安装了一个RHEL6.1,启动virsh命令想通过console命令连接至客户机的终端,结果输入命令后终端卡在这个状态:

virsh # console test Connected to domain testEscape character is ^]

敲任何键都没反应,求助万能的Google。原来想通过console连接客户机终端还要在客户机上做相应设置,大致如下:

添加ttyS0:

echo “ttyS0″ >>/etc/securetty

在/etc/grub.conf文件中为内核添加参数:

console=ttyS0

在/etc/inittab中添加agetty:

S0:12345:respawn:/sbin/agetty ttyS0 115200

重启客户机:

reboot

此时,再次尝试console命令可以正常使用:

virsh # console testConnected to domain test Escape character is ^]Red Hat Enterprise Linux Server release 6.1 (Santiago) Kernel 2.6.32-131.0.15.el6.x86_64 on an x86_64station32.loveky.info login:

 

参考:

 

2 Error: No hypervisor options were found for this connection。

运行qemu-kvm命令一直提示错误open /dev/kvm: No such file or directory,同时在图形化管理程序virt-manager中无法添加虚拟机提示错误Error: No hypervisor options were found for this connection。

首先到确认了我的CPU(Intel? Core?2 Duo Processor T5600)是支持虚拟化技术的。

接着去BISO里检查是否开启了虚拟化支持。在我的BIOS里有一项叫VirtualizationTechnology默认是关闭的,打开该选项。重启电脑,进入系统后载入kvm模块:

modprobe kvmmodprobe kvm-intel

系统依旧报错:

FATAL: Error inserting kvm_intel (/lib/modules/2.6.32-131.0.15.el6.x86_64/kernel/arch/x86/kvm/kvm-intel.ko): Operation not supported

在百思不得其解的时候发现了问题所在,我修改BIOS的方式是 reboot->进入BISO,修改选项->进入系统
后来发现在修改BIOS以后一定要给系统断一次电才能生效,也就是 reboot->进入BIOS,修改选项->断电->开机,进入系统

再次载入kvm,kvm-intel两个模块,没有报错,此时再次运行qemo-kvm不再提示/dev/kvm文件不存在。问题解决。

rhel6 kvm virsh console

对象客户机
1,编辑inittab文件
添加下面一行:
T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100
注:默认有类似的配置,不过是被注释掉的,可以根据注释掉的配置来进行修改

2,编辑/boot/grub/menu.lst文件,红色的部分为追加的部分。

serial –speed=115200 –unit=0 –word=8 –parity=no –stop=1
terminal –timeout=5 serial console

title Debian GNU/Linux, kernel 2.6.26-2-686
root (hd0,0)
kernel /vmlinuz-2.6.26-2-686 root=/dev/mapper/s01v01.unumu.com-root ro 
quiet splash console=tty0 console=tyS0,115200n8
initrd /initrd.img-2.6.26-2-686

–extra-args “console=tty0 console=ttyS0,115200n8″


The point is: Let the guest use serial port 0 as its default console.

In linux, this can be achieved by passing the console= kernel parameter. For example, we can start a CentOS 5.6 install like this:

virt-install --name testvm \
--ram 1024 \
--vcpus 2 \
--os-type linux \
--os-variant rhel5.4 \
--disk path=/vmrepo/disk/testvm.img,size=12 \
 --location http://ftp.twaren.net/Linux/CentOS/5/os/x86_64/ \
--network bridge=br1 \
--extra-args console=ttyS0

Notes:

  • Here, I use rhel6 as the KVM host, if you use other Linux distribution/version as host, the option of virt-install may be slightly different
  • use the –extra-args to pass kernel parameter, tell kernel to set serial port 0 as console.
  • Use the –location option to specify an installation URL, this is important and required. You may want to use the –cdrom option and install from DVD, but that will not work, because if you use cdrom instead of URL, there will be no way for virt-install to pass the console=parameter.

Run this command, then you can continue the installation as usual(use text mode/or start VNC based installation)



KVM ゲスト OS のシリアルコンソール(在客户机添加

RHEL60 を KVM ゲストとする際に、virsh console で仮想シリアル接続するための設定です。

まず、GRUB の画面出力と Kernel メッセージの出力先を仮想シリアル端末に設定します。

/etc/grub.conf

# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/vda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/vda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu

#在客户机添加
serial --speed=115200 --unit=0 --word=8 --parity=no --stop=1 # GRUB の出力先を
terminal --timeout=5 serial console                                 # 仮想シリアルに設定


title Red Hat Enterprise Linux (2.6.32-71.el6.x86_64)
        root (hd0,0)
#        kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=UUID=807c23e0-693b-46bb-9485-7db5d665e303 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=ja_JP.UTF-8 KEYBOARDTYPE=pc KEYTABLE=jp106 crashkernel=auto rhgb quiet
        kernel /vmlinuz-2.6.32-71.el6.x86_64 ro root=UUID=807c23e0-693b-46bb-9485-7db5d665e303 rd_NO_LUKS rd_NO_LVM rd_NO_MD rd_NO_DM LANG=ja_JP.UTF-8 KEYBOARDTYPE=pc KEYTABLE=jp106 crashkernel=auto console=ttyS0,115200n8 console=tty0
        initrd /initramfs-2.6.32-71.el6.x86_64.img
# ↑ Kernel メッセージの出力先を仮想シリアルに設定


続いて、仮想シリアルコンソールからのログインを受け付けます。先に説明した upstart による mingetty 起動スクリプトに agetty の起動を追加します。
#在客户机添加


/etc/init/start-ttys.conf
#
# This service starts the configured number of gettys.

start on stopped rc RUNLEVEL=[2345]
env ACTIVE_CONSOLES=/dev/tty[1-6]
env X_TTY=/dev/tty1
task
script
        . /etc/sysconfig/init
        for tty in $(echo $ACTIVE_CONSOLES) ; do
                [ "$RUNLEVEL" = "5" -a "$tty" = "$X_TTY" ] && continue
                initctl start tty TTY=$tty
        done
        initctl start serial DEV=ttyS0 SPEED=115200   # 仮想シリアルコンソールからのログインを受け付け
end script


修改  /etc/securetty 到最后添加 ttyS0


loop デバイスの追加方法

/etc/udev/makedev.d/50-udev.nodes が無くなっているので道に迷った気分になりますが???。まず、デフォルトの loop0 ? loop7 は、50-udev.nodes ではなく、start_udev の中で直接作成されるようになっています。

/sbin/start_udev から抜粋

        for i in 0 1 2 3 4 5 6 7; do
                [ -b /dev/loop$i ] || /bin/mknod -m 0640 /dev/loop$i b 7 $i
                /bin/chown root:disk /dev/loop$i
        done



start_udev の中に 50-udev.nodes を参照して追加のデバイスを作成する仕組みは残っていますので、loop8 以降については、これまで同様に 50-udev.nodes で指定することができます。下記は、loop8 ? loop511 を追加しています。

# for i in $(seq 8 511); do echo "loop$i" >> /etc/udev/makedev.d/50-udev.nodes


ただし、256 個以上の loop デバイスを作成する場合は、/etc/makedev.d/01linux-2.6.x で指定される最大値も修正しておきます。

/etc/makedev.d/01linux-2.6.x

#b $STORAGE               7   0  1 256 loop%d   # デフォルトは最大 256 個
b $STORAGE               7   0  1 2046 loop%d   # 最大 2046 個に変更



----------------------

说明:这篇笔记为更好的管理虚拟化服务,如果仅仅是测试虚拟化,查看另一篇笔记:

补充:这篇笔记使用的libvirt版本为0.6.3,kernel为2.6.18-194.26.1.el5
遇到了几个bug,建议首先升级kernel,然后升级libvirt,kvm。
源:
yum clean all
yum makecache (一定要做)
yum list kernel
20110428:kernel 2.6.18-238.9.1.el5

yum update kernel kernel-devel.x86_64 kernel-headers.x86_64


一、安装虚拟化管理工具
1、yum install virt-manager libvirt libvirt-python python-virtinst
  1. 1、kmod-kvm : kvm kernel module(s)
  2. 2、kvm : Kernel-based Virtual Machine
  3. 3、kvm-qemu-img : Qemu disk image utility
  4. 4、kvm-tools : KVM debugging and diagnostics tools
  5. 5、python-virtinst : Python modules and utilities for installing virtual machines
  6. 6、virt-manager : Virtual Machine Manager (GUI app, to install and configure VMs)
  7. 7、virt-viewer: Virtual Machine Viewer (another lightweight app to view VM console and/or install VMs)
  8. 9、bridge-utils : Utilities for configuring the Linux Ethernet bridge (this is recommended for KVM networking)
KVM Package Group RHEL comes with KVM software group which includes full virtualization support with KVM. You can list all packages in the group as follows:

# yum groupinfo KVM


2、Start the libvirtd daemon service
service libvirtd start

关于错误:
注:该错误出现在centos5.4,libvirtd 0.6.3
重启libvirtd后,启动guest时会出现下面的问题。
Apr 22 09:16:20 srv4 kernel: libvirtd[27156]: segfault at 00000000444e3000 rip 0000003e5501e161 rsp 00000000444dfcd0 error 4
Apr 22 09:16:20 srv4 libvirtd: 09:16:20.739: error : internal error Unable to daemonize QEMU process 
重启了host,问题解决
原因:
Everytime the virGetCapabilities() method is run, it destroys the existing virCapsPtr object and creates it again....without any of the selinux info. virt-manager runs this method. so once virt-manager has run, subsequent calls to get the security info will give the crash you see.

二、有关网络
1、一般网络(又名NAT):guest可以访问外网,外网不能访问guest。
安装了libvirt后,一个NAT网络即可用。host被设置为192.168.122.1(virbr0),所有的guest将被分配为192.168.122.x
如果看不到virbr0
或者在virsh start 时出现下面的错误:
Apr 22 08:50:37 srv4 libvirtd: 08:50:37.839: error : internal error Failed to add tap interface 'vnet%d' to bridge 'virbr0' : No such device 

查看/var/log/message.log
Apr 22 08:57:10 srv4 dnsmasq[26000]: failed to open pidfile /var/run/libvirt/network/default.pid: Permission denied
Apr 22 08:57:10 srv4 dnsmasq[26000]: FAILED to start up

手动启动default network
virsh net-start default
error: Failed to start network default
error: internal error '/usr/sbin/dnsmasq --strict-order --bind-interfaces --pid-file=/var/run/libvirt/network/default.pid --conf-file=  --listen-address 192.168.122.1 --except-interface lo --dhcp-range 192.168.122.2,192.168.122.254 --dhcp-lease-max=253' exited with non-zero status 3 and signal 0: 
dnsmasq: failed to open pidfile /var/run/libvirt/network/default.pid: Permission denied
解决办法:
我的当前系统:selinux-policy.noarch                                               2.4.6-203.el5  
yum updata selinux-policy.noarch
selinux-policy.noarch                                               2.4.6-279.el5_5.2 
service libvirtd restart
注:需要重启host,上面写了原因
  1. # ifconfig virbr0
  2. virbr0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
  3. inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
  4. inet6 addr: fe80::200:ff:fe00:0/64 Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:57 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:0
  9. RX bytes:0 (0.0 b) TX bytes:10962 (10.7 KiB)
一个故障:
不论是NAT,还是Bridge模式,guest都无法与其他服务器通信,而且NAT模式时无法通过dhcp获取IP。没有查到错误原因。
不使用virsh来启动,按照http://blog.chinaunix.net/space.php?uid=1838361&do=blog&id=234090文 章介绍的方法来启动时,NAT及bridge模式都可以正常访问其他服务器。所以按照该文章的方法,在guest的xml文件中定义interface的 部分,指定虚拟网卡为tap0,网络正常了,将指定的网卡名去掉,由virsh自动分配为vnet0,网络也是正常的,也可以通过dhcp获取IP了。原 因未知。
另:virsh启动的log文件位于:/var/log/libvirt/qemu/

20110711:
nat方式无法获取IP,service libvirtd restart 重启服务,恢复。

关于NAT方式,guest可以获取ip,但无法与访问公网:
有多个网卡?你的物理机由哪个网卡访问公网?例如,我的物理机由br0访问公网,则,修改default.xml文件,添加forward的dev参数。
  1. virsh # net-dumpxml default
  2. <network>
  3.   <name>default</name>
  4.   <uuid>8d004490-ee73-4e36-b9ef-821d7e73f9f6</uuid>
  5.   <forward dev='br0' mode='nat'/>
  6.   <bridge name='virbr0' stp='on' forwardDelay='0' />
  7.   <ip address='192.168.122.1' netmask='255.255.255.0'>
  8.     <dhcp>
  9.       <range start='192.168.122.2' end='192.168.122.254' />
  10.     </dhcp>
  11.   </ip>
  12. </network>
  1. virsh # net-define /etc/libvirt/qemu/network/default.xml
  2. virsh # net-destroy default
  3. virsh # net-start  default

2、Bridge网络(又名:共享物理网卡):guest可以访问外网,外网也可以访问guest
# cd /etc/sysconfig/network-scripts/
# cp ifcfg-eth0 ifcfg-br0 
两个配置文件内容分别为:
/etc/sysconfig/network-scripts/ifcfg-eth0
  1. DEVICE=eth0
  2. TYPE=Ethernet
  3. ONBOOT=yes
  4. NM_CONTROLLED=no
  5. BRIDGE=br0
/etc/sysconfig/network-scripts/ifcfg-br0
  1. DEVICE=br0
  2. TYPE=Bridge
  3. NM_CONTROLLED=no
  4. BOOTPROTO=static
  5. IPADDR=221.193.136.121
  6. NETMASK=255.255.255.0
  7. ONBOOT=yes
The NM_CONTROLLED=no option was added because both device should not be controlled by the Network Manager for bridge to work. 
重启network(当前连接会丢失,确保你总是可以访问host,否则要格外小心
service network restart

关于错误:Bringing up interface eth0:  tg3 device eth0 does not seem to be present, delaying initialization.
原因:br0里没有加配置 TYPE=Bridge
Disable Netfilter processing in the bridged traffic.
/etc/sysctl.conf文件中添加:
  1. net.bridge.bridge-nf-call-ip6tables = 0
  2. net.bridge.bridge-nf-call-iptables = 0
  3. net.bridge.bridge-nf-call-arptables = 0
  4. net.bridge.bridge-nf-filter-vlan-tagged = 0
        验证是否生效
ifconfig
br0现在扮演eth0的角色。
br0       Link encap:Ethernet  HWaddr 00:14:5E:C2:1E:40  
          inet addr:221.193.136.121   Bcast:221.193.136.255  Mask:255.255.255.0 
          inet6 addr: fe80::214:5eff:fec2:1e40/64 Scope:Link 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
          RX packets:125 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:81 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:0 
          RX bytes:16078 (15.7 KiB)  TX bytes:18542 (18.1 KiB) 
eth0      Link encap:Ethernet  HWaddr 00:14:5E:C2:1E:40  
          inet6 addr: fe80::214:5eff:fec2:1e40/64 Scope:Link 
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
          RX packets:206 errors:0 dropped:0 overruns:0 frame:0 
          TX packets:58 errors:0 dropped:0 overruns:0 carrier:0 
          collisions:0 txqueuelen:1000 
          RX bytes:27308 (26.6 KiB)  TX bytes:13881 (13.5 KiB) 
          Interrupt:74 Memory:da000000-da012800

也可以通过下面的命令查看桥接网络,可以看到eth0加到了桥接设备br0中:
# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.000000000000       yes
br0             8000.000e0cb30550       no              eth0

三、创建KVM虚拟机的XML定义文件
关于KVM XML format  
1.1 用于从磁盘启动的XML文件
1、kvm guest 定义开始
  1. <domain type='kvm'>
2、guest的short name。由字母和数字组成,不能包含空格
  1. <name>centosvm1</name>
3、uuid,由命令行工具 uuidgen生成。
  1. <uuid>bdbb89fb-57d1-4d01-b3b7-ff33a9346ae6</uuid>
4、在不reboot guest的情况下,guset可以使用的最大内存,以KB为单位
  1. <memory>2048000</memory>
5、guest启动时内存,可以通过virsh setmem来调整内存,但不能大于最大可使用内存。
  1. <currentMemory>1024000</currentMemory>
6、分配的虚拟cpu
  1. <vcpu>2</vcpu>
7、有关OS
x86架构:i686  
hvm:全虚拟化
kernel:指定guest使用的内核,如果使用ISO(安装时)或guset系统中(系统已经安装完成)的内核,不需要指定该项
initrd:指定guest使用的ram disk,如果使用ISO(安装时)或guest系统中(系统已经安装完成)的ram disk,不需要指定该项
注:kernel 和initrd文件位于RHEL系统光盘的images/pxeboot目录,拷贝这两个文件到本地磁盘,并指定路径。
注:这两个元素,如果是为了安装guset而指定,在安装完成以后即可以删除。
如 果host开启了SELINUX,you also need to change the type of security context for the files to virt_image_t to allow libvirtd to access them for booting: 
  1. # chcon -t virt_image_t /tmp/vmlinuz-rhel54
  2. # chcon -t virt_image_t /tmp/initrd-rhel54.img
After you change the security context, verify that the correct security context is assigned to them as shown in the following example: 
  1. # ls -Z /tmp|grep virt
  2. -r--r--r-- root root root:object_r:virt_image_t initrd-rhel54.img
  3. -r--r--r-- root root root:object_r:virt_image_t vmlinuz -rhel54
boot:指定启动设备,可以重复多行,指定不同的值,作为一个启动设备列表。
可以在cmdline元素中添加启动参数,例如,使用kickstart文件:
method=
ks=
  1. <os>
  2.       <type arch='x86_64' machine='pc'>hvm</type>
  3.       <kernel>/tmp/vmlinuz-rhel54</kernel>
  4.       <initrd>/tmp/initrd-rhel54.img</initrd>
  5.       <boot dev='hd'/>
  6.     </os>
8、处理器特性
  1. <features>
  2.       <acpi/>
  3.       <apic/>
  4.       <pae/>
  5.     </features>
关于处理器特性,查看:

9、时钟。使用本地时间:localtime
   

仍然需要再计划任务中添加时间同步:
*/15 * * * * (/usr/sbin/ntpdate 210.72.145.44 > /dev/null 2>&1;/sbin/hwclock -w)
10、定义了在kvm环境中power off,reboot,或crash时的默认的动作为destroy。其他允许的动作包括:restart,preserve,rename-restart.
在 这个例子中,destory之所以也用在了on_reboot元素中,是因为一旦安装完成,期望的guest动作是stop,以便从the installed guest operating system而不是从the installation kernel or ISO编辑guest的定义。
destroy:停止该虚拟机。相当于关闭电源
  1.     <on_poweroff>destroy</on_poweroff>
  2.     <on_reboot>destroy</on_reboot>
  3.     <on_crash>destroy</on_crash>
11、设备定义开始
  1. <devices>
12、模拟元素,此处写法用于kvm的guest(xen怎么写?)
  1. <emulator>/usr/libexec/qemu-kvm</emulator>  
13、用于kvm存储的文件。在这个例子中,在guest中显示为IDE设备。
使用qemu-img命令创建该文件,kvm image的默认目录为:/var/lib/libvirt/images/
  1. <disk type='file' device='disk'>
  2.  cache='none'/>
  3.         <source file='/var/lib/libvirt/images/kvm3.img'/>
  4.         <target dev='hda' bus='ide'/>
  5.       </disk>
这里需要注意:使用libvirt 0.6.3写的xml,没有参数,当将该xml放到libvirt 0.8版本时,该参数会默认为raw,所以会造成在0.6中可以启动的guest无法启动,更改参数即可
使用virtio:
采用普通的驱动,即硬盘和网卡都采用默认配置情况下,硬盘是 ide 模式,而网卡工作在 模拟的rtl 8139 网卡下,速度为100M 全双工。采用 virtio 驱动后,网卡工作在 1000M 的模式下,硬盘工作是SCSI模式下。
  1. <disk type='file' device='disk'>
  2.    <driver name='qemu' type='raw' cache='none'/>
  3.    <source file='/usr/local/kvm/vmsample/disk.os'/>
  4.    <target dev='vda' bus='virtio'/>
  5. </disk>
硬盘采用 virtio 后,安装windows 系统,将不能正常的识别硬盘,解决的方法是:
从kvm 的官网下载virtio的驱动iso。
1. 先采用ide模式安装系统。
2. 安装完成后,添加一个virtio模式的硬盘。
3. 启动vm后,系统会自动搜索 SCSI的驱动,找到下载的virtio 驱动后,安装即可。
4. 修改vm 配置文件,删除掉添加的 vitro 硬盘后,修改ide硬盘为 virtio模式即可。

A Note About Virtio Drivers

Virtio is paravirtualized drivers for kvm/Linux. With this you can can run multiple virtual machines running unmodified Linux or Windows VMs. Each virtual machine has private virtualized hardware a network card, disk, graphics adapter, etc. According to Redhat:

Para-virtualized drivers enhance the performance of fully virtualized guests. With the para-virtualized drivers guest I/O latency decreases and throughput increases to near bare-metal levels. It is recommended to use the para-virtualized drivers for fully virtualized guests running I/O heavy tasks and applications. 


关于cache=none
如果不加该参数,默认cache policy为write through。
kvm官方及IBM都推荐使用raw,设置cache为none,以关闭kvm磁盘的cache策略,让数据可以直接落在实体硬盘上(实体机上其实也有磁盘cache的,并不需要kvm为cache操心)
For the best storage performance on guest operating systems that use raw disk volumes or partitions,completely avoid the page cache on the host.
下面的帖子中有一段关于cache的内容:

  
补充:可以定义多个磁盘,其他可行的磁盘定义:
Logical volume device:
  1. <disk type='block' device='disk'>
  2.             <source 'dev=/dev/mapper/VolGroup01-LVM1'/>
  3.             <target dev='hdb'/>
  4.         </disk>
Disk partition:
  1. <disk type='block' device='disk'>
  2.             <source dev='/dev/sda4'/>
  3.             <target dev='hdb' bus='ide'/>
  4.         </disk>
CD-ROM device:
  1. <disk type='block' device='cdrom'>
  2.             <source 'dev=/dev/sde'>
  3.             <target dev='hdb'/>
  4.             <readonly/>
  5.         </disk>
 
 14、使用网桥类型。确保每个kvm guest的mac地址唯一。将创建tun设备,名称为vnetx(x为0,1,2...)
  1. <interface type='bridge'>
  2.         <source bridge='br0'/>
  3.         <mac address="3B:6E:01:69:3A:11"/>
  4. </interface>
 补充:使用默认的虚拟网络代替网桥,即guest为NAT模式。也可以省略mac地址元素,这样将自动生成mac地址。
  1. <interface type='network'>
  2.             <source network='default'/>
  3.             <mac address="3B:6E:01:69:3A:11"/>
  4.         </interface>
 默认分配192.168.122.x/24的地址,也可以手动指定。网关为192.168.122.1

使用virtio:
采用普通的驱动,即硬盘和网卡都采用默认配置情况下,硬盘是 ide 模式,而网卡工作在 模拟的rtl 8139 网卡下,速度为100M 全双工。采用 virtio 驱动后,网卡工作在 1000M 的模式下,硬盘工作是SCSI模式下。
rhel5.6 guest with virtio interface can not boot successfully if the system installation would use the ide interface

  1. <interface type='bridge'>
  2.    <source bridge='br1'/>
  3.    <model type='virtio' />
  4. </interface>
性能:通过物理机上拷贝
普通驱动:26.1M
virtio驱动:44.7M  
因为交换机为100M,无法测试从其他物理机上拷贝。

 15、输入设备
  1. <input type='mouse' bus='ps2'/>
 
 16、定义与guset交互的图形设备。在这个例子中,使用vnc协议。listen的地址为host的地址。prot为-1,表示自动分配端口号,通过以下的命令查找端口号:
virsh vncdisplay     
注意:是passwd 而不是password 
VNC is configured to listen on 127.0.0.1 by default. To make it listen on all public interfaces, edit /etc/libvirt/qemu.conf file.
  1. <graphics type='vnc' listen='221.191.134.123' passwd='123456' port='-1' autoport='yes' keymap='en-us'/>  
 17、设备定义结束
  1. </devices>
18、KVM定义结束
  1. </domain>
  完整的xml
  1. <domain type='kvm'>
  2.   <name>centoskvm3</name>
  3.   <uuid>bdbb89fb-57d1-4d01-b3b7-ff33a9346ae6</uuid>
  4.   <memory>2048000</memory>
  5.   <currentMemory>1024000</currentMemory>
  6.   <vcpu>2</vcpu>
  7.   <os>
  8.     <type arch='x86_64' machine='pc'>hvm</type>
  9.     <boot dev='hd'/>
  10.   </os>
  11.   <features>
  12.     <acpi/>
  13.     <apic/>
  14.     <pae/>
  15.   </features>
  16.   <clock offset='localtime'/>
  17.   <on_poweroff>destroy</on_poweroff>
  18.   <on_reboot>destroy</on_reboot>
  19.   <on_crash>destroy</on_crash>
  20.   <devices>
  21.     <emulator>/usr/libexec/qemu-kvm</emulator>
  22.     <disk type='file' device='disk' cache='none'>
  23.       <source file='/usr/local/kvm/centvm3.img'/>
  24.       
  25.     </disk>
  26.     <interface type='bridge'>
  27.       <mac address='3b:6e:01:69:3a:11'/>
  28.       <source bridge='br0'/>
  29.      
  30.     </interface>
  31.     <input type='mouse' bus='ps2'/>
  32.     <graphics type='vnc' port='-1' autoport='yes' listen='221.196.131.121' keymap='en-us' passwd='123456'/>
  33.   </devices>
  34. </domain>
   1.2 用于从ISO启动的XML文件
virt-install --name vm1 --ram 512 --disk path=/usr/local/kvm/vm1/disk.os,size=10 --accelerate --cdrom=/usr/local/centos5.6/CentOS-5.6-x86_64-bin-DVD-1of2.iso --vnc 0.0.0.0:3
  1. <domain type='kvm'>
  2.   <name>vmsample</name>
  3.   <uuid>fee1dfbb-a8f8-9144-d15a-852dbc175219</uuid>
  4.  <memory>2677721</memory>
  5.   <currentMemory>2677312</currentMemory>
  6.   <vcpu>2</vcpu>
  7.   <os>
  8.     <type arch='x86_64' machine='rhel5.4.0'>hvm</type>
  9.     <boot dev='cdrom'/>
  10.   </os>
  11.   <features>
  12.     <acpi/>
  13.     <apic/>
  14.     <pae/>
  15.   </features>
  16.   <clock offset='localtime'>
  17.     <timer name='pit' tickpolicy='delay'/>
  18.   </clock>
  19.   <on_poweroff>destroy</on_poweroff>
  20.   <on_reboot>destroy</on_reboot>
  21.   <on_crash>destroy</on_crash>
  22.   <devices>
  23.     <emulator>/usr/libexec/qemu-kvm</emulator>
  24.     <disk type='file' device='disk'>
  25.       <driver name='qemu' type='raw' cache='none'/>
  26.       <source file='/usr/local/kvm/vmsample/disk.os'/>
  27.         <target dev='vda' bus='virtio'/>
  28.     </disk>

  29.    
  30.      
  31.      
  32.      
  33.    
  34.     <interface type='bridge'>
  35.       <source bridge='br0'/>
  36.     </interface>
  37.     <interface type='bridge'>
  38.       <source bridge='br1'/>
  39.       <model type='virtio' />
  40.     </interface>
  41.     <input type='mouse' bus='ps2'/>
  42.     <graphics type='vnc' port='-1' autoport='yes' listen='1.1.1.1' passwd='11111v' keymap='en-us'/>
  43.     <video>
  44.       <model type='cirrus' vram='9216' heads='1'/>
  45.     </video>
  46.   </devices>
  47. </domain>
启动后,磁盘空间为0?进入virsh ,etdi 虚拟机.xml,查看磁盘类型,为raw?你创建的磁盘是qcow2? 
四、 使用virsh创建一个kvm虚拟机
 当创建或编辑了一个kvm xml文件后,使用virsh tool创建kvm。
 1、定义KVM
virsh define
For example:
# virsh define /usr/local/kvm/centvm3.xml
在后台,这个命令拷贝该xml文件到/etc/libvirt/qemu目录,文件名为xml里定义的kvm名称。并且正式定义一个新的kvm。
如何修改?
方法1:
virsh edit
virsh define
方法2:
virsh undefine
virsh define
2、启动KVM
virsh start
For example:
# virsh start centosvm3
查看vnc信息:
virsh vncdisplay centosvm3
也可以通过virt-viewer查看
virt-viewer
关闭kvm虚拟机:virsh destory
查看运行中的虚拟机:
virsh list

3、克隆虚拟机
virt-clone -o vm1 -n vm2 -f /usr/local/kvm/vm2/disk.os /usr/local/kvm/vm2/disk.data -d --prompt
因为我的vm1虚拟机有两个磁盘,所以,在克隆时需要制定两个磁盘文件,如果你不清楚顺序,可以将-f部分去掉,后面的--prompt,交互式,可以根据提示输入磁盘文件。
注意:克隆的xml文件会丢失vnc的passwd参数,需要手动添加。

克隆后,某个磁盘变为实际使用的大小?
qemu-img create -F qcow2 -b ./disk.data.1 -f raw disk.data 80G

错误:
在convert 或 create时,报错:
qemu-img could not open 磁盘文件
问题:当 qemu-img info 磁盘文件存在backing file 项时,会报这个错误。
backing file 来源:当使用qemu-img create创建磁盘时,会创建这个项
另外:当虚拟机正在使用该磁盘时,qemu-img info 磁盘文件时,也会出现这个错误。

关于qemu-img convert:
会创建新格式的磁盘,旧格式的磁盘仍然存在。

关于error: unknown OS type hvm
kernel没有加载模块,检查bios里是否开启了虚拟化

 
杂项:
1、关闭guest的磁盘监控
# service smartd stop # chkconfig --del smartd


一篇不错的文章:
 





阅读(3506) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~