最近工作忙,好久没写博客,现在正好有空介绍一下我这里使用私有云的经验。

现在分享一下如何使用oz自动化制作openstack镜像,下面有我自己制作centos 6.4 6.5 6.7 7.2与ubuntu 12.04.5与14.04.4的模板,模板也存放到github里,地址是https://github.com/dl528888/oz_create_openstack_p_w_picpath_templates。

之前使用镜像要不使用别人做好的,要不是自己制作,但很麻烦,现在使用oz可以简化操作,定制一个模板(类似cobbler)的就可以自动化制作了,十分方便。

官方地址是

下面是介绍如何安装

1、安装epel源

rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpmyum install -y oz

如果还是没有生效,可以使用下面的配置在安装

[epel]name=Extra Packages for Enterprise Linux 6 - $basearch#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearchbaseurl=http://mirrors.sohu.com/fedora-epel/6/$basearch##mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearchfailovermethod=priorityenabled=1gpgcheck=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

2、启动虚拟网络

virsh net-start default

如果出现下面错误

错误:开始网络 default 失败错误:internal error Child process (/usr/sbin/dnsmasq --strict-order --pid-file=/var/run/libvirt/network/default.pid --conf-file= --except-interface lo --bind-interfaces --listen-address 192.168.122.1 --dhcp-range 192.168.122.2,192.168.122.254 --dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases --dhcp-lease-max=253 --dhcp-no-override --dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile --addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts) unexpected exit status 2:dnsmasq: failed to set SO_REUSE{ADDR|PORT} on DHCP socket: Protocol not available

查看一下dnsmasq的版本,如果是2.48就升级

14:12:07 # rpm -qa|grep dnsmasqdnsmasq-2.48-16.el6_7.x86_64

升级方法如下

yum install -y gccwget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.75.tar.gztar zxvf dnsmasq-2.75.tar.gzcd dnsmasq-2.75make V=smv /usr/sbin/dnsmasq /usr/sbin/dnsmasq_backcp src/dnsmasq /usr/sbin/service dnsmasq stopvirsh net-start default

再查看应该有个桥接网卡virbr0启动,ip是192.168.122.1

如果遇到下面错误

01:56:22 # virsh net-start defaulterror: Failed to reconnect to the hypervisorerror: no valid connectionerror: Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory

解决的方法是

service libvirtd start

3、上传或下载镜像

如果当前已有下载好的镜像,可以使用上传到oz所在服务器里,如果没有可以在线下载,下面是介绍如果下降镜像到服务器里。

rsync -avz --progress mirror.nsc.liu.se::centos-store/6.5/isos/x86_64/CentOS-6.5-x86_64-bin-DVD1.iso /mnt/iso/

默认国内是只有最新的版本,旧版本只能从国外下载或通过bt下载

4、编写模板

模板我都写完了,大家可以直接使用或在我模板上修改

所有模板都在存放到/etc/oz/conf里

A、制作centos 6.4 x86_64系统的

centos64.tdl配置

centos64.ks配置

