Ubuntu 20.04 (focal)のVMをいつもの通りvirt-installと--locationを使ってセットアップしていたら、WARNINGに遭遇したので、気になっていたcloud-initでインストールしてみたところうまくいったのでまとめる。
sudo virt-install (略) --location 'http://ftp.jaist.ac.jp/pub/Linux/ubuntu/dists/focal/main/installer-amd64/' --extra-args 'console=ttyS0,115200n8 serial'
WARNING Using legacy d-i based installer, that has been deprecated and will be removed in the future. https://discourse.ubuntu.com/c/server
概要
Ubuntu 20.04 ServerのVM「cloudtest」を
Ubuntu Cloud Imagesのイメージを使って、virt-installでセットアップする。
ディスクは、"vg0"というLVM volume groupにroot用, swap用のvolumeを作成して利用する。
LVMでボリュームの作成
lvcreateでボリュームを作る。
$ sudo lvcreate -n cloudtest-root -L 8G vg0
$ sudo lvcreate -n cloudtest-swap -L 4G vg0
イメージのダウンロードと展開
cloud-images.ubuntu.comからイメージをダウンロードする。QCOW2 Image (v2)形式らしいので、qemu-img convertを使って、rawイメージに変換しつつLVM volumeに流し込む。
$ wget https://cloud-images.ubuntu.com/releases/20.04/release/ubuntu-20.04-server-cloudimg-amd64.img
$ sudo qemu-img convert ubuntu-20.04-server-cloudimg-amd64.img -O raw /dev/vg0/cloudtest-root
cloud-init用の設定ファイルの作成
Ubuntu Cloud Imagesでは、インストーラでの設定入力ではなく、cloud-initで初期設定を行う。
NoCloudデータソースでは、設定内容をイメージから読んでくれるので、それを使う。そのイメージのための設定ファイルをまず用意する。
(詳しくは
第561回 ローカルインストール時もcloud-initを活用する:Ubuntu Weekly Recipeを参照)
今回の主な設定内容は以下の通り。
- ホスト名
- ユーザー名・初期パスワード
- パーティションテーブルの作成
- ファイルシステムの作成
- マウントポイントの設定
- ネットワーク
(その他の例は
Cloud config examplesを参照)
ファイルにするとこんな感じ。
ここで注意すべきなのは、networkは別ファイルだということと、disk_setupに罠があるということ。
examplesによく出てくるdisk_setupのlayout: Trueは
ドキュメントを読むと、
The layout option specifies how partitions on the device are to be arranged. If layout is set to true, a single partition using all the space on the device will be created.
とのことだが、新しめの環境とmbrでは機能しないので、Trueと同じように100%の領域を使いたい場合は以下のように指定する。
disk_setup:
/dev/vdb:
table_type: 'mbr'
layout:
- [100]
overwrite: False
詳しくは
Bug #1851438 “cloud-init disk_setup fails to partition disk for ...” : Bugs : cloud-initを参照。
設定ファイルが並ぶ感じになると思うので、gitで並べる用のテンプレートを作った。hosts/{hostname}以下にconfigファイルを書いて./build-images.shするとbuild/にできる。
https://github.com/sunnyone/cloud-init-configs
cloud-init用のイメージの作成
cloud-init用のイメージの作成にはcloud-localdsコマンドが便利なので、cloud-image-utilsパッケージをインストールして使う。
$ sudo apt install cloud-image-utils
$ cloud-localds --network-config network-config.yaml user-data.img user-data
virt-installの実行
これで準備できたので、virt-installを実行してVMを作る。
sudo virt-install \
--name cloudtest --ram 512 --arch x86_64 --vcpus 1 \
--os-type linux --os-variant ubuntu20.04 \
--disk path=/dev/vg0/cloudtest-root \
--disk path=/dev/vg0/cloudtest-swap \
--disk path=$PWD/user-data.img,device=cdrom \
--network bridge=br0,model=virtio \
--graphics none --serial pty --console pty \
--import
しばらくしたら設定が完了して、sshでログインできる。
cloud-init設定のデバッグ
インストーラと違って対話的でないので、うまくいかないときは積極的なデバッグが必要。
基本的にはログを見ると良くて、ログは以下の場所にある。
- /var/log/cloud-init-output.log
- /var/log/cloud-init.log
詳しくは
FAQを参照。
イメージごときれいにしなおしても良いが、/var/lib/cloudをきれいにするともう1度走らせることができる。
$ sudo rm -rf /var/lib/cloud/*
cloud-init用のイメージのeject
最後にuser-data.imgを外してあげておしまい。
$ sudo virsh change-media cloudtest sda --eject --config