为OpenStack制作UBUNTU 12.04系统镜像

感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正!
如果转载,请保留作者信息。
博客地址:http://blog.csdn.net/gaoxingnengjisuan
邮箱地址:dong.liu@siat.ac.cn


本文是本人借鉴了网上的一些资料和本人在实际制作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;

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值