ubuntu 16.04でのkvm環境の作り方

ubuntu 16.04がリリースされたので、kvmの環境を作り直したついでに手順を記録しておいた。

test@tx1310m1:~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
UBUNTU_CODENAME=xenial
test@tx1310m1:~$ 

いろいろ必要なものを入れる。依存関係がわからないのでコマンドを実行して入ってなかったら入れる。できるだけ最低限のものしか入れたくないので試しながら入れる。

test@tx1310m1:~$ virsh
The program 'virsh' is currently not installed. You can install it by typing:
sudo apt install libvirt-bin
test@tx1310m1:~$ sudo apt install libvirt-bin
test@tx1310m1:~$
test@tx1310m1:~$ qemu-img
The program 'qemu-img' is currently not installed. You can install it by typing:
sudo apt install qemu-utils
test@tx1310m1:~$ sudo apt install qemu-utils
test@tx1310m1:~$ ovs-vsctl
The program 'ovs-vsctl' is currently not installed. You can install it by typing:
sudo apt install openvswitch-switch
test@tx1310m1:~$ sudo apt install openvswitch-switch
test@tx1310m1:~$ kvm
The program 'kvm' is currently not installed. You can install it by typing:
sudo apt install qemu-kvm
test@tx1310m1:~$ sudo apt install qemu-kvm

Open vSwitchが使いたいので、デフォルトで作られるLinuxブリッジを削除する。

test@tx1310m1:~$ sudo virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes

test@tx1310m1:~$ sudo virsh net-info
error: command 'net-info' requires <network> option
test@tx1310m1:~$ sudo virsh net-info default
Name:           default
UUID:           73000481-2759-4b73-89bf-71822032a568
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0

test@tx1310m1:~$ 
test@tx1310m1:~$ sudo virsh net-destroy default
Network default destroyed

test@tx1310m1:~$ sudo virsh net-autostart default --disable
Network default unmarked as autostarted

仮想マシンのネットワークを適当に何個かつくる。 Using DevStack with neutron Networking — DevStack 0.0.1.dev7579 documentation これに則るならば2つ作る。ここでは3つ作っている。

test@tx1310m1:~$ sudo ovs-vsctl add-br br-external
test@tx1310m1:~$ sudo ovs-vsctl add-br br-internal-1
test@tx1310m1:~$ sudo ovs-vsctl add-br br-internal-2
test@tx1310m1:~$ sudo ovs-vsctl add-port br-external enp2s0
test@tx1310m1:~$ sudo ovs-vsctl show
c23e6cb4-b027-4255-ab0f-9cca018b573a
    Bridge "br-internal-2"
        Port "br-internal-2"
            Interface "br-internal-2"
                type: internal
    Bridge br-external
        Port br-external
            Interface br-external
                type: internal
        Port "enp2s0"
            Interface "enp2s0"
    Bridge "br-internal-1"
        Port "br-internal-1"
            Interface "br-internal-1"
                type: internal
    ovs_version: "2.5.0"
test@tx1310m1:~$ 

空っぽのqcow2イメージを作って、仮想マシンの実体を作る。

test@tx1310m1:~$ cd vm/
test@tx1310m1:~/vm$ qemu-img create -f qcow2 ctrl.qcow2 20G
Formatting 'ctrl.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16
test@tx1310m1:~/vm$ file ctrl.qcow2 
ctrl.qcow2: QEMU QCOW Image (v3), 21474836480 bytes
test@tx1310m1:~/vm$ 
test@tx1310m1:~$ virt-install
The program 'virt-install' is currently not installed. You can install it by typing:
sudo apt install virtinst
test@tx1310m1:~$ sudo apt install virtinst
test@tx1310m1:~$ sudo virt-install -n controller -r 4096 --disk path=/home/test/vm/ctrl.qcow2,bus=virtio --nonetworks --graphics vnc,password=vnc,listen=0.0.0.0,keymap=ja --noautoconsole -v --boot hd

Starting install...
Creating domain...                                          |    0 B  00:00     
Domain creation completed.
test@tx1310m1:~$ 
test@tx1310m1:~/vm$ sudo virsh list
 Id    Name                           State
----------------------------------------------------
 1     controller                     running

test@tx1310m1:~$ 

仮想マシンの設定ファイルを編集する。起動順序をcdrom -> hddに変更する、CDデバイスとネットワークデバイスを追記する。

test@tx1310m1:~/vm$ virsh edit controller
----------これ追加
    <boot dev='cdrom'/>
----------これ追加
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file=‘/home/test/iso/ubuntu-16.04-server-amd64.iso’/>
      <target dev='hdc' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' unit='0'/>
    </disk>
    <interface type='bridge'>
      <source bridge='br-external'/>
      <virtualport type='openvswitch'>
      </virtualport>
      <model type='virtio'/>
    </interface>
    <interface type='bridge'>
      <source bridge='br-internal-1'/>
      <virtualport type='openvswitch'>
      </virtualport>
      <model type='virtio'/>
    </interface>
    <interface type='bridge'>
      <source bridge='br-internal-2'/>
      <virtualport type='openvswitch'>
      </virtualport>
      <model type='virtio'/>
    </interface>

編集が終了したら、適用した仮想マシンを再スタートする。ついでにvncのディスプレイ番号をチェックしておく。

test@tx1310m1:~$ sudo virsh destroy controller
Domain controller destroyed

test@tx1310m1:~$ sudo virsh start controller
Domain controller started

test@tx1310m1:~$ virsh vncdisplay controller
:0

test@tx1310m1:~$ 

vncで接続してインストールする。vncクライアントはMacのFinderを使ったが、たぶんなんでもいい。パスワードの入力を促されるので「vnc」と入れる。インストールが完了したら設定ファイルを編集し、isoファイルを指定している行を削除する。削除したら設定ファイルを再適用して終わり。

test@tx1310m1:~$ virsh edit controller
Domain controller XML configuration edited.

----------これ削除
      <source file=‘/home/test/iso/ubuntu-16.04-server-amd64.iso’/>
----------

test@tx1310m1:~$ virsh destroy controller
Domain controller destroyed

test@tx1310m1:~$ virsh start controller
Domain controller started