この記事は約93分0秒で読むことができます。

kvm上の仮想マシンipを固定ipにしようかなって思った話

まえがき

固定ipにでもしようかなとおもいました。

参考文献

KVMにDHCPで固定IPを設定する  
Libvirt/KVM で VM に静的な IP アドレスを配布する  
フィルタ言語 AWK (2)  
はじめてのAWK  
awkでOSのコマンドを実行させる  
KVM仮想マシンの名前変更と移動  

Vagrantfile

ここらへんもホスト名違いだけだし、うまくすっきり出来そうだけど、今はこれでよしとして楽しみにとっておこう。やり方はあるとおもうんだよな。

コード表示

[oracle@centos vx]$ cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.synced_folder '.', '/mnt', type: 'rsync'
  config.vm.synced_folder '.', '/vagrant', disabled: true
  config.vm.define "node1" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node1"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node2" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node2"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node3" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node3"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node4" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node4"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node5" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node5"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
  config.vm.define "node6" do |centos_on_kvm|
    centos_on_kvm.vm.provision :shell, :path => "a.sh"
    centos_on_kvm.vm.hostname = "node6"
    centos_on_kvm.vm.provider "libvirt" do |spec|
      spec.memory = 2048
      spec.cpus = 1
    end
  end
end

a.sh

a.shの中身だよ

コード表示

[oracle@centos vx]$ cat a.sh
#!/bin/bash
yum install -y net-tools
yum install -y lsof
yum install -y psmisc
yum install -y traceroute
yum install -y bridge-utils
yum install -y expect

vagrantツールでlibvirt管理のkvm上の仮想マシン立ち上げ!

コード表示

[oracle@centos vx]$ time vagrant up
real	0m54.930s
user	0m8.535s
sys	0m0.917s
[oracle@centos vx]$ vagrant status
Current machine states:

node1                     running (libvirt)
node2                     running (libvirt)
node3                     running (libvirt)
node4                     running (libvirt)
node5                     running (libvirt)
node6                     running (libvirt)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

vagrantプラグインのvagrant-libvirtによるネットワーク設定確認

コード表示

[oracle@centos vx]$ vagrant ssh-config
Host node1
  HostName 192.168.121.233
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/oracle/vx/.vagrant/machines/node1/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host node2
  HostName 192.168.121.193
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/oracle/vx/.vagrant/machines/node2/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host node3
  HostName 192.168.121.18
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/oracle/vx/.vagrant/machines/node3/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host node4
  HostName 192.168.121.17
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/oracle/vx/.vagrant/machines/node4/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host node5
  HostName 192.168.121.227
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/oracle/vx/.vagrant/machines/node5/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Host node6
  HostName 192.168.121.98
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/oracle/vx/.vagrant/machines/node6/libvirt/private_key
  IdentitiesOnly yes
  LogLevel FATAL

ref_net.sh

コード表示

[root@centos vx]# cat ref_net.sh
#!/bin/bash
SRC_FILE="$1"
TAR_FILE="$2"

while read line; do
  sed -e "/range/a @" <<<${line};
done < <(cat ${SRC_FILE}) > tmp

cat ${TAR_FILE} | tr "\n" " " | xargs -I{} bash -c 'awk "{gsub(\"@\",\"{}\");print}" tmp' > ${SRC_FILE}
[root@centos vx]# ll ref_net.sh
-rwxr-xr-x. 1 oracle docker 232  6月  2 16:00 ref_net.sh

libvirt管理のネットワークを確認する

コード表示

[root@centos vx]# cd /etc/libvirt/qemu/networks
[root@centos networks]# ll
合計 24
drwx------. 2 root root 4096  5月 26 16:59 autostart
-rw-------. 1 root root  591  6月  2 16:04 mynet100.xml
-rw-------. 1 root root  591  6月  1 16:22 mynet101.xml
-rw-------. 1 root root  591  6月  1 16:22 mynet102.xml
drwxr-xr-x. 2 root root 4096  6月  1 16:21 tmpl
-rw-------. 1 root root  603  6月  1 14:57 vagrant-libvirt.xml
[root@centos networks]# virsh net-list --all
 名前               状態     自動起動  永続
