Debuggen von BASH Skripten mit sh -x
BASH Skripte können sehr elegant mittels sh -x näher analysiert werden. Dieser Artikel zeigt ein konkretes Beispiel wie man mit Hilfe dieser Funktion einen Fehler eingrenzen kann.
Inhaltsverzeichnis |
Beispielfehler
In diesem Beispiel tritt der folgende Fehler bei der Konfiguration eines veth Devices in einer Virtuozzo Umgebung auf (die vollständige Fehlerbeschreibung und Lösung finden Sie in Virtuozzo CentOS VE mit veth Fehler: sysfs class device: Invalid argument):
[root@sr2500test-node1 ~]# vznetcfg br new bridgeEth2 networkEth2
[root@sr2500test-node1 ~]# vznetcfg br attach networkEth2 eth2
[root@sr2500test-node1 ~]# vzlist 104
VEID NPROC STATUS IP_ADDR HOSTNAME
104 11 running centos4test
[root@sr2500test-node1 ~]# vzctl exec 104 cat /etc/redhat-release
CentOS release 4.7 (Final)
[root@sr2500test-node1 ~]# vzctl set 104 --netif_add eth0 --save
Configure veth devices: veth104.0
Saved parameters for VE 104
[root@sr2500test-node1 ~]# vzctl set 104 --ifname eth0 --network networkEth2 --ipadd 10.20.1.101/255.255.255.0 --gw 10.20.1.1 --save
Configure veth devices:
Configure bridged network...
Error, some other host already uses address 10.20.1.101.
Shutting down interface eth0: [ OK ]
Shutting down interface venet0: [ OK ]
Shutting down loopback interface: [ OK ]
Setting network parameters: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0: sysfs class device: Invalid argument
Error, some other host already uses address 10.20.1.101.
[FAILED]
Bringing up interface venet0: [ OK ]
Saved parameters for VE 104
[root@sr2500test-node1 ~]#
Relevante Fehlerbereiche eingrenzen
Interessant sind in diesem Zusammenhang konkret folgende Fehlermeldungen:
- Error, some other host already uses address 10.20.1.101
- Bringing up interface eth0: sysfs class device: Invalid argument
Weitere Analyse: Teil 1
Da es sich um ein VE mit CentOS 4 handelt, werfen wir einen Blick in die Konfigurationsdatei für CentOS 4 Gastsysteme in Virtuozzo:
[root@sr2500test-node1 ~]# cat /etc/vz/dists/centos-4.conf # This configuration file is meant to be used with # the CentOS distribution kit. # # Copyright (C) 2001-2005 by SWsoft # All rights reserved. ADD_IP=redhat-add_ip.sh DEL_IP=redhat-del_ip.sh NETIF_ADD=redhat-add_netif.sh NETIF_DEL=redhat-del_netif.sh SET_HOSTNAME=redhat-set_hostname.sh SET_DNS=set_dns.sh SET_USERPASS=set_userpass.sh SET_UGID_QUOTA=set_ugid_quota.sh POST_CREATE=postcreate.sh POST_MIGRATE=redhat-post_migrate.sh GET_V2PMIGRATE_EXCLUDES=redhat-v2pmigrate-excludes.sh [root@sr2500test-node1 ~]#
Nachdem der Fehler in Zusammenhand mit netif_add aufgetreten wird, gilt der nächste Blick der Datei redhat-add_netif.sh. Diese hat 256 Zeilen und wir finden dort zunächst keinen direkten Hinweis.
Weitere Analyse: Teil 2
Wir entfernen für die weitere Analyse das Device nochmal und führen die entsprechenden Kommandos erneut aus:
[root@sr2500test-node1 ~]# vzctl set 104 --netif_del eth0 --save Deleting veth devices: veth104.0 Saved parameters for VE 104 [root@sr2500test-node1 ~]# vzctl set 104 --netif_add eth0 --save Configure veth devices: veth104.0 Saved parameters for VE 104 [root@sr2500test-node1 ~]# vzctl set 104 --ifname eth0 --network networkEth2 --ipadd 10.20.1.101/255.255.255.0 --gw 10.20.1.1 --save [...]
Gleichzeitig verfolgen wir in einer weiteren Console innerhalb des VEs die Informationen in /var/log/messages:
[root@sr2500test-node1 ~]# vzctl enter 104 entered into VE 104 -bash-3.00# tail -n 0 -f /var/log/messages May 29 13:28:48 centos4test network: Shutting down interface eth0: succeeded May 29 13:28:49 centos4test network: Shutting down interface venet0: succeeded May 29 13:28:50 centos4test network: Shutting down loopback interface: succeeded May 29 13:28:50 centos4test sysctl: net.ipv4.ip_forward = 0 May 29 13:28:50 centos4test network: Setting network parameters: succeeded May 29 13:28:50 centos4test network: Bringing up loopback interface: succeeded May 29 13:28:50 centos4test ifup: sysfs class device: Invalid argument May 29 13:28:50 centos4test ifup: Error, some other host already uses address 10.20.1.101. May 29 13:28:50 centos4test network: Bringing up interface eth0: failed May 29 13:28:51 centos4test ifup: RTNETLINK answers: File exists May 29 13:28:51 centos4test ifup: RTNETLINK answers: File exists May 29 13:28:51 centos4test network: Bringing up interface venet0: succeeded
Das Problem scheint also mit dem ifup BASH Skript zusammenzuhängen.
Weitere Analyse: Teil 3
Nun hilft uns sh -x - und zwar die Einträge ganz am Ende unten:
-bash-3.00# sh -x /sbin/ifup eth0 + . /etc/init.d/functions ++ TEXTDOMAIN=initscripts ++ umask 022 ++ PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin ++ export PATH ++ '[' -z '' ']' ++ COLUMNS=80 ++ '[' -z '' ']' +++ /sbin/consoletype ++ CONSOLETYPE=pty ++ '[' -f /etc/sysconfig/i18n -a -z '' ']' ++ . /etc/sysconfig/i18n +++ LANG=C ++ '[' pty '!=' pty ']' ++ '[' -n '' ']' ++ export LANG ++ '[' -z '' ']' ++ '[' -f /etc/sysconfig/init ']' ++ . /etc/sysconfig/init +++ BOOTUP=color +++ GRAPHICAL=yes +++ RES_COL=60 +++ MOVE_TO_COL='echo -en \033[60G' +++ SETCOLOR_SUCCESS='echo -en \033[0;32m' +++ SETCOLOR_FAILURE='echo -en \033[0;31m' +++ SETCOLOR_WARNING='echo -en \033[0;33m' +++ SETCOLOR_NORMAL='echo -en \033[0;39m' +++ LOGLEVEL=3 +++ PROMPT=no ++ '[' pty = serial ']' ++ '[' color '!=' verbose ']' ++ INITLOG_ARGS=-q + cd /etc/sysconfig/network-scripts + . network-functions ++ PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin ++ export PATH + '[' -f ../network ']' + . ../network ++ NETWORKING=yes ++ GATEWAY=191.255.255.1 ++ HOSTNAME=centos4test + CONFIG=eth0 + '[' -z eth0 ']' + need_config eth0 + CONFIG=ifcfg-eth0 + '[' -f ifcfg-eth0 ']' + return + '[' -f ifcfg-eth0 ']' + '[' 0 '!=' 0 ']' + source_config ++ basename ifcfg-eth0 ++ sed 's/^ifcfg-//g' + DEVNAME=eth0 + basename ifcfg-eth0 + grep -q '[^g]-' + . ifcfg-eth0 ++ DEVICE=eth0 ++ ONBOOT=yes ++ BOOTPROTO=static ++ IPADDR=10.20.1.101 ++ NETMASK=255.255.255.0 ++ GATEWAY=10.20.1.1 + '[' -r keys-eth0 ']' + case "$TYPE" in + '[' -z '' ']' ++ echo eth0 ++ sed 's/[0-9]*$//' + DEVICETYPE=eth + '[' -z '' -a -n '' ']' + '[' -z '' ']' + REALDEVICE=eth0 + '[' eth0 '!=' eth0 ']' + ISALIAS=no + '[' -n '' ']' + '[' -n '' ']' + '[' foo = fooboot ']' + '[' -n '' ']' + '[' -n '' -a '' = Bridge ']' + '[' -x /sbin/vconfig -a '' = yes -a no = no ']' + '[' '' = yes ']' + '[' static = bootp -o static = dhcp ']' + '[' -x /sbin/ifup-pre-local ']' + OTHERSCRIPT=/etc/sysconfig/network-scripts/ifup-eth + '[' -x /etc/sysconfig/network-scripts/ifup-eth ']' + is_available eth0 + LC_ALL= + LANG= + ip -o link + grep -q eth0 + '[' 0 = 1 ']' + return 0 + '[' -n '' ']' + '[' '' = Bridge ']' + '[' -n '' -a -x /usr/sbin/brctl ']' + is_available eth0 + LC_ALL= + LANG= + ip -o link + grep -q eth0 + '[' 0 = 1 ']' + return 0 + is_wireless_device eth0 + '[' -x /sbin/iwconfig ']' + return 1 + '[' -n '' ']' + '[' '' = yes -a no = no -a '' '!=' '' -a -x /sbin/ifenslave ']' + '[' no = no ']' + '[' '' = Bonding ']' + ethtool -i eth0 + grep -q 'driver: bonding' + '[' -n '' ']' + FWACTIVE= + iptables -L -n + LC_ALL=C + grep -q RH-Lokkit-0-50-INPUT + modprobe -r iptable_filter + '[' -w /etc/dhclient-eth0.conf ']' + '[' -n '' ']' + '[' -z 10.20.1.101 ']' + expand_config + '[' -z 255.255.255.0 ']' + '[' -z '' ']' ++ /bin/ipcalc --prefix 10.20.1.101 255.255.255.0 + eval PREFIX=24 ++ PREFIX=24 + '[' -z '' ']' ++ /bin/ipcalc --broadcast 10.20.1.101 255.255.255.0 + eval BROADCAST=10.20.1.255 ++ BROADCAST=10.20.1.255 + '[' -z '' ']' ++ /bin/ipcalc --network 10.20.1.101 255.255.255.0 + eval NETWORK=10.20.1.0 ++ NETWORK=10.20.1.0 + '[' -n '' ']' + ip link set dev eth0 up + '[' -n '' ']' + '[' -n '' ']' + arping -q -c 2 -w 3 -D -I eth0 10.20.1.101 sysfs class device: Invalid argument + echo 'Error, some other host already uses address 10.20.1.101.' Error, some other host already uses address 10.20.1.101. + exit 1 -bash-3.00#
Lösung
Hier ist zu sehen dass die ursprüngliche Fehlermeldung vom arping Kommando verursacht wird:
+ arping -q -c 2 -w 3 -D -I eth0 10.20.1.101 sysfs class device: Invalid argument + echo 'Error, some other host already uses address 10.20.1.101.' Error, some other host already uses address 10.20.1.101. + exit 1
Die weitere Recherche hat ergeben, dass in arping der Support für Infiband ergänzt wurde, wo wiederrum der Zugriff auf das sysfs notwendig wird. Die Lösung ist daher im konkreten Fall die sysfs Virtualisierung zu aktivieren wie in Virtuozzo CentOS VE mit veth Fehler: sysfs class device: Invalid argument beschrieben.