天天看點

完整部署CentOS7.2+OpenStack+kvm 雲平台環境(4)--用OZ工具制作openstack鏡像

在部署openstack雲平台環境的時候,需要上傳鏡像到glance。

首先下載下傳iso鏡像,這裡下載下傳了centos6.5鏡像,放到/usr/local/src目錄下

然後用OZ工具制作openstack的鏡像

*******************************安裝libvirt虛拟機軟體****************************************

[root@openstack-server src]# yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install 

[root@openstack-server src]# systemctl enable libvirtd && systemctl start libvirtd 

************************************************************************************

[root@openstack-server src]#yum install -y oz libguestfs-tools

[root@openstack-server src]# pwd

/usr/local/src

[root@openstack-server src]# ll CentOS-6.5-x86_64-bin-DVD1.iso #下載下傳的iso鏡像

-rw-r--r--. 1 root root 4467982336 Nov 29 2013 CentOS-6.5-x86_64-bin-DVD1.iso

[root@openstack-server src]# cat CentOS6u5-x86_64.tdl #建立tdl 檔案

<template>

<name>CentOS6u5-x86_64</name>

<description>CentOS6u5-x86_64 template</description>

<os>

<name>CentOS-6</name>

<version>5</version>

<arch>x86_64</arch>

<rootpw>PASSWORD</rootpw>             #這個是虛拟機建立好後,root的登陸密碼,密碼是在這個鏡像裡定義的!

<install type='iso'>

<iso>file:///usr/local/src/CentOS-6.5-x86_64-bin-DVD1.iso</iso>

</install>

</os>

<commands>                  #centos7系統裡沒有/boot/grub/grub.conf檔案,這塊可以不用寫,但寫了也無妨。這裡我是centos7,也寫了

<command name='console'>

sed -i 's/ rhgb//g' /boot/grub/grub.conf

sed -i 's/ quiet//g' /boot/grub/grub.conf

sed -i 's/ console=tty0 / serial=tty0 console=ttyS0,115200n8 /g' /boot/grub/grub.conf

</command>

</commands>

</template>

在用OZ工具制作openstack鏡像,有報錯:

[root@openstack-server src]# oz-install -u -d3 CentOS6u5-x86_64.tdl #報錯如下:

..................

raise oz.OzException.OzException("Could not find a libvirt bridge. Please run 'virsh net-start default' to start the default libvirt network, or see http://github.com/clalancette/oz/wiki/Oz-Network-Configuration for more information")

oz.OzException.OzException: Could not find a libvirt bridge. Please run 'virsh net-start default' to start the default libvirt network, or see http://github.com/clalancette/oz/wiki/Oz-Network-Configuration for more information

[root@openstack-server src]# virsh net-start default

error: failed to get network 'default'

error: Network not found: no network with matching name 'default'

查閱資料,說是因為 default 網絡不存在

回想了一下,我開始在搗鼓openstack 的時候,可能用指令删除了default,也或許沒有啟動default網絡

因為在不同的環境下,default.xml 的存放路徑不同,這裡筆者以自己的 centos7 為例

[root@openstack-server src]# find / -name "default.xml"

/etc/libvirt/qemu/networks/autostart/default.xml

/etc/libvirt/qemu/networks/default.xml

/usr/share/backgrounds/default.xml

/usr/share/libvirt/networks/default.xml

[root@openstack-server src]# virsh net-define /usr/share/libvirt/networks/default.xml

Network default defined from /usr/share/libvirt/networks/default.xml

[root@openstack-server src]# virsh net-start default

Network default started

[root@openstack-server src]# virsh net-list

Name State Autostart Persistent

----------------------------------------------------------

default active no yes

[root@openstack-server src]#

然後接着再進行openstack鏡像的制作: 

[root@openstack-server src]# oz-install -u -d3 CentOS6u5-x86_64.tdl 

libvirt bridge name is virbr0

Libvirt type is kvm

Name: CentOS6u5-x86_64, UUID: 0a9b1d18-f517-40ae-9de9-1fd6101878e2

MAC: 52:54:00:fb:0b:c9, distro: CentOS-6

update: 5, arch: x86_64, diskimage: /var/lib/libvirt/images/CentOS6u5-x86_64.dsk

nicmodel: virtio, clockoffset: utc

mousetype: ps2, disk_bus: virtio, disk_dev: vda

icicletmp: /var/lib/oz/icicletmp/CentOS6u5-x86_64, listen_port: 36050

Original ISO path: /var/lib/oz/isos/CentOS-65x86_64-iso.iso

Modified ISO cache: /var/lib/oz/isos/CentOS-65x86_64-iso-oz.iso

Output ISO path: /var/lib/libvirt/images/CentOS6u5-x86_64-iso-oz.iso

ISO content path: /var/lib/oz/isocontent/CentOS6u5-x86_64-iso

Checking for guest conflicts with CentOS6u5-x86_64

Generating install media

Attempting to get the lock for /var/lib/oz/isos/CentOS-65x86_64-iso.iso

Got the lock for /var/lib/oz/isos/CentOS-65x86_64-iso.iso

Fetching the original media

Fetching the original install media from file:///usr/local/src/CentOS-6.5-x86_64-bin-DVD1.iso

15kB of 4363264kB

10255kB of 4363264kB

20494kB of 4363264kB

30734kB of 4363264kB

40973kB of 4363264kB

51212kB of 4363264kB

....................

Cleaning up guestfs handle for CentOS6u5-x86_64

Syncing

Unmounting all

Libvirt XML was written to CentOS6u5-x86_64Aug_30_2016-13:47:18

注意:

鏡像制作完預設會存放到/var/lib/libvirt/images/ 目錄下,可以在/etc/oz/oz.cfg 配置檔案中進行修改路徑。

[root@openstack-server src]# cd /var/lib/libvirt/images/

[root@openstack-server images]# pwd

/var/lib/libvirt/images

[root@openstack-server images]# ll

total 1087336

-rw-rw-rw-. 1 root root 10737418240 Aug 30 13:47 CentOS6u5-x86_64.dsk

[root@openstack-server images]# virt-sysprep --add CentOS6u5-x86_64.dsk 

[ 0.0] Examining the guest ...

[ 5.0] Performing "abrt-data" ...

...........

[ 5.0] Setting a random seed

[ 5.0] Performing "lvm-uuids" ...

檢視檔案資訊

[root@openstack-server images]# qemu-img info CentOS6u5-x86_64.dsk 

image: CentOS6u5-x86_64.dsk

file format: raw

virtual size: 10G (10737418240 bytes)

disk size: 1.0G

上傳鏡像到 Glance

[root@openstack-server images]# glance image-create --name "CentOS-6.5" --file /var/lib/libvirt/images/CentOS6u5-x86_64.dsk --disk-format qcow2 --container-format bare --visibility public --progress

[=============================>] 100%

+------------------+--------------------------------------+

| Property | Value |

| checksum | 2d16e5ef687fead34fa801aafe37f058 |

| container_format | bare |

| created_at | 2016-08-30T06:03:22Z |

| disk_format | qcow2 |

| id | 508db9d4-6c9f-459d-8782-065ee8b6f2c2 |

| min_disk | 0 |

| min_ram | 0 |

| name | CentOS-6.5 |

| owner | 65a0c00638c247a0a274837aa6eb165f |

| protected | False |

| size | 10737418240 |

| status | active |

| tags | [] |

| updated_at | 2016-08-30T06:04:26Z |

| virtual_size | None |

| visibility | public |

檢視鏡像

[root@openstack-server images]# glance image-list

+--------------------------------------+------------+

| ID | Name |

| 508db9d4-6c9f-459d-8782-065ee8b6f2c2 | CentOS-6.5 |

 登陸openstack界面,發現可以檢視到上面已經上傳到glance裡面的鏡像。上面使用OZ工具制作的openstack所需要的Centos6/Centos7鏡像的預設大小是10G(如下圖)。

完整部署CentOS7.2+OpenStack+kvm 雲平台環境(4)--用OZ工具制作openstack鏡像

----------------------------------------------------------------------------------------------------------

如果不想用OZ工具制作,可以直接下載下傳centos的qcow2格式鏡像

下載下傳位址:http://cloud.centos.org/centos 【有centos6/7的鏡像】

比如:下載下傳centos7的qcow2格式鏡像

#wget http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2

上傳到 galnce

#glance image-create --name "CentOS-7-x86_64" --disk-format qcow2 --container-format bare --file

CentOS-7-x86_64-GenericCloud.qcow2 --visibility public --progress

-------------------------------------------------------------------------------------------------------------

當然,也可以使用OZ工具制作openstack需要的qcow2的Centos7鏡像

/usr/local/src 

[root@openstack-server src]# ls CentOS-7-x86_64-DVD-1511.iso 

CentOS-7-x86_64-DVD-1511.iso

[root@openstack-server src]# ls CentOS-7-x86_64.tdl 

CentOS-7-x86_64.tdl

[root@openstack-server src]# cat CentOS-7-x86_64.tdl

<name>CentOS-7-x86_64</name>

<description>CentOS-7-x86_64 template</description>

<name>CentOS-7</name>

<version>2</version>

<rootpw>PASSWORD</rootpw> 

<iso>file:///usr/local/src/CentOS-7-x86_64-DVD-1511.iso</iso>

<commands> 

[root@openstack-server src]# oz-install -u -d3 CentOS-7-x86_64.tdl

後面的步驟跟上面制作Centos6.5版本的操作一樣

下面是centos6.8版本鏡像制作的tdl檔案模闆:

[root@openstack-server src]# pwd

[root@openstack-server src]# ls CentOS-6.8-x86_64-bin-DVD1.iso 

CentOS-6.8-x86_64-bin-DVD1.iso

[root@openstack-server src]# cat CentOS6u8-x86_64.tdl 

<name>CentOS6u8-x86_64</name>

<description>CentOS6u8-x86_64 template</description>

<version>8</version>

<iso>file:///usr/local/src/CentOS-6.8-x86_64-bin-DVD1.iso</iso>

<commands>

[root@openstack-server src]# oz-install -u -d3 CentOS6u8-x86_64.tdl

------------------------------------------------------------------------------------------------------------

下面說下使用OZ工具制作openstack虛拟化環境下的ubuntu版本鏡像的過程記錄:

下面以ubuntu12.04版本為例,tdl内容參考OZ在github上的模闆樣式。

https://github.com/rcbops/oz-image-build/tree/master/templates

[root@openstack-server src]# ls ubuntu-12.04-server-amd64.iso 

ubuntu-12.04-server-amd64.iso

下面ubuntu的tdl模闆可直接使用~

[root@openstack-server src]# cat ubuntu-12.04_x86_64.tdl 

<name>ubuntu-12.04_x86_64</name>

<description>Ubuntu 12.04 15GB template</description>

<disk>

<size>15</size>                                                  #鏡像大小為15G

</disk>

<name>Ubuntu</name>

<version>12.04</version>

<rootpw>ROOT-PW_CHANGE-ME!!!</rootpw>                  #root賬号登入密碼

<iso>file:///usr/local/src/ubuntu-12.04-server-amd64.iso</iso>

sed -i 's/splash//g' /etc/default/grub

sed -i 's/quiet/console=ttyS0/g' /etc/default/grub

/usr/sbin/update-grub

<command name='update'>

apt-get update

apt-get -y upgrade

echo "cloud-init cloud-init/datasources string NoCloud, OVF, Ec2" > /tmp/debconf-selections 

/usr/bin/debconf-set-selections /tmp/debconf-selections

rm -f /tmp/debconf-selections

apt-get -y install cloud-init

apt-get clean

/usr/sbin/useradd -m stack

echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

sed -i 's/^user: ubuntu/user: stack/g' /etc/cloud/cloud.cfg

echo -n > /etc/udev/rules.d/70-persistent-net.rules

echo -n > /lib/udev/rules.d/75-persistent-net-generator.rules

[root@openstack-server src]# oz-install -u -d3 ubuntu-12.04_x86_64.tdl

............

Waiting for ubuntu-12.04_x86_64 to finish installing, 1200/1200

Waiting for ubuntu-12.04_x86_64 to finish installing, 1190/1200

Waiting for ubuntu-12.04_x86_64 to finish installing, 1180/1200

Waiting for ubuntu-12.04_x86_64 to finish installing, 1170/1200

Libvirt XML was written to ubuntu-12.04_x86_64Oct_31_2016-23:01:36

[root@openstack-server src]# cd /var/lib/oz/isos/

[root@openstack-server isos]# pwd

/var/lib/oz/isos

[root@openstack-server isos]# ls                    #發現oz下已經有了ubuntu12.04鏡像 

Ubuntu12.04x86_64-iso.iso

[root@openstack-server src]# cd /var/lib/libvirt/images/

[root@openstack-server images]# ls

ubuntu-12.04_x86_64.dsk

[root@openstack-server images]# virt-sysprep --add ubuntu-12.04_x86_64.dsk 

[ 5.0] Performing "lvm-uuids" .

[root@openstack-server images]# qemu-img info ubuntu-12.04_x86_64.dsk 

image: ubuntu-12.04_x86_64.dsk

virtual size: 15G (16106127360 bytes)

disk size: 1.3G

[root@openstack-server images]# glance image-create --name "ubuntu-12.04" --file /var/lib/libvirt/images/ubuntu-12.04_x86_64.dsk --disk-format qcow2 --container-format bare --visibility public --progress

| checksum | 15d25f4da354d8fbd5a248fc01894ceb |

| created_at | 2016-10-31T15:25:29Z |

| id | 042073da-e6cb-4b0b-97dd-1d5ef5be236a |

| name | ubuntu-12.04 |

| owner | 0cd3632df93d48d6b2c24c67f70e56b8 |

| size | 64424509440 |

| updated_at | 2016-10-31T15:38:09Z |

+------------------+-----------------------------------------+

[root@linux-node2 images]# glance image-list

+--------------------------------------+---------------------+

| 042073da-e6cb-4b0b-97dd-1d5ef5be236a | ubuntu-12.04 |

登入openstack界面,發現上面上傳到openstack的ubuntu12.04鏡像已經有了,鏡像大小為15G。

完整部署CentOS7.2+OpenStack+kvm 雲平台環境(4)--用OZ工具制作openstack鏡像

***************當你發現自己的才華撐不起野心時,就請安靜下來學習吧***************

本文轉自散盡浮華部落格園部落格,原文連結:http://www.cnblogs.com/kevingrace/p/5821823.html,如需轉載請自行聯系原作者

繼續閱讀