----------------------------------------------------------
 mynet100             動作中  いいえ (no) はい (yes)
 mynet101             動作中  いいえ (no) はい (yes)
 mynet102             動作中  いいえ (no) はい (yes)
 vagrant-libvirt      動作中  いいえ (no) はい (yes)

[root@centos networks]# seq 100 102 | xargs -t -I@ bash -c 'cat mynet@.xml'
bash -c cat mynet100.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh net-edit mynet100
or other application using the libvirt API.
-->

<network ipv6='yes'>
  <name>mynet100</name>
  <uuid>a4541103-3100-44ef-91c2-7c624e2db293</uuid>
  <forward mode='nat'/>
  <bridge name='virbr100' stp='on' delay='0'/>
  <mac address='52:54:00:e3:ab:b1'/>
  <ip address='192.168.100.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.100.2' end='192.168.100.254'/>
    </dhcp>
  </ip>
</network>
bash -c cat mynet101.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh net-edit mynet101
or other application using the libvirt API.
-->

<network ipv6='yes'>
  <name>mynet101</name>
  <uuid>5681498d-dc77-4180-b24a-f8de3dacc458</uuid>
  <forward mode='nat'/>
  <bridge name='virbr101' stp='on' delay='0'/>
  <mac address='52:54:00:28:6e:9c'/>
  <ip address='192.168.101.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.101.2' end='192.168.101.254'/>
    </dhcp>
  </ip>
</network>
bash -c cat mynet102.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
  virsh net-edit mynet102
or other application using the libvirt API.
-->

<network ipv6='yes'>
  <name>mynet102</name>
  <uuid>c8acdbb8-8802-4fa0-a443-ccf283de7aa7</uuid>
  <forward mode='nat'/>
  <bridge name='virbr102' stp='on' delay='0'/>
  <mac address='52:54:00:85:24:5e'/>
  <ip address='192.168.102.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.102.2' end='192.168.102.254'/>
    </dhcp>
  </ip>
</network>
[root@centos networks]# brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fb2b351e	no		
virbr0		8000.5254006df710	yes		virbr0-nic
							vnet0
							vnet1
							vnet2
							vnet3
							vnet4
							vnet5
virbr100		8000.525400e3abb1	yes		virbr100-nic
virbr101		8000.525400286e9c	yes		virbr101-nic
virbr102		8000.52540085245e	yes		virbr102-nic

ref_net.shを実行してマシン毎に反映する

コード表示

[root@centos networks]# seq 100 102 | xargs -t -I@ bash -c 'cat mynet@.xml'
bash -c cat mynet100.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit mynet100
or other application using the libvirt API.
-->

<network ipv6='yes'>
<name>mynet100</name>
<uuid>a4541103-3100-44ef-91c2-7c624e2db293</uuid>
<forward mode='nat'/>
<bridge name='virbr100' stp='on' delay='0'/>
<mac address='52:54:00:e3:ab:b1'/>
<ip address='192.168.100.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.100.2' end='192.168.100.254'/>
<host mac='52:54:00:5d:4a:3f' name='node1' ip='192.168.100.2'/> <host mac='52:54:00:dc:5b:1b' name='node2' ip='192.168.100.3'/> 
</dhcp>
</ip>
</network>
bash -c cat mynet101.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit mynet101
or other application using the libvirt API.
-->

<network ipv6='yes'>
<name>mynet101</name>
<uuid>5681498d-dc77-4180-b24a-f8de3dacc458</uuid>
<forward mode='nat'/>
<bridge name='virbr101' stp='on' delay='0'/>
<mac address='52:54:00:28:6e:9c'/>
<ip address='192.168.101.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.101.2' end='192.168.101.254'/>
<host mac='52:54:00:77:33:99' name='node3' ip='192.168.101.2'/> <host mac='52:54:00:61:72:67' name='node4' ip='192.168.101.3'/> 
</dhcp>
</ip>
</network>
bash -c cat mynet102.xml 
<!--
WARNING: THIS IS AN AUTO-GENERATED FILE. CHANGES TO IT ARE LIKELY TO BE
OVERWRITTEN AND LOST. Changes to this xml configuration should be made using:
virsh net-edit mynet102
or other application using the libvirt API.
-->

