openstack制作centos6.5镜像

1、创建一个空的镜像文件
[root@KVM-node4 images]# qemu-img create -f qcow2 centos-6.5.qcow2 5G
Formatting 'centos-6.5.qcow2', fmt=qcow2 size=5368709120 encryption=off cluster_size=65536 lazy_refcounts=off 
[root@KVM-node4 images]# ll
总用量 4349172
-rw-r--r-- 1 root root     197120 11月 20 23:56 centos-6.5.qcow2
-rw-r--r-- 1 root root 1044356608 11月 20 11:48 centos-6.7-cloud.qcow2
-rw-r--r-- 1 qemu qemu 3409117184 11月 20 11:43 centos-6.7.qcow2

2、创建虚拟机
[root@KVM-node4 images]# virt-install  --name centos-6.5 --ram 1024 --cdrom=/var/www/webvirtmgr/images/CentOS-6.5-x86_64-bin-DVD1.iso  --disk path=/var/lib/libvirt/images/centos-6.5.qcow2 --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=rhel6

开始安装......
创建域......                                                                                                                                                       |    0 B  00:00:00     
域安装仍在进行。您可以重新连接
到控制台以便完成安装进程。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------

3、系统安装过程
分区只分一个,挂载到“/”,格式为ext4;
不要swap,分区

[root@KVM-node4 images]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos-6.5                     关闭
 -     centos6.7                      关闭
 -     centos7.2-x86                  关闭

[root@KVM-node4 images]#  virsh start centos-6.5
域 centos-6.5 已开始

4、配置yum源
centos6.x
cd /etc/yum.repos.d/
\cp CentOS-Base.repo CentOS-Base.repo.$(date +%F)
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo

5、防火墙及SELINUX关闭
/etc/init.d/iptables stop
chkconfig iptables off
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
grep SELINUX=disabled /etc/selinux/config
setenforce 0

6、要使nova console-log 能将实例启动过程输出到实例启动日志中,要在文件/boot/grub/menu.lst 中kernel参数中增加下面的内容: 
kernel ...(省略n个参数)... console=tty0 console=ttyS0,115200n8

[root@localhost yum.repos.d]# cat /boot/grub/menu.lst 
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/vda2
#          initrd /initrd-[generic-]version.img
#boot=/dev/vda
default=0
timeout=1
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS (2.6.32-431.el6.x86_64)
	root (hd0,0)
	kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=f0a5d467-5101-4a63-bc26-48682cac231b rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_MD crashkernel=auto LANG=zh_CN.UTF-8 rd_NO_LVM rd_NO_DM rhgb quiet console=tty0 console=ttyS0,115200n8
	initrd /initramfs-2.6.32-431.el6.x86_64.img

7、修改网络信息 /etc/sysconfig/network-scripts/ifcfg-eth0 (删掉mac信息)如下: TYPE=Ethernet
 DEVICE=eth0
 ONBOOT=yes
 BOOTPROTO=dhcp
 NM_CONTROLLED=no 
   
增加一行到/etc/sysconfig/network : 
NOZERCONF=yes

8、增加epel源、更新系统,安装git: 
yum install -y http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 
yum -y distro-sync 
yum -y install git

9、安装ACPI服务,能让宿主机对虚拟机进行开关机等电源管理操作 
yum install acpid chkconfig acpid on

10、安装linux rootfs resize,使得实例启动时可以自动扩展根分区 cd /tmp
下载地址:
https://github.com/flegmatik/linux-rootfs-resize

