您的位置:首页 > 运维架构

[镜像制作]VMWare搭建Openstack——Libvirt命令包(virt)制作虚拟机镜像

2015-05-16 16:00 513 查看
前面我们分别介绍了两种虚拟机镜像制作的方法,分别是利用libvirt管理界面和KVM命令来制作虚拟机镜像,其实Libvirt除了提供了manager来制作镜像以外,它还提供了相关的命令行工具,通过这个命令行工具同样也可以对镜像进行管理,特别是并不是所有的用户现场都可能给你提供可视化界面的,多掌握几种制作镜像的技术我们可以应付不同的用户场景。

1、跟Libvirt Manger的制作一样,我们首先要检查CPU是否支持虚拟化

如果你是物理机,建议修改BIOS,在CPU选项中有一个Virtualization Technology(VT),默认是Disabled,你可以修改为Enabled。

如果你是VM Ware Workstation,同样可以修改虚拟机的配置文件。



我们可以使用如下命令进行检测

sm@computer:~$ sudo kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used


2、安装服务端软件包,除了ubuntu-virt-server,也包括qemu-kvm和libvirt-bin

sm@computer:~$ sudo apt-get install ubuntu-virt-server


3、安装用户端软件包

sm@computer:~$ sudo apt-get install virtinst


安装完该包,系统包括几个比较重要的命令

sm@computer:~$ virt-
virt-clone          virt-host-validate  virt-install        virt-pki-validate
virt-convert        virt-image          virt-login-shell    virt-xml-validate


Virt-Convert:转换虚拟机格式,例如将raw转换为qcow2

Virt-clone:克隆虚拟机

Virt-image:创建虚拟机镜像

virt-install:创建新的虚拟机

这些命令不仅仅支持KVM,也同样支持Xen。

创建虚拟机

1、准备虚拟网卡

KVM创建的虚拟机默认使用NAT网络,VM可以访问外面,外面看不到VM,NAT机制无需经过网卡,只是使用内核的IP转发实现,为了让外面集群可以访问新建VM,需要使用桥接技术,把虚拟网卡接到真实网卡中。

a:启用IP转发,设置内核,启用IP转发,修改/etc/sysctl.conf

找到如下内容,去掉注释即可

net.ipv4.ip_forward = 1
将新配置生效即可
sm@computer:~$ sudo sysctl -p
net.ipv4.ip_forward = 1


b:创建桥接设备,安装bridge-utils软件包

sm@computer:~$ sudo apt-get install bridge-utils


修改网卡设置,/etc/network/interfaces

sm@computer:~$ sudo cat /etc/network/interfaces
[sudo] password for sm:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet manual

auto br0
iface br0 inet dhcp
bridge_ports eth0
bridge_stp off
bridge_maxwait 0
bridge_fd 0


对Ubtuntu来说,修改完IP信息,需要重新启动操作系统。

重启之后,我们查看一下桥接设备

sm@computer:~$ brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000c2907af38       no              eth0
virbr0          8000.000000000000       yes


查看一下网络IP

sm@computer:~$ ifconfig br0
br0       Link encap:Ethernet  HWaddr 00:0c:29:07:af:38
inet addr:192.168.195.139  Bcast:192.168.195.255  Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe07:af38/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:1338 errors:0 dropped:0 overruns:0 frame:0
TX packets:938 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:508694 (508.6 KB)  TX bytes:110788 (110.7 KB)


2、创建虚拟机

我们首先将操作系统的ISO文件上传到宿主机中,然后使用virt-install命令即可

sm@computer:~$ sudo virt-install --name=vm --ram=1024 --network bridge:br0 --disk path=/home/sm/a.img,bus=virtio,size=4 --graphics vnc,listen=0.0.0.0 --noautoconsole --hvm --cdrom /home/sm/ubuntu-14.04-server-amd64.iso

Starting install...
Creating storage file a.img                                                                                     | 4.0 GB     00:00
Creating domain...                                                                                              |    0 B     00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.


--name:虚拟机的名称vm

--ram=1024 :内存大小1024MB

--network bridge:br0  :设置虚拟机网卡,使用桥接方式,接口是br0

--disk path=/home/sm/a.img,bus=virtio,size=4 :指定虚拟存储位置,如果不首先安装,系统会默认创建一个raw格式的文件,该文件使用virtio总线,大小是4GB。

--graphics vnc,listen=0.0.0.0 :指定图形界面,我们使用vnc连接图形界面,监听所有网络接口

--noautoconsole --hvm :创建功能完整的虚拟机

--cdrom /home/sm/ubuntu-14.04-server-amd64.iso :指定ISO位置

由于前面已经查看了宿主机的IP为192.168.195.139,直接使用VNC连接该IP即可

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: