本文是本人借鑑了網上的一些資料和本人在實際製做OpenStack UBUNTU 12.04的系統鏡像過程當中進行的總結。記錄下來以便往後使用方便。
安裝 Ubuntu 鏡像
首先下載要安裝的 ubuntu 版本:
# wget http://releases.ubuntu.com/lucid/ubuntu-12.04-desktop-amd64.iso
建立一個 10GB 大小的 「硬盤」(raw 格式):
# kvm-img create -f raw ubuntu.img 10G
Formatting 'ubuntu.img', fmt=raw size=10737418240
使用剛纔下載的 ubuntu 「安裝盤」 和剛建立的 「硬盤」 引導啓動系統,爲了簡單起見,在這裏使用 kvm 虛擬技術,避開 xen 繁瑣的配置。-vnc 參數表明打開 vnc 訪問,以即可以用其餘機器遠程登陸到這個引導界面進行安裝操做:
# sudo kvm -m 1024 -cdrom ubuntu-12.04-desktop-amd64.iso -drive file=ubuntu.img -boot d -nographic -vnc :0
用 vncviewer 登陸引導界面後按照屏幕的提示完成 ubuntu 的安裝工做(和在本身電腦上安裝 ubuntu 過程同樣)。須要注意的是在分區階段把 10GB 硬盤所有劃分紅一個 ext4 root 分區,不要建立多個分區也不要建立 swap 區:
# vncviewer 172.21.6.92:5900
安裝完後退出(必要時 kill 掉 kvm 進程),按照下面命令啓動剛剛安裝好的虛擬機鏡像 ubuntu.img,若是出現 failed to find romfile 「pxe-rtf8139.bin」 的錯誤提示能夠經過安裝 kvm-pxe 解決:
# sudo kvm -m 1024 -drive file=ubuntu.img -boot c -nographic -vnc :0
kvm: pci_add_option_rom: failed to find romfile "pxe-rtl8139.bin"
# sudo apt-get install kvm-pxe
再次用 vnc 登陸虛擬機鏡像,安裝一些必要工具(由於這個鏡像將會是模板,因此最好保持最簡最小化,不要亂裝東西):
# sudo kvm -m 1024 -drive file=ubuntu.img -boot c -nographic -vnc :0
# vncviewer 172.21.6.92:5900
# apt-get update
# apt-get upgrade
# apt-get install openssh-server cloud-init
70-persistent-net.rules 會自動添加其餘的網絡接口,須要刪除這個文件避免自動添加除了 eth0 之外的接口。刪除後系統基本就準備好了,關閉虛擬機:
# sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules
# sudo shutdown -h now
調整 Ubuntu 鏡像
由於 OpenStack 只接受 ext4 文件系統格式的鏡像,因此須要把上面建立的 raw 鏡像(kvm-img create -f raw)轉換成 ext4 鏡像。下面的步驟用來找出鏡像文件裏面的分區起點是從哪裏開始的:
# losetup -f ubuntu.img
# losetup -a
/dev/loop0: [0801]:17432601 (/home/shinian/NovaUbuntuImage/ubuntu.img)
# fdisk -l /dev/loop0
Disk /dev/loop0: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00073f87
Device Boot Start End Blocks Id System
/dev/loop0p1 * 2048 20969471 10483712 83 Linux
上面最後一行顯示分區是從扇區(sector)2048開始的,每一個扇區是512個字節,因此是從 2048 x 512 = 1048576個字節開始的。記住這個1048576,下面會用到。
卸載 loop 後從新從1048576字節開始掛載:
# sudo losetup -d /dev/loop0
# sudo losetup -f -o 1048576 ubuntu.img
# sudo losetup -a
/dev/loop0: [0801]:17432601 (/home/shinian/NovaUbuntuImage/ubuntu.img), offset 1048576
把這整個分區拷貝到一個新文件就是一個咱們要的 ext4 文件系統鏡像:
# sudo dd if=/dev/loop0 of=ubuntutemplate.img
20969472+0 records in
20969472+0 records out
10736369664 bytes (11 GB) copied, 219.291 s, 49.0 MB/s
用完 loop 後記得卸載:
# sudo losetup -d /dev/loop0
掛載(mount)剛建立的 ext4 文件系統鏡像,並修改分區加載表(/etc/fstab),註釋或刪除之前的,加上 UUID=uec-rootfs 一行:
# sudo mount -o loop ubuntutemplate.img /mnt
# sudo vi /mnt/etc/fstab
#UUID=1dc3a59e-faab-41ee-b232-3300163676bf / ext4 errors=remount-ro 0 1
UUID=uec-rootfs / ext4 defaults 0 0
把剛纔的虛擬機鏡像 ubuntutemplate.img 的文件系統標誌改爲 ‘uec-rootfs’:
# sudo tune2fs -L uec-rootfs ubuntutemplate.img
tune2fs 1.42 (29-Nov-2011)
發佈 Ubuntu 鏡像
好了,ubuntu 鏡像已經作好了,如今能夠發佈到雲裏了,須要3個東西,虛擬機的內核文件、虛擬機的內存盤文件和虛擬機鏡像文件:
# cp /mnt/boot/vmlinuz-3.2.0-23-generic /home/shinian/NovaUbuntuImage
# cp /mnt/boot/initrd.img-3.2.0-23-generic /home/shinian/NovaUbuntuImage
# glance add name="ubuntu initrd" disk_format=qcow2 container_format=ovf is_public=true < initrd.img-3.2.0-23-generic
Added new image with ID: b7680f8f-867a-47cb-a887-3dc6846de2d6
# glance add name="ubuntu vmlinuz" disk_format=qcow2 container_format=ovf is_public=true < vmlinuz-3.2.0-23-generic
Added new image with ID: 89627d66-7536-4c38-8a15-8e0353985b9d
# glance add name="ubuntu OS" disk_format=qcow2 container_format=ovf is_public=ture ramdisk_id="b7680f8f-867a-47cb-a887-3dc6846de2d6" kernel_id="89627d66-7536-4c38-8a15-8e0353985b9d" < ubuntutemplate.img
Added new image with ID: d402ac7d-7508-4519-a8e9-2994c19659da
# glance index
ID Name Disk Format Container Format Size
------------------------------------ ------------------------------ -------------------- -------------------- --------------
d402ac7d-7508-4519-a8e9-2994c19659da ubuntu OS qcow2 ovf 10736369664
89627d66-7536-4c38-8a15-8e0353985b9d ubuntu vmlinuz qcow2 ovf 4965840
b7680f8f-867a-47cb-a887-3dc6846de2d6 ubuntu initrd qcow2 ovf 14178168
鏡像在物理機上的實際位置:
root@node02:/var/lib/glance# ls
glance.sqlite image-cache images keystone-signing
建立虛擬機密鑰對:
# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa2
/root/.ssh/id_rsa2 already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa2.
Your public key has been saved in /root/.ssh/id_rsa2.pub.
The key fingerprint is:
eb:6c:b0:8d:c0:71:c6:00:d3:eb:43:44:7c:af:60:d5 root@node02
The key's randomart image is:
+--[ RSA 2048]----+
| o=. . |
| .= o E |
| . * . |
| * + . |
| = = .S |
| = o . |
| o =. |
| ooo |
| .o |
+-----------------+
導入密鑰:
# nova keypair-list
# nova keypair-add --pub_key .ssh/id_rsa2.pub key2
查看鏡像:
# nova image-list
+--------------------------------------+-----------------------------+--------+--------+
| ID | Name | Status | Server |
+--------------------------------------+-----------------------------+--------+--------+
| d402ac7d-7508-4519-a8e9-2994c19659da | ubuntu OS | ACTIVE | |
| b7680f8f-867a-47cb-a887-3dc6846de2d6 | ubuntu initrd | ACTIVE | |
| 89627d66-7536-4c38-8a15-8e0353985b9d | ubuntu vmlinuz | ACTIVE | |
+--------------------------------------+-----------------------------+--------+--------+
查看虛擬機規格:
# nova flavor-list
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
| ID | Name | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public | extra_specs |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
| 1 | m1.tiny | 512 | 0 | 0 | | 1 | 1.0 | True | {} |
| 2 | m1.small | 2048 | 20 | 0 | | 1 | 1.0 | True | {} |
| 3 | m1.medium | 4096 | 40 | 0 | | 2 | 1.0 | True | {} |
| 4 | m1.large | 8192 | 80 | 0 | | 4 | 1.0 | True | {} |
| 5 | m1.xlarge | 16384 | 160 | 0 | | 8 | 1.0 | True | {} |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+-------------+
建立虛擬機:
# nova boot --flavor 2 --key_name key2 --image d402ac7d-7508-4519-a8e9-2994c19659da test05
+-------------------------------------+--------------------------------------+
| Property | Value |
+-------------------------------------+--------------------------------------+
| OS-EXT-STS:task_state | scheduling |
| image | ubuntu OS |
| OS-EXT-STS:vm_state | building |
| OS-EXT-SRV-ATTR:instance_name | instance-00000006 |
| flavor | m1.small |
| id | c4978044-c86e-4797-bac3-ff9c33a3b237 |
| security_groups | [{u'name': u'default'}] |
| user_id | 66b5f4f637384bc9883978c782b03e50 |
| OS-DCF:diskConfig | MANUAL |
| accessIPv4 | |
| accessIPv6 | |
| progress | 0 |
| OS-EXT-STS:power_state | 0 |
| OS-EXT-AZ:availability_zone | nova |
| config_drive | |
| status | BUILD |
| updated | 2013-08-09T04:49:33Z |
| hostId | |
| OS-EXT-SRV-ATTR:host | None |
| key_name | key2 |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None |
| name | test05 |
| adminPass | d5b2iW5k6by6 |
| tenant_id | a8da1a4eaabf4eafb566ca92902bdb5f |
| created | 2013-08-09T04:49:32Z |
| metadata | {} |
+-------------------------------------+--------------------------------------+
--flavor:指定虛擬機規格
--image:指定使用鏡像
--key_name:指定使用key
「test05」爲虛擬機名
(一個image能夠建立多個虛擬機,但須要你的image,是採用qcow2的格式。)
查看新建立的虛擬機:
# nova show test05
+-------------------------------------+------------------------------------------------------------------------------------+
| Property | Value |
+-------------------------------------+------------------------------------------------------------------------------------+
| status | ERROR |
| updated | 2013-08-09T04:49:33Z |
| OS-EXT-STS:task_state | None |
| OS-EXT-SRV-ATTR:host | None |
| key_name | key2 |
| image | ubuntu OS (d402ac7d-7508-4519-a8e9-2994c19659da) |
| hostId | |
| OS-EXT-STS:vm_state | error |
| OS-EXT-SRV-ATTR:instance_name | instance-00000006 |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None |
| flavor | m1.small (2) |
| id | c4978044-c86e-4797-bac3-ff9c33a3b237 |
| security_groups | [{u'name': u'default'}] |
| user_id | 66b5f4f637384bc9883978c782b03e50 |
| name | test05 |
| created | 2013-08-09T04:49:32Z |
| tenant_id | a8da1a4eaabf4eafb566ca92902bdb5f |
| OS-DCF:diskConfig | MANUAL |
| metadata | {} |
| accessIPv4 | |
| accessIPv6 | |
| fault | {u'message': u'NoValidHost', u'code': 500, u'details': u'No valid host was found. |
| | ', u'created': u'2013-08-09T04:49:33Z'} |
| OS-EXT-STS:power_state | 0 |
| OS-EXT-AZ:availability_zone | nova |
| config_drive | |
+-------------------------------------+------------------------------------------------------------------------------------+
注:公網沒有配置,因此創建實例的狀態暫時爲error;node