installtextkey --skipkeyboard uslang en_US.UTF-8skipxnetwork --device eth0 --bootproto dhcprootpw denglei2016firewall --disabledauthconfig --enableshadow --enablemd5selinux --disabledtimezone --utc Asia/Chongqingbootloader --location=mbr --append="console=tty0 console=ttyS0,115200"zerombr yesclearpart --allpart / --fstype ext4 --size=19768 --growreboot%postrm -rf /etc/yum.repos.d/*cat <
 /etc/yum.repos.d/CentOS6.repo# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client.  You should use this for CentOS updates# unless you are manually picking other mirrors.## If the mirrorlist= does not work for you, as a fall back you can try the# remarked out baseurl= line instead.##[base]name=CentOS-6 - Base - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/os/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=osgpgcheck=0gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#released updates[updates]name=CentOS-6 - Updates - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/updates/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=updatesgpgcheck=1gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#additional packages that may be useful[extras]name=CentOS-6 - Extras - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/extras/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=extrasgpgcheck=1gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#additional packages that extend functionality of existing packages[centosplus]name=CentOS-6 - Plus - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/centosplus/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=centosplusgpgcheck=1enabled=0gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6[epel]name=CentOS-6 - Epel - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/epel/6/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=contribgpgcheck=1enabled=1gpgkey=http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-6EOLyum clean allsed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_configcat >> /etc/rc.local << EOF/bin/bash /usr/local/bin/instance_init.shEOFcat >/usr/local/bin/instance_init.sh<
 /tmp/metadata-key 2>/dev/null  if [ \$? -eq 0 ]; then    cat /tmp/metadata-key >> /root/.ssh/authorized_keys    chmod 0600 /root/.ssh/authorized_keys    restorecon /root/.ssh/authorized_keys    rm -f /tmp/metadata-key    echo "Successfully retrieved public key from instance metadata"    echo "*****************"    echo "AUTHORIZED KEYS"    echo "*****************"    cat /root/.ssh/authorized_keys    echo "*****************"    curl -f http://169.254.169.254/latest/meta-data/reservation-id > /tmp/metadata-hostname 2>/dev/null    if [ \$? -eq 0 ]; then      TEMP_HOST=\$(cat /tmp/metadata-hostname)      #TEMP_HOST=\$(cat /tmp/metadata-hostname|awk -F '.novalocal' '{print \$1}')      sed -i "s/^HOSTNAME=.*\$/HOSTNAME=\$TEMP_HOST/g" /etc/sysconfig/network      /bin/hostname \$TEMP_HOST      echo "Successfully retrieved hostname from instance metadata"      echo "*****************"      echo "HOSTNAME CONFIG"      echo "*****************"      cat /etc/sysconfig/network      echo "*****************"    else      echo "Failed to retrieve hostname from instance metadata.  This is a soft error so we'll continue"    fi    rm -f /tmp/metadata-hostname    sed -i '/instance_init/d' /etc/rc.d/rc.local    rm -rf /usr/local/bin/instance_init.sh  else    FAILED=\$((\$FAILED + 1))    if [ \$FAILED -ge \$ATTEMPTS ]; then      echo "Failed to retrieve public key from instance metadata after \$FAILED attempts, quitting"      break    fi      echo "Could not retrieve public key from instance metadata (attempt #\$FAILED/\$ATTEMPTS), retrying in 5 seconds..."      sleep 5    fidoneEOFrm -rf /var/log/yum.logrm -rf /var/lib/yum/*rm -rf /root/install.logrm -rf /root/install.log.syslogrm -rf /root/anaconda-ks.cfgrm -rf /var/log/anaconda*rm -rf /tmp/yum.logrm -rf /tmp/ks-script-*%packages --nobase --excludedocsopenssh-serveropenssh-clientspythonacpidwgetvim

默认的root密码是denglei2016。

下面是自动化制作

命令如下

oz-install -p -u -d3 -a centos64.ks centos64.tdl -x /tmp/centos64.xml

下面是运行结果

[root@ip-10-10-125-22 finish]# ll /var/lib/libvirt/p_w_picpaths/CentOS-6.4-x86_64.qcow2-rw-rw-rw-. 1 root root 1219493888 Jul 20 10:58 /var/lib/libvirt/p_w_picpaths/CentOS-6.4-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /var/lib/libvirt/p_w_picpaths/CentOS-6.4-x86_64.qcow21.2G  /var/lib/libvirt/p_w_picpaths/CentOS-6.4-x86_64.qcow2

当前是1.2G,下面压缩一下

压缩命令是

qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-6.4-x86_64.qcow2 -O qcow2 /tmp/CentOS-6.4-x86_64.qcow2

运行结果

[root@ip-10-10-125-22 finish]# ll /tmp/CentOS-6.4-x86_64.qcow2-rw-r--r--. 1 root root 268037120 Jul 20 11:00 /tmp/CentOS-6.4-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /tmp/CentOS-6.4-x86_64.qcow2256M  /tmp/CentOS-6.4-x86_64.qcow2

可以看到从1.2G压缩到了256M,压缩了差不多4/5.

B.制作centos6.5 x86_64

下面是模板配置

centos65.tdl配置

下面是centos65.ks配置

installtextkey --skipkeyboard uslang en_US.UTF-8skipxnetwork --device eth0 --bootproto dhcprootpw denglei2016firewall --disabledauthconfig --enableshadow --enablemd5selinux --disabledtimezone --utc Asia/Chongqingbootloader --location=mbr --append="console=tty0 console=ttyS0,115200"zerombr yesclearpart --allpart / --fstype ext4 --size=19768 --growreboot%postrm -rf /etc/yum.repos.d/*cat <
 /etc/yum.repos.d/CentOS6.repo# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client.  You should use this for CentOS updates# unless you are manually picking other mirrors.## If the mirrorlist= does not work for you, as a fall back you can try the# remarked out baseurl= line instead.##[base]name=CentOS-6 - Base - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/os/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=osgpgcheck=0gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#released updates[updates]name=CentOS-6 - Updates - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/updates/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=updatesgpgcheck=1gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#additional packages that may be useful[extras]name=CentOS-6 - Extras - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/extras/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=extrasgpgcheck=1gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#additional packages that extend functionality of existing packages[centosplus]name=CentOS-6 - Plus - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/centosplus/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=centosplusgpgcheck=1enabled=0gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6[epel]name=CentOS-6 - Epel - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/epel/6/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=contribgpgcheck=1enabled=1gpgkey=http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-6EOLyum clean allsed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_configcat >> /etc/rc.local << EOF/bin/bash /usr/local/bin/instance_init.shEOFcat >/usr/local/bin/instance_init.sh<
 /tmp/metadata-key 2>/dev/null  if [ \$? -eq 0 ]; then    cat /tmp/metadata-key >> /root/.ssh/authorized_keys    chmod 0600 /root/.ssh/authorized_keys    restorecon /root/.ssh/authorized_keys    rm -f /tmp/metadata-key    echo "Successfully retrieved public key from instance metadata"    echo "*****************"    echo "AUTHORIZED KEYS"    echo "*****************"    cat /root/.ssh/authorized_keys    echo "*****************"    curl -f http://169.254.169.254/latest/meta-data/reservation-id > /tmp/metadata-hostname 2>/dev/null    if [ \$? -eq 0 ]; then      TEMP_HOST=\$(cat /tmp/metadata-hostname)      sed -i "s/^HOSTNAME=.*\$/HOSTNAME=\$TEMP_HOST/g" /etc/sysconfig/network      /bin/hostname \$TEMP_HOST      echo "Successfully retrieved hostname from instance metadata"      echo "*****************"      echo "HOSTNAME CONFIG"      echo "*****************"      cat /etc/sysconfig/network      echo "*****************"    else      echo "Failed to retrieve hostname from instance metadata.  This is a soft error so we'll continue"    fi    rm -f /tmp/metadata-hostname    sed -i '/instance_init/d' /etc/rc.d/rc.local    rm -rf /usr/local/bin/instance_init.sh  else    FAILED=\$((\$FAILED + 1))    if [ \$FAILED -ge \$ATTEMPTS ]; then      echo "Failed to retrieve public key from instance metadata after \$FAILED attempts, quitting"      break    fi      echo "Could not retrieve public key from instance metadata (attempt #\$FAILED/\$ATTEMPTS), retrying in 5 seconds..."      sleep 5    fidoneEOFrm -rf /var/log/yum.logrm -rf /var/lib/yum/*rm -rf /root/install.logrm -rf /root/install.log.syslogrm -rf /root/anaconda-ks.cfgrm -rf /var/log/anaconda*rm -rf /tmp/yum.logrm -rf /tmp/ks-script-*%packages --nobase --excludedocsopenssh-serveropenssh-clientspythonacpidwgetvim

下面是自动化制作

制作命令

oz-install -p -u -d3 -a centos65.ks centos65.tdl -x /tmp/centos65.xml

运行结果

[root@ip-10-10-125-22 finish]# ll /var/lib/libvirt/p_w_picpaths/CentOS-6.5-x86_64.qcow2-rw-rw-rw-. 1 root root 1250230272 Jul 20 11:56 /var/lib/libvirt/p_w_picpaths/CentOS-6.5-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /var/lib/libvirt/p_w_picpaths/CentOS-6.5-x86_64.qcow21.2G  /var/lib/libvirt/p_w_picpaths/CentOS-6.5-x86_64.qcow2

开始压缩,压缩命令是

qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-6.5-x86_64.qcow2 -O qcow2 /tmp/CentOS-6.5-x86_64.qcow2
[root@ip-10-10-125-22 finish]# qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-6.5-x86_64.qcow2 -O qcow2 /tmp/CentOS-6.5-x86_64.qcow2[root@ip-10-10-125-22 finish]# ll /tmp/CentOS-6.5-x86_64.qcow2-rw-r--r--. 1 root root 280721408 Jul 20 12:12 /tmp/CentOS-6.5-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /tmp/CentOS-6.5-x86_64.qcow2268M  /tmp/CentOS-6.5-x86_64.qcow2

C、制作centos 6.7 x86_64镜像

下面是模板配置

centos67.tdl配置

centos67.ks配置

installtextkey --skipkeyboard uslang en_US.UTF-8skipxnetwork --device eth0 --bootproto dhcprootpw denglei2016firewall --disabledauthconfig --enableshadow --enablemd5selinux --disabledtimezone --utc Asia/Chongqingbootloader --location=mbr --append="console=tty0 console=ttyS0,115200"zerombr yesclearpart --allpart / --fstype ext4 --size=19768 --growreboot%postrm -rf /etc/yum.repos.d/*cat <
 /etc/yum.repos.d/CentOS6.repo# CentOS-Base.repo## The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and# geographically close to the client.  You should use this for CentOS updates# unless you are manually picking other mirrors.## If the mirrorlist= does not work for you, as a fall back you can try the# remarked out baseurl= line instead.##[base]name=CentOS-6 - Base - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/os/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=osgpgcheck=0gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#released updates[updates]name=CentOS-6 - Updates - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/updates/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=updatesgpgcheck=1gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#additional packages that may be useful[extras]name=CentOS-6 - Extras - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/extras/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=extrasgpgcheck=1gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6#additional packages that extend functionality of existing packages[centosplus]name=CentOS-6 - Plus - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/centos/6/centosplus/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=centosplusgpgcheck=1enabled=0gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6[epel]name=CentOS-6 - Epel - mirrors.ustc.edu.cnbaseurl=http://mirrors.ustc.edu.cn/epel/6/\$basearch/#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=contribgpgcheck=1enabled=1gpgkey=http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-6EOLyum clean allsed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_configcat >> /etc/rc.local << EOF/bin/bash /usr/local/bin/instance_init.shEOFcat >/usr/local/bin/instance_init.sh<
 /tmp/metadata-key 2>/dev/null  if [ \$? -eq 0 ]; then    cat /tmp/metadata-key >> /root/.ssh/authorized_keys    chmod 0600 /root/.ssh/authorized_keys    restorecon /root/.ssh/authorized_keys    rm -f /tmp/metadata-key    echo "Successfully retrieved public key from instance metadata"    echo "*****************"    echo "AUTHORIZED KEYS"    echo "*****************"    cat /root/.ssh/authorized_keys    echo "*****************"    curl -f http://169.254.169.254/latest/meta-data/reservation-id > /tmp/metadata-hostname 2>/dev/null    if [ \$? -eq 0 ]; then      TEMP_HOST=\$(cat /tmp/metadata-hostname)      sed -i "s/^HOSTNAME=.*\$/HOSTNAME=\$TEMP_HOST/g" /etc/sysconfig/network      /bin/hostname \$TEMP_HOST      echo "Successfully retrieved hostname from instance metadata"      echo "*****************"      echo "HOSTNAME CONFIG"      echo "*****************"      cat /etc/sysconfig/network      echo "*****************"    else      echo "Failed to retrieve hostname from instance metadata.  This is a soft error so we'll continue"    fi    rm -f /tmp/metadata-hostname    sed -i '/instance_init/d' /etc/rc.d/rc.local    rm -rf /usr/local/bin/instance_init.sh  else    FAILED=\$((\$FAILED + 1))    if [ \$FAILED -ge \$ATTEMPTS ]; then      echo "Failed to retrieve public key from instance metadata after \$FAILED attempts, quitting"      break    fi      echo "Could not retrieve public key from instance metadata (attempt #\$FAILED/\$ATTEMPTS), retrying in 5 seconds..."      sleep 5    fidoneEOFrm -rf /var/log/yum.logrm -rf /var/lib/yum/*rm -rf /root/install.logrm -rf /root/install.log.syslogrm -rf /root/anaconda-ks.cfgrm -rf /var/log/anaconda*rm -rf /tmp/yum.logrm -rf /tmp/ks-script-*%packages --nobase --excludedocsopenssh-serveropenssh-clientspythonacpidwgetvim

开始自动化制作镜像

制作命令

oz-install -p -u -d3 -a centos67.ks centos67.tdl -x /tmp/centos67.xml

运行结果

[root@ip-10-10-125-22 finish]# ll /var/lib/libvirt/p_w_picpaths/CentOS-6.7-x86_64.qcow2-rw-rw-rw-. 1 root root 1319043072 Jul 20 12:30 /var/lib/libvirt/p_w_picpaths/CentOS-6.7-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /var/lib/libvirt/p_w_picpaths/CentOS-6.7-x86_64.qcow21.3G  /var/lib/libvirt/p_w_picpaths/CentOS-6.7-x86_64.qcow2

开始压缩,压缩命令

qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-6.7-x86_64.qcow2 -O qcow2 /tmp/CentOS-6.7-x86_64.qcow2

压缩结果

[root@ip-10-10-125-22 finish]# qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-6.7-x86_64.qcow2 -O qcow2 /tmp/CentOS-6.7-x86_64.qcow2[root@ip-10-10-125-22 finish]# ll /tmp/CentOS-6.7-x86_64.qcow2-rw-r--r--. 1 root root 313454592 Jul 20 14:14 /tmp/CentOS-6.7-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /tmp/CentOS-6.7-x86_64.qcow2299M  /tmp/CentOS-6.7-x86_64.qcow2

D、制作centos 7.2 x86_64镜像

下面是模板配置

centos72.tdl

centos72.ks

installtextkeyboard uslang en_US.UTF-8skipxnetwork --device eth0 --bootproto dhcprootpw denglei2016firewall --disabledauthconfig --enableshadow --enablemd5selinux --disabledservices --enabled=NetworkManager,sshdtimezone --utc Asia/Chongqing --isUtc --nontpbootloader --location=mbr --append="console=tty0 console=ttyS0,115200"zerombrclearpart --all --initlabelpart / --fstype ext4 --size=19768 --growreboot%postchmod 0655 /etc/rc.d/rc.localyum clean allsed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_configcat >> /etc/rc.local << EOF/bin/bash /usr/local/bin/instance_init.shEOFcat >/usr/local/bin/instance_init.sh<
 /tmp/metadata-key 2>/dev/null  if [ \$? -eq 0 ]; then    cat /tmp/metadata-key >> /root/.ssh/authorized_keys    chmod 0600 /root/.ssh/authorized_keys    restorecon /root/.ssh/authorized_keys    rm -f /tmp/metadata-key    echo "Successfully retrieved public key from instance metadata"    echo "*****************"    echo "AUTHORIZED KEYS"    echo "*****************"    cat /root/.ssh/authorized_keys    echo "*****************"    curl -f http://169.254.169.254/latest/meta-data/reservation-id > /tmp/metadata-hostname 2>/dev/null    if [ \$? -eq 0 ]; then      TEMP_HOST=\$(cat /tmp/metadata-hostname)      sed -i "s/^HOSTNAME=.*\$/HOSTNAME=\$TEMP_HOST/g" /etc/sysconfig/network      /bin/ hostnamectl --static set-hostname \$TEMP_HOST      /bin/hostnamectl set-hostname \$TEMP_HOST     # /bin/hostname \$TEMP_HOST      echo "Successfully retrieved hostname from instance metadata"      echo "*****************"      echo "HOSTNAME CONFIG"      echo "*****************"      cat /etc/sysconfig/network      echo "*****************"    else      echo "Failed to retrieve hostname from instance metadata.  This is a soft error so we'll continue"    fi    rm -f /tmp/metadata-hostname    sed -i '/instance_init/d' /etc/rc.d/rc.local    rm -rf /usr/local/bin/instance_init.sh    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*    rm -rf /tmp/yum.log    rm -rf /tmp/ks-script-*  else    FAILED=\$((\$FAILED + 1))    if [ \$FAILED -ge \$ATTEMPTS ]; then      echo "Failed to retrieve public key from instance metadata after \$FAILED attempts, quitting"      break    fi      echo "Could not retrieve public key from instance metadata (attempt #\$FAILED/\$ATTEMPTS), retrying in 5 seconds..."      sleep 5    fidoneEOF%end%packages --nobase --excludedocsopenssh-serveropenssh-clientsacpidwgetvim%end

开始自动化制作

制作命令

oz-install -p -u -d3 -a centos72.ks centos72.tdl -x /tmp/centos72.xml

运行结果

[root@ip-10-10-125-22 finish]# ll /var/lib/libvirt/p_w_picpaths/CentOS-7.2-x86_64.qcow2-rw-rw-rw-. 1 root root 1569193984 Jul 20 14:39 /var/lib/libvirt/p_w_picpaths/CentOS-7.2-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /var/lib/libvirt/p_w_picpaths/CentOS-7.2-x86_64.qcow21.5G  /var/lib/libvirt/p_w_picpaths/CentOS-7.2-x86_64.qcow2

开始压缩,压缩命令是

qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-7.2-x86_64.qcow2 -O qcow2 /tmp/CentOS-7.2-x86_64.qcow2

压缩结果

[root@ip-10-10-125-22 finish]# qemu-img convert -c /var/lib/libvirt/p_w_picpaths/CentOS-7.2-x86_64.qcow2 -O qcow2 /tmp/CentOS-7.2-x86_64.qcow2[root@ip-10-10-125-22 finish]# ll /tmp/CentOS-7.2-x86_64.qcow2-rw-r--r--. 1 root root 461023232 Jul 20 15:03 /tmp/CentOS-7.2-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /tmp/CentOS-7.2-x86_64.qcow2439M  /tmp/CentOS-7.2-x86_64.qcow2

E、制作ubuntu 12.0.4.5 x86_64镜像

下面是模板配置

ubuntu_12.04.5.tdl

ubuntu_12.04.5.ks

d-i debian-installer/locale string en_USd-i console-setup/ask_detect boolean falsed-i console-setup/layoutcode string usd-i netcfg/choose_interface select autod-i netcfg/get_hostname string unassigned-hostnamed-i netcfg/get_domain string unassigned-domaind-i netcfg/wireless_wep stringd-i clock-setup/utc boolean trued-i time/zone string Asia/Shanghaid-i partman-auto/method string regulard-i partman-auto/choose_recipe select homed-i partman/confirm_write_new_label boolean trued-i partman/choose_partition select finishd-i partman/confirm boolean trued-i partman/confirm_nooverwrite boolean trued-i passwd/root-login boolean trued-i passwd/make-user boolean falsed-i passwd/root-password password denglei2016d-i passwd/root-password-again password denglei2016tasksel tasksel/first multiselect standardd-i pkgsel/include/install-recommends boolean trued-i pkgsel/include string openssh-server curld-i grub-installer/only_debian boolean trued-i grub-installer/with_other_os boolean trued-i apt-setup/security_host stringbase-config apt-setup/security-updates boolean falseubiquity ubiquity/summary noteubiquity ubiquity/reboot boolean trued-i finish-install/reboot_in_progress note# In Debian/Ubuntu, ssh keys are generated at package install time.  Because# the disk p_w_picpath may be cached, we need to remove the ssh keys, but this means# that ssh'ing into the server won't work later.  So we remove the keys, but# setup a service that will generate the keys on boot if necessary.d-i preseed/late_command string \    in-target rm -f /etc/ssh/ssh_host_dsa_key /etc/ssh/ssh_host_dsa_key.pub /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_rsa_key.pub /etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_ed25519_key.pub ; \    echo '#! /bin/sh' > /target/etc/init.d/oz-generate-ssh ; \    echo '#' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# Debian/Ubuntu generate ssh host keys at package installation time.' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# This is problematic for Oz, since the final disk p_w_picpath may be cached' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# and reused, leading to duplicate host keys.  To work around this, Oz' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# deletes the SSH host keys at the end of installation.  This solves' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# the above problem, but introduces the problem of having no way to' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# SSH into the machine without manual intervention.  This service checks' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# to see if host keys are already installed, and if not, recreates them.' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# Note that after the very first boot, this service could be removed.' >> /target/etc/init.d/oz-generate-ssh ; \    echo '#' >> /target/etc/init.d/oz-generate-ssh ; \    echo 'case "$1" in' >> /target/etc/init.d/oz-generate-ssh ; \    echo '  start)' >> /target/etc/init.d/oz-generate-ssh ; \    echo '      [ -r /etc/ssh/ssh_host_rsa_key ] || /usr/sbin/dpkg-reconfigure openssh-server' >> /target/etc/init.d/oz-generate-ssh ; \    echo '      ;;' >> /target/etc/init.d/oz-generate-ssh ; \    echo 'esac' >> /target/etc/init.d/oz-generate-ssh ; \    echo 'exit 0' >> /target/etc/init.d/oz-generate-ssh ; \    in-target chmod 755 /etc/init.d/oz-generate-ssh ; \    in-target ln -s /etc/init.d/oz-generate-ssh /etc/rc2.d/S40oz-generate-ssh

开始自动化制作

制作命令

 oz-install -p -u -d3 -a ubuntu_12.04.5.ks ubuntu_12.04.5.tdl -x /tmp/ubuntu12.04.5.xml -t 3000

请注意制作ubuntu镜像需要把超时时间延迟,默认是1200s,由于安装库太多,默认超时时间不足,会导致制作失败,需要延长到3000s。

运行结果

 [root@ip-10-10-125-22 finish]# ll /var/lib/libvirt/p_w_picpaths/Ubuntu-12.04.5-x86_64.qcow2-rw-rw-rw-. 1 root root 1621164032 Sep  9 17:00 /var/lib/libvirt/p_w_picpaths/Ubuntu-12.04.5-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh  /var/lib/libvirt/p_w_picpaths/Ubuntu-12.04.5-x86_64.qcow21.6G    /var/lib/libvirt/p_w_picpaths/Ubuntu-12.04.5-x86_64.qcow2

开始压缩,压缩命令

qemu-img convert -c /var/lib/libvirt/p_w_picpaths/Ubuntu-12.04.5-x86_64.qcow2 -O qcow2 /tmp/Ubuntu-12.04.5-x86_64.qcow2

压缩结果

[root@ip-10-10-125-22 finish]# du -sh /tmp/Ubuntu-12.04.5-x86_64.qcow2418M    /tmp/Ubuntu-12.04.5-x86_64.qcow2

F、制作ubuntu 14.04.4 x86_64镜像

下面是模板配置

ubuntu_14.04.4.tdl

ubuntu_14.04.4.ks

d-i debian-installer/locale string en_USd-i console-setup/ask_detect boolean falsed-i console-setup/layoutcode string usd-i netcfg/choose_interface select autod-i netcfg/get_hostname string unassigned-hostnamed-i netcfg/get_domain string unassigned-domaind-i netcfg/wireless_wep stringd-i clock-setup/utc boolean trued-i time/zone string Asia/Shanghaid-i partman-auto/method string regulard-i partman-auto/choose_recipe select homed-i partman/confirm_write_new_label boolean trued-i partman/choose_partition select finishd-i partman/confirm boolean trued-i partman/confirm_nooverwrite boolean trued-i passwd/root-login boolean trued-i passwd/make-user boolean falsed-i passwd/root-password password denglei2016d-i passwd/root-password-again password denglei2016tasksel tasksel/first multiselect standardd-i pkgsel/include/install-recommends boolean trued-i pkgsel/include string openssh-server curld-i grub-installer/only_debian boolean trued-i grub-installer/with_other_os boolean trued-i apt-setup/security_host stringbase-config apt-setup/security-updates boolean falseubiquity ubiquity/summary noteubiquity ubiquity/reboot boolean trued-i finish-install/reboot_in_progress note# In Debian/Ubuntu, ssh keys are generated at package install time.  Because# the disk p_w_picpath may be cached, we need to remove the ssh keys, but this means# that ssh'ing into the server won't work later.  So we remove the keys, but# setup a service that will generate the keys on boot if necessary.d-i preseed/late_command string \    in-target rm -f /etc/ssh/ssh_host_dsa_key /etc/ssh/ssh_host_dsa_key.pub /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_rsa_key.pub /etc/ssh/ssh_host_ed25519_key /etc/ssh/ssh_host_ed25519_key.pub ; \    echo '#! /bin/sh' > /target/etc/init.d/oz-generate-ssh ; \    echo '#' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# Debian/Ubuntu generate ssh host keys at package installation time.' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# This is problematic for Oz, since the final disk p_w_picpath may be cached' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# and reused, leading to duplicate host keys.  To work around this, Oz' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# deletes the SSH host keys at the end of installation.  This solves' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# the above problem, but introduces the problem of having no way to' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# SSH into the machine without manual intervention.  This service checks' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# to see if host keys are already installed, and if not, recreates them.' >> /target/etc/init.d/oz-generate-ssh ; \    echo '# Note that after the very first boot, this service could be removed.' >> /target/etc/init.d/oz-generate-ssh ; \    echo '#' >> /target/etc/init.d/oz-generate-ssh ; \    echo 'case "$1" in' >> /target/etc/init.d/oz-generate-ssh ; \    echo '  start)' >> /target/etc/init.d/oz-generate-ssh ; \    echo '      [ -r /etc/ssh/ssh_host_rsa_key ] || /usr/sbin/dpkg-reconfigure openssh-server' >> /target/etc/init.d/oz-generate-ssh ; \    echo '      ;;' >> /target/etc/init.d/oz-generate-ssh ; \    echo 'esac' >> /target/etc/init.d/oz-generate-ssh ; \    echo 'exit 0' >> /target/etc/init.d/oz-generate-ssh ; \    in-target chmod 755 /etc/init.d/oz-generate-ssh ; \    in-target ln -s /etc/init.d/oz-generate-ssh /etc/rc2.d/S40oz-generate-ssh

开始自动化制作,下面是制作命令

oz-install -p -u -d3 -a ubuntu_12.04.5.ks ubuntu_14.04.4.tdl -x /tmp/ubuntu14.04.4.xml

运行结果

[root@ip-10-10-125-22 finish]# ll /var/lib/libvirt/p_w_picpaths/Ubuntu-14.04.4-x86_64.qcow2-rw-rw-rw-. 1 root root 1938292736 Sep  9 17:48 /var/lib/libvirt/p_w_picpaths/Ubuntu-14.04.4-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /var/lib/libvirt/p_w_picpaths/Ubuntu-14.04.4-x86_64.qcow21.9G    /var/lib/libvirt/p_w_picpaths/Ubuntu-14.04.4-x86_64.qcow2

开始压缩,压缩命令是

qemu-img convert -c /var/lib/libvirt/p_w_picpaths/Ubuntu-14.04.4-x86_64.qcow2 -O qcow2 /tmp/Ubuntu-14.04.4-x86_64.qcow2

压缩结果

[root@ip-10-10-125-22 finish]# ll /tmp/Ubuntu-14.04.4-x86_64.qcow2-rw-r--r--. 1 root root 536608768 Sep  9 17:55 /tmp/Ubuntu-14.04.4-x86_64.qcow2[root@ip-10-10-125-22 finish]# du -sh /tmp/Ubuntu-14.04.4-x86_64.qcow2511M    /tmp/Ubuntu-14.04.4-x86_64.qcow2

模板大家可以在上面代码里直接复制,也可以去我的github里下载。

有问题大家可以留言