ubuntu server安装kvm

参考资料:

1. https://help.ubuntu.com/community/KVM

2.http://wiki.ubuntu.org.cn/Kvm%E6%95%99%E7%A8%8B

3.http://tkg.im.ncue.edu.tw/?p=710

正文:

一些概念:

vbox 是由 qemu 改写而成,包含大量 qemu 代码。

  • 可以使用于"不支持"虚拟化技术的cpu。
  • 值得说的一点:vbox 在图形方面比较好,能进行2D 3D加速。
  • 但cpu控制不理想(估计是因为图形支持的缘故)。
  • 操作上有独立的图形界面,易于上手。

kvm 是linux内核包含的东西,使用qemu作为上层管理(命令行)。

  • 要求cpu 必须支持虚拟化。
  • 性能:作为服务器很好,可是图形能力十分的差。即使放电影,图像也是像刷油漆一样,一层一层的。
  • cpu使用率控制很好。
  • 控制上比较简洁,功能比较丰富:比如使用 “无敌功能”所有更改指向内存,你的镜像永远保持干净。 “母镜像”功能让你拥有n个独立快照点。 还有很多参数。另外,kvm作为内核级的虚拟机,刚开始发展关注的公司比较多——但是还没有达到商业应用的水平。

总体而言:在支持 虚拟化的情况下,vbox 和 kvm 的性能差不多,主要是面向对象不同:kvm使用于服务器,vbox使用于桌面应用。

=========================================================================================

安装前检查列表:

安装kvm,需要处理器支持硬件虚拟化,intel的VT-x和AMD的AMD-V都具有该功能。可以用如下命令检查;

egrep -c '(vmx|svm)' /proc/cpuinfo
如果返回结果为0,表明CPU不支持硬件虚拟
如果为1或者大于1的数字,说明支持硬件虚拟化--但你要保证在bios中开启了此功能。

kvm-ok
===========================================================================================
安装:

对于ubuntu10.04或以后的版本使用如下命令:
$ sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils
注:在这里包含了软件包ubuntu-vm-builder

  • libvirt-bin provides libvirtd which you need to administer qemu and kvm instances using libvirt

  • qemu-kvm (kvm in Karmic and earlier) is the backend

  • ubuntu-vm-builder powerful command line tool for building virtual machines

  • bridge-utils provides a bridge from your network to the virtual machines

  • 各个软件包的作用,以上,在libvrit-bin包中可以看到virsh这个命令,这个包主要是用来管理kvm虚拟机实例的。
  • qemu-kvm是后台,在(Karmic及之前的版本中是kvm)
  • ubuntu-vm-builder 是一个创建虚拟机的命令行管理工具
  • bridge-utils用于实现虚拟机桥接到网络

添加用户到组;

$ sudo adduser `id -un` libvirtd
Adding user '<username>' to group 'libvirtd' ...
之后,你要重新登录,使得之前的上面的命令生效。

检测安装是否成功:
virsh -c qemu:///system list
 Id Name                 State
----------------------------------

$

出现上述即成功安装。
sock文件的权限应该与下面类似:
$ sudo ls -la /var/run/libvirt/libvirt-sock
srwxrwx--- 1 root libvirtd 0 2010-08-24 14:54 /var/run/libvirt/libvirt-sock
==================================================================================

网络配置:
有两种方式可以让虚拟机访问外部网络:
1.NAT, 默认为此,即用户模式网络,
2.Bridge,这样,外部网络也可以访问虚拟机的OS

桥接模式配置:
安装软件包: qemu-kvm libcap2-bin
给特定用户授予CAP_NET_ADMIN权限。
  1.授予qemu



设定网络:/etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
        address 192.168.0.10
        network 192.168.0.0
        netmask 255.255.255.0
        broadcast 192.168.0.255
        gateway 192.168.0.1
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

======================================================================

虚拟机安装
以下三行摘自:http://tkg.im.ncue.edu.tw/?p=710
KVM 目前有三套主流管理工具:
virt-manager:一個圖形介面的管理工具,可以安裝在有 X window 的 Linux 機器上。
virt- install:一個用 python 撰寫的文字介面管理工具,Red Hat 開發。(ubuntu上的软件包是virtinst)
ubuntu-vm-builder:文字介面管理工 具,Canonical 開發。

没有xserver情况下安装
virt-install -d -n kvm-lucid -r 2048 --disk path=/data0/kvm/kvm-ubuntu.img,size=6,bus=virtio,cache=none --network bridge=br0,model=virtio --nographics --cdrom=/data1/software/ubuntu-10.04-server-amd64.iso
简单介绍参数;
-n 指定虚拟机的名字
-r 指定内存
--disk 虚拟硬盘存放位置
--network 网络,使用桥接
--nographics 纯文本安装
--cdrom指定iso文件

以上是不能正常执行的。
查阅资料http://www.techotopia.com/index.php/Installing_a_KVM_Guest_OS_from_the_Command-line_(virt-install)
--nographics参数
No graphical console will be allocated for the guest. Fully virtualized guests (Xen FV or QEmu/KVM) will need to have a text console configured on the first serial port in the guest (this can be done via the --extra-args option). Xen PV will set this up automatically. The command ’virsh console NAME’ can be used to connect to the serial device.
--》 需要串行端口,使用--extra-args参数指定
-x EXTRA, --extra-args=EXTRA
Additional kernel command line arguments to pass to the installer when performing a guest install from "--location".
依赖于location,需要指定location参数
-l LOCATION, --location=LOCATION
nstallation source for guest virtual machine kernel+initrd pair. The "LOCATION" can take one of the following forms:
  • DIRECTORY - Path to a local directory containing an installable distribution image
  • nfs:host:/path or nfs://host/path - An NFS server location containing an installable distribution image
  • http://host/path - An HTTP server location containing an installable distribution image
  • ftp://host/path - An FTP server location containing an installable distribution image

可用的包括目录,nfs,http,ftp,不能使用iso镜像。

按照上面的要求进行修改:

virt-install -n kvm-test -r 1024 --disk path=/home/akayer/kvm/kvm.img,size=6,bus=virtio,cache=none --network bridge=br0,model=virtio --nographics --location=ftp://192.168.10.10 -x console=ttyS0,115200

或者

virt-install -n kvm-test -r 1024 --disk path=/home/akayer/kvm/kvm.img,size=6,bus=virtio,cache=none --network bridge=br0,model=virtio --nographics --location=ftp://192.168.10.10 -x console=ttyS0,115200

都可以。

注:中间一步要求填写mirror地址,可以在主机上安装一个apache,然后将iso文件中的所有内容拷贝到/var/www/ubuntu/目录下,修改其权限,即可。

填写的是地址:192.168.10.10(不是http://192.168.10.10)

目录是:ubuntu

这样虚拟机即可以正常安装下去。

=================================================================================================

以上是no-X-server方式安装。

另一种是使用vnc安装

首先需要安装vnc4server

apt-get install vnc4server

开启

vnc4server

但是这样从另一台电脑上登录是灰色的没有输入界面

原因是

1.修改~/.vnc/xstartup文件,取消下面一行的注释

exec /etc/X11/xinit/xinitrc

2.修改/etc/X11/xinit/xinitrc的权限,从644--》755

如果是ubuntu server

显示一个命令行界面

如果是ubuntu desktop

不需vnc,直接virt-manager即可



原文地址:https://www.cnblogs.com/silenceli/p/3378421.html