设为首页收藏本站

追梦Linux

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 376|回复: 1

在Ubuntu上安装KVM并搭建虚拟环境

[复制链接]

482

主题

485

帖子

16万

积分

CEO

Rank: 9Rank: 9Rank: 9

积分
168233

最佳新人活跃会员热心会员推广达人宣传达人灌水之王突出贡献优秀版主荣誉管理论坛元老

QQ
发表于 2016-12-23 21:50:50 | 显示全部楼层 |阅读模式
一、环境准备客户端 windows,提前下载有putty

Server CPU需支持虚拟化,ubuntu 14.04
  • 安装openssh-server,使其可被远程ssh
[Bash shell] 纯文本查看 复制代码
apt-get install openssh-server -y

注:以下的所有操作都通过SSH进行。
  • Server系统环境
[Bash shell] 纯文本查看 复制代码
uname -a

Linux ubuntu 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
[Bash shell] 纯文本查看 复制代码
cat /etc/issue

Ubuntu 14.04.2 LTS \n \l
  • 查看CPU的虚拟化支持
[Bash shell] 纯文本查看 复制代码
egrep 'svm|vmx' /proc/cpuinfo

flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm ida dtherm tpr_shadow vnmi flexpriority
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx lm constant_tsc arch_perfmon pebs bts nopl aperfmperf pni dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm sse4_1 xsave lahf_lm ida dtherm tpr_shadow vnmi flexpriority

我机器是intel的CPU,可以查看vmx的标识

  • Server上,配置ubuntu下载源sources.list,我用的是sohu源。
PS:官方源在国外下载过慢
[Bash shell] 纯文本查看 复制代码
vim /etc/apt/sources.list

deb http://mirrors.sohu.com/ubuntu/ precise main restricted
deb-src http://mirrors.sohu.com/ubuntu/ precise main restricted
deb http://mirrors.sohu.com/ubuntu/ precise-updates main restricted
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates main restricted
deb http://mirrors.sohu.com/ubuntu/ precise universe
deb-src http://mirrors.sohu.com/ubuntu/ precise universe
deb http://mirrors.sohu.com/ubuntu/ precise-updates universe
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates universe
deb http://mirrors.sohu.com/ubuntu/ precise multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise multiverse
deb http://mirrors.sohu.com/ubuntu/ precise-updates multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise-updates multiverse
deb http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise-backports main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ precise-security main restricted
deb-src http://mirrors.sohu.com/ubuntu/ precise-security main restricted
deb http://mirrors.sohu.com/ubuntu/ precise-security universe
deb-src http://mirrors.sohu.com/ubuntu/ precise-security universe
deb http://mirrors.sohu.com/ubuntu/ precise-security multiverse
deb-src http://mirrors.sohu.com/ubuntu/ precise-security multiverse
deb http://extras.ubuntu.com/ubuntu precise main
deb-src http://extras.ubuntu.com/ubuntu precise main
#更新源
[Bash shell] 纯文本查看 复制代码
apt-get update


二、安装KVM
  • Server上,安装KVM及virt管理软件

[Bash shell] 纯文本查看 复制代码
apt-get install kvm qemu
apt-get install virtinst Python-libvirt virt-viewer virt-manager


三、配置桥接网卡
  • 安装网桥

[Bash shell] 纯文本查看 复制代码
apt-get install bridge-utils

  • 查看网卡配置

[Bash shell] 纯文本查看 复制代码
ifconfig

[Bash shell] 纯文本查看 复制代码
eth0 Link encap:Ethernet HWaddr 00:1c:25:a1:99:fc
inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::21c:25ff:fea1:99fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:736674 errors:0 dropped:0 overruns:0 frame:0
TX packets:1098750 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:63783787 (63.7 MB) TX bytes:1540374899 (1.5 GB)
Interrupt:20 Memory:fc200000-fc220000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:1156 errors:0 dropped:0 overruns:0 frame:0
TX packets:1156 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:114090 (114.0 KB) TX bytes:114090 (114.0 KB)