<network ipv6='yes'>
<name>mynet102</name>
<uuid>c8acdbb8-8802-4fa0-a443-ccf283de7aa7</uuid>
<forward mode='nat'/>
<bridge name='virbr102' stp='on' delay='0'/>
<mac address='52:54:00:85:24:5e'/>
<ip address='192.168.102.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.102.2' end='192.168.102.254'/>
<host mac='52:54:00:88:c2:54' name='node5' ip='192.168.102.2'/> <host mac='52:54:00:a8:bc:df' name='node6' ip='192.168.102.3'/> 
</dhcp>
</ip>
</network>

独自ネットワークを強制停止し、再定義して、再起動する

コード表示

[root@centos networks]# seq 100 102 | xargs -t -I@ bash -c 'virsh net-destroy mynet@ && virsh net-define mynet@.xml && virsh net-start mynet@'
bash -c virsh net-destroy mynet100 && virsh net-define mynet100.xml && virsh net-start mynet100 
ネットワーク mynet100 は強制停止されました

ネットワーク mynet100 が mynet100.xml から定義されました

ネットワーク mynet100 が起動されました

bash -c virsh net-destroy mynet101 && virsh net-define mynet101.xml && virsh net-start mynet101 
ネットワーク mynet101 は強制停止されました

ネットワーク mynet101 が mynet101.xml から定義されました

ネットワーク mynet101 が起動されました

bash -c virsh net-destroy mynet102 && virsh net-define mynet102.xml && virsh net-start mynet102 
ネットワーク mynet102 は強制停止されました

ネットワーク mynet102 が mynet102.xml から定義されました

ネットワーク mynet102 が起動されました

[root@centos networks]# virsh net-list --all
 名前               状態     自動起動  永続
----------------------------------------------------------
 mynet100             動作中  いいえ (no) はい (yes)
 mynet101             動作中  いいえ (no) はい (yes)
 mynet102             動作中  いいえ (no) はい (yes)
 vagrant-libvirt      動作中  いいえ (no) はい (yes)

仮想マシンが所属するネットワークを仮想マシンの設定ファイルを修正して変更する

変更するんだ

コード表示

[root@centos networks]# cd /etc/libvirt/qemu/
[root@centos qemu]# ll
合計 28
drwx------. 4 root root 4096  6月  2 16:09 networks
-rw-------. 1 root root 2367  6月  2 11:12 vx_node1.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node2.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node3.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node4.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node5.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node6.xml
[root@centos qemu]# seq 6 | xargs -t -I@ bash -c "cat vx_node@.xml | awk '/<interface/,/interface>/'"
bash -c cat vx_node1.xml | awk '/<interface/,/interface>/' 
    <interface type='network'>
      <mac address='52:54:00:5d:4a:3f'/>
      <source network='vagrant-libvirt'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </interface>
bash -c cat vx_node2.xml | awk '/<interface/,/interface>/' 
    <interface type='network'>
      <mac address='52:54:00:dc:5b:1b'/>
      <source network='vagrant-libvirt'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </interface>
bash -c cat vx_node3.xml | awk '/<interface/,/interface>/' 
    <interface type='network'>
      <mac address='52:54:00:77:33:99'/>
      <source network='vagrant-libvirt'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </interface>
bash -c cat vx_node4.xml | awk '/<interface/,/interface>/' 
    <interface type='network'>
      <mac address='52:54:00:61:72:67'/>
      <source network='vagrant-libvirt'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </interface>
bash -c cat vx_node5.xml | awk '/<interface/,/interface>/' 
    <interface type='network'>
      <mac address='52:54:00:88:c2:54'/>
      <source network='vagrant-libvirt'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </interface>
bash -c cat vx_node6.xml | awk '/<interface/,/interface>/' 
    <interface type='network'>
      <mac address='52:54:00:a8:bc:df'/>
      <source network='vagrant-libvirt'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </interface>
[root@centos qemu]# seq 6 | xargs -t -I@ bash -c "cat vx_node@.xml | grep \"source network\""
bash -c cat vx_node1.xml | grep "source network" 
      <source network='vagrant-libvirt'/>
bash -c cat vx_node2.xml | grep "source network" 
      <source network='vagrant-libvirt'/>
bash -c cat vx_node3.xml | grep "source network" 
      <source network='vagrant-libvirt'/>
bash -c cat vx_node4.xml | grep "source network" 
      <source network='vagrant-libvirt'/>
