首页 / 操作系统 / Linux / 为OpenStack制作CoreOS虚拟机镜像(基于CoreOS官方提供镜像)
1.下载CoreOS镜像(633.1.0版本)CoreOS官网已经有openstack使用的虚拟机镜像,可以直接下载,然后进行修改http://stable.release.core-os.net/amd64-usr/current/coreos_production_openstack_image.img.bz2#如果需要使用iso文件制作镜像,可以参考另一篇文章 http://www.linuxidc.com/Linux/2015-05/117025.htm2.guestfish修改CoreOS镜像2.1 安装guestfish工具yum install libguestfs-tools-c-1.20.11-11.el6.x86_642.2 使用guestfish挂载CoreOS镜像 guestfish -a coreos_production_openstack_image.img -i
#挂载相应分区
mount /dev/sda9 /
mkdir /cloudinit
mount /dev/sda6 /cloudinit
#cloud-config.yml是系统提供的开机配置文件,我们可以把需要开机执行的服务,或者对系统的修改定制以一定语法写入这个文件(下文中设置sshd),如果此文件语法没有错误,CoreOS会在每次开机时执行它
#cloujd-config.yml位于系统的/usr/share/oem目录下,sda6分区上, 本次是临时挂载在/cloudinit目录下2.3 开启root账户默认情况下,CoreOS禁用root账户,所以需要修改/etc/shadow文件,修改root用户那行,把第二字段置为空即可2.4 设置sshd#编辑/cloudinit/cloud-init.yml文件,设置sshd_config文件,允许root用户登录,允许密码认证#cloud-config
coreos:
units:
- name: user-configdrive.service
mask: yes
- name: user-configvirtfs.service
mask: yes
write_files:
- path: /etc/ssh/sshd_config
permissions: 0600
owner: root:root
content: |
UsePrivilegeSeparation sandbox
Subsystem sftp internal-sftp
PermitRootLogin yes
AllowUsers root
PasswordAuthentication yes
ChallengeResponseAuthentication no3 启动虚拟机上面步骤使用guestfish工具修改了CoreOS镜像,现在可以使用这个镜像启动虚拟机,使用root账号无需密码登录系统(上面/etc/shadow中设置)
•如果你使用libvirt方式管理虚拟机,那么需要定义xml文件,然后使用virsh命令启动虚拟机
•如果直接使用命令行启动虚拟机,那么使用 qemu-kvm -m 1024 coreos_production_openstack_image.img 启动4 设置cloudinit.sh脚本4.1 新建/etc/cloud-init.sh脚本#需要编写一个cloud-init.sh脚本,此脚本在系统开机时从metadata服务器获取数据, 完成修改主机名,修改root密码,ssh密钥注入#!/bin/bash
#cloud-init.sh
#author:xxx
#date:2015-4-15
#get the env
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest`
if [ ! "$STATUS_CODE" -eq "200" ]; then
/bin/sleep 3
fi
# set the root password using user data
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest/user-data`
if [ "$STATUS_CODE" -eq "200" ]; then
PASS=`curl -m 10 -s http://169.254.169.254/latest/user-data | awk -F """ "{for(i=1;i<=NF;i++){if($i ~ /password/) print $(i+2)}}"`
if [ "$PASS" != " " ]; then
/usr/bin/echo "root:${PASS}" > tmp.txt
/usr/sbin/chpasswd < tmp.txt
rm -f tmp.txt
fi
fi
# set the hostname using the meta-data service
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest/meta-data/hostname`
if [ "$STATUS_CODE" -eq "200" ]; then
curl -f http://169.254.169.254/latest/meta-data/hostname > /tmp/metadata-hostname 2>/dev/null
if [ $? -eq 0 ]; then
TEMP_HOST=`cat /tmp/metadata-hostname | awk -F ".novalocal" "{print $1}"`
/usr/bin/hostnamectl set-hostname ${TEMP_HOST}
/usr/bin/hostname $TEMP_HOST
rm -f /tmp/metadata-hostname
fi
fi
# get the user ssh key using the meta-data service
STATUS_CODE=`curl -I -m 10 -o /dev/null -s -w %{http_code} http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key`
if [ "$STATUS_CODE" -eq "200" ]; then
mkdir -p /root/.ssh
/usr/bin/echo >> /root/.ssh/authorized_keys
curl -m 10 -s http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key | grep "ssh-rsa" >> /root/.ssh/authorized_keys
chmod 0700 /root/.ssh
chmod 0600 /root/.ssh/authorized_keys
fi 4.2 设置开机启动新建一个配置单元cloudinit.service, 此配置单元用来在开机时执行cloud-init.sh脚本 #cat /etc/systemd/system/cloudinit.service
[Unit]
Description=OpenStack nova
Requires=coreos-setup-environment.service
After=coreos-setup-environment.service
Before=user-config.target
[Service]
Type=oneshot
RemainAfterExit=yes
EnvironmentFile=-/etc/environment
ExecStart=/usr/bin/bash /etc/cloud-init.sh #执行的脚本文件cloud-init.sh
[Install]
WantedBy=multi-user.target 4.3 设置cloudinit.service开机启动#开机启动
systemctl enable cloudinit.service
#检测是否生效
systemctl is-enabled cloudinit 5 设置网络
5.1 使用dhcp方式在/etc/systemd/network/目录下新建eth0.network文件,文件内容如下[Match]
Name=eth0
[Network]
DHCP=yes5.2 使用固定IP方式 cat eth0.network
[Match]
Name=eth0
[Network]
Address=192.168.1.15/24
Gateway=192.168.1.1
DNS=223.5.5.56 设置时间同步,时区6.1设置ntpd#查看ntpd状态, 启用systemctl status ntpd
systemctl enable ntpd6.2修改ntp time servers#ntp servers 可以通过修改/etc/ntp.conf配置文件,格式如下server 0.pool.example.com
server 1.pool.example.com6.3 设置时区#查看当前状态
timedatectl status
#查看可用时区
timedatectl list-timezones
#修改为上海市区
timedatectl set-timezone Asia/ShangHai
#再次查看当前状态
timedatectl status在Ubuntu 12.10 上安装部署Openstack http://www.linuxidc.com/Linux/2013-08/88184.htmUbuntu 12.04 OpenStack Swift单节点部署手册 http://www.linuxidc.com/Linux/2013-08/88182.htmOpenStack云计算快速入门教程 http://www.linuxidc.com/Linux/2013-08/88186.htm企业部署OpenStack:该做与不该做的事 http://www.linuxidc.com/Linux/2013-09/90428.htmCentOS 6.5 x64bit 快速安装OpenStack http://www.linuxidc.com/Linux/2014-06/103775.htm本文永久更新链接地址