Oz 创建Centos 7镜像

Oz简介

做openstack的centos 镜像的步骤,比较繁琐,最近在查比较好用的工具,查到官方提到的Oz,它的功能还是很强大的,它可以自动的安装操作系统,并且可以定制自己的操作系统,还可以生成操作系统的元数据,包括Centos,Ubuntu,Windows等等,它提供了三条很有用的命令,分别是oz-install,oz-customize,oz-generate-icicle,详细介绍可以查看它在github上的代码仓库:Oz

Oz安装

首先,导入epel源,执行如下命令:

rpm -ivh http://mirrors.sohu.com/fedora-epel/7/x86_64/e/epel-release-7-2.noarch.rpm

安装oz和它所需要的依赖,执行如下命令:

yum install -y oz libguestfs-tools

查看oz安装信息,执行如下命令:

rpm -qa | grep oz
rpm -ql oz-0.15.0-1.el7.noarch

安装完成之后:

各操作系统自动化安装模板所在目录:/usr/lib/python2.7/site-packages/oz/auto

tdl配置模板样例的目录:/usr/share/doc/oz-0.15.0/examples

修改镜像类型为qcow修改配置文件:/etc/oz/oz.cfg

[paths]
output_dir = /var/lib/libvirt/images
data_dir = /var/lib/oz
screenshot_dir = /var/lib/oz/screenshots
# sshprivkey = /etc/oz/id_rsa-icicle-gen
[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

创建centos7 镜像

首先创建自己的tdl文件,我创建的是centos7.1.tdl,具体的语法信息请参考oz-Template Description Language,我的tdl文件内容如下:

<template>
 <name>centos7_x86_64</name>
 <description>RedHat 7 x86_64 template</description>
 <os>
  <name>RHEL-7</name>
  <version>1</version>
  <arch>x86_64</arch>
  <rootpw>admin</rootpw>
  <install type='iso'>
    <iso>file:///root/centos7_image/CentOS-7-x86_64-Minimal-1511.iso</iso>
  </install>
 </os>
 <disk>
     <size>20</size>
 </disk>
 <packages>
    <package name='epel-release'/>
    <package name='cloud-utils'/>
    <package name='cloud-init'/>
  </packages>
 <files>
    <file name='/etc/hosts'>
    127.0.0.1     localhost
    ::1           localhost
    </file>
 </files>
  <commands>
   <command name='console'>
    sed -i 's/ rhgb//g' /boot/grub/grub.conf
    sed -i 's/ quiet//g' /boot/grub/grub.conf
   </command>
   <command name='update'>
    sed -i '/^HWADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i '/^UUID/d' /etc/sysconfig/network-scripts/ifcfg-eth0
    echo -n > /etc/udev/rules.d/70-persistent-net.rules
    echo -n > /lib/udev/rules.d/75-persistent-net-generator.rules
    sed -i '/^SELINUX=/s/enforcing/disabled/' /etc/selinux/config
   </command>
   <command name='info'>
    echo 'default root passwd is admin, please change your passwd before you do anything!!!!'
   </command>
 </commands>
</template>

注意在os下面的name一定要写正确,不然会出错。

其次创建centos7.1.ks文件,可以复制各操作系统自动化安装模板所在目录下相应版本的操作系统的auto文件,我的文件内容如下:

install
text
url --url REPLACE_ME
keyboard us
lang en_US
#mouse generic3ps/2 --device psaux
skipx
network --device eth0 --bootproto dhcp
rootpw admin
firewall --disabled
auth --useshadow --enablemd5
timezone --utc America/New_York
lilo --location mbr --linear
zerombr yes
clearpart --all --drives=hda
part /boot --size=200 --ondisk=hda
part / --size=1024 --grow --ondisk=hda
part swap --size 768 --grow --maxsize=1536 --ondisk=hda
reboot
services --disabled="avahi-daemon,iscsi,iscsid,firstboot,kdump" --enabled="network,sshd,rsyslog,tuned"
%post
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
# let's randomise the root password
head -n1 /dev/urandom | md5sum| awk {'print $1'} | passwd --stdin root
yum clean all
rm -rf /var/log/yum.log
rm -rf /root/anaconda-ks.cfg
%end
%packages
python
openssh
openssh-server
nc
wget
%end

最后执行创建命令如下:

oz-install -p -u -d3 -a centos7.1.ks centos7.1.tdl 

创建命令的各个参数意思请参考:oz-install

执行过程信息:

libvirt bridge name is virbr0
Libvirt type is qemu
Name: centos7_x86_64, UUID: e1238d0e-1633-4aa3-9769-0fedfcdc9ac8
MAC: 52:54:00:81:14:a3, distro: RHEL-7
update: 1, arch: x86_64, diskimage: /var/lib/libvirt/images/centos7_x86_64.qcow2
nicmodel: virtio, clockoffset: utc
mousetype: ps2, disk_bus: virtio, disk_dev: vda
icicletmp: /var/lib/oz/icicletmp/centos7_x86_64, listen_port: 45011
Original ISO path: /var/lib/oz/isos/RHEL-71x86_64-iso.iso
Modified ISO cache: /var/lib/oz/isos/RHEL-71x86_64-iso-oz.iso
Output ISO path: /var/lib/libvirt/images/centos7_x86_64-iso-oz.iso
ISO content path: /var/lib/oz/isocontent/centos7_x86_64-iso

执行成功

错误信息

libvirt.libvirtError: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

解决方法:

查看libvirt进程是否启动

   ps -le | grep libvirt*  如果没有启动,那么上面的错误就是这个原因

现在启动libvirt进程 libvirtd -d

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/centos7_x86_64-1464282071.67.ppm for more detail

解决方法:

这个问题一般是centos7.1.ks文件出错,查看/var/lib/oz/screenshots/centos7_x86_64-1464282071.67.ppm该文件,它是一个图片,打开之后如下图:

Clipboard Image.png

错误很清晰,最后一行缺少%end

raise oz.OzException.OzException("Unsupported " + tdl.distro + " update " + tdl.update

oz.OzException.OzException: Unsupported centos-7 update 1

解决办法:

修改centos7.1.tdl 文件,将centos-7 修改为RHEL-7

登录发表评论 注册

Toder

这里 呼唤你

反馈意见