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

现在分享一下如何使用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)的就可以自动化制作了,十分方便。

官方地址是https://github.com/clalancette/oz/

下面是介绍如何安装

1、安装epel源

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

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

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
baseurl=http://mirrors.sohu.com/fedora-epel/6/$basearch
##mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=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 dnsmasq
dnsmasq-2.48-16.el6_7.x86_64

升级方法如下

yum install -y gcc
wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.75.tar.gz
tar zxvf dnsmasq-2.75.tar.gz
cd dnsmasq-2.75
make V=s
mv /usr/sbin/dnsmasq /usr/sbin/dnsmasq_back
cp src/dnsmasq /usr/sbin/
service dnsmasq stop
virsh net-start default

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

如果遇到下面错误

01:56:22 # virsh net-start default
error: Failed to reconnect to the hypervisor
error: no valid connection
error: 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配置

<template>
  <name>CentOS-6.4-x86_64</name>
  <disk>
    <size>20</size>
  </disk>
  <os>
    <name>CentOS-6</name>
    <version>4</version>
    <arch>x86_64</arch>
    <install type='iso'>
      <iso>file:///mnt/iso/CentOS-6.4-x86_64-bin-DVD1.iso</iso>
    </install>
  </os>
  <description>CentOS 6.4 x86_64</description>
  <commands>
    <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
    </command>
  </commands>
</template>

centos64.ks配置

install
text
key --skip
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp
rootpw denglei2016
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc Asia/Chongqing
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200"
zerombr yes
clearpart --all

