undyingのブログ
Amebaでブログを始めよう!
1 | 2 | 3 | 4 | 5 | 最初次のページへ >>

CentOS6用 cloud-init

CentOSでcloud-initが必要なので以下のように取得、修正した。
今回はec2でデフォルトで使用されているものを取得し適用した。

# get_reference_source -p cloud-init
Please enter your AWS account id: xxxxxxxxxx

Requested package: cloud-init

Found package from local RPM database: cloud-init-0.5.15-4.amzn1
Corresponding source RPM to found package : cloud-init-0.5.15-4.amzn1.src.rpm

Your AWS account id: [口座番号]

Are these parameters correct? Please type 'yes' to continue: yes
Source RPM for 'cloud-init-0.5.15-4.amzn1' downloaded to: /usr/src/srpm/debug/cloud-init-0.5.15-4.amzn1.src.rpm


依存パッケージインストールのためにEpelを追加

# wget http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
# rpm --import RPM-GPG-KEY-EPEL-6
# rpm -ivh http://download.fedora.redhat.com/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm

普段はepelを使用しないため/etc/yum.repos.d/epel.repo の enabledを0にして無効にする。
依存パッケージインストール
# yum --enablerepo=epel install PyYAML libyaml

specファイルを以下のように修正
# diff -u cloud-init.spec cloud-init.spec.org 
--- cloud-init.spec 2012-01-19 20:15:00.941128056 +0900
+++ cloud-init.spec.org 2012-01-18 06:12:05.476273451 +0900
@@ -16,12 +16,10 @@
Requires: rpm
Requires: yum

-#BuildRequires: python-devel-abi
-BuildRequires: python-devel
+BuildRequires: python-devel-abi
Requires: python-configobj
Requires: python-cheetah
-#Requires: python-yaml
-Requires: PyYAML
+Requires: python-yaml

%define cloud_user ec2-user
%define cloud_user_id 222
@@ -137,7 +135,7 @@
%attr(0755,root,root) /usr/bin/cloud-init
%attr(0755,root,root) /usr/bin/write-mime-multipart

