Install nested devstack(stable/mitaka) on KVM

# Install nested devstack(stable/mitaka) on KVM

## 本書の目的

* KVM ホスト上にネストの devstack 環境を構築するための備忘録
* とりあえず動かしはじめることを目的にしています

## リファレンス

* https://blog.davelindon.me/install-openstack-devstack-on-centos-7/
* http://docs.openstack.org/developer/devstack/guides/devstack-with-nested-kvm.html
* https://github.com/vagrant-libvirt/vagrant-libvirt

## System 構成

### KVM

* Ubuntu 14.04.3 LTS
* qemu-kvm 2.0.0
* libvirt 1.2.2

### Vagrant

* Vagrant 1.8.1
* vagrant-libvirt (0.0.32)

### devstack

* Mitaka
* CentOS7 box
  * 4GB memory
  * 4 cpus
  * 100G disk
  * devstack ホストは ubuntu でも良かったのですが libvirt 版の公式 ubuntu box が無いので CentOS7 にしています

## Check Host Requirements

```shell-session
~$ cat /sys/module/kvm_intel/parameters/nested
Y
~$ modinfo kvm_intel | grep nested
parm:           nested:bool
```

## 構築手順

### 1. Vagrant ファイルの作成

```shell-session
~$ cd working-dir
~$ mkdir -p vagrant/devstack
~$ cd vagrant/devstack
~$ vagrant box add centos/7
~$ vagrant init centos/7
~$ vagrant box update
~$ mv -iv Vagrantfile Vagrantfile.org
```
```shell-session
~$ cat << EOF > Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.define :vm1 do |vm1|
    vm1.vm.box = "centos/7"
    vm1.vm.network :private_network, :ip => "192.168.25.100"
    vm1.vm.provider :libvirt do |domain|
      domain.memory = 4096
      domain.cpus = 4
      domain.nested = true
      domain.machine_virtual_size = 100
    end
  end
end
EOF
```
```shell-session
~$ cat Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.define :vm1 do |vm1|
    vm1.vm.box = "centos/7"
    vm1.vm.network :private_network, :ip => "192.168.25.100"
    vm1.vm.provider :libvirt do |domain|
      domain.memory = 4096
      domain.cpus = 4
      domain.nested = true
      domain.machine_virtual_size = 100
    end
  end
end
```

### 2. Nested Host の起動とログイン確認

```shell-session
~$ vagrant up
~$ vagrant ssh

[vagrant@localhost ~]$
[vagrant@localhost ~]$ exit

~$ vagrant halt
~$ virsh list --all | grep devstack
 -     devstack_vm1                   shut off
```

### 3. VM’s libvirt XML の編集

```shell-session
~$ virsh edit devstack_vm1
```
```shell-session:before
  <cpu mode='host-model'>
    <model fallback='allow'/>
    <feature policy='optional' name='vmx'/>
    <feature policy='optional' name='svm'/>
  </cpu>
```
```shell-session:after
  <cpu mode='host-passthrough'>
  </cpu>
```
```shell-session
~$ vagrant up
~$ virsh list --all | grep devstack
 4     devstack_vm1                   running
```

### 4. Host OS の root パーティション拡張 (If needed)

```shell-session
~$ vagrant ssh

[vagrant@localhost ~]$ sudo su -
[root@localhost ~]# fdisk /dev/vda
(snip)
Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
Selected partition 4
First sector (83886080-209715199, default 83886080):
Last sector, +sectors or +size{K,M,G} (83886080-209715199, default 209715199):
(snip)
Command (m for help): t
Hex code (type L to list all codes): 8e
(snip)
Command (m for help): w
(snip)
[root@localhost ~]# partprobe

[root@localhost ~]# pvcreate /dev/vda4
[root@localhost ~]# pvdisplay
[root@localhost ~]# vgextend VolGroup00 /dev/vda4
[root@localhost ~]# lvextend -l +100%FREE /dev/VolGroup00/LogVol00
[root@localhost ~]# lvdisplay /dev/VolGroup00/LogVol00

[root@localhost ~]# resize2fs /dev/mapper/VolGroup00-LogVol00
[root@localhost ~]# df -h
```

### 5. devstack のインストール

#### 5-1. install git

```shell-session
[root@localhost ~]# yum -y update
[root@localhost ~]# yum install -y git yum-utils
```

#### 5-2. install pip

* http://www.liquidweb.com/kb/how-to-install-pip-on-centos-7/
* http://dl.fedoraproject.org/pub/epel/7/x86_64/e/

```shell-session
[root@localhost ~]# rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-6.noarch.rpm
[root@localhost ~]# yum -y update
[root@localhost ~]# yum -y install python-pip
[root@localhost ~]# pip -V
pip 7.1.0 from /usr/lib/python2.7/site-packages (python 2.7)

[root@localhost ~]# pip install -U os-testr
```

#### 5-3. git clone

```shell-session
[root@localhost ~]# useradd -d /opt/stack -m -s /bin/bash stack
[root@localhost ~]# echo "stack ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
[root@localhost ~]# su - stack
[stack@localhost ~]$ git clone -b stable/mitaka https://git.openstack.org/openstack-dev/devstack
[stack@localhost ~]$ cd devstack/
[stack@localhost devstack]$ git branch
* stable/mitaka
```

#### 5-4. create localrc

```shell-session
[stack@localhost devstack]$ cat << EOF > localrc
HOST_IP=192.168.25.100
DEST=/opt/stack

ADMIN_PASSWORD=secret
DATABASE_PASSWORD=\$ADMIN_PASSWORD
RABBIT_PASSWORD=\$ADMIN_PASSWORD
SERVICE_PASSWORD=\$ADMIN_PASSWORD
EOF
```
```shell-session
[stack@localhost devstack]$ cat localrc
HOST_IP=192.168.25.100
DEST=/opt/stack

ADMIN_PASSWORD=secret
DATABASE_PASSWORD=$ADMIN_PASSWORD
RABBIT_PASSWORD=$ADMIN_PASSWORD
SERVICE_PASSWORD=$ADMIN_PASSWORD
```

#### 5-5. exec stack.sh

```shell-session
[stack@localhost devstack]$ ./stack.sh
(snip)
========================
DevStack Components Timed
========================

run_process - 46 secs
pip_install - 715 secs
restart_apache_server - 8 secs
wait_for_service - 16 secs
yum_install - 475 secs
git_timed - 301 secs



This is your host IP address: 192.168.25.100
This is your host IPv6 address: ::1
Horizon is now available at http://192.168.25.100/dashboard
Keystone is serving at http://192.168.25.100:5000/
The default users are: admin and demo
The password: secret
2016-05-16 18:44:42.087 | stack.sh completed in 2603 seconds.
[stack@localhost devstack]$
```

### 6. 動作確認

```shell-session
[stack@localhost devstack]$ source openrc admin admin
WARNING: setting legacy OS_TENANT_NAME to support cli tools.
[stack@localhost devstack]$ nova list
+----+------+--------+------------+-------------+----------+
| ID | Name | Status | Task State | Power State | Networks |
+----+------+--------+------------+-------------+----------+
+----+------+--------+------------+-------------+----------+
[stack@localhost devstack]$
```