5
9

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.

Nested VMの作り方

Last updated at Posted at 2019-06-30

#1 はじめに
kickstartを使って、仮想マシンの中に仮想マシンを作成してみます。

#2 環境
PCは富士通のFMV ESPRIMOです。8CPU、メモリ32G、ディスク2Tです。
ハイパーバイザはVMware Workstation 14 Playerを使いました。
作成する環境は以下のとおりです。
VM1の中で、Nested VM(Nested VM1,Nested VM2)を動かしてみます。

+--------------------------- PC(Windows 10) ----------------------------+
|                                                                       |
|                                                                       |
|   +------------ VM1(Virtual Machine) ------------+    +---- VM2 ---+  |
|   |                 (CentOS7.6)                  |    |            |  |
|   |                                              |    |            |  |
|   |                                              |    |            |  |
|   |  +-- Nested VM1 --+      +-- Nested VM2 --+  |    |            |  |
|   |  |  (CentOS7.6)   |      |  (CentOS7.6)   |  |    |            |  |
|   |  |                |      |                |  |    |            |  |
|   |  |                |      |                |  |    |            |  |
|   |  +----- eth0 -----+      +----- eth0 -----+  |    |            |  |
|   |          | .100                 | .200      |    |            |  |
|   |          |                       |           |    |            |  |
|   |          |                       |           |    |            |  |
|   |          |    192.168.122.0/24   |           |    |            |  |
|   |          |                       |           |    |            |  |
|   |          |                       |           |    |            |  |
|   |  +----- vnet0 ----------------- vnet1 ----+  |    |            |  |
|   |  |                                        |  |    |            |  |
|   |  |             virbr0 (192.168.122.1)     |  |    |            |  |
|   |  |                                        |  |    |            |  |
|   |  +-------------- virbr0-nic --------------+  |    |            |  |
|   |                      |                       |    |            |  |
|   |                      |                       |    |            |  |
|   +-------------------- eth0 --------------------+    +--- eth0 ---+  |
|                          | .50                              | .240    |
|                          |                                  |         |
|                          |         192.168.3.0/24           |         |
|                          |                                  |         |
|   +----------------------------------------------------------------+  |
|   |                   VMware Workstation 14 Player                 |  |
|   +----------------------------------------------------------------+  |
|                                                                       |
+-----------------------------------------------------------------------+
                                   |
                                   |
                                   | 192.168.3.1
                         +-------------------+
                         |  Default Gateway  |----------------> Internet
                         +-------------------+

#3 VMの作成
外側のVM1を作成した後、内側のNested VMを作成します。
VM1は、4CPU,メモリ16G,ディスク40Gで作成。
「Intel VT-x/EPT または AMD-V/RVIを有効化(V)」にチェックを入れて作成しました。
nested1.png

ソフトウェアの選択では、「仮想化ホスト」を選択しました。
nested2.png

Nested VM用のISOファイルをVMの下記場所に用意しました。

[root@vm1 ~]# ls -l /iso/*
-rw-r--r--. 1 qemu qemu 4588568576  5月  3 20:03 /iso/CentOS-7-x86_64-DVD-1810.iso

ISOファイルは、理研から取得しました。
http://ftp.riken.jp/Linux/centos/7.6.1810/isos/x86_64/

#4 Nested VMの作成方法
次に、VM1上で動くNested VM(nested-vm1,nested-vm2)を作成します。
私の環境では、10~15分程度で作成できました。

##4.1 virt-installのインストール
Nested VMは、virt-installを使って作成します。
インストールされていなかったら、インストールします。

[root@vm1 ~]# yum install virt-install
[root@vm1 ~]# virt-install --version
1.5.0

##4.2 virt-installに指定するパラメータ
virt-installに指定するパラメータが多いので、シェルスクリプト(ins.sh)にしました。

[root@vm1 vm]# pwd
/root/vm

[root@vm1 vm]# ls -F
ins.sh*  nested-vm-ks.cfg

nameはドメイン名になります。ドメイン名は、virsh listで表示される名前です。
Nested VM1を作成するときは、nameにnested-vm1、
Nested VM2を作成するときは、nameにnested-vm2を指定しました。
その他のパラメータについては、Nested VM1もNested VM2も同じにしました。

[root@vm1 vm]# cat ins.sh
virt-install \
  --name nested-vm1 \
  --hvm \
  --virt-type kvm \
  --ram 1024 \
  --vcpus 2 \
  --arch x86_64 \
  --os-type linux \
  --os-variant rhel7 \
  --boot hd \
  --disk pool=default,size=20,format=qcow2 \
  --network network=default \
  --graphics none \
  --serial pty \
  --console pty \
  --location /iso/CentOS-7-x86_64-DVD-1810.iso \
  --initrd-inject /root/vm/nested-vm-ks.cfg \
  --extra-args "ks=file:/nested-vm-ks.cfg console=ttyS0"

##4.3 キックスタートファイルの作成
作成したNested VMにインストールするパッケージ、設定するIPアドレス、
パーティション等の情報をキックスタートファイルを使って指定します。
なお、ここでは、Nested VMのログインパスワードを平文で"test"と設定しています。
実運用で使う場合は、暗号化(--iscrypted)した方がよいでしょう。

Nested VMの名前 --ip --hostname
Nested VM1 192.168.122.100 nested-vm1
Nested VM2 192.168.122.200 nested-vm2

以下は、Nested VM1作成時のキックスタートファイルです。

[root@vm1 vm]# cat nested-vm-ks.cfg
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
#graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
keyboard --vckeymap=jp --xlayouts='jp'
# System language
lang ja_JP.UTF-8