bash -c cat vx_node5.xml | grep "source network" 
      <source network='vagrant-libvirt'/>
bash -c cat vx_node6.xml | grep "source network" 
      <source network='vagrant-libvirt'/>

いざ変更!のまえに使用するref_nett.sh

awkいい

コード表示

[root@centos qemu]# cat ref_nett.sh
#!/bin/bash
RPT="$1"
seq ${RPT} | xargs -I@ bash -c 'echo /etc/libvirt/qemu/vx_node@.xml'>vx_node
paste -d ' ' /home/oracle/vx/grp vx_node > vx_node_grp

while read line; do
  echo ${line} | awk '
    BEGIN{
    }
    {
      net_name="mynet"$1+100;
      system("sed -i -e s/vagrant-libvirt/"net_name"/g "$2"");
    }
    END{
    }
  '; 
done < <(cat vx_node_grp)

いざ変更

コード表示

[root@centos qemu]# pwd
/etc/libvirt/qemu
[root@centos qemu]# ll
合計 40
drwx------. 4 root root 4096  6月  2 16:09 networks
-rwxr-xr-x. 1 root root  366  6月  2 18:38 ref_nett.sh
-rw-r--r--. 1 root root  186  6月  2 18:35 vx_node
-rw-------. 1 root root 2367  6月  2 11:12 vx_node1.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node2.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node3.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node4.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node5.xml
-rw-------. 1 root root 2367  6月  2 11:12 vx_node6.xml
-rw-r--r--. 1 root root  198  6月  2 18:35 vx_node_grp
[root@centos qemu]# seq 6 | xargs -I@ bash -c 'cat vx_node@.xml | grep -E "<name>|<source network|mynet"'
  <name>vx_node1</name>
      <source network='vagrant-libvirt'/>
  <name>vx_node2</name>
      <source network='vagrant-libvirt'/>
  <name>vx_node3</name>
      <source network='vagrant-libvirt'/>
  <name>vx_node4</name>
      <source network='vagrant-libvirt'/>
  <name>vx_node5</name>
      <source network='vagrant-libvirt'/>
  <name>vx_node6</name>
      <source network='vagrant-libvirt'/>
[root@centos qemu]# ./ref_nett.sh 6 | grep -E "<name>|<source network|mynet"
[root@centos qemu]# seq 6 | xargs -I@ bash -c 'cat vx_node@.xml | grep -E "<name>|<source network|mynet"'
  <name>vx_node1</name>
      <source network='mynet100'/>
  <name>vx_node2</name>
      <source network='mynet100'/>
  <name>vx_node3</name>
      <source network='mynet101'/>
  <name>vx_node4</name>
      <source network='mynet101'/>
  <name>vx_node5</name>
      <source network='mynet102'/>
  <name>vx_node6</name>
      <source network='mynet102'/>
[root@centos qemu]# ll
合計 40
drwx------. 4 root root 4096  6月  2 16:09 networks
-rwxr-xr-x. 1 root root  366  6月  2 18:40 ref_nett.sh
-rw-r--r--. 1 root root  186  6月  2 18:40 vx_node
-rw-------. 1 root root 2360  6月  2 18:40 vx_node1.xml
-rw-------. 1 root root 2360  6月  2 18:40 vx_node2.xml
-rw-------. 1 root root 2360  6月  2 18:40 vx_node3.xml
-rw-------. 1 root root 2360  6月  2 18:40 vx_node4.xml
-rw-------. 1 root root 2360  6月  2 18:40 vx_node5.xml
-rw-------. 1 root root 2360  6月  2 18:40 vx_node6.xml
-rw-r--r--. 1 root root  198  6月  2 18:40 vx_node_grp
[root@centos qemu]# seq 6 | xargs -I@ bash -c 'virsh define vx_node@.xml'
ドメイン vx_node1 が vx_node1.xml から定義されました

ドメイン vx_node2 が vx_node2.xml から定義されました

ドメイン vx_node3 が vx_node3.xml から定義されました

ドメイン vx_node4 が vx_node4.xml から定義されました

ドメイン vx_node5 が vx_node5.xml から定義されました

ドメイン vx_node6 が vx_node6.xml から定義されました

起動している仮想マシンを再起動前の状態

