参考链接:
https://github.com/clalancette/oz/wiki/Oz-template-description-language
https://github.com/clalancette/oz/wiki/oz-install
http://dl528888.blog.51cto.com/2382721/1862568
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.3 镜像
首先创建自己的tdl文件,我创建的是centos7.3.tdl,具体的语法信息请参考oz-Template Description Language,我的tdl文件内容如下:
<template>
<name>qemu-centos73</name>
<os>
<name>CentOS-7</name>
<version>3</version>
<arch>x86_64</arch>
<install type='iso'>
<iso>file:///data/md0/iso/CentOS-7.3-x86_64-Minimal-1611.iso</iso>
</install>
<rootpw>123123</rootpw>
</os>
<disk>
<size>20</size>
</disk>
<description>CentOS 7.3 x86_64</description>
<packages>
<package name='net-tools'/>
<package name='cloud-utils-growpart'/>
<package name='cloud-init-*'/>
<package name='acpid'/>
<package name='NetworkManager'/>
</packages>
<commands>
<command name='install_qga'>
cd /root
tar xf qga.20171124.tar.gz
cd qga
bash ./install_qga_local.sh
</command>
<command name='sed_profile'>
echo "NOZEROCONF=yes" >> /etc/sysconfig/network
rm -rf /etc/udev/rules.d/70-persistent-net.rules
touch /etc/udev/rules.d/75-persistent-net-generator.rules
chmod +x /var/lib/cloud/scripts/per-boot/resetroot
chmod +x /var/lib/cloud/scripts/per-instance/ssh.sh
chmod +x /usr/local/bin/nic_set_mq.sh
echo "/usr/local/bin/nic_set_mq.sh > /tmp/nic_set_mq.log 2>&1" >> /etc/rc.d/rc.local
echo "[connection]" >> /etc/NetworkManager/NetworkManager.conf
echo "ipv4.route-metric=0" >> /etc/NetworkManager/NetworkManager.conf
chmod +x /etc/rc.d/rc.local
</command>
<command name='console'>
grub2-mkconfig -o /boot/grub2/grub.cfg
</command>
<command name='services'>
systemctl enable sshd
systemctl disable firewalld
systemctl enable cloud-init
systemctl enable cloud-config
systemctl enable cloud-final
systemctl enable cloud-init-local
systemctl enable acpid
systemctl enable NetworkManager
</command>
<command name='clear-logs'>
rm -rf /root/*
rm -rf /var/log/anaconda*
rm -rf /var/log/message
rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0
>/var/log/boot.log
>/var/log/messages
>/var/log/cloud-init-output.log
>/var/log/yum.log
>/var/log/lastlog
</command>
</commands>
<files>
<file name="/root/qga.20171124.tar.gz" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/qga.20171124.tar.gz
</file>
<file name="/var/lib/cloud/scripts/per-boot/resetroot" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/resetroot
</file>
<file name="/etc/cloud/cloud.cfg" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/cloud.cfg_centos7
</file>
<file name="/usr/local/bin/nic_set_mq.sh" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/nic_set_mq.sh
</file>
<file name="/etc/default/grub" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/grub_centos7
</file>
<file name="/var/lib/cloud/scripts/per-instance/ssh.sh" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/ssh-centos7.ssh
</file>
<file name="/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg" type="url">
file:///data/nvme0n1/OZ-build-image/scripts/99-disable-network-config.cfg
</file>
</files>
</template>
注意在os下面的name一定要写正确,不然会出错。
其次创建centos7.3.ks文件,可以复制各操作系统自动化安装模板所在目录下相应版本的操作系统的auto文件,我的文件内容如下:
install
text
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp --onboot=on
rootpw Ct@2017Yun!@$%.CN
firewall --disabled
authconfig --enableshadow --enablemd5
timezone --utc Asia/Shanghai
selinux --disabled
zerombr
clearpart --all --drives=vda
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200n8"
part / --fstype xfs --size=2048 --grow --ondisk=vda
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
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
%end
%packages --nobase --excludedocs
%end
最后执行创建命令如下:
oz-install -p -u -d3 -a centos7.3.ks centos7.3.tdl -x centos7.3-libvirt.xml
生成的虚机xml文件
<domain type="kvm">
<name>centos73</name>
<memory>1048576</memory>
<currentMemory>1048576</currentMemory>
<uuid>e7d815ac-ccde-4068-91fc-8925f7b03888</uuid>
<clock offset="utc"/>
<vcpu>1</vcpu>
<features>
<acpi/>
<apic/>
<pae/>
</features>
<os>
<type>hvm</type>
<boot dev="hd"/>
</os>
<on_poweroff>destroy</on_poweroff>
<on_reboot>destroy</on_reboot>
<on_crash>destroy</on_crash>
<devices>
<graphics type="vnc" port="-1" listen="0.0.0.0"/>
<interface type="bridge">
<source bridge="virbr0"/>
<mac address="52:54:00:58:ae:5b"/>
<model type="virtio"/>
</interface>
<input bus="ps2" type="mouse"/>
<serial type="pty">
<target port="0"/>
</serial>
<serial type="tcp">
<source host="127.0.0.1" mode="bind" service="53197"/>
<protocol type="raw"/>
<target port="1"/>
</serial>
<disk device="disk" type="file">
<target bus="virtio" dev="vda"/>
<source file="/data/nvme0n1/oz/images/centos73.dsk"/>
<driver type="raw" name="qemu"/>
</disk>
<channel type="unix">
<source path="/var/lib/libvirt/qemu/org.qemu.guest_agent.0.centos73.sock" mode="bind"/>
<target type="virtio" name="org.qemu.guest_agent.0"/>
</channel>
</devices>
</domain>