linux-rootfs-resize
Supported Linux distributions: CentOS 6, Debian 6, Debian 7.
Rework of my previous project, that was limited only to CentOS 6.
This tool creates new initrd (initramfs) image with ability to resize root filesystem over available space. Tipically you need this when you provision your virtual machine on OpenStack cloud for the first time (your image becomes flavor aware)
For now, filesystem resize is limited to ext2, ext3 and ext4 (resize2fs) including LVM volumes.
This code was successfuly tested on: CentOS 6.5, Debian 6 and Debian 7.2
DEPENDENCIES:
cloud-utils (https://launchpad.net/cloud-utils)
parted (CentOS)
INSTALL:
Install git, clone this project on your machine, run 'install'. 
On CentOS:

cd /opt
rpm -ivh http://ftp-stud.hs-esslingen.de/pub/epel/6/i386/epel-release-6-8.noarch.rpm
yum install git parted cloud-utils
git clone https://github.com/flegmatik/linux-rootfs-resize.git
cd linux-rootfs-resize
./install
Tool is designed in modular fashion, so support for other distributions can be added without much work (I hope).

11、安装cloud-init 
yum install -y cloud-utils cloud-init parted    
修改配置文件/etc/cloud/cloud.cfg ,在cloud_init_modules 下面增加:  
- resolv-conf

12、清除key
没有压缩的是900多M,压缩过的是400多M。默认的用户名是:centos
目前各大发行版对镜像的定制,安全性基本都是一致的。CentOS,同样也是不允许root登录,使用密钥也是不行的。
如果是希望root使用密钥登录,做法和Cirros镜像基本是一样的
#!/bin/sh
mv /root/.ssh/authorized_keys /root/.ssh/authorized_keys.old
cp  /home/centos/.ssh/authorized_keys /root/.ssh/


镜像的ssh设置,是允许root的登录的,所以就不需要修改ssh。如果你希望root可以使用密码登录。那么你需要用脚本修改ssh设置
#!/bin/sh
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
systemctl restart sshd
13、精简开机自启动
for A in `chkconfig --list |grep 3:on |awk '{print $1}' `;do chkconfig $A off;done
for B in acpid auditd cloud-config cloud-final cloud-init cloud-init-local crond ip6tables iptables kdump mdmonitor netfs network postfix rsyslog sshd udev-post;do chkconfig $B on;done  
chkconfig --list |grep 3:on   
14、关机:  init 0
14、清除网络相关硬件生成信息


[root@KVM-node4 images]# virsh list --all
 Id    名称                         状态
----------------------------------------------------
 -     centos-6.5                     关闭
 -     centos6.7                      关闭
 -     centos7.2-x86                  关闭


[root@KVM-node4 images]# virt-sysprep -d centos-6.5
[   0.0] Examining the guest ...
[  46.0] Performing "abrt-data" ...
[  46.0] Performing "bash-history" ...
[  46.0] Performing "blkid-tab" ...
[  46.0] Performing "crash-data" ...
[  46.0] Performing "cron-spool" ...
[  46.0] Performing "dhcp-client-state" ...
[  46.0] Performing "dhcp-server-state" ...
[  46.0] Performing "dovecot-data" ...
[  46.0] Performing "logfiles" ...
[  46.0] Performing "machine-id" ...
[  46.0] Performing "mail-spool" ...
[  46.0] Performing "net-hostname" ...
[  46.0] Performing "net-hwaddr" ...
[  46.0] Performing "pacct-log" ...
[  46.0] Performing "package-manager-cache" ...
[  46.0] Performing "pam-data" ...
[  46.0] Performing "puppet-data-log" ...
[  46.0] Performing "rh-subscription-manager" ...
[  46.0] Performing "rhn-systemid" ...
[  46.0] Performing "rpm-db" ...
[  46.0] Performing "samba-db-log" ...
[  46.0] Performing "script" ...
[  46.0] Performing "smolt-uuid" ...
[  46.0] Performing "ssh-hostkeys" ...
[  46.0] Performing "ssh-userdir" ...
[  46.0] Performing "sssd-db-log" ...
[  46.0] Performing "tmp-files" ...
[  46.0] Performing "udev-persistent-net" ...
[  46.0] Performing "utmp" ...
[  46.0] Performing "yum-uuid" ...
[  46.0] Performing "customize" ...
[  46.0] Setting a random seed
[  46.0] Performing "lvm-uuids" ...
15、压缩镜像 
[root@KVM-node4 images]# virt-sparsify --compress centos-6.5.qcow2 centos-6.5-cloud.qcow2
Input disk virtual size = 5368709120 bytes (5.0G)
Create overlay file in /tmp to protect source disk ...
Examine source disk ...
◓ 50% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒════◑ 75% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Fill free space in /dev/sda1 with zero ...
◓ 85% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◑ 93% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Fill free space in /dev/sda2 with zero ...
◓  4% ⟦▒▒▒▒▒▒▒════════════════════════════════════════════════════════════════════════════════════◑  4% ⟦▒▒▒▒▒▒▒═══════════════════════════════════════════════════════════════════════════════════◒  4% ⟦▒▒▒▒▒▒▒════════════════════════════════════════════════════════════════════════════════════◐  4% ⟦▒▒▒▒▒▒▒═══════════════════════════════════════════════════════════════════════════════════◓  4% ⟦▒▒▒▒▒▒▒▒═══════════════════════════════════════════════════════════════════════════════════◑  5% ⟦▒▒▒▒▒▒▒▒══════════════════════════════════════════════════════════════════════════════════◒  5% ⟦▒▒▒▒▒▒▒▒▒══════════════════════════════════════════════════════════════════════════════════◐  7% ⟦▒▒▒▒▒▒▒▒▒▒▒═══════════════════════════════════════════════════════════════════════════════◓  9% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒═══════════════════════════════════════════════════════════════════════════◑ 13% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒════════════════════════════════════════════════════════════════════◒ 18% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒═════════════════════════════════════════════════════════════◐ 25% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒═════════════════════════════════════════════════◓ 34% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒═══════════════════════════════════◑ 44% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒═══════════════════◒ 52% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒══════◐ 61% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◓ 70% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◑ 77% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◒ 82% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◐ 87% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◓ 90% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◑ 93% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◒ 97% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒◐ 99% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Copy to destination and make sparse ...


Sparsify operation completed with no errors.  Before deleting the old disk, 
carefully check that the target disk boots and works correctly.
[root@KVM-node4 images]# ll
总用量 9167560
-rw-r--r-- 1 root root  954482688 11月 21 01:35 centos-6.5-cloud.qcow2
-rw-r--r-- 1 qemu qemu 3979870208 11月 21 01:31 centos-6.5.qcow2
-rw-r--r-- 1 root root 1044356608 11月 20 11:48 centos-6.7-cloud.qcow2
-rw-r--r-- 1 qemu qemu 3409117184 11月 20 11:43 centos-6.7.qcow2
[root@KVM-node4 images]# pwd
/var/lib/libvirt/images
[root@KVM-node4 images]# du -sh 
8.8G	.
[root@KVM-node4 images]# du -sh *
911M	centos-6.5-cloud.qcow2
3.8G	centos-6.5.qcow2
996M	centos-6.7-cloud.qcow2
3.2G	centos-6.7.qcow2































你可能感兴趣的:(云计算)