Subject: [PATCH] [BZ 137087] dbginfo.sh: Add collection of domain xml files.
From: Sa Liu <saliu@de.ibm.com>

Description:  dbginfo.sh: Add collection of domain xml files.
Symptom:      dbginfo.sh does not collect domain xml files for transient and 
              running domains.  
Problem:      Domain xml files of running domains are not collected.
Solution:     Add collection of domain xml files for running domains.
              Replace plain text passwords in xml files to asterisks.
              Rearrange step numbers of the whole collections. 
Reproduction: Run this script and verify the output
Upstream-ID:  -
Problem-ID:   137087 

Signed-off-by: Sa Liu <saliu@de.ibm.com>
---
 scripts/dbginfo.sh   |   55 ++++++++++++++++++++++++++++++++++++++++++---------
 scripts/dbginfo.sh.1 |   22 ++++++++++++--------
 2 files changed, 59 insertions(+), 18 deletions(-)

--- a/scripts/dbginfo.sh
+++ b/scripts/dbginfo.sh
@@ -18,7 +18,7 @@ readonly SCRIPTNAME="${0##*/}"
 print_version() {
     cat <<EOF
 ${SCRIPTNAME}: Debug information script version %S390_TOOLS_VERSION%
-Copyright IBM Corp. 2002, 2015
+Copyright IBM Corp. 2002, 2016
 EOF
 }
 
@@ -174,11 +174,14 @@ readonly OUTPUT_FILE_JOURNALCTL="${WORKP
 # File that includes the output of OpenVSwitch
 readonly OUTPUT_FILE_OVS="${WORKPATH}openvswitch"
 
+# File that includes the KVM domain xml file
+readonly OUTPUT_FILE_XML="${WORKPATH}domain_xml"
+
 # Mount point of the debug file system
 readonly MOUNT_POINT_DEBUGFS="/sys/kernel/debug"
 
 # The amount of steps running the whole collections
-readonly COLLECTION_COUNT=8
+readonly COLLECTION_COUNT=10
 
 # The kernel version (e.g. '2' from 2.6.32 or '3' from 3.2.1)
 readonly KERNEL_VERSION=$(uname -r 2>/dev/null | cut -d'.' -f1)