# Network information
network  --bootproto=static --device=eth0 --gateway=192.168.122.1 --ip=192.168.122.100 --nameserver=192.168.3.1 --netmask=255.255.255.0 --ipv6=auto --activate
network  --hostname=nested-vm1

# Root password
rootpw --plaintext "test"
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Tokyo --isUtc
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=vda
# Partition clearing information
clearpart --none --initlabel
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=vda --size=256
part swap --fstype="swap" --ondisk=vda --size=256
part / --fstype="xfs" --ondisk=vda --size=10000

%packages
@development
chrony
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

##4.4 Nested VMの作成
シェルを実行して、Nested VMを作成します。

[root@vm1 vm]# ./ins.sh

シェル実行時の様子を以下に示します。
nested3.png

##4.5 実行結果の確認

シェルの実行が完了したら、VM1で作成したNested VMの状態を確認してみます。

virsh_listの実行結果
[root@vm1 ~]# virsh list
 Id    名前                         状態
----------------------------------------------------
 13    nested-vm1                     実行中
 15    nested-vm2                     実行中

ブリッジ(virbr0)のポートを確認してみます。
ブリッジには、3つのポートがあることがわかります。
vnet0はnested-vm1と接続、vnet1はnested-vm2と接続しています。

[root@vm1 ~]# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.525400e23945       yes             virbr0-nic
                                                        vnet0
                                                        vnet1

Nested VMのイメージは、下記の場所に保存されます。

イメージの確認
[root@vm1 ~]# ls -l /var/lib/libvirt/images/*
-rw-------. 1 qemu qemu 21478375424  6月 30 19:23 /var/lib/libvirt/images/nested-vm1.qcow2
-rw-------. 1 qemu qemu 21478375424  6月 30 19:32 /var/lib/libvirt/images/nested-vm2.qcow2

#5 Nested VMへのコンソール接続、切断方法

##5.1 接続方法
Nested VMの状態を確認します。
2つのNested VMが動作していることがわかります。

VMの確認
[root@vm1 ~]# virsh list
 Id    名前                         状態
----------------------------------------------------
 13    nested-vm1                     実行中
 15    nested-vm2                     実行中

nested-vm1にコンソール接続してみます。
パスワードは"test"と入力してください。

コンソール接続
[root@vm1 ~]# virsh console nested-vm1
ドメイン nested-vm1 に接続しました
エスケープ文字は ^] です

CentOS Linux 7 (Core)
Kernel 3.10.0-957.el7.x86_64 on an x86_64

nested-vm1 login: root
パスワード: ★パスワード入力
最終ログイン: Sun Jun 30 17:19:14  ttyS0 上

コンソール接続したNested VM内でipコマンドを実行してみます。
インタフェース(eth0)のIPアドレスが、192.168.122.100であることがわかります。
また、ホスト名がnested-vm1であることも確認できます。

ipコマンド実行結果
[root@nested-vm1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:df:e7:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.100/24 brd 192.168.122.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5054:ff:fedf:e7e2/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

##5.2 切断方法
コンソール接続の切断は、Ctlキーを押下しながら、]キーを押下します。

[root@nested-vm1 ~]#
[root@vm1 ~]#

#6 Nested VMの停止、起動方法

##6.1 停止方法
Nested VMの状態を確認します。
2つのNested VMが動作中であることがわかります。

VMの確認
[root@vm1 ~]# virsh list
 Id    名前                         状態
----------------------------------------------------
 13    nested-vm1                     実行中
 15    nested-vm2                     実行中

nested-vm1を停止します。

nested-vm1の停止
[root@vm1 ~]# virsh shutdown nested-vm1
ドメイン nested-vm1 はシャットダウン中です

nested-vm2を停止します。

nested-vm2の停止
[root@vm1 ~]# virsh shutdown nested-vm2
ドメイン nested-vm2 はシャットダウン中です

Nested VMの状態を確認します。
2つのNested VMが停止していることがわかります。

VMの確認
[root@vm1 ~]# virsh list --all
 Id    名前                         状態
----------------------------------------------------
 -     nested-vm1                     シャットオフ
 -     nested-vm2                     シャットオフ

##6.2 起動方法
Nested VMの状態を確認します。
2つのNested VMが停止中であることがわかります。

VMの確認
[root@vm1 ~]# virsh list --all
 Id    名前                         状態
----------------------------------------------------
 -     nested-vm1                     シャットオフ
 -     nested-vm2                     シャットオフ

nested-vm1を起動します。

nested-vm1の起動
[root@vm1 ~]# virsh start nested-vm1
ドメイン nested-vm1 が起動されました

nested-vm2を起動します。

nested-vm2の起動
[root@vm1 ~]# virsh start nested-vm2
ドメイン nested-vm2 が起動されました

Nested VMの状態を確認します。
2つのNested VMが動作中であることがわかります。

VMの確認
[root@vm1 ~]# virsh list --all
 Id    名前                         状態
----------------------------------------------------
 16    nested-vm1                     実行中
 17    nested-vm2                     実行中

#7 動作確認
作成したNested VM1からwww.example.comにHTTPアクセスしてみます。
正しく応答が返ってきていることがわかります。
なお、www.example.comはRFC2606で予約されているドメインです。

[root@nested-vm1 ~]# curl -I https://www.example.com/
HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Sun, 30 Jun 2019 11:46:27 GMT
Etag: "1541025663"
Expires: Sun, 07 Jul 2019 11:46:27 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (sjc/4E44)
X-Cache: HIT
Content-Length: 606

#Z 参考情報
KickStartその2(ks.cfg設定ファイルの説明)
Running Nested VMs

5
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
5
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?