Jan 082014
 

一直以来,我都是使用Ubuntu的镜像,自己没动手做过镜像。对于Openstack的镜像来说,需要在image里安装cloud init,这样才能注入密钥,网络的设置,也是需要cloud init。

做镜像的步骤,比较繁琐,其实就是一个kvm的image,有什么办法可以简化呢?Oz这个工具,应该可以完全满足你的要求,可以很方便让你定制各种操作系统的image,功能还是很强大的,Oz开发和更新还是非常频繁,非常值得关注的一个软件

目前EPEL源已经有Oz的安装包,更新还是非常及时,和开发同步,并且Redhat也是推荐使用这个工具来创建镜像。由于Oz需要使用网络来自动化创建镜像,所以建议别在Openstack的环境下进行使用,建议采用一台干净的机器来做。

我用下面的配置文件制作的镜像,放到网盘上,大家可以测试一下。

http://yunpan.cn/QiQ6syasRAH7Q 访问密码 90e3

 

安装

yum -y  install oz

装完后,你最好就重启机器,你用ifconfig,可以看到virbr0这个网络,不然你就安装失败。

配置Oz

默认Oz是使用raw格式,我的习惯是使用qcow2格式,

# cat /etc/oz/oz.cfg 
[paths]
output_dir = /var/lib/libvirt/images
data_dir = /var/lib/oz
screenshot_dir = /var/lib/oz/screenshots

[libvirt]
uri = qemu:///system
#image_type = raw
image_type = qcow2
# type = kvm
# bridge_name = virbr0
# cpus = 1
# memory = 1024

[cache]
original_media = yes
modified_media = no
jeos = no

[icicle]
safe_generation = no

 

这个就是Oz的配置文件,我把默认文件格式改成qcow2.

Oz支持的系统

目前EPEL的源,已经更新到最新的版本,支持Centos 6.5。

当前Oz的版本

# rpm -qa | grep oz
oz-0.12.0-1.el6.noarch

创建配置文件

我们需要两个配置文件,centos65.tdl 和centos6.ks

看一个具体的Oz的tdl文件,就全部明白

创建 centos65.tdl 文件

<template>
   <name>centos_65_x86_64</name>
   <description>CentOS 6.5 x86_64 template</description>
   <os>
      <name>CentOS-6</name>
      <version>5</version>
      <arch>x86_64</arch>
      <install type='url'>
         <url>http://172.28.0.1/cobbler/ks_mirror/CentOS6.5-x86_64/</url>
      </install>
   </os>
  <disk>
    <size>10</size>
  </disk>
</template>

 

做Openstack的镜像的kickstart文件,参考http://repos.fedorapeople.org/repos/openstack/guest-images/

使用我的centos6.ks 文件,才能支持镜像的resize。

oz-install -p -u -d3 -a centos6.ks centos65.tdl

 

镜像创建完后,会保存在 /var/lib/libvirt/images ,对image进行压缩

qemu-img convert -c /var/lib/libvirt/images/centos_65_x86_64.qcow2 -O qcow2 \
/root/centos_65_x86_64.qcow2

压缩后的镜像大小在300M左右.没压缩前,大概是1.3G。

如果你生成的是raw格式,可以进行转换成qcow2格式

qemu-img convert /var/lib/libvirt/images/centos65_x86_64.dsk -O qcow2 /root/centos65_x86_64.qcow2

 

参考文章

http://waipeng.wordpress.com/2013/05/20/building-centos-images-for-openstack/

http://docs.openstack.org/grizzly/openstack-image/content/ch_creating_images_automatically.html

http://www.jansipke.nl/creating-a-centos-vm-image-for-openstack/

https://github.com/rackerjoe/oz-image-build

我的centos6.ks 文件。

Oz介绍

Oz内置各个操作系统安装的模板,对于Redhat系列来说,其实就是内置了kickstart脚本,不过这些默认的ks文件,可能无法满足你的需求,那么你可以自己制定自己的ks文件。

Oz内置了各个操作系统的自动化安装模板,/usr/lib/python2.6/site-packages/oz/auto

 

我的centos6.ks文件