コード表示

[oracle@centos vx]$ brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fb2b351e	no		
virbr0		8000.5254006df710	yes		virbr0-nic
							vnet0
							vnet1
							vnet2
							vnet3
							vnet4
							vnet5
virbr100		8000.525400e3abb1	yes		virbr100-nic
virbr101		8000.525400286e9c	yes		virbr101-nic
virbr102		8000.52540085245e	yes		virbr102-nic
[oracle@centos vx]$ sudo virsh net-list --all
[sudo] oracle のパスワード:
 名前               状態     自動起動  永続
----------------------------------------------------------
 mynet100             動作中  いいえ (no) はい (yes)
 mynet101             動作中  いいえ (no) はい (yes)
 mynet102             動作中  いいえ (no) はい (yes)
 vagrant-libvirt      動作中  いいえ (no) はい (yes)

[oracle@centos vx]$ vagrant ssh-config | grep -E "^Host|\s{1,}Host"
Host node1
  HostName 192.168.121.233
Host node2
  HostName 192.168.121.193
Host node3
  HostName 192.168.121.18
Host node4
  HostName 192.168.121.17
Host node5
  HostName 192.168.121.227
Host node6
  HostName 192.168.121.98

起動している仮想マシンを再起動

コード表示

[oracle@centos vx]$ time vagrant reload
==> node1: Halting domain...
==> node1: Starting domain.
==> node1: Waiting for domain to get an IP address...
==> node1: Waiting for SSH to become available...
==> node1: Creating shared folders metadata...
==> node1: Rsyncing folder: /home/oracle/vx/ => /mnt
==> node1: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node1: flag to force provisioning. Provisioners marked to run always will still run.
==> node2: Halting domain...
==> node2: Starting domain.
==> node2: Waiting for domain to get an IP address...
==> node2: Waiting for SSH to become available...
==> node2: Creating shared folders metadata...
==> node2: Rsyncing folder: /home/oracle/vx/ => /mnt
==> node2: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node2: flag to force provisioning. Provisioners marked to run always will still run.
==> node3: Halting domain...
==> node3: Starting domain.
==> node3: Waiting for domain to get an IP address...
==> node3: Waiting for SSH to become available...
==> node3: Creating shared folders metadata...
==> node3: Rsyncing folder: /home/oracle/vx/ => /mnt
==> node3: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node3: flag to force provisioning. Provisioners marked to run always will still run.
==> node4: Halting domain...
==> node4: Starting domain.
==> node4: Waiting for domain to get an IP address...
==> node4: Waiting for SSH to become available...
==> node4: Creating shared folders metadata...
==> node4: Rsyncing folder: /home/oracle/vx/ => /mnt
==> node4: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node4: flag to force provisioning. Provisioners marked to run always will still run.
==> node5: Halting domain...
==> node5: Starting domain.
==> node5: Waiting for domain to get an IP address...
==> node5: Waiting for SSH to become available...
==> node5: Creating shared folders metadata...
==> node5: Rsyncing folder: /home/oracle/vx/ => /mnt
==> node5: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node5: flag to force provisioning. Provisioners marked to run always will still run.
==> node6: Halting domain...
==> node6: Starting domain.
==> node6: Waiting for domain to get an IP address...
==> node6: Waiting for SSH to become available...
==> node6: Creating shared folders metadata...
==> node6: Rsyncing folder: /home/oracle/vx/ => /mnt
==> node6: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node6: flag to force provisioning. Provisioners marked to run always will still run.

real	4m1.601s
user	0m5.994s
sys	0m0.554s

起動している仮想マシンを再起動後の状態

コード表示

[oracle@centos vx]$ brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fb2b351e	no		
virbr0		8000.5254006df710	yes		virbr0-nic
virbr100		8000.525400e3abb1	yes		virbr100-nic
							vnet4
							vnet5
virbr101		8000.525400286e9c	yes		virbr101-nic
							vnet0
							vnet1
virbr102		8000.52540085245e	yes		virbr102-nic
							vnet2
							vnet3
[oracle@centos vx]$ vagrant ssh-config | grep -E "^Host|\s{1,}Host"
Host node1
  HostName 192.168.100.2
Host node2
  HostName 192.168.100.3
Host node3
  HostName 192.168.101.2
