虚拟化技术汇总

作者: 李佶澳   转载请保留:原文地址   更新时间:2017/10/28 12:37:36

摘要

经常需要在一台工作的电脑上,使用虚拟技术,模拟部署系统,这里记录了一些可用的工具,已经遇到问题的解决。

kvm

kvm

libvirt

http://libvirt.org/

http://libvirt.org/format.html

网络接入

可以通过配置<devices>的子元素<interface>,将虚拟机以不同的方式接入不同的网络:

<devices>
    <interface>
    ...
    </interface>
</devices>

<interface>代表网卡设备,可以通过配置它的<address>子元素,将其绑定到指定pci插槽。

Virtual network (虚拟网络)

Depending on the virtual network’s “forward mode” configuration,

the network may be totally isolated (no <forward> element given),

NAT’ing to an explicit network device or to the default route (<forward mode='nat'>),

routed with no NAT (<forward mode='route'/>),

or connected directly to one of the host’s network interfaces (via macvtap) or bridge devices ((<forward mode='bridge|private|vepa|passthrough'/> Since 0.9.4)

<devices>

    <interface type='network'>
        <source network='default'/>
    </interface>

    <interface type='network'>
        <source network='default' portgroup='engineering'/>
        <target dev='vnet7'/>
        <mac address="00:11:22:33:44:55"/>
        <virtualport>
            <parameters instanceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
        </virtualport>
    </interface>

</devices>

Bridge to LAN (桥接模式)

<devices>

    <!-- 创建名为vnetN的tun设备,将其接入br0 -->
    <interface type='bridge'>
        <source bridge='br0'/>
    </interface>

    <!-- 创建名为vnet7的tun设备,将其接入br1 -->
    <interface type='bridge'>
        <source bridge='br1'/>
        <target dev='vnet7'/>
        <mac address="00:11:22:33:44:55"/>
    </interface>

    <!-- 创建名为vnet7的tun设备,将其接入br1 -->
    <interface type='bridge'>
        <source bridge='ovsbr'/>
        <virtualport type='openvswitch'>
            <parameters profileid='menial' interfaceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
        </virtualport>
    </interface>

</devices>

Userspace SLIRP stack (NAT模式)

The virtual network has DHCP & DNS services and will give the guest VM addresses starting from 10.0.2.15.

The default router will be 10.0.2.2 and the DNS server will be 10.0.2.3.

This networking is the only option for unprivileged users who need their VMs to have outgoing access.

<devices>
<interface type='user'/>

<interface type='user'>
    <mac address="00:11:22:33:44:55"/>
</interface>

</devices>

Generic ethernet connection

Provides a means for the administrator to execute an arbitrary script to connect the guest’s network to the LAN.

<devices>

<!-- 默认使用/etc/qemu-ifup脚本设置 -->
<interface type='ethernet'/>

<!-- 默认使用/etc/qemu-ifup-mynet脚本设置 -->
<interface type='ethernet'>
    <target dev='vnet7'/>
    <script path='/etc/qemu-ifup-mynet'/>
</interface>

</devices>

Direct attachment to physical interface

Provides direct attachment of the virtual machine’s NIC to the given physical interface of the host.

This setup requires the Linux macvtap driver to be available. (Since Linux 2.6.34.)

One of the modes ‘vepa’ (‘Virtual Ethernet Port Aggregator’), ‘bridge’ or ‘private’ can be chosen for the operation mode of the macvtap device.

vepa:

All VMs' packets are sent to the external bridge. Packets whose destination is a VM on the same host 
as where the packet originates from are sent back to the host by the VEPA capable bridge 
(today's bridges are typically not VEPA capable).

bridge:

Packets whose destination is on the same host as where they originate from are directly delivered to
the target macvtap device. Both origin and destination devices need to be in bridge mode for direct delivery.
If either one of them is in vepa mode, a VEPA capable bridge is required.

private:

All packets are sent to the external bridge and will only be delivered to a target VM on the same host 
if they are sent through an external router or gateway and that device sends them back to the host. 
This procedure is followed if either the source or destination device is in private mode.

passthrough:

This feature attaches a virtual function of a SRIOV capable NIC directly to a VM without losing the migration capability.
All packets are sent to the VF/IF of the configured network device.
Depending on the capabilities of the device additional prerequisites or limitations may apply; 
for example, on Linux this requires kernel 2.6.38 or newer.

<devices>
    <interface type='direct' trustGuestRxFilters='no'>
        <source dev='eth0' mode='vepa'/>
    </interface>
</devices>

PCI Passthrough

A PCI network device (specified by the element) is directly assigned to the guest using generic device passthrough.

Multicast tunnel

A multicast group is setup to represent a virtual network.

Any VMs whose network devices are in the same multicast group can talk to each other even across hosts.

This mode is also available to unprivileged users. There is no default DNS or DHCP support and no outgoing network access.

TCP tunnel

A TCP client/server architecture provides a virtual network. One VM provides the server end of the network, all other VMS are configured as clients.

All network traffic is routed between the VMs via the server.

There is no default DNS or DHCP support and no outgoing network access.

To provide outgoing network access, one of the VMs should have a 2nd NIC which is connected to one of the first 4 network types and do the appropriate routing

UDP unicast tunnel

A UDP unicast architecture provides a virtual network which enables connections between QEMU instances using QEMU’s UDP infrastructure.

Vagrant

Mac上安装

使用brew安装vargrant和virtualbox

brew cask install virtualbox
brew cask install vagrant

如果安装virtualbox的时候遇到错误:

error: Failure while executing; `/usr/bin/sudo -E -- env LOGNAME=lijiao USER=lijiao USERNAME=lijiao /usr/sbin/installer -pkg /usr/local/Caskroom/virtualbox/5.2.18,124319/VirtualBox.pkg -target /` exited with 1. Here's the output:
installer: Package name is Oracle VM VirtualBox
installer: Installing at base path /
installer: The install failed (安装器遇到了一个错误,导致安装失败。请联系软件制造商以获得帮助。)
...

需要到Mac的Security&Privacy的“Allow apps downloaded from”中设置允许来自Oracle Aemerica. Inc的软件。设置之后,重启电脑,重新安装。

下载安装box

box就是virtualbox的虚拟机镜像,Vargrant还支持aws, 以后用到再来补充。

查找box: https://vagrantcloud.com/boxes/search

安装box:

vagrant box add ubuntu/trusty64

境外的资源获取很慢,可以到下面到镜像站下载box文件:

http://mirrors.opencas.cn/ubuntu-vagrant/vagrant/

vargrant box add [box文件到url地址,或者本地路径]

查看:

vargrant box list

启动

建立一个新目录,作为vargrant虚拟机的工作目录:

mkdir VargrantTest
cd VargrantTest

注意下面的vargrant命令必须在工作目录下执行。

初始化:

vargrant init

修改初始化后得到的Vargrantfile文件:

config.vm.box = "base"    //默认是base,修改为要是用的box 

启动:

vagrant up

SSH登录进入系统:

vagrant ssh

查看状态:

vagrant status

其他操作:

vagrant -h

Virtualbox

设置Serial Number等DMI信息

VirtualBox的虚拟机默认没有Serial Number:

[[email protected] ~]$ sudo dmidecode |grep Serial
Serial Number: 0
Serial Number: 0
Serial Number: Not Specified

设置方法(官方设置手册:

VBoxManage setextradata VM_NAME VBoxInternal/Devices/pcbios/0/Config/DmiSystemSerial "ABC12345"

In case your VM is configured to use EFI firmware you need to replace pcbios by efi in the keys.

在设置前,可以用VBoxManage list vms 查看虚拟机是否存在:

$ VBoxManage list vms
"OpenStack_default_1450335064104_94502" {d576c27d-f9b3-4f19-b2e8-49775b75f2b0}
"Ubuntu_default_1451556534267_81756" {f1d56285-4ec8-431f-9c41-acc5c9edba23}
"ONOS_default_1451891349199_4105" {358501fa-5029-4faf-ba5a-215eca35abe8}

参考

  1. https://docs.vagrantup.com/

本文原创首发于网站:www.lijiaocn.com

QQ交流群

区块链实践互助QQ群:576555864

Kubernetes实践互助QQ群:947371129

Prometheus实践互助QQ群:952461804

Kong/Envoy实践互助QQ群:952503851

Ansible实践互助QQ群:955105412

Copyright @2011-2019 All rights reserved. 转载请添加原文连接,合作请加微信lijiaocn或者发送邮件: [email protected],备注网站合作 友情链接: lijiaocn github.com