CoreOS官网已经有openstack使用的虚拟机镜像,可以直接下载,然后进行修改
http://stable.release.core-os.net/amd64-usr/current/coreos_production_openstack_image.img.bz2
#如果需要使用iso文件制作镜像,可以参考另一篇文章http://www.cnblogs.com/jython/p/4225287.html
yum install libguestfs-tools-c-1.20.11-11.el6.x86_64
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目录下
默认情况下,CoreOS禁用root账户,所以需要修改/etc/shadow文件,修改root用户那行,把第二字段置为空即可
#编辑/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 no
上面步骤使用guestfish工具修改了CoreOS镜像,现在可以使用这个镜像启动虚拟机,使用root账号无需密码登录系统(上面/etc/shadow中设置)
#需要编写一个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
新建一个配置单元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
#开机启动 systemctl enable cloudinit.service #检测是否生效 systemctl is-enabled cloudinit
在/etc/systemd/network/目录下新建eth0.network文件,文件内容如下
[Match] Name=eth0 [Network] DHCP=yes
cat eth0.network [Match] Name=eth0 [Network] Address=192.168.1.15/24 Gateway=192.168.1.1 DNS=223.5.5.5
#查看ntpd状态, 启用
systemctl status ntpd systemctl enable ntpd
#ntp servers 可以通过修改/etc/ntp.conf配置文件,格式如下
server 0.pool.example.com server 1.pool.example.com
#查看当前状态 timedatectl status #查看可用时区 timedatectl list-timezones #修改为上海市区 timedatectl set-timezone Asia/ShangHai #再次查看当前状态 timedatectl status