コンテンツにスキップ

KVMをScientific Linuxに構築する

インストール作業

最低限入れるべきもの

yum install qemu-kvm libvirt
yum install virt-install libvirt-python virt-manager virt-install libvirt-client

入れておくと良いものを含めたセット

yum install libguestfs libvirt libvirt-client python-virtinst qemu-kvm virt-manager virt-top virt-viewer virt-who virt-install bridge-utils


systemctl start libvirtd

systemctl enable libvirtd

設定ファイルを編集する

ifcfg-br0

# HWADDR=60:A4:4C:AE:54:DD
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.203.10.201
NETMASK=255.255.254.0
GATEWAY=10.203.10.1
DNS1=10.200.70.35

ifcfg-br0

# HWADDR=60:A4:4C:AE:54:DD
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=static
IPADDR=10.203.10.201
NETMASK=255.255.254.0
GATEWAY=10.203.10.1

コマンドを使ってブリッジをチェック

brctl show

ip a show br0

WebVirtMgrの構築

Pythonの構築

yum -y install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-6.noarch.rpm
yum -y install git python-pip libvirt-python libxml2-python python-websockify supervisor nginx
yum -y install gcc python-devel
pip install numpy

gitからwebvirtmgrを入れる

git clone https://github.com/retspen/webvirtmgr.git
cd webvirtmgr
pip install -r requirements.txt
./manage.py syncdb

Djangoの実行ユーザを設定

https://github.com/retspen/webvirtmgr/wiki/Install-WebVirtMgr

設定ファイルをコピー

cp /var/www/conf/initd/webvirtmgr-console-redhat /etc/init.d/webvirtmgr-novnc

https://www.hiroom2.com/2015/01/30/webvirtmgr/#sec-1-5

KVMの確認

ゲストCPUモデルの一覧表示

virsh cpu-models x86_64

一覧をXML形式で出力

cat /usr/share/libvirt/cpu_map.xml

virt-install を使用したゲストの作成

例1.

virt-install \
--name=guest1-rhel7-64 \
--disk path=/var/lib/libvirt/images/guest1-rhel7-64.dsk,size=8,sparse=false,cache=none \
--graphics spice \
--vcpus=2 --ram=2048 \
--location=http://example1.com/installation_tree/RHEL7.2-Serverx86_64/os\
--network bridge=br0 \
--os-type=linux \
--os-variant=rhel7

例2.

virt-install \
--name=guest1-rhel7 \
--controller type=scsi,model=virtio-scsi \
--disk path=/var/lib/libvirt/images/guest1-rhel7.dsk,size=8,sparse=false,cache=none,bus=scsi \
--graphics spice \
--vcpus=2 --ram=2048 \
--location=http://example1.com/installation_tree/RHEL7.1-Serverx86_64/os \
--network bridge=br0 \
--os-type=linux \
--os-variant=rhel7

実例1.

virt-install \
--name centos7a \
--ram 4096 \
--disk path=/var/kvm/images/centos7a.img,size=30 \
--vcpus 2 \
--os-type linux \
--os-variant rhel7 \
--network bridge=br0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.iij.ad.jp/pub/linux/centos/7/os/x86_64/' \
--extra-args 'console=ttyS0,115200n8 serial'

各パラメータの説明

  • name: 仮想マシン名
  • disk: 仮想マシンの詳細設定
    • 仮想ディスクのパス
    • 仮想ディスクのサイズ
  • graphics: 仮想マシンのGUIインストールを可能にするオプション(noneで自動化可能) [重要]
  • vcpus: ゲストに割り当てる仮想CPU数
  • ram: ゲストに割り当てるメモリ(MiB指定)
  • location: インストールメディアの場所
  • network-bridge: インストール前のネットワークブリッジ
  • os-type: OSのタイプ
  • os-variant: ゲストOSを最適化するために使用される重要なオプション [重要]

正常なシャットダウンに向けたオプションの方法

libvirt-guestsは安全なシャットダウンを保証する。ホストがシャットダウンされる時にゲストをディスクに自動的に保存して、ホスト再起動時にシャットダウン前の状態に復元する。デフォルトではゲストをサスペンドする設定になっている。これを正常なシャットダウンにする場合はlibvirt-guestsの設定ファイルのパラメータを変更する必要がある。

ON_SHUTDOWN=suspendON_SHUTDOWN=shutdownに変更。

vim /etc/sysconfig/libvirt-guests

virt-cloneによるゲストのクローン

virt-clone --original demo --auto-clone

demoという仮想マシンをクローン作成

ネットワーク設定

libvirtを使用したNAT

virsh net-list --all

デフォルトのネットワーク設定は/etc/libvirt/qemu/networks/default.xmlで定義されている。

デフォルトネットワークが自動で開始するように設定。

virsh net-autostart default

デフォルトのネットワークを開始

virsh net-start default

ブリッジ設定

libvirtを使用して設定する際にはNetworkManagerを無効化しておく

libvirt