virbr0 Link encap:Ethernet HWaddr be:65:ec:88:86:3d
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  • 已有网卡配置
  • eth0是直接插入网线的以太网卡,
  • lo是本地网络,本机内部资源
  • virbr0是刚才我们装完KVM后,自己生成的虚拟网卡

  • 配置网卡桥接

增加一个叫br0的虚拟网卡,让这个网卡可以和eth0以太网的网卡做桥接

[Bash shell] 纯文本查看 复制代码
vim /etc/network/interfaces

[Bash shell] 纯文本查看 复制代码
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet manual
auto br0
iface br0 inet static
address 192.168.1.10
netmask 255.255.255.0
gateway 192.168.1.1
bridge_ports eth0

保存,重起网卡设置
[Bash shell] 纯文本查看 复制代码
/etc/init.d/networking restart

再查看网卡的设置

[Bash shell] 纯文本查看 复制代码
ifconfig

[Bash shell] 纯文本查看 复制代码
br0 Link encap:Ethernet HWaddr 00:1c:25:a1:99:fc
inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::21c:25ff:fea1:99fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:91 errors:0 dropped:0 overruns:0 frame:0
TX packets:100 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:6260 (6.2 KB) TX bytes:15470 (15.4 KB)

eth0 Link encap:Ethernet HWaddr 00:1c:25:a1:99:fc
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:605 errors:0 dropped:0 overruns:0 frame:0
TX packets:425 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:112441 (112.4 KB) TX bytes:61529 (61.5 KB)
Interrupt:20 Memory:fc200000-fc220000

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:36 errors:0 dropped:0 overruns:0 frame:0
TX packets:36 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2100 (2.1 KB) TX bytes:2100 (2.1 KB)

virbr0 Link encap:Ethernet HWaddr 2a:b8:36:cf:cc:c6
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)


这个时候eth0,已经没有具体的IP地址。IP地址出在了br0的虚拟网上面。
测试是否可以联通外网,ping qq.com


四、创建虚拟机
通过virt虚拟机管理软件,创建虚拟机(Guest)。
上传2个安装镜像到Server: ubuntu-14.04.3-server-amd64.iso, windows_server_2008_r2_sp1_x64.iso
放在/home/work/iso下
  • 安装ubuntu 14.04 (通过VNC,有可视化界面)
在server端,我要打开vnc_listen的端口支持
[Bash shell] 纯文本查看 复制代码
vim /etc/libvirt/qemu.conf
#打开注释
vnc_listen="0.0.0.0"
重启virt软件
[Bash shell] 纯文本查看 复制代码
/etc/init.d/libvirt-bin restart
确认是否启动,端口监听
[Bash shell] 纯文本查看 复制代码
netstat -nlt|grep 5900

tcp 0 0 0.0.0.0:5900 0.0.0.0:* LISTEN
我们需要使用到Xming和putty配合,可以让putty远程传输的VNC信号通过Xming显示出来。
在putty里面,我们要修改一下配置(putty开启x11转发)。


启动提前装好的Xming软件。
创建一个100G的磁盘
[Bash shell] 纯文本查看 复制代码
qemu-img create -f qcow2 ubuntu-14.qcow2 100G -o preallocation=metadata

创建虚拟机
相关参数解释:虚拟机名 vm_006 ,  内存8G ,cpu 2核 ,  磁盘地址 /home/work/vm/vm06/ubuntu-14.qcow2 ,磁盘大小100G,ISO镜像  /home/work/iso/ubuntu-14.04.3-server-amd64.iso
[Bash shell] 纯文本查看 复制代码
sudo virt-install --connect=qemu:///system --name vm_006 --ram 8192 --vcpus=2 --os-type=linux --os-variant=ubuntuprecise --accelerate --hvm --disk path=/home/work/vm/vm06/ubuntu-14.qcow2,size=100,bus=virtio   --cdrom /home/work/iso/ubuntu-14.04.3-server-amd64.iso --network bridge=br0,model=virtio --graphics vnc


