labunix's blog

labunixのラボUnix

chroot-jessieにpython-nwdiagを導入、画像を使用する。

■chroot-jessieにpython-nwdiagを導入、画像を使用する。

 条件は3つ。wheezy上のchroot-jessieなら出来るよというお話。
 ただし、SVGが真っ白になるのはTrac1.0.2では解決出来なかったので回避手段を。。。

 ・画像は、wheezyでは対応していないが、jessieなら対応している。
 ・画像の名前にスペースは使えないので、画像のファイル名を「_」に変換。
 ・画像を使ったSVGでは真っ白になってしまうので、PNG、PDF形式を使用する。

 blockdiag/nwdiag/actdiagでA4縦、A4横に自動的にSVG/PDFを拡大、縮小出力する。
 http://labunix.hateblo.jp/entry/20131225/1387903263

 nwdiagで、ネットワーク構成を変更を検討する。
 http://labunix.hateblo.jp/entry/20131230/1388349360

■wheezyのnwdiagではバージョンが古くて、
 「blockdiagcontrib-*」形の追加は出来ない。

 NWが複雑になってくると、L3なのかL2なのかLinuxなのか。。。
 Windowsは無いにしても、NASなのかとか。。。

$ python --version 
Python 2.7.3

$ grep ^# /usr/bin/nwdiag | \
  awk '/.*nwdiag.*/ {print $3}' | \
  awk -F\, '{print $1}'
'nwdiag==0.7.0'

$ apt-cache show python-nwdiag | grep ^Ver
Version: 0.7.0-1

$ apt-cache show python-nwdiag | grep ^Depend | tr ',' '\n'
Depends: python2.7
 python (>= 2.7)
 python (<< 2.8)
 python-pkg-resources
 python-blockdiag (>= 1.1.0)
 python-funcparserlib (>= 0.3.5)
 python-imaging (>= 1.1.5)

■chroot環境にjessieを導入
 たまに自分でも良く分からないことをしてますね。。。

 Squeeze上のsid chrootにjessieのchroot環境を構築、Squeezeへ移動
 http://d.hatena.ne.jp/labunix/20130507

■ホスト側の準備
 プロキシとか必要なパッケージは導入済みとして飛ばします。

$ tail -n 9 /etc/schroot/schroot.conf
[jessie-root]
description=Debian jessie
directory=/var/local/jessie
users=root
[jessie]
description=Debian jessie
directory=/var/local/jessie
users=labunix

$ grep wheezy /etc/fstab | sed s/"wheezy"/"jessie"/g
none    /var/local/jessie/dev/pts       devpts  gid=5,mode=620  0       3
none    /var/local/jessie/dev/shm       tmpfs   defaults        0       3
none    /var/local/jessie/proc          proc    defaults        0       3
none    /var/local/jessie/sys           sysfs   defaults        0       3

■chroot-jessieの導入
 細かい説明は飛ばします。
 python-nwdiagの日本語サポートはUTF-8のみです。

$ su
# INSTALL=/var/local/jessie
# mkdir $INSTALL && env LANG=C debootstrap jessie $INSTALL http://ftp.debian.or.jp/debian && exit
$ INSTALL=/var/local/jessie
$ for opt in update "upgrade -y" autoremove autoclean;do
    sudo chroot $INSTALL apt-get $opt; \
  done
$ sudo chroot $INSTALL apt-get purge -y isc-dhcp* nano
$ sudo chroot $INSTALL apt-get install -y vim chkconfig locales less && sudo chroot $INSTALL dpkg-reconfigure locales
$ echo "chroot-jessie" | sudo tee $INSTALL/etc/debian_chroot > /dev/null

$ USER=labunix
$ sudo chroot $INSTALL useradd $USER && sudo chroot $INSTALL passwd $USER
$ sudo mkdir $INSTALL/home/$USER && sudo chown $USER:$USER $INSTALL/home/$USER

■プロキシの設定が必要な人だけ。

$ INSTALL=/var/local/jessie
$ ls /etc/profile.d/proxy.sh
/etc/profile.d/proxy.sh
$ sudo cp /etc/profile.d/proxy.sh $INSTALL/etc/profile.d/proxy.sh
$ sudo cp -b /etc/wgetrc $INSTALL/etc/profile.d/wgetrc
$ sudo cp -b /etc/apt/apt.conf $INSTALL/etc/apt/apt.conf

■python.*diag等の導入

$ INSTALL=/var/local/jessie
$ sudo chroot $INSTALL apt-get install -y wget curl w3m apt-file dnsutils
$ grep `hostname` /etc/hosts | sudo tee -a $INSTALL/etc/hosts >/dev/null
$ sudo chroot $INSTALL apt-file update

