LoginSignup
9
9

More than 5 years have passed since last update.

Cloud imageをCloudで使わずにqemu/libvirt(virsh)で起動させる

Last updated at Posted at 2016-06-07

Cloud imageをCloudで使わずにqemuで起動させる

本記事ではRedhat,Ubuntuが提供しているCloudイメージをlibguestfsを使って通常のQemu(libvirt)から起動できるようにします。

はじめに

最近はAWS等のサービスもあって、またUbuntuのcloud-initの普及も相俟ってISOのCDイメージだけではなく、プリインストール済みのVMイメージも配布されるようになりました。その中でもCloudイメージはOpenStack/AWSで使えることもあって非常に一般的になっています。
ですがCloudイメージはcloud-initというツールが入っており、そのままqemu/virshで起動するとログインができません。まぁ、Cloudで動くことを前提としている訳ですから動きませんよね。
しかし、自宅のちょっとした環境などで毎回インストールをするのもめんどい訳ですし、せっかくあるこのCloudイメージを使って家でも簡単にイメージを起動したいと思うのは人の常なのではないでしょうか。

ということで、このページではそれらのCloudイメージを元に、cloud-initを外した上で通常のpassword認証を使ってqemu/virshで普通にログインする方法について説明します。

現在のところRHELとUbuntuですが必要に応じて増える予定です ;)

(補足) Gentooの方へ

libguestfsのemergeの際には"ocaml"をUSEに入れてからemergeしないとvirt-customizeが入りませんのでご注意下さい。

手順

下の<image file>にqcow2(又は他の)イメージファイル、にrootのパスワードを入れて実行して下さい。

Redhat系列(RHEL, CentOS, Fedora)の場合

shell
virt-customize -a <image file> --run-command 'yum remove cloud-init* -y' \
 --root-password password:<password>

又は

shell
virt-customize -a <image file> --run-command 'touch /etc/cloud/cloud-init.disabled' \
 --root-password password:<password>

Ubuntu (XenialXerus, 16.04の場合)

shell
virt-customize -a <image file> --run-command 'apt-get remove -y cloud-init' \
 --run-command 'apt-get purge --auto-remove -y cloud-init' \
 --run-command 'ssh-keygen -A' \
 --root-password password:<password>

Gentoo (Gentoo Cloud Imageの場合)

かなり力技ではあるのですが、こんな感じでイケます。ログイン後にemerge -C cloud-initを使うことで削除も可能です。

shell
virt-customize -a <image file> \
--run-command 'rc-config delete cloud-init-local boot' \
--run-command 'rc-config delete cloud-init default' \
--run-command 'rc-config delete cloud-final default' \
--run-command 'rc-config delete cloud-config default' \
--root-password password:<password>

sshdの設定

Cloudイメージの場合sshdはplain passwordの設定を無効にしています。
もしsshdでログインしたい場合には以下の設定を変更しましょう。セキュリティ的には問題があるとも思われますので、実際のネットワークに繋げる場合にはお気をつけ下さい。

/etc/ssh/sshd_configの設定

PermitRootLogin yes
PasswordAuthentication yes

鍵の作成

ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
9
9
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
9
9