Host node4
  HostName 192.168.101.3
Host node5
  HostName 192.168.102.2
Host node6
  HostName 192.168.102.3
[oracle@centos vx]$ while read line;do echo ${line};sleep 5; echo ${line}|bash;done < <(seq 6 | xargs -I@ bash -c "awk '{print \"vagrant ssh node@ -c \"\"\x5c\x27\"\"ip a show eth0\"\"\x5c\x27\"}' dummy_oneline")
vagrant ssh node1 -c 'ip a show eth0'
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:5d:4a:3f brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.2/24 brd 192.168.100.255 scope global noprefixroute dynamic eth0
       valid_lft 3309sec preferred_lft 3309sec
    inet6 fe80::5054:ff:fe5d:4a3f/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node2 -c 'ip a show eth0'
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:dc:5b:1b brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.3/24 brd 192.168.100.255 scope global noprefixroute dynamic eth0
       valid_lft 3341sec preferred_lft 3341sec
    inet6 fe80::5054:ff:fedc:5b1b/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node3 -c 'ip a show eth0'
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:77:33:99 brd ff:ff:ff:ff:ff:ff
    inet 192.168.101.2/24 brd 192.168.101.255 scope global noprefixroute dynamic eth0
       valid_lft 3375sec preferred_lft 3375sec
    inet6 fe80::5054:ff:fe77:3399/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node4 -c 'ip a show eth0'
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:61:72:67 brd ff:ff:ff:ff:ff:ff
    inet 192.168.101.3/24 brd 192.168.101.255 scope global noprefixroute dynamic eth0
       valid_lft 3410sec preferred_lft 3410sec
    inet6 fe80::5054:ff:fe61:7267/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node5 -c 'ip a show eth0'
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:88:c2:54 brd ff:ff:ff:ff:ff:ff
    inet 192.168.102.2/24 brd 192.168.102.255 scope global noprefixroute dynamic eth0
       valid_lft 3445sec preferred_lft 3445sec
    inet6 fe80::5054:ff:fe88:c254/64 scope link 
       valid_lft forever preferred_lft forever
vagrant ssh node6 -c 'ip a show eth0'
2: eth0:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:00:a8:bc:df brd ff:ff:ff:ff:ff:ff
    inet 192.168.102.3/24 brd 192.168.102.255 scope global noprefixroute dynamic eth0
       valid_lft 3479sec preferred_lft 3479sec
    inet6 fe80::5054:ff:fea8:bcdf/64 scope link 
       valid_lft forever preferred_lft forever

疎通確認でもしようかな

前やったときとは違って、セグメント越えられていないぞ!!!やった!!!仮想マシン側の設定ファイルと独自ネットワークの設定ファイルがちゃんと再定義、再起動して固定ipにした時に、いいかんじになるのかな。吟味必要。大事なとこ。

libvirt管理の仮想ゲストOSネットワークセグメントを切り分ける話  
コード表示

[oracle@centos vx]$ vagrant ssh node1
Last login: Sun Jun  2 10:31:45 2019 from 192.168.100.1
[vagrant@node1 ~]$ seq 6 | xargs -t -I% bash -c 'traceroute node% && ping -c 1 node%'
bash -c traceroute node1 && ping -c 1 node1 
traceroute to node1 (127.0.0.1), 30 hops max, 60 byte packets
 1  node1 (127.0.0.1)  0.009 ms  0.003 ms  0.003 ms
PING node1 (127.0.0.1) 56(84) bytes of data.
64 bytes from node1 (127.0.0.1): icmp_seq=1 ttl=64 time=0.005 ms

--- node1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.005/0.005/0.005/0.000 ms
bash -c traceroute node2 && ping -c 1 node2 
traceroute to node2 (192.168.100.3), 30 hops max, 60 byte packets
 1  node2 (192.168.100.3)  0.209 ms  0.191 ms  0.180 ms
PING node2 (192.168.100.3) 56(84) bytes of data.
64 bytes from node2 (192.168.100.3): icmp_seq=1 ttl=64 time=0.101 ms