#version=DEVEL
# Firewall configuration
firewall --enabled --service=ssh
repo --name="repo0" --baseurl=http://172.28.0.1/cobbler/ks_mirror/CentOS6.5-x86_64
repo --name="repo1" --baseurl=http://172.28.0.1/cobbler/repo_mirror/Centos6-UPDATES
repo --name="repo2" --baseurl=http://172.28.0.1/cobbler/repo_mirror/epel6-x86_64
# Root password
rootpw --iscrypted --lock $1$2e74e5$wMj25e4rEb4rJxqm7BAnk0
# System authorization information
auth --useshadow --enablemd5
# System keyboard
keyboard us
# System language
lang en_US.UTF-8
# SELinux configuration
selinux --enforcing
# Installation logging level
logging --level=info
# Reboot after installation
reboot
# System services
services --disabled="avahi-daemon,iscsi,iscsid,firstboot,kdump" --enabled="network,sshd,rsyslog,tuned"
# System timezone
timezone --isUtc Asia/Chongqing
# Network information
network  --bootproto=dhcp --device=eth0 --onboot=on
# System bootloader configuration
bootloader --append="console=ttyS0,115200n8 console=tty0" --location=mbr --driveorder="sda" --timeout=1
# Clear the Master Boot Record
zerombr
# Partition clearing information
clearpart --all  
# Disk partitioning information
#part / --fstype="ext4" --grow --size=1
part / --fstype ext4 --size=512 --grow

%post

# make sure firstboot doesn't start
echo "RUN_FIRSTBOOT=NO" > /etc/sysconfig/firstboot

cat <<EOL >> /etc/rc.local
if [ ! -d /root/.ssh ] ; then
    mkdir -p /root/.ssh
    chmod 0700 /root/.ssh
    restorecon /root/.ssh
fi
EOL

cat <<EOL >> /etc/ssh/sshd_config
UseDNS no
PermitRootLogin without-password
EOL

# bz705572
ln -s /boot/grub/grub.conf /etc/grub.conf

# bz688608
sed -i 's|\(^PasswordAuthentication \)yes|\1no|' /etc/ssh/sshd_config

# allow sudo powers to cloud-user
echo -e 'cloud-user\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers

# bz983611
echo "NOZEROCONF=yes" >> /etc/sysconfig/network

# set virtual-guest as default profile for tuned
echo "virtual-guest" > /etc/tune-profiles/active-profile

#bz 1011013
# set eth0 to recover from dhcp errors
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE="eth0"
BOOTPROTO="dhcp"
ONBOOT="yes"
TYPE="Ethernet"
USERCTL="yes"
PEERDNS="yes"
IPV6INIT="no"
PERSISTENT_DHCLIENT="1"
EOF
#bz912801
# prevent udev rules from remapping nics
touch /etc/udev/rules.d/75-persistent-net-generator.rules

#setup getty on ttyS0
echo "ttyS0" >> /etc/securetty
cat <<EOF > /etc/init/ttyS0.conf
start on stopped rc RUNLEVEL=[2345]
stop on starting runlevel [016]
respawn
instance /dev/ttyS0
exec /sbin/agetty /dev/ttyS0 115200 vt100-nav
EOF

# lock root password
#passwd -d root
#passwd -l root

# let's randomise the root password
head -n1 /dev/urandom | md5sum| awk {'print $1'} | passwd --stdin root

