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

仮想ゲストOS固定ip化の手番整理

まえがき

shell力が足りない。

実行ディレクトリ

_main.shに一元化してみた。

コード表示

[oracle@centos vx]$ pwd
/home/oracle/vx
[oracle@centos vx]$ ll
合計 16
-rwx------. 1 oracle docker 1721  6月  2 11:07 Vagrantfile
-rwx------. 1 oracle docker 3015  5月 24 05:16 Vagrantfile_org
-rwxr-xr-x. 1 oracle docker 3583  6月  6 23:28 _main.sh
-rwxr-xr-x. 1 oracle docker  155  6月  6 23:33 a.sh

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

固定ip化前の状態

コード表示

[oracle@centos vx]$ time vagrant up
real	0m55.164s
user	0m8.515s
sys	0m0.832s
[oracle@centos vx]$ vagrant ssh-config | grep -E "^Host|\s{1,}Host"
Host node1
  HostName 192.168.121.199
Host node2
  HostName 192.168.121.240
Host node3
  HostName 192.168.121.140
Host node4
  HostName 192.168.121.129
Host node5
  HostName 192.168.121.72
Host node6
  HostName 192.168.121.208
[oracle@centos vx]$ brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fb2b351e	no		veth1486633
virbr0		8000.5254006df710	yes		virbr0-nic
							vnet0
							vnet1
							vnet2
							vnet3
							vnet4
							vnet5
virbr100		8000.525400ecc4a6	yes		virbr100-nic
virbr101		8000.525400922d8b	yes		virbr101-nic
virbr102		8000.5254003f5854	yes		virbr102-nic

_main.shをキックする

コード表示

[oracle@centos vx]$ su root
パスワード:
[root@centos vx]# ./_main.sh
ネットワーク mynet100 は強制停止されました

ネットワーク mynet100 の定義が削除されました

ネットワーク mynet101 は強制停止されました

ネットワーク mynet101 の定義が削除されました

ネットワーク mynet102 は強制停止されました

ネットワーク mynet102 の定義が削除されました

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

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

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

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

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

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

ネットワーク mynet100 は強制停止されました

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

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

ネットワーク mynet101 は強制停止されました

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

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

ネットワーク mynet102 は強制停止されました

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

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

ドメイン 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 から定義されました


vagrant reload

コード表示