-%{python_sitelib}/*
+%{_python_sitelib}/*

%changelog
* Mon Feb 21 2011 04:49:31 UTC Cristian Gafton <gafton@amazon.com>


また、cloud-initはもともとubuntu用のパッケージなので
ファイル配置やフォーマットが異なるため、CentOS用にするため以下を修正した。

/usr/bin/cloud-init
diff -u cloud-init.org cloud-init | less
--- cloud-init.org 2012-01-19 20:15:10.000000000 +0900
+++ cloud-init 2012-01-27 07:16:47.664125602 +0900
@@ -130,6 +130,21 @@
# read hostname from a 'hostname' file
# allow for comments and stripping line endings.
# if file doesn't exist, or no contents, return default
+def read_etc_sysconfig(filename, default=None):
+ try:
+ fp = open(filename,"r")
+ lines = fp.readlines()
+ fp.close()
+ lists = [value.split("=", 1) for value in lines]
+ dic = dict(lists)
+ hostname = dic['HOSTNAME'].rstrip()
+ print hostname
+ if hostname:
+ return hostname
+ except IOError, e:
+ if e.errno == errno.ENOENT: pass
+ return default
+
def read_hostname(filename, default=None):
try:
fp = open(filename,"r")
@@ -145,18 +160,37 @@
except IOError, e:
if e.errno == errno.ENOENT: pass
return default
-
+
def set_hostname(hostname, log):
try:
subprocess.Popen(['hostname', hostname]).communicate()
- util.write_file("/etc/hostname","%s\n" % hostname, 0644)
- log.debug("populated /etc/hostname with %s on first boot", hostname)
+ hostname = "HOSTNAME=" + hostname
+ util.write_file("/etc/sysconfig/network","%s\n" % hostname, 0644, 'ab')
+ log.debug("populated /etc/sysconfig/network with %s on first boot", hostname)
except:
log.error("failed to set_hostname")

+def string_replace(hostname, log):
+ etc_file = "/etc/sysconfig/network"
+ tmp_file = "/etc/sysconfig/network.tmp"
+ hostname_before = read_hostname(etc_file)
+ hostname_rep = hostname
+ try:
+ read_file = open(etc_file, 'r')
+ write_file = open(tmp_file, 'w')
+ for line in read_file:
+ line = line.replace(hostname_before, hostname_rep)
+ write_file.write(line)
+ finally:
+ read_file.close()
+ write_file.close()
+
+ os.remove(etc_file)
+ os.rename(tmp_file, etc_file)
+
def update_hostname(hostname, log):
prev_file="%s/%s" % (cloudinit.datadir,"previous-hostname")
- etc_file = "/etc/hostname"
+ etc_file = "/etc/sysconfig/network"

hostname_prev = None
hostname_in_etc = None
@@ -167,24 +201,24 @@
log.warn("Failed to open %s" % prev_file)

try:
- hostname_in_etc = read_hostname(etc_file)
+ hostname_in_etc = read_etc_sysconfig(etc_file)
except:
log.warn("Failed to open %s" % etc_file)

- update_files = []
if not hostname_prev or hostname_prev != hostname:
- update_files.append(prev_file)
+ try:
+ util.write_file(prev_file,"%s\n" % hostname, 0644)
+ log.debug("wrote %s to %s" % (hostname,prev_file))
+ except:
+ log.warn("failed to write hostname to %s" % prev_file)

if (not hostname_in_etc or
(hostname_in_etc == hostname_prev and hostname_in_etc != hostname)):
- update_files.append(etc_file)
-
- try:
- for fname in update_files:
- util.write_file(fname,"%s\n" % hostname, 0644)
- log.debug("wrote %s to %s" % (hostname,fname))
- except:
- log.warn("failed to write hostname to %s" % fname)
+ try:
+ string_replace(hostname)
+ log.debug("wrote %s to %s" % (hostname,etc_file))
+ except:
+ log.warn("failed to write hostname to %s" % fname)

if hostname_in_etc and hostname_prev and hostname_in_etc != hostname_prev:
log.debug("%s differs from %s. assuming user maintained" %




/usr/lib/python2.6/site-packages/cloudinit/DistAction/dist_defaults.py
diff -u dist_defaults.py.org dist_defaults.py
--- dist_defaults.py.org 2011-02-21 13:49:31.000000000 +0900
+++ dist_defaults.py 2012-01-27 05:28:06.604128724 +0900
@@ -53,7 +53,8 @@
cloudinit.log.debug("-- defaults handler/set_hostname() --")

subprocess.Popen(['hostname', hostname]).communicate()
- f=open("/etc/hostname","wb")
+ hostname = "HOSTNAME=" + hostname
+ f=open("/etc/sysconfig/network","ab")
f.write("%s\n" % hostname)
f.close()


/etc/cloud/dist-defs.cfg
diff -u dist-defs.cfg.org dist-defs.cfg                                                                                                            
--- dist-defs.cfg.org 2012-01-26 00:31:33.514119503 +0900
+++ dist-defs.cfg 2012-01-27 05:32:54.879125423 +0900
@@ -19,16 +19,10 @@
# init : sysv # sysv or upstart
# common : linux-distro-name # or point to similar one (ubuntu, redhat, etc)

-name : 000001F7 amazon-linux
+name : centos

repo:
- name : amzn
- #default_mirror : 'repo.amazonaws.com'
- regional_mirror : 'repo.%(ec2_az)s.amazonaws.com'
+ name : yum

distribution-handlers:
repo : yum
-# handlers not 000006A6 implemented yet
-# init : sysv
-# c 000005B4 ommon : amzn
-

/etc/cloud/cloud.cfg がそのままだと
preserve_hostname: True
となっており、ホスト名が変更できないので、この記述は削除しておく。

最後に、ここまでやるならamazonから持ってくるのではなく、
ubuntuパッケージの最新から持って来るべきだとおもった。

ec2で使用しているサーバがredhat系なので、すべて対応しているとおもったが
やってみるとhostnameに対応してなかったので、修正が必要になってしまった。

ubuntu Cache-Limitエラー

ubuntuパッケージをインストールするときに以下エラーが発生した

E: Dynamic MMap ran out of room. Please increase the size of APT::Cache-Limit. Current value: 25165824. (man 5 apt.conf)

/etc/apt/apt.conf に以下を記述でエラーなく処理が終わった。

APT
{
Cache-Limit "50331648";
};

以下のブログそのまんま
http://i94025.at.webry.info/201103/article_3.html

ubuntu10.04 lucidでloopデバイスを増やす

ubuntu10.04 lucidでloopデバイスの増やし方

デフォルトは/dev/loop7までしかない
centosとかならmodprobe.confに設定するが、
新しいカーネルはloopデバイスが組み込まれており、
/etc/modprobe.d/に設定しても反映されないためgrubに設定する

grubに設定を入れる
ubuntu10.04はgrub2を使用しているため
/boot/grub/grub.cfgは直接編集しない

■増やし方
vim /etc/default/grub
以下を追記
GRUB_CMDLINE_LINUX="max_loop=64"
以下実行
update-grub2
確認
grep loop /boot/grub/grub.cfg
linux /vmlinuz-2.6.32-21-server root=UUID=ff6355a0-eb6a-4241-8e42-6ffbf82b98d1 ro max_loop=64 quiet splash
linux /vmlinuz-2.6.32-21-server root=UUID=ff6355a0-eb6a-4241-8e42-6ffbf82b98d1 ro single max_loop=64

reboot

1 | 2 | 3 | 4 | 5 | 最初次のページへ >>