--- node2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.101/0.101/0.101/0.000 ms
bash -c traceroute node3 && ping -c 1 node3 
node3: Name or service not known
Cannot handle "host" cmdline arg `node3' on position 1 (argc 1)
bash -c traceroute node4 && ping -c 1 node4 
node4: Name or service not known
Cannot handle "host" cmdline arg `node4' on position 1 (argc 1)
bash -c traceroute node5 && ping -c 1 node5 
node5: Name or service not known
Cannot handle "host" cmdline arg `node5' on position 1 (argc 1)
bash -c traceroute node6 && ping -c 1 node6 
node6: Name or service not known
Cannot handle "host" cmdline arg `node6' on position 1 (argc 1)
[vagrant@node1 ~]$ logout
Connection to 192.168.100.2 closed.
[oracle@centos vx]$ vagrant ssh node2
Last login: Sun Jun  2 10:33:18 2019 from 192.168.100.1
[vagrant@node2 ~]$ seq 6 | xargs -I% bash -c 'traceroute node% && ping -c 1 node%'
traceroute to node1 (192.168.100.2), 30 hops max, 60 byte packets
 1  node1 (192.168.100.2)  0.247 ms  0.215 ms  0.205 ms
PING node1 (192.168.100.2) 56(84) bytes of data.
64 bytes from node1 (192.168.100.2): icmp_seq=1 ttl=64 time=0.152 ms

--- node1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.152/0.152/0.152/0.000 ms
traceroute to node2 (127.0.0.1), 30 hops max, 60 byte packets
 1  node2 (127.0.0.1)  0.006 ms  0.002 ms  0.002 ms
PING node2 (127.0.0.1) 56(84) bytes of data.
64 bytes from node2 (127.0.0.1): icmp_seq=1 ttl=64 time=0.005 ms

--- node2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.005/0.005/0.005/0.000 ms
node3: Name or service not known
Cannot handle "host" cmdline arg `node3' on position 1 (argc 1)
node4: Name or service not known
Cannot handle "host" cmdline arg `node4' on position 1 (argc 1)
node5: Name or service not known
Cannot handle "host" cmdline arg `node5' on position 1 (argc 1)
node6: Name or service not known
Cannot handle "host" cmdline arg `node6' on position 1 (argc 1)
[vagrant@node2 ~]$ logout
Connection to 192.168.100.3 closed.
[oracle@centos vx]$ vagrant ssh node3
[vagrant@node3 ~]$ seq 6 | xargs -I% bash -c 'traceroute node% && ping -c 1 node%'
node1: Name or service not known
Cannot handle "host" cmdline arg `node1' on position 1 (argc 1)
node2: Name or service not known
Cannot handle "host" cmdline arg `node2' on position 1 (argc 1)
traceroute to node3 (127.0.0.1), 30 hops max, 60 byte packets
 1  node3 (127.0.0.1)  0.010 ms  0.003 ms  0.002 ms
PING node3 (127.0.0.1) 56(84) bytes of data.
64 bytes from node3 (127.0.0.1): icmp_seq=1 ttl=64 time=0.008 ms

--- node3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.008/0.008/0.008/0.000 ms
traceroute to node4 (192.168.101.3), 30 hops max, 60 byte packets
 1  node4 (192.168.101.3)  0.323 ms  0.301 ms  0.269 ms
PING node4 (192.168.101.3) 56(84) bytes of data.
64 bytes from node4 (192.168.101.3): icmp_seq=1 ttl=64 time=0.096 ms

--- node4 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.096/0.096/0.096/0.000 ms
node5: Name or service not known
Cannot handle "host" cmdline arg `node5' on position 1 (argc 1)
node6: Name or service not known
Cannot handle "host" cmdline arg `node6' on position 1 (argc 1)
[vagrant@node3 ~]$ logout
Connection to 192.168.101.2 closed.
[oracle@centos vx]$ vagrant ssh node4
Last login: Sun Jun  2 03:00:49 2019 from 192.168.121.1
[vagrant@node4 ~]$ seq 6 | xargs -I% bash -c 'traceroute node% && ping -c 1 node%'
node1: Name or service not known
Cannot handle "host" cmdline arg `node1' on position 1 (argc 1)
node2: Name or service not known
Cannot handle "host" cmdline arg `node2' on position 1 (argc 1)
traceroute to node3 (192.168.101.2), 30 hops max, 60 byte packets
 1  node3 (192.168.101.2)  0.090 ms  0.077 ms  0.068 ms