[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	1m42.110s
user	0m5.937s
sys	0m0.601s

固定ip化後の状態

コード表示

[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]$ brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.0242fb2b351e	no		veth1486633
virbr0		8000.5254006df710	yes		virbr0-nic
virbr100		8000.525400106400	yes		virbr100-nic
							vnet3
							vnet4
virbr101		8000.5254009fdb10	yes		virbr101-nic
							vnet0
							vnet5
virbr102		8000.5254009e9318	yes		virbr102-nic
							vnet1
							vnet2

疎通確認

外部のみ

コード表示

[oracle@centos vx]$ while read line;do echo ${line};sleep 10; echo ${line}|bash;done < <(seq 6 | xargs -I@ bash -c "echo a |awk '{print \"vagrant ssh node@ -c \"\"\x5c\x27\"\"traceroute 8.8.8.8\"\"\x5c\x27\"}'")
vagrant ssh node1 -c 'traceroute 8.8.8.8'
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  gateway (192.168.100.1)  0.141 ms  0.117 ms  0.098 ms
 2  192.168.1.1 (192.168.1.1)  1.009 ms  1.000 ms  0.988 ms
 3  nas827.p-kanagawa.nttpc.ne.jp (210.153.251.235)  4.888 ms  4.874 ms  4.866 ms
 4  210.139.125.169 (210.139.125.169)  4.972 ms  4.941 ms  4.929 ms
 5  210.165.249.177 (210.165.249.177)  6.271 ms  5.383 ms  6.357 ms
 6  0-0-0-18.tky-no-acr01.sphere.ad.jp (210.153.241.89)  9.074 ms  7.535 ms  7.492 ms
 7  0-0-1-0--2025.tky-t4-bdr01.sphere.ad.jp (202.239.117.14)  7.545 ms  6.645 ms  6.815 ms
 8  72.14.202.229 (72.14.202.229)  6.865 ms 72.14.205.32 (72.14.205.32)  6.279 ms 72.14.202.229 (72.14.202.229)  6.369 ms
 9  * * *
10  google-public-dns-a.google.com (8.8.8.8)  6.498 ms  11.164 ms  11.162 ms
vagrant ssh node2 -c 'traceroute 8.8.8.8'
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  gateway (192.168.100.1)  0.177 ms  0.145 ms  0.130 ms
 2  192.168.1.1 (192.168.1.1)  2.078 ms  2.140 ms  2.126 ms
 3  nas827.p-kanagawa.nttpc.ne.jp (210.153.251.235)  4.553 ms  4.540 ms  5.028 ms
 4  210.139.125.169 (210.139.125.169)  5.133 ms  5.099 ms  5.600 ms
 5  210.165.249.177 (210.165.249.177)  6.771 ms  6.915 ms  7.005 ms
 6  0-0-0-18.tky-no-acr01.sphere.ad.jp (210.153.241.89)  7.796 ms  6.482 ms  6.469 ms
 7  0-0-1-0--2025.tky-t4-bdr01.sphere.ad.jp (202.239.117.14)  6.912 ms  9.759 ms  9.742 ms
 8  72.14.202.229 (72.14.202.229)  9.722 ms 72.14.205.32 (72.14.205.32)  9.735 ms 72.14.202.229 (72.14.202.229)  9.851 ms
 9  * * *
10  google-public-dns-a.google.com (8.8.8.8)  9.791 ms  10.098 ms  10.236 ms
vagrant ssh node3 -c 'traceroute 8.8.8.8'
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  gateway (192.168.101.1)  0.105 ms  0.081 ms  0.071 ms
 2  192.168.1.1 (192.168.1.1)  1.157 ms  1.221 ms  1.209 ms
 3  nas827.p-kanagawa.nttpc.ne.jp (210.153.251.235)  4.555 ms  4.547 ms  5.230 ms
 4  210.139.125.169 (210.139.125.169)  5.218 ms  4.581 ms  5.193 ms
 5  210.165.249.177 (210.165.249.177)  6.360 ms  5.170 ms  5.684 ms
 6  0-0-0-18.tky-no-acr01.sphere.ad.jp (210.153.241.89)  10.565 ms  10.326 ms  10.306 ms
 7  0-0-1-0--2025.tky-t4-bdr01.sphere.ad.jp (202.239.117.14)  7.237 ms  6.757 ms  6.739 ms
 8  72.14.205.32 (72.14.205.32)  6.692 ms  6.684 ms  6.672 ms
 9  * * *
10  google-public-dns-a.google.com (8.8.8.8)  7.246 ms  7.269 ms  7.310 ms
vagrant ssh node4 -c 'traceroute 8.8.8.8'
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  gateway (192.168.101.1)  0.117 ms  0.098 ms  0.086 ms
 2  192.168.1.1 (192.168.1.1)  1.337 ms  1.328 ms  1.315 ms
 3  nas827.p-kanagawa.nttpc.ne.jp (210.153.251.235)  5.040 ms  5.031 ms  5.015 ms
 4  210.139.125.169 (210.139.125.169)  5.064 ms  5.045 ms  5.037 ms
 5  210.165.249.177 (210.165.249.177)  6.263 ms  5.925 ms  6.818 ms
 6  0-0-0-18.tky-no-acr01.sphere.ad.jp (210.153.241.89)  8.944 ms  7.222 ms  7.423 ms
 7  0-0-1-0--2025.tky-t4-bdr01.sphere.ad.jp (202.239.117.14)  8.024 ms  7.253 ms  7.240 ms
 8  72.14.205.32 (72.14.205.32)  6.980 ms 72.14.202.229 (72.14.202.229)  6.966 ms 72.14.205.32 (72.14.205.32)  7.013 ms
 9  * * *
10  google-public-dns-a.google.com (8.8.8.8)  6.952 ms  7.687 ms  6.928 ms
vagrant ssh node5 -c 'traceroute 8.8.8.8'
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  gateway (192.168.102.1)  0.134 ms  0.112 ms  0.100 ms
 2  192.168.1.1 (192.168.1.1)  1.683 ms  1.674 ms  1.663 ms
 3  nas827.p-kanagawa.nttpc.ne.jp (210.153.251.235)  4.572 ms  4.561 ms  4.544 ms
 4  210.139.125.169 (210.139.125.169)  4.636 ms  5.272 ms  5.260 ms
 5  210.165.249.177 (210.165.249.177)  6.053 ms  6.396 ms  6.731 ms
 6  0-0-0-18.tky-no-acr01.sphere.ad.jp (210.153.241.89)  7.816 ms  6.286 ms  6.272 ms
 7  0-0-1-0--2025.tky-t4-bdr01.sphere.ad.jp (202.239.117.14)  10.755 ms  6.881 ms  6.861 ms
 8  72.14.205.32 (72.14.205.32)  5.745 ms  6.561 ms 72.14.202.229 (72.14.202.229)  7.336 ms
 9  * * *
10  google-public-dns-a.google.com (8.8.8.8)  6.604 ms  6.460 ms  7.875 ms
vagrant ssh node6 -c 'traceroute 8.8.8.8'
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
 1  gateway (192.168.102.1)  0.099 ms  0.082 ms  0.067 ms
 2  192.168.1.1 (192.168.1.1)  1.620 ms  1.609 ms  1.599 ms
 3  nas827.p-kanagawa.nttpc.ne.jp (210.153.251.235)  4.549 ms  5.257 ms  4.521 ms
 4  210.139.125.169 (210.139.125.169)  5.314 ms  5.304 ms  5.290 ms
 5  210.165.249.177 (210.165.249.177)  6.331 ms  6.404 ms  6.728 ms
 6  0-0-0-18.tky-no-acr01.sphere.ad.jp (210.153.241.89)  9.462 ms  6.419 ms  6.397 ms
 7  0-0-1-0--2025.tky-t4-bdr01.sphere.ad.jp (202.239.117.14)  7.266 ms  7.219 ms  7.347 ms
 8  72.14.205.32 (72.14.205.32)  7.070 ms  6.498 ms  7.053 ms
 9  * * *
10  google-public-dns-a.google.com (8.8.8.8)  6.993 ms  6.983 ms  9.076 ms

_main.shの中身

すきっりさせるのは後日。。。

コード表示

[oracle@centos vx]$ cat _main.sh
#!/bin/bash
OUTPUT=$(pwd)/output
CMD_DIR=$(pwd)/cmd
TMP_DIR=$(pwd)/tmp
AWK_DIR=$(pwd)/awk
LVT_DIR=/etc/libvirt/qemu
LVT_NET_DIR=${LVT_DIR}/networks

_offnet(){
  START_RN=$1
  END_RN=$2
  ( cd ${LVT_NET_DIR} && \
    seq ${START_RN} ${END_RN} | while read RN;do
      virsh net-destroy mynet${RN} && virsh net-undefine mynet${RN};
    done )
}

_mknet(){
  START_RN=$1
  END_RN=$2
  ( cd ${LVT_NET_DIR} && \
    seq ${START_RN} ${END_RN} | while read RN;do
      cp $(pwd)/tmpl/mynet@.xml $(pwd)/mynet${RN}.xml && sed -i s/@/${RN}/g $(pwd)/mynet${RN}.xml;
    done )
}

_onnet(){
  START_RN=$1
  END_RN=$2
  ( cd ${LVT_NET_DIR} && \
    seq ${START_RN} ${END_RN} | while read RN;do
      virsh net-define mynet${RN}.xml && virsh net-start mynet${RN};
    done )
}

_rebnet(){
  START_RN=$1
  END_RN=$2
  ( cd ${LVT_NET_DIR} && \
    seq ${START_RN} ${END_RN} | while read RN;do
      virsh net-destroy mynet${RN} && virsh net-define mynet${RN}.xml && virsh net-start mynet${RN}
    done )
}

_buildnet(){
  START_RN=$1
  END_RN=$2
  _offnet ${START_RN} ${END_RN}
  _mknet ${START_RN} ${END_RN}
  _onnet ${START_RN} ${END_RN}
}

_rmdir(){
  rm -rf {${OUTPUT},${CMD_DIR},${TMP_DIR},${AWK_DIR}};  
}

_mkdir(){
  mkdir -p {${OUTPUT},${CMD_DIR},${TMP_DIR},${AWK_DIR}};
}

_initdir(){
  _rmdir
  _mkdir
}

_mkvxnm(){
  START_RN=$1
  END_RN=$2
  seq ${START_RN} ${END_RN} | while read RN;do
    echo ${LVT_DIR}/vx_node${RN}.xml >>${OUTPUT}/vx_node;
  done
}

_grp(){
  RN=$1
  GRP=$2
  while read line; do
     echo ${line} | sed -e s/GRP/${GRP}/ | bash;
  done < <(seq ${RN} | xargs -I@ bash -c 'echo echo $\(\(@%GRP\)\)') | sort >${OUTPUT}/grp
}

_join(){
  LFT=$1
  RGT=$2
  OPT_FNM=$3
  paste -d ' ' ${LFT} ${RGT} >${OPT_FNM};
}

_callcmd(){
  RPT=$1
  while read line; do
    OPT_FNM=$(basename ${line} | sed -e s/\_/\\t/g | awk '{print $2}')
    [ -e ${OUTPUT}/${OPT_FNM} ] && rm -f ${OUTPUT}/${OPT_FNM};
    seq ${RPT} | while read rpt; do
      cat ${line} | sed -e s/@/${rpt}/ | bash >>${OUTPUT}/${OPT_FNM};
    done
  done < <(find ${CMD_DIR}/* -name "*")
}

_mkcmd(){
  CMD_FNM=$1
  CMD=$2
  echo ${CMD} > ${CMD_DIR}/${CMD_FNM};
}

_split(){
  LFT=$1
  RGT=$2
  paste -d ' ' ${LFT} ${RGT} | awk '
    OUTPUT="'"${OUTPUT}"'"
    {print>OUTPUT"\x2f""split_"$1}
  ' 1>/dev/null 
}

_mk_def_ip_script_with_awk(){
  cat <<EOF >${AWK_DIR}/def_ip.awk
{
  gsub(/[^ ]+/,"\x27&\x27");
  print "<host mac="\$3" name="\$2" ip=\x27""192.168."third_octet"."NR+1"\x27""/>"
}
EOF
}

_call_def_ip_script_with_awk(){
  START_RN=$1
  END_RN=$2
  seq ${START_RN} ${END_RN} | while read RN;do
    gawk -v "third_octet=$((${RN}+100))" -f ${AWK_DIR}/def_ip.awk ${OUTPUT}/split_${RN} > ${OUTPUT}/def_host_tag_$((${RN}+100));
  done
}

_kvm_guest_modify_network(){
  START_RN=$1
  END_RN=$2
  
  while read line; do
    OPT_FNM=$(basename ${line})
    sed -e "/range/a @" < <(cat ${line}) > ${TMP_DIR}/${OPT_FNM}
  done < <(find ${LVT_NET_DIR} -maxdepth 1 -name "mynet*")
  
  seq ${START_RN} ${END_RN} | while read RN; do
    SRC_FILE=${TMP_DIR}/mynet${RN}.xml;
    EMBED_STR=$(cat ${OUTPUT}/def_host_tag_${RN} | tr "\n" " ");
    TAR_FILE=${LVT_NET_DIR}/mynet${RN}.xml
    awk '{
      SRC_FILE="'"${SRC_FILE}"'"
      EMBED_STR="'"${EMBED_STR}"'"
      gsub("@",EMBED_STR);
      print;
    }' ${SRC_FILE} > ${LVT_NET_DIR}/mynet${RN}.xml
  done
}

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

_redefvm(){
  START_RN=$1
  END_RN=$2
  ( cd ${LVT_DIR} && \
    seq ${START_RN} ${END_RN} | while read RN;do
      virsh define vx_node${RN}.xml;
    done )
}

_buildnet 100 102
_initdir
_mkcmd "get_macaddr_cmd" "virsh dumpxml vx_node@ | grep \"mac address\" | awk 'match(\$0, /[a-f0-9]{2}(:[a-f0-9]{2}){5}/) {print substr(\$0, RSTART, RLENGTH)}'"
_mkcmd "get_nodename_cmd" "echo node@"
_callcmd 6
_join ${OUTPUT}/nodename ${OUTPUT}/macaddr ${OUTPUT}/vminfo
_grp 6 3
_split ${OUTPUT}/grp ${OUTPUT}/vminfo
_mk_def_ip_script_with_awk
_call_def_ip_script_with_awk 0 2
_kvm_guest_modify_network 100 102
_rebnet 100 102
_mkvxnm 1 6
_join ${OUTPUT}/grp ${OUTPUT}/vx_node ${OUTPUT}/vx_node_grp
_kvm_guest_modify_machine
_redefvm 1 6

あとがき

やりきった。。後で見直しだ。。ねる。。

Leave a Reply

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