4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

cloud-init メモ

Last updated at Posted at 2020-01-17

cloud-init のテストは最初は lxd を使うと効率がよい。lxc create の引数で user-data を指定する(※参考)。内部的には lxd イメージに備わっているテンプレート機能で NoCloud datasource がセットアップされる (※参考)。

# 下のパスワードは "ubuntu" です。
lxc launch ubuntu:bionic test01 -c user.user-data="$(cat <<EOF
# cloud-config
system_info:
  default_user:
    passwd: ZdCKISm06M3Yk
    lock_passwd: false
ssh_pwauth: true
EOF
)"

次点は kvm 直接起動。libvirt 環境下でテストするよりも手軽。cloud-localds でデフォルトで NoCloud drive の ISO9600 イメージが生成できる。

# qcow2 backing を使うとテストが軽くなって簡単
qemu-img create -f qcow2 -b bionic-server-cloudimg-amd64.img base.img
# NoCloud datasource を作る。下のパスワードは "ubuntu" です。
cloud-localds seed.iso <(cat <<EOF
# cloud-config
system_info:
  default_user:
    passwd: ZdCKISm06M3Yk
    lock_passwd: false
ssh_pwauth: true
EOF
);
# 上の二つを使って起動させる。メモリが無いと cloud-init が OOM にあう
kvm -cdrom seed.iso -nographic -serial mon:stdio -m 512 base.img

user-data は cloud-config 単体でも構わないけれども、mime-multipart で複数の内容を連結したものでも構わない。ドキュメントで例が少ないですが。

いっぽうで実は /etc/cloud/cloud.cfg.d/ 以下に cloud-config を置いても良い。この場合は YAML である cloud-config のみであることに注意。

user-data の cloud-config と /etc/cloud/cloud.cfg.d/ の cloud-config を両方使うときは、「後勝ち」で置き換えられることに注意。merge ルールが導入されているけれども、どうにもこうにも対応が不完全で、現状の実装では同じ場所の cloud-config 同士でしか merge ルールが効かない。

実行順序を読み解くには stage(generator, local, network, config, final) と frequency(always,once-per-instance,once) の 2 軸がある。

cloud-init.target は systemd generator で target goal として設定されるものなので、After=cloud-init.target とはしないこと。

cloud-boothook

  • なるべく各種 daemon 起動前にやりたいことを
  • 書いた内容が boothook 用ディレクトリ(/var/lib/cloud/instances/${name}/boothooks/)に展開される
  • そのファイルが exec される
  • cloud-init-per で wrap してしまうと frequency の制御が簡単になる

例えば行頭の #! をつけ忘れないようにする。mime multipart を使わない場合は、次のようになる(ユーザ名 testuserweakpassword でログインできるようになる)。

# cloud-boothook
# !/bin/bash
useradd -p '$6$rounds=4096$t98l1Iej6$jsob4xXuA0cL6rI8NiAkEuH4jAQSyNBR6RAIzDsrQhp4096nhT4.5i0RHqMEDEv0ubIlV2HjCqMgrAVkhpkmK.' testuser
# generate password with:
#  echo "weakpassword" | mkpasswd -s --method=SHA-512 --rounds=4096

cloud-config-archive

  • mime-multipart の代わりに使える
  • mime-multipart にネストしても構わない
  • 機械生成したいときで、mime-multipart が面倒な時便利
  • 小さい cloud-config をたくさん連結したいときは小さくなるかもしれない。mime multipart のヘッダとインデントの量を秤にかける
# cloud-config-archive
---
- type: text/cloud-config
  content: |
    write_file:
      path: /tmp/hello
      content: |
        hello, world
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?