PING node3 (192.168.101.2) 56(84) bytes of data.
64 bytes from node3 (192.168.101.2): icmp_seq=1 ttl=64 time=0.108 ms

--- node3 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.108/0.108/0.108/0.000 ms
traceroute to node4 (127.0.0.1), 30 hops max, 60 byte packets
 1  node4 (127.0.0.1)  0.010 ms  0.005 ms  0.002 ms
PING node4 (127.0.0.1) 56(84) bytes of data.
64 bytes from node4 (127.0.0.1): icmp_seq=1 ttl=64 time=0.005 ms

--- node4 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.005/0.005/0.005/0.000 ms
node5: Name or service not known
Cannot handle "host" cmdline arg `node5' on position 1 (argc 1)
node6: Name or service not known
Cannot handle "host" cmdline arg `node6' on position 1 (argc 1)
[vagrant@node4 ~]$ logout
Connection to 192.168.101.3 closed.
[oracle@centos vx]$ vagrant ssh node5
[vagrant@node5 ~]$ seq 6 | xargs -I% bash -c 'traceroute node% && ping -c 1 node%'
node1: Name or service not known
Cannot handle "host" cmdline arg `node1' on position 1 (argc 1)
node2: Name or service not known
Cannot handle "host" cmdline arg `node2' on position 1 (argc 1)
node3: Name or service not known
Cannot handle "host" cmdline arg `node3' on position 1 (argc 1)
node4: Name or service not known
Cannot handle "host" cmdline arg `node4' on position 1 (argc 1)
traceroute to node5 (127.0.0.1), 30 hops max, 60 byte packets
 1  node5 (127.0.0.1)  0.009 ms  0.003 ms  0.002 ms
PING node5 (127.0.0.1) 56(84) bytes of data.
64 bytes from node5 (127.0.0.1): icmp_seq=1 ttl=64 time=0.006 ms

--- node5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.006/0.006/0.006/0.000 ms
traceroute to node6 (192.168.102.3), 30 hops max, 60 byte packets
 1  node6 (192.168.102.3)  0.388 ms  0.368 ms  0.358 ms
PING node6 (192.168.102.3) 56(84) bytes of data.
64 bytes from node6 (192.168.102.3): icmp_seq=1 ttl=64 time=0.186 ms

--- node6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.186/0.186/0.186/0.000 ms
[vagrant@node5 ~]$ logout
Connection to 192.168.102.2 closed.
[oracle@centos vx]$ vagrant ssh node6
[vagrant@node6 ~]$ seq 6 | xargs -I% bash -c 'traceroute node% && ping -c 1 node%'
node1: Name or service not known
Cannot handle "host" cmdline arg `node1' on position 1 (argc 1)
node2: Name or service not known
Cannot handle "host" cmdline arg `node2' on position 1 (argc 1)
node3: Name or service not known
Cannot handle "host" cmdline arg `node3' on position 1 (argc 1)
node4: Name or service not known
Cannot handle "host" cmdline arg `node4' on position 1 (argc 1)
traceroute to node5 (192.168.102.2), 30 hops max, 60 byte packets
 1  node5 (192.168.102.2)  0.084 ms  0.075 ms  0.067 ms
PING node5 (192.168.102.2) 56(84) bytes of data.
64 bytes from node5 (192.168.102.2): icmp_seq=1 ttl=64 time=0.084 ms

--- node5 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.084/0.084/0.084/0.000 ms
traceroute to node6 (127.0.0.1), 30 hops max, 60 byte packets
 1  node6 (127.0.0.1)  0.007 ms  0.002 ms  0.002 ms
PING node6 (127.0.0.1) 56(84) bytes of data.
64 bytes from node6 (127.0.0.1): icmp_seq=1 ttl=64 time=0.005 ms

--- node6 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.005/0.005/0.005/0.000 ms
[vagrant@node6 ~]$ logout
Connection to 192.168.102.3 closed.
[oracle@centos vx]$ 

あとがき

ネットワーク楽しい!!!awk楽しい!!!面白くなってきた!!!あとでスクリプトにまとめておきたいな。仮想環境でiptablesの自作ルータ作りたい!!!以上、ありがとうございました。

Leave a Reply

Your email address will not be published. Required fields are marked *