$ for opt in update "upgrade -y" autoremove autoclean;do
    sudo chroot $INSTALL apt-get $opt; \
  done
$ grep ^deb $INSTALL/etc/apt/sources.list
deb http://ftp.debian.or.jp/debian jessie main

$ sudo chroot $INSTALL apt-get install -y ^python-.*diag

■chroot環境にssh接続する設定

$ tail -5 /etc/rc.local
if [ -x /var/local/jessie/etc/rc.chroot-ssh ]; then
/usr/sbin/chroot /var/local/jessie /etc/rc.chroot-ssh restart
fi

exit 0

$ cat /var/local/wheezy/etc/rc.chroot-ssh
#!/bin/bash
export TZ=Asia/Tokyo
/etc/init.d/ssh restart
$ cat /var/local/wheezy/etc/rc.chroot-ssh
#!/bin/bash
export TZ=Asia/Tokyo
/etc/init.d/ssh restart

$ sudo cp /var/local/wheezy/etc/rc.chroot-ssh /var/local/jessie/etc/
$ sudo chmod +x /var/local/jessie/etc/rc.chroot-ssh
$ sudo chroot $INSTALL apt-get install -y openssh-server
$ sudo sed -i s/"^Port 22"/"Port 9022\nAddressFamily inet"/ /var/local/jessie/etc/ssh/sshd_config
$ sudo sed -i s/"#ListenAddress 0.0.0.0"/"ListenAddress 127.0.0.1"/ /var/local/jessie/etc/ssh/sshd_config
$ echo "UseDNS no" | sudo tee -a /var/local/jessie/etc/ssh/sshd_config >/dev/null
$ sudo /etc/rc.local
[ ok ] Restarting OpenBSD Secure Shell server: sshd.
[ ok ] Restarting OpenBSD Secure Shell server: sshd.
$ netstat -an | grep 9022 | awk '/127.0.0.1/ {print}'
tcp        0      0 127.0.0.1:9022          0.0.0.0:*               LISTEN

$ schroot -c jessie -p
(chroot-jessie)$ ssh-keygen -t rsa
(chroot-jessie)$ exit
$ sudo mount -a
$ ssh -x -p 9022 localhost
(chroot-jessie)$
(chroot-jessie)$ exit

■jessieのバージョンの確認

$ python --version
Python 2.7.8

$ grep ^# /usr/bin/nwdiag | \
  awk '/.*nwdiag.*/ {print $3}' | \
  awk -F\, '{print $1}'
'nwdiag==1.0.3'

$ apt-cache show python-nwdiag | grep ^Ver
Version: 1.0.3-3

$ apt-cache show python-nwdiag | grep ^Depend | tr ',' '\n'
Depends: python2.7
 python-blockdiag (>= 1.4.1)
 python-pkg-resources
 python
 python:any (>= 2.7.5-5~)
 python:any (<< 2.8)
 python-funcparserlib (>= 0.3.4)
 python-pil
 python-docutils
 python-reportlab

$ sudo schroot -c jessie-root -p
(chroot-jessie)# apt-get install -y python-setuptools python-imaging fonts-ipa*
(chroot-jessie)# easy_install pip
(chroot-jessie)# pip install --upgrade distribute
(chroot-jessie)# pip install pil
(chroot-jessie)# pip install blockdiagcontrib-cisco
(chroot-jessie)# pip install blockdiagcontrib-nationalflags
(chroot-jessie)# pip install blockdiagcontrib-square
(chroot-jessie)# pip install blockdiagcontrib-class
(chroot-jessie)# pip install trac
(chroot-jessie)# pip list | grep ^block
blockdiag (1.4.7)
blockdiagcontrib-cisco (0.1.8)
blockdiagcontrib-class (0.1.3)
blockdiagcontrib-nationalflags (0.1.6)
blockdiagcontrib-qb (0.1.3)
blockdiagcontrib-square (0.1.3)
(chroot-jessie)#  pip list | grep report
reportlab (3.1.8)
(chroot-jessie)#  pip list | grep -i trac
Trac (1.0.2)
(chroot-jessie)#  exit

■都度schrootするのは面倒なので、sudoを導入

$ INSTALL=/var/local/jessie
$ sudo chroot $INSTALL apt-get install -y sudo
$ sudo chroot $INSTALL usermod -a -G sudo labunix
$ sudo chroot $INSTALL id labunix | tr ' ' '\n' | tr ',' '\n' | grep sudo
27(sudo)
$ grep sudo $INSTALL/etc/group | awk -F\: '/sudo/ {print $NF}'
labunix

$ ssh -x -p 9022 localhost
$ sudo apt-get update
Password:

