手机
当前位置:查字典教程网 >操作系统 >Ubuntu/Debian >在Ubuntu系统上安装部署KVM虚拟机的基本教程
在Ubuntu系统上安装部署KVM虚拟机的基本教程
摘要:一、安装所需软件包#apt-getinstallkvm#apt-getinstallqemu#apt-getinstalllibvirt-b...

一、安装所需软件包

#apt-get install kvm

#apt-get install qemu

#apt-get install libvirt-bin libvirt管理

#apt-get install bridge-utils网络桥接

如果需要支持kvm虚机从网卡启动,还需安装kvm-pxe包

#apt-get install kvm-pxe

二、建立一个虚机

可以参考libvirt官网<http://libvirt.org/formatdomain.html>,自己新建一个xml定义域文件

其中可以定义虚机的域名、UUID、启动选项<hd、cdrom、network>

内存大小、cpu个数、硬盘与光驱镜像路径、网卡相关等

虚机磁盘格式为qcow2

下面为实例:建一个centos系统

###ubuntu下建议vim用vim-gnome###

删除自带的vim

#apt-get remove vim-tiny

安装vim-gnome

#apt-get install vim-gnome

1、创建一个目录用于存放虚机相关文件

#mkdir /datastore

2、先要创建网络桥接设备<保证eth0没有IP,传递给vSwich0;vSwitch0名称可自定义>

#ifconfig eth0 先得到eth0的IP,假设为192.168.1.22

#/sbin/ifconfig eth0 0.0.0.0 promisc up

#/usr/sbin/brctl addbr vSwitch0

#/usr/sbin/brctl addif vSwitch0 eth0

#/sbin/ifconfig vSwitch0 192.168.1.22 netmask 255.255.255.0 up

#/sbin/route add default gw 192.168.1.1 dev vSwitch0

以上命令重启失效,可以写一个脚本,然后在/etc/rc.local中添加执行路径

#!/bin/sh

if /sbin/ifconfig vSwitch0 down > /etc/null 2>&1 &

then

/usr/sbin/brctl delbr vSwitch0 > /etc/null 2>&1 &

/sbin/ifconfig eth0 0.0.0.0 promisc up

/usr/sbin/brctl addbr vSwitch0

/usr/sbin/brctl addif vSwitch0 eth0

/sbin/ifconfig vSwitch0 192.168.1.22 netmask 255.255.255.0 up

/sbin/route add default gw 192.168.1.1 dev vSwitch0

else

/sbin/ifconfig eth0 0.0.0.0 promisc up

/usr/sbin/brctl addbr vSwitch0

/usr/sbin/brctl addif vSwitch0 eth0

/sbin/ifconfig vSwitch0 192.168.1.22 netmask 255.255.255.0 up

/sbin/route add default gw 192.168.1.1 dev vSwitch0

fi

3、建立xml定义文件,下面是我的xml文件

XML/HTML Code复制内容到剪贴板 <domaintype='kvm'id='1'> <name>templates-1</name>##域名 <uuid>0112310a-ff1a-bfb5-7cd8-5b2b22ad8de1</uuid>##UUID <memory>262144</memory>##最大内存 <currentMemory>262144</currentMemory>##当前内存 <vcpu>1</vcpu>##cpu个数 <os> <typearch='x86_64'machine='pc'>hvm</type> <bootdev='cdrom'/>##启动模式 </os> <features> <acpi/> <apic/> <pae/> </features> <clockoffset='localtime'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/bin/kvm</emulator> <disktype='file'device='disk'> <sourcefile='/datastore/templates-1.qcow2'/>##此虚机磁盘文件路径 <targetdev='hda'bus='ide'/>##盘符 </disk> <disktype='file'device='cdrom'> <targetdev='hdc'bus='ide'/> <sourcefile='/datastore/centos-5.4.iso'/>##虚拟光驱镜像路径 <readonly/> </disk> <interfacetype='bridge'> <macaddress='00:e0:4c:f0:e0:11'/>##mac地址 <sourcebridge='vSwitch0'/>##桥接设备 <targetdev='vnet0'/>##虚拟设备 </interface> <inputtype='tablet'bus='usb'/> <inputtype='mouse'bus='ps2'/> <graphicstype='vnc'port='5900'autoport='yes'listen='0.0.0.0'keymap='en-us'/> <soundmodel='es1370'/> <video> <modeltype='cirrus'vram='9216'heads='1'/> </video> </devices> </domain>

定义文件中的磁盘与光盘镜像路径一定要真实存在

4、创建虚拟机磁盘

# qemu-img create -f qcow2 templates-1.qcow2 20G

Formatting 'templates.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=0

指创建一个名称为templates.qcow2 格式为qcow2 大小为20G的虚拟磁盘

# du -sh templates.qcow2

136K templates.qcow2

此大小会随着系统的变化而变化,但最大应该不超过20G

5、启动安装

#virsh define templates-1.xml 定义一个虚机

#virsh start templstes-1 启动一个虚机

#virsh vncdisplay templates-1 查看vnc端口,用于安装过程的窗口化管理

#vncviewer :0 打开vnc窗口

此命令需要安装其中一个

# vncviewer

程序“vncviewer”已包含在下列软件包中:

* xtightvncviewer

* xvnc4viewer

* tightvnc-java

* vnc-java

请尝试:

apt-get install <选定的软件包>

###如果有多个虚机,各自的vnc端口应该是不同的###

#virsh destoy DOMAINNAME关闭一个虚机

更多命令详解

#virsh --help

6、如果需要图形化管理,可以安装诸如virt-manager类包

【在Ubuntu系统上安装部署KVM虚拟机的基本教程】相关文章:

ubuntu 14.04设置Apache虚拟主机的方法

Ubuntu系统中用ramlog将日志文件转存至内存中的教程

Ubuntu 12.04系统配置方法教程(图文)

ubuntu系统怎么安装gcc编程工具?

ubuntu12.04 amd64系统中lamp环境搭建方法

Ubuntu 15.04系统安装后要做的15件事

ubuntu系统中安装editplus的方法

Ubuntu系统软件安装方式

Ubuntu下安装google chrome浏览器教程

在Ubuntu系统中清楚Bash日志记录的方法

精品推荐
分类导航