# clean up installation logs"
yum clean all
rm -rf /var/log/yum.log
rm -rf /var/lib/yum/*
rm -rf /root/install.log
rm -rf /root/install.log.syslog
rm -rf /root/anaconda-ks.cfg
rm -rf /var/log/anaconda*
%end

%packages --nobase --excludedocs
wget
vim
cloud-init
cloud-utils
dracut-modules-growroot
%end

说明

安装途径

<install type='url'>
      <url>http://download.fedoraproject.org/pub/fedora/linux/releases/15/Fedora/x86_64/os/</url>
</install>

 

如果iso在本地,如果是url,就写成http://iso路径 就可以

<install type=’iso’>
<iso>file:///root/rhel-server-6.3-x86_64-dvd.iso</iso>
</install>
</os>

 

默认磁盘改为10G

<disk>
    <size>10</size>
  </disk>

  37 Responses to “Oz制作CentOS镜像”

  1. 1.文件比较大,与kickstart中选择的安装包有关系,我用自定义的包,只安装base,6.4 x86_64出来是400M左右的;
    2.metadata server 的问题,需要在neutron.conf中打开isolate_medata_server的配置,设置为True,然后重启neutron-metadata-agent 服务即可;
    3.resize的问题,需要修改启动镜像(或者在kickstart 文件中操作),用growpart来实现。并且最好是只有一个独立的根分区,或者用lvm,手动扩展。

    • 文件大这个,应该还好办,应该就是这样就可以解决。
      2:metadata server的问题,其实不是我openstack配置的问题,我用ubuntu的镜像是没问题,指主要还是镜像制作本身的问题,查了半天,https://bugzilla.redhat.com/show_bug.cgi?id=983611
      3:这是麻烦的地方,调整硬盘,涉及linux很多内容,好好学习。https://ask.openstack.org/en/question/106/howto-install-cloud-init-into-a-centos-image/

    • 我做的镜像也存在resize问题,求陈老师指导下,最好是有文档的话,分享下。

  2. 陈老师,我在按照你的方法制作镜像的时候出现下面的错误

    Waiting for centos_65_x86_64 to finish installing, 20/1200
    Waiting for centos_65_x86_64 to finish installing, 10/1200
    Cleaning up guest named centos_65_x86_64
    Cleaning up after install
    Traceback (most recent call last):
    File “/usr/bin/oz-install”, line 162, in
    libvirt_xml = guest.install(timeout, force_download)
    File “/usr/lib/python2.6/site-packages/oz/RedHat.py”, line 685, in install
    self.initrdfname, self.cmdline)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 1659, in _do_install
    self._wait_for_install_finish(dom, timeout)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 766, in _wait_for_install_finish
    raise oz.OzException.OzException(“Timed out waiting for install to finish. %s” % (screenshot_text))
    oz.OzException.OzException: Timed out waiting for install to finish. Check screenshot at /var/lib/oz/screenshots/centos_65_x86_64-393315072.33.ppm for more detail
    不知道为什么 ,能给点指点吗?

    • Hi沙克,我也遇到同样的问题,您制作镜像的时候遇到了吗?求解决方法,O(∩_∩)O谢谢~!

      • 我做了很多镜像,发现了很多问题。基本上都能解决
        1、首先电脑要能支持kvm虚拟化
        2、oz安装是否正确,安装完后应该多出一个虚拟的网络接口virbr0,看是否启动了
        3、tdl文件中的属性一定要正确
        redhat的是这种 “RHEL-6″
        Fedora的是 ”Fedora“
        我没记错的话这个问题应该就是名字没有写对。
        4、你给放置镜像的盘的空间是否够大,不然也不行。

        还想问沙克老师一个问题再做win7的时候不能全自动吗,必须要手动配置那个安装界面吗?

    • 这个问题,请加上-t 7200

  3. 那个iso格式安装 好像必须要dvd格式的 我是了minimal的iso是不行的 改成url的安装就OK了

  4. 陈老师,为什么我制作的镜像大小就是磁盘大小,比如制作Ubuntu,指定磁盘大小为5G,结果生成的镜像也是5G。

  5. hi 陈老师,
    我执行oz-install 后报错如下:
    Waiting for centos_65_x86_64 to finish installing, 620/1200
    Waiting for centos_65_x86_64 to finish installing, 610/1200
    Cleaning up guest named centos_65_x86_64
    Cleaning up after install
    Traceback (most recent call last):
    File “/usr/bin/oz-install”, line 162, in
    libvirt_xml = guest.install(timeout, force_download)
    File “/usr/lib/python2.6/site-packages/oz/RedHat.py”, line 685, in install
    self.initrdfname, self.cmdline)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 1663, in _do_install
    self._wait_for_install_finish(dom, timeout)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 775, in _wait_for_install_finish
    raise oz.OzException.OzException(“No disk activity in %d seconds, failing. %s” % (inactivity_timeout, screenshot_text))
    oz.OzException.OzException: No disk activity in 300 seconds, failing. Check screenshot at /var/lib/oz/screenshots/centos_65_x86_64-1403507439.58.ppm for more detail

    我确认磁盘空间没有问题,但仍然报错 no disk activity ?
    求指教~

  6. 陈老师,我用的是ubuntu linux 安装了oz 创建镜像时遇到许多问题,是不是ubuntu安装oz不能创建镜像啊!

  7. Waiting for centos_63_x86_64 to finish installing, 810/1200
    Waiting for centos_63_x86_64 to finish installing, 800/1200
    Cleaning up guest named centos_63_x86_64
    Cleaning up after install
    Traceback (most recent call last):
    File “/usr/bin/oz-install”, line 162, in
    libvirt_xml = guest.install(timeout, force_download)
    File “/usr/lib/python2.6/site-packages/oz/RedHat.py”, line 685, in install
    self.initrdfname, self.cmdline)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 1663, in _do_install
    self._wait_for_install_finish(dom, timeout)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 775, in _wait_for_install_finish
    raise oz.OzException.OzException(“No disk activity in %d seconds, failing. %s” % (inactivity_timeout, screenshot_text))
    oz.OzException.OzException: No disk activity in 300 seconds, failing. Check screenshot at /var/lib/oz/screenshots/centos_63_x86_64-1406628079.95.ppm for more detail
    陈老师:
    这个问题怎么解决啊?试了各种镜像都不行啊,有什么解决方法吗? 能不能把centos6.ks给做一些解释啊!! 谢谢了

    • 出现错误,先好好读读。Check screenshot at /var/lib/oz/screenshots/centos_63_x86_64-1406628079.95.ppm 把这个文件下载到windows下打开,就可以指定是啥错误。怎么打开这个图片,就自己google搞定。xnview直接搞定。

    • 通过virt-mananger或vnc方式连接到虚拟机console看得更明白。

  8. 符号是中文符号,

  9. 陈老师,我的报这个错误,
    [root@test000 opt]# oz-install -p -u -d3 -a centos6.ks centos65.tdl
    Traceback (most recent call last):
    File “/usr/bin/oz-install”, line 150, in
    netdev, diskbus, macaddress)
    File “/usr/lib/python2.6/site-packages/oz/GuestFactory.py”, line 98, in guest_factory
    diskbus, macaddress)
    File “/usr/lib/python2.6/site-packages/oz/RHEL_6.py”, line 65, in get_class
    macaddress)
    File “/usr/lib/python2.6/site-packages/oz/RHEL_6.py”, line 36, in __init__
    True, True, “cpio”, macaddress)
    File “/usr/lib/python2.6/site-packages/oz/RedHat.py”, line 45, in __init__
    url_allowed, macaddress)
    File “/usr/lib/python2.6/site-packages/oz/Linux.py”, line 34, in __init__
    url_allowed, macaddress)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 1384, in __init__
    url_allowed, macaddress)
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 221, in __init__
    self.connect_to_libvirt()
    File “/usr/lib/python2.6/site-packages/oz/Guest.py”, line 116, in connect_to_libvirt
    self.libvirt_conn = libvirt.open(self.libvirt_uri)
    File “/usr/lib64/python2.6/site-packages/libvirt.py”, line 236, in open
    if ret is None:raise libvirtError(‘virConnectOpen() failed’)
    libvirt.libvirtError: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock’: No such file or directory

  10. 你好,陈老师,我按照你的方法做了centos65和rhel65镜像,centos镜像可以resize,但是rhel65不可以resize,请问这是rhel镜像的问题还是少装了什么东西,上面那几个cloud软件包都装了。

  11. cloud-init 安装完后,怎么样修改才能让它使用openstack的源
    默认使用datasourceNone

  12. 陈老师,
    最近在做openstack的centos镜像发现在EPEL源里没有找到oz这个软件了是这个这个软件以及不包含在EPEL源里了吗

    • 知道问题所在了,这个工具在已经停止了6的更新在7下是有的,我在centos7下进行了制作6.7的镜像测试按您的ks文件到了安装cloud-init时报错,按报错截图说是cloud-init包不存在,这个是什么情况 请陈老师帮助分析

    • 这个真的不好说,改天看看。

 Leave a Reply

(required)

(required)