■rsyncでchroot-jessieの/homeディレクトリをssh経由で同期する場合

$ mkdir ~/jesie_home
$ sudo apt-get install -y rsync
$ sudo chroot /var/local/jessie/ apt-get install -y rsync
$ rsync -avz -e "ssh -p 9022" localhost:~/ ~/jessie_home

■やっぱり面倒なので、chroot側にmountする。

$ sudo rm -fr /var/local/jessie/home/labunix
$ mkdir jessie_home
$ grep labunix /etc/fstab
/home/labunix/jessie_home /var/local/jessie/home/labunix none bind 0 0
$ sudo mount -a

$ find jessie_home/ /var/local/jessie/home/labunix/ | grep "\.diag"
jessie_home/new.diag
jessie_home/sample.diag
/var/local/jessie/home/labunix/new.diag
/var/local/jessie/home/labunix/sample.diag

■サンプルのPDFとPNG出力
 ※SVGだと白抜きで表示されない。

$ cat new.diag 
nwdiag {
  a [shape = cisco.router];
  a -- c;
}
$ nwdiag -T svg new.diag
$ nwdiag -T png new.diag
$ nwdiag -T pdf new.diag

$ grep cisco jessie_home/new.svg | awk '/jpg/ {print $(NF-2)}'
xlink:href="/usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images/cisco/router.jpg"

■扱える画像と指定方法
 ※スペース付きだと「""」で囲っても扱えない様子。

$ find /usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images/ | \
  sed s%'/usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images/'%%g | \
  tr -d '.jpg' | tr '/' '.' | grep -v " " | wc -l
321

$ IMAGE_HOME=/usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images; \
  find /var/local/jessie/$IMAGE_HOME -type d -name "cisco" -exec ls {} \; | \
  grep -v " " | \
  wc -l
292

■例えば、以下のようにする。

$ IMAGE_HOME=/usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images; \
  sudo cp layer\ 2\ remote\ switch.jpg l2switch.jpg

$ cat new.diag 
nwdiag {
  a [shape = "cisco.router"];
  b [shape = "cisco.router_firewall"];
  c [shape = "cisco.l2switch"];
  a -- b -- c;
}

f:id:labunix:20141031002145p:plain

■上記では統一感がなくなるので、以下のようにスペースを_に変換したイメージでコピーする。

$ IMAGE_HOME=/usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images; \
  ls *" "* | while read list;do sudo cp "$list" `echo "$list" | tr ' ' '_'`;done

$ ls "www server.jpg" www_server.jpg
www server.jpg	www_server.jpg

■ciscoの画像を一覧で確認する。

$ IMAGE_HOME=/usr/local/lib/python2.7/dist-packages/blockdiagcontrib/images; \
  gthumb /var/local/jessie/$IMAGE_HOME/cisco/

■調整無しです。

$ cat mynw.diag 
diagram {
	class obj_old		[color = lighgray,style = dotted];
	class obj_new		[color = lightblue,style = dotted];
	class obj_null		[style = dotted,stacked];
	class obj_router	[shape = cisco.router];
	class obj_l2sw		[shape = cisco.layer_2_remote_switch];
 	class obj_fw		[shape = cisco.firewall];
	class obj_wlan		[shape = cisco.wireless_router];
	class obj_pc		[shape = cisco.pc];
	class obj_mobile	[shape = cisco.pda];

	network untrust {
		address = "X.X.X.0/28"

		main-router	[address = ".n",class = obj_router];
	}

	network dmz {
		address = "192.168.X.0/24"

		main-router	[address = ".n\ndmz/24 only",class = obj_router];
		debian-fw1	[address = ".n+16",class = obj_fw];
		debian-fw2	[address = ".m+17",class = obj_fw];
	}
	network trust {
		address = "172.X.X.0/24"

		VLAN-Switch	[address = ".n",class = obj_l2sw];
		debian-fw1	[address = ".n+16\n<->VLAN-Switch only",class = obj_fw];
		debian-fw2	[address = ".n+17\n<->VLAN-Switch only",class = obj_fw];
		nat-router	[address = ".n+64\n<->VLAN-Switch only",class = obj_router];
	}
	network wlan-seg {
		address = "10.X.X.0/24"

		nat-router	[address = ".n+64\n<->wlan-router's IP only",class = obj_router];
		wlan-router	[address = ".n\n<->sheeva-debian only\n<->note-debian only",class = obj_wlan];
		sheeva-debian	[address = ".n+16",class = obj_pc];
		note-debian	[address = ".n+17",class = obj_pc];
		iPod		[address = ".n+128",class = obj_mobile];
		Android		[address = ".n+129",class = obj_mobile];
	}
}

f:id:labunix:20141031011616p:plain