@@ -541,9 +544,9 @@ collect_vmcmdsout() {
 	    pr_log_stdout " "
 	    return 1
 	fi
-	VMUSERID=$(${cp_command} q userid 2>/dev/null | sed -ne 's/^\([^[:space:]]*\).*$/\1/p')
+	vm_userid=$(${cp_command} q userid 2>/dev/null | sed -ne 's/^\([^[:space:]]*\).*$/\1/p')
 
-	vm_cmds=$(echo "${VM_CMDS}" | sed "s/VMUSERID/${VMUSERID}/g")
+	vm_cmds=$(echo "${VM_CMDS}" | sed "s/VMUSERID/${vm_userid}/g")
 
 	IFS=:
 	for vm_command in ${vm_cmds}; do
@@ -593,7 +596,6 @@ collect_sysfs() {
     local debugfs_mounted=0
     local dir_name
     local file_name
-    local rc_mount
 
     # Requires kernel version newer then 2.4
     if test "${LINUX_SUPPORT_SYSFS}" -eq 0; then
@@ -697,7 +699,7 @@ collect_ovs() {
                   :ovs-vsctl -t 5 show\
                   :ovsdb-client dump\
                   "
-    local OVSBRCMDS
+    local ovsbrcmds
 
     if test -n "${br_list}"; then
         pr_syslog_stdout "8 of ${COLLECTION_COUNT}: Collecting OpenVSwitch output"
@@ -708,12 +710,12 @@ collect_ovs() {
         IFS="${ifs_orig}"
 
         while read -r bridge; do
-        OVSBRCMDS="ovs-ofctl show ${bridge}\
+        ovsbrcmds="ovs-ofctl show ${bridge}\
                     :ovs-ofctl dump-flows ${bridge}\
                     :ovs-appctl fdb/show ${bridge}\
                     "
          IFS=:
-          for ovsbrcmd in ${OVSBRCMDS}; do
+          for ovsbrcmd in ${ovsbrcmds}; do
             IFS=${ifs_orig} call_run_command "${ovsbrcmd}" "${OUTPUT_FILE_OVS}.out"
           done
          IFS="${ifs_orig}"
@@ -726,13 +728,30 @@ collect_ovs() {
 }
 
 ########################################
+collect_domain_xml() {
+    local domain_list=$(virsh list --all --name)
+    local domain
+
+    if test -n "${domain_list}"; then
+        pr_syslog_stdout "9 of ${COLLECTION_COUNT}: Collecting domain xml files"
+	  for domain in ${domain_list}; do
+	    call_run_command "virsh dumpxml ${domain}" "${OUTPUT_FILE_XML}_${domain}.xml"
+          done
+    else
+        pr_syslog_stdout "9 of ${COLLECTION_COUNT}: Collecting domain xml files skipped"
+    fi
+
+    pr_log_stdout " "
+}
+
+########################################
 post_processing() {
     local file_mtime
     local file_mtime_epoche
     local tmp_file
     local file_name
 
-    pr_syslog_stdout "8 of ${COLLECTION_COUNT}: Postprocessing"
+    pr_syslog_stdout "10 of ${COLLECTION_COUNT}: Postprocessing"
 
     find "${WORKPATH}etc/libvirt/qemu/" -maxdepth 1 -name "*.xml" 2>/dev/null | while IFS= read -r file_name; do
 	file_mtime_epoche=$(stat --format=%Y "${file_name}")
@@ -764,6 +783,22 @@ post_processing() {
         touch --time=mtime -t "${file_mtime}" "${file_name}"
     done
 
+    find "${WORKPATH}" -maxdepth 1 -name "*.xml" 2>/dev/null | while IFS= read -r file_name; do
+        file_mtime_epoche=$(stat --format=%Y "${file_name}")
+        file_mtime=$(date +%Y%m%d%H%M.%S --date="@${file_mtime_epoche}")
+        tmp_file=${file_name}.$$
+
+        echo " ${file_name}"
+        if ! sed "s/\( \+passwd='\).*\('\)/\1********\2/g" "${file_name}" > "${tmp_file}"; then
+            echo "${SCRIPTNAME}: Warning: Postprocessing failed on ${file_name}"
+            echo
+        fi
+
+        mv "${tmp_file}" "${file_name}"
+        touch --time=mtime -t "${file_mtime}" "${file_name}"
+    done
+
+
     pr_log_stdout " "
 }
 
@@ -992,6 +1027,8 @@ collect_osaoat
 
 collect_ovs
 
+collect_domain_xml
+
 post_processing
 
 create_package
--- a/scripts/dbginfo.sh.1
+++ b/scripts/dbginfo.sh.1
@@ -52,7 +52,7 @@ Sample invocation:
 .br
 dbginfo.sh: Debug information script version %S390_TOOLS_VERSION%
 .br
-Copyright IBM Corp. 2002, 2015
+Copyright IBM Corp. 2002, 2016
 .PP
 Hardware platform     = s390x
 .br
@@ -60,21 +60,25 @@ Kernel version        = <kernel\-version
 .br
 Runtime environment   = z/VM
 .PP
-1 of 8: Collecting command output
+1 of 10: Collecting command output
 .PP
-2 of 8: Collecting z/VM command output
+2 of 10: Collecting z/VM command output
 .PP
-3 of 8: Collecting procfs
+3 of 10: Collecting procfs
 .PP
-4 of 8: Collecting sysfs
+4 of 10: Collecting sysfs
 .PP
-5 of 8: Collecting log files
+5 of 10: Collecting log files
 .PP
-6 of 8: Collecting config files
+6 of 10: Collecting config files
 .PP
-7 of 8: Collecting osa oat output skipped \- not available
+7 of 10: Collecting osa oat output skipped \- not available
 .PP
-8 of 8: Postprocessing
+8 of 10: Collecting OpenVSwitch output
+.PP
+9 of 10: Collecting domain xml files
+.PP
+10 of 10: Postprocessing
 .PP
 Finalizing: Creating archive with collected data
 .PP