通过Xming显示了VNC的界面,ubuntu安装画面
然后依次下一步下一步,直至完成。


  • 安装ubuntu 14.04 (通过全命令安装,无可视化界面

挂载镜像到mnt目录
[Bash shell] 纯文本查看 复制代码
mount -o loop /home/work/iso/ubuntu-14.04.3-server-amd64.iso mnt

执行命令
[Bash shell] 纯文本查看 复制代码
virt-install --connect=qemu:///system \
 --name vm_005 \
 --ram 4096 \
 --vcpus=2 \
 --os-type=linux \
 --os-variant=ubuntuprecise \
 --accelerate \
 --hvm \
 --disk path=/home/work/vm/vm05/ubuntu-14.qcow2,size=100,bus=virtio \
 --location mnt \
 --extra-args='console=tty0 console=ttyS0' \
 --network bridge=br0,model=virtio \
 --graphics none

进入安装界面
lqqqqqqqqqqqqqqqqqqqqqqqu [!!] Select a language tqqqqqqqqqqqqqqqqqqqqqqqqk
x x
x Choose the language to be used for the installation process. The x
x selected language will also be the default language for the installed x
x system. x
x x
x Language: x
x x
x C x
x English x
x x
x <Go Back> x
x x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
<Tab> moves; <Space> selects; <Enter> activates buttons
安装系统
lqqqqqqqqqqqqqqqqqqqqqu [!] Configure the network tqqqqqqqqqqqqqqqqqqqqqk
x x
x Please enter the hostname for this system. x
x x
x The hostname is a single word that identifies your system to the x
x network. If you don't know what your hostname should be, consult your x
x network administrator. If you are setting up your own home network, x
x you can make something up here. x
x x
x Hostname: x
x x
x localhost____________________________________________________________ x
x x
x <Go Back> <Continue> x
x x
mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj
<Tab> moves; <Space> selects; <Enter> activates buttons
直至安装完成

  • windows-server 2008的安装,必须要使用VNC界面方式,没有命令直接安装的方法

创建磁盘
[Bash shell] 纯文本查看 复制代码
qemu-img create -f qcow2 win2008.img 150G

注意(安装时一定要选择virtio驱动程序文件 ,否则导致网卡驱动异常):
--disk path=/home/work/vm/vm01/virtio-win-0.1-81.iso,device=cdrom  表示将virtio-win-0.1-81.iso(virtio驱动程序文件)镜像以光驱的方式挂载到系统中。使用vnc客户端安装OS。
此镜像安装时,版本选项一般选择第三个选项Enterprise企业完全版。
[Bash shell] 纯文本查看 复制代码
virt-install \
--connect=qemu:///system   \ 
--name vm_001 --ram 4096  \         
--vcpus=2 --os-type=windows  \     
--accelerate -c /home/work/iso/windows_server_2008_r2_sp1_x64.iso  \     
--disk path=/home/work/iso/virtio-win-0.1-81.iso,device=cdrom  \           
--disk path=/home/work/vm/vm07/win2008.img,format=qcow2,bus=virtio  \  
--network bridge=br0  \    
--vnc  \                               
--vncport=5997  \               
--vnclisten=0.0.0.0 \     
--force  \
--autostart \
--video cirrus

避免遇到starting windows  添加参数  
--video cirrus








安装完成后我们通过服务器管理可以看到已识别硬盘。

windows server2008 镜像:http://pan.baidu.com/s/1b7fIge

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

0

主题

3

帖子

312

积分

高工

Rank: 3Rank: 3

积分
312
发表于 2017-4-21 11:02:03 | 显示全部楼层
很好,安装windows加上图片就一目了然了

QQ|小黑屋|手机版|Archiver|追梦Linux ( 粤ICP备14096197号  点击这里给我发消息

GMT+8, 2019-7-17 16:30 , Processed in 0.353535 second(s), 37 queries .

Powered by 追梦Linux! X3.3 Licensed

© 2015-2017 追梦Linux!.

快速回复 返回顶部 返回列表