part / --fstype ext4 --size=19768 --grow
reboot
%post
rm -rf /etc/yum.repos.d/*
cat <<EOL > /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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/os/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=os
gpgcheck=0
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-6 - Updates - mirrors.ustc.edu.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/updates/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=updates
gpgcheck=1
gpgkey=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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/extras/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=extras
gpgcheck=1
gpgkey=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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/centosplus/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6

[epel]
name=CentOS-6 - Epel - mirrors.ustc.edu.cn
baseurl=http://mirrors.ustc.edu.cn/epel/6/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=contrib
gpgcheck=1
enabled=1
gpgkey=http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-6
EOL
yum clean all
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
cat >> /etc/rc.local << EOF
/bin/bash /usr/local/bin/instance_init.sh
EOF
cat >/usr/local/bin/instance_init.sh<<EOF
#!/bin/bash
if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi
# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /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
    fi
done
EOF
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-*
%packages --nobase --excludedocs
openssh-server
openssh-clients
python
acpid
wget
vim

默认的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.qcow2
1.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.qcow2
256M  /tmp/CentOS-6.4-x86_64.qcow2

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

B.制作centos6.5 x86_64

下面是模板配置

centos65.tdl配置

<template>
  <name>CentOS-6.5-x86_64</name>
  <disk>
    <size>20</size>
  </disk>
  <os>
    <name>CentOS-6</name>
    <version>5</version>
    <arch>x86_64</arch>
    <install type='iso'>
      <iso>file:///mnt/iso/CentOS-6.5-x86_64-bin-DVD1.iso</iso>
    </install>
  </os>
  <description>CentOS 6.5 x86_64</description>
  <commands>
    <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
    </command>
  </commands>
</template>

下面是centos65.ks配置

install
text
key --skip
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp
rootpw denglei2016
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc Asia/Chongqing
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200"
zerombr yes
clearpart --all

part / --fstype ext4 --size=19768 --grow
reboot
%post
rm -rf /etc/yum.repos.d/*
cat <<EOL > /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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/os/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=os
gpgcheck=0
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-6 - Updates - mirrors.ustc.edu.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/updates/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=updates
gpgcheck=1
gpgkey=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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/extras/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=extras
gpgcheck=1
gpgkey=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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/centosplus/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6

[epel]
name=CentOS-6 - Epel - mirrors.ustc.edu.cn
baseurl=http://mirrors.ustc.edu.cn/epel/6/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=contrib
gpgcheck=1
enabled=1
gpgkey=http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-6
EOL
yum clean all
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
cat >> /etc/rc.local << EOF
/bin/bash /usr/local/bin/instance_init.sh
EOF
cat >/usr/local/bin/instance_init.sh<<EOF
#!/bin/bash
if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi
# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /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
    fi
done
EOF
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-*
%packages --nobase --excludedocs
openssh-server
openssh-clients
python
acpid
wget
vim

下面是自动化制作

制作命令

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.qcow2
1.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.qcow2
268M  /tmp/CentOS-6.5-x86_64.qcow2

C、制作centos 6.7 x86_64镜像

下面是模板配置

centos67.tdl配置

<template>
  <name>CentOS-6.7-x86_64</name>
  <disk>
    <size>20</size>
  </disk>
  <os>
    <name>CentOS-6</name>
    <version>7</version>
    <arch>x86_64</arch>
    <install type='iso'>
      <iso>file:///mnt/iso/CentOS-6.7-x86_64-bin-DVD1.iso</iso>
    </install>
  </os>
  <description>CentOS 6.7 x86_64</description>
  <commands>
    <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
    </command>
  </commands>
</template>

centos67.ks配置

install
text
key --skip
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp
rootpw denglei2016
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc Asia/Chongqing
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200"
zerombr yes
clearpart --all

part / --fstype ext4 --size=19768 --grow
reboot
%post
rm -rf /etc/yum.repos.d/*
cat <<EOL > /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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/os/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=os
gpgcheck=0
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6

#released updates
[updates]
name=CentOS-6 - Updates - mirrors.ustc.edu.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/updates/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=updates
gpgcheck=1
gpgkey=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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/extras/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=extras
gpgcheck=1
gpgkey=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.cn
baseurl=http://mirrors.ustc.edu.cn/centos/6/centosplus/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-6

[epel]
name=CentOS-6 - Epel - mirrors.ustc.edu.cn
baseurl=http://mirrors.ustc.edu.cn/epel/6/\$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=6&arch=\$basearch&repo=contrib
gpgcheck=1
enabled=1
gpgkey=http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-6
EOL
yum clean all
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
cat >> /etc/rc.local << EOF
/bin/bash /usr/local/bin/instance_init.sh
EOF
cat >/usr/local/bin/instance_init.sh<<EOF
#!/bin/bash
if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi
# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /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
    fi
done
EOF
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-*
%packages --nobase --excludedocs
openssh-server
openssh-clients
python
acpid
wget
vim

开始自动化制作镜像

制作命令

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.qcow2
1.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.qcow2
299M  /tmp/CentOS-6.7-x86_64.qcow2

D、制作centos 7.2 x86_64镜像

下面是模板配置

centos72.tdl

<template>
  <name>CentOS-7.2-x86_64</name>
  <disk>
    <size>20</size>
  </disk>
  <os>
    <name>CentOS-7</name>
    <version>2</version>
    <arch>x86_64</arch>
    <install type='iso'>
      <iso>file:///mnt/iso/CentOS-7-x86_64-DVD-1511.iso</iso>
    </install>
  </os>
  <description>CentOS 7.2 x86_64</description>
  <commands>
    <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
    </command>
  </commands>
</template>

centos72.ks

install
text
keyboard us
lang en_US.UTF-8
skipx
network --device eth0 --bootproto dhcp
rootpw denglei2016
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
services --enabled=NetworkManager,sshd
timezone --utc Asia/Chongqing --isUtc --nontp
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200"
zerombr
clearpart --all --initlabel
part / --fstype ext4 --size=19768 --grow
reboot
%post
chmod 0655 /etc/rc.d/rc.local
yum clean all
sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
cat >> /etc/rc.local << EOF
/bin/bash /usr/local/bin/instance_init.sh
EOF
cat >/usr/local/bin/instance_init.sh<<EOF
#!/bin/bash
if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi
# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /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
    fi
done
EOF
%end

%packages --nobase --excludedocs
openssh-server
openssh-clients
acpid
wget
vim
%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.qcow2
1.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.qcow2
439M  /tmp/CentOS-7.2-x86_64.qcow2

E、制作ubuntu 12.0.4.5 x86_64镜像

下面是模板配置

ubuntu_12.04.5.tdl

<template>
	<name>Ubuntu-12.04.5-x86_64</name>
	<os>
		<name>Ubuntu</name>
		<version>12.04.5</version>
		<arch>x86_64</arch>
		<install type='iso'>
			<iso>file:///mnt/iso/ubuntu-12.04.5-server-amd64.iso</iso>
		</install>
		<rootpw>denglei2016</rootpw>
	</os>
	<commands>
		<command name='console'>
sed -i 's/splash//g' /etc/default/grub
sed -i 's/quiet/console=ttyS0/g' /etc/default/grub
/usr/sbin/update-grub
		</command>

   <command name='modify'>
echo -n > /etc/udev/rules.d/70-persistent-net.rules
echo -n > /lib/udev/rules.d/75-persistent-net-generator.rules
cat >/etc/apt/sources.list &lt;&lt; EOF
deb http://debian.ustc.edu.cn/ubuntu/ precise main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ precise-backports restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ precise-security main restricted universe multiverse
deb http://debian.ustc.edu.cn/ubuntu/ precise-updates main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-backports main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-security main restricted universe multiverse
deb-src http://debian.ustc.edu.cn/ubuntu/ precise-updates main restricted universe multiverse
EOF
apt-get clean
rm -rf /var/lib/apt/lists/*
apt-get update
apt-get install -y curl
sed -i '/exit 0/d' /etc/rc.local
echo "/bin/bash /usr/local/bin/instance_init.sh">>/etc/rc.local
echo "exit 0">>/etc/rc.local
cat >> /usr/local/bin/instance_init.sh &lt;&lt; EOF
if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi


# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /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.local
    sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
    sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
    /etc/init.d/ssh restart
    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
    fi
done
EOF
</command>
	</commands>
</template>

ubuntu_12.04.5.ks

d-i debian-installer/locale string en_US
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us

d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/wireless_wep string

d-i clock-setup/utc boolean true
d-i time/zone string Asia/Shanghai

d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select home
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

d-i passwd/root-login boolean true
d-i passwd/make-user boolean false
d-i passwd/root-password password denglei2016
d-i passwd/root-password-again password denglei2016

tasksel tasksel/first multiselect standard
d-i pkgsel/include/install-recommends boolean true
d-i pkgsel/include string openssh-server curl

d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true

d-i apt-setup/security_host string
base-config apt-setup/security-updates boolean false

ubiquity ubiquity/summary note
ubiquity ubiquity/reboot boolean true

d-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.qcow2
1.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.qcow2
418M    /tmp/Ubuntu-12.04.5-x86_64.qcow2

F、制作ubuntu 14.04.4 x86_64镜像

下面是模板配置

ubuntu_14.04.4.tdl

<template>
	<name>Ubuntu-14.04.4-x86_64</name>
	<os>
		<name>Ubuntu</name>
		<version>14.04.1</version>
		<arch>x86_64</arch>
		<install type='iso'>
			<iso>file:///mnt/iso/ubuntu-14.04.4-server-amd64.iso</iso>
		</install>
		<rootpw>denglei2016</rootpw>
	</os>
	<commands>
		<command name='console'>
sed -i 's/splash//g' /etc/default/grub
sed -i 's/quiet/console=ttyS0/g' /etc/default/grub
/usr/sbin/update-grub
		</command>

   <command name='modify'>
echo -n > /etc/udev/rules.d/70-persistent-net.rules
echo -n > /lib/udev/rules.d/75-persistent-net-generator.rules
cat >/etc/apt/sources.list &lt;&lt; EOF
deb http://debian.ustc.edu.cn/ubuntu/ trusty main multiverse restricted universe
deb http://debian.ustc.edu.cn/ubuntu/ trusty-backports main multiverse restricted universe
deb http://debian.ustc.edu.cn/ubuntu/ trusty-proposed main multiverse restricted universe
deb http://debian.ustc.edu.cn/ubuntu/ trusty-security main multiverse restricted universe
deb http://debian.ustc.edu.cn/ubuntu/ trusty-updates main multiverse restricted universe
deb-src http://debian.ustc.edu.cn/ubuntu/ trusty main multiverse restricted universe
deb-src http://debian.ustc.edu.cn/ubuntu/ trusty-backports main multiverse restricted universe
deb-src http://debian.ustc.edu.cn/ubuntu/ trusty-proposed main multiverse restricted universe
deb-src http://debian.ustc.edu.cn/ubuntu/ trusty-security main multiverse restricted universe
deb-src http://debian.ustc.edu.cn/ubuntu/ trusty-updates main multiverse restricted universe
EOF
apt-get clean
rm -rf /var/lib/apt/lists/*
apt-get update
apt-get install -y curl
sed -i '/exit 0/d' /etc/rc.local
echo "/bin/bash /usr/local/bin/instance_init.sh">>/etc/rc.local
echo "exit 0">>/etc/rc.local
cat >> /usr/local/bin/instance_init.sh &lt;&lt; EOF
if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi


# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /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.local
    sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
    sed -i 's/#PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
    service ssh restart
    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
    fi
done
EOF
</command>
	</commands>
</template>

ubuntu_14.04.4.ks

d-i debian-installer/locale string en_US
d-i console-setup/ask_detect boolean false
d-i console-setup/layoutcode string us

d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string unassigned-hostname
d-i netcfg/get_domain string unassigned-domain
d-i netcfg/wireless_wep string

d-i clock-setup/utc boolean true
d-i time/zone string Asia/Shanghai

d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select home
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

d-i passwd/root-login boolean true
d-i passwd/make-user boolean false
d-i passwd/root-password password denglei2016
d-i passwd/root-password-again password denglei2016

tasksel tasksel/first multiselect standard
d-i pkgsel/include/install-recommends boolean true
d-i pkgsel/include string openssh-server curl

d-i grub-installer/only_debian boolean true
d-i grub-installer/with_other_os boolean true

d-i apt-setup/security_host string
base-config apt-setup/security-updates boolean false

ubiquity ubiquity/summary note
ubiquity ubiquity/reboot boolean true

d-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.qcow2
1.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.qcow2
511M    /tmp/Ubuntu-14.04.4-x86_64.qcow2

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

有问题大家可以留言