Subject: [PATCH] [BZ 116959] dbginfo.sh: Add option to specify directory for data collection
From: Wolfgang Taphorn <taphorn@de.ibm.com>

Description:  dbginfo.sh: Add option to specify directory for data collection
Symptom:      Data collection via dbginfo.sh is not possible.
Problem:      In some environments, the directory /tmp may not provide enough
              space to run the data collection via dbginfo.sh properly.
Solution:     Introducing an option '-d' for the user that allows to specify
              a directory, where the data collection takes place and where
              the final TAR archive is stored. The specified directory must
              already exist, it is not created during the script execution.
              If the parameter is not specified, the default directory /tmp 
              is used.
Reproduction: Run dbginfo.sh on a machine where the space in /tmp is very
              limited (e.g. small size or file system is filled up)
Upstream-ID:  -
Problem-ID:   116959

Signed-off-by: Wolfgang Taphorn <taphorn@de.ibm.com>
---
 scripts/dbginfo.sh   |  212 +++++++++++++++++++++++++++------------------------
 scripts/dbginfo.sh.1 |   12 ++
 2 files changed, 124 insertions(+), 100 deletions(-)

--- a/scripts/dbginfo.sh
+++ b/scripts/dbginfo.sh
@@ -26,15 +26,115 @@
 LC_ALL=C
 export LC_ALL
 
-# The kernel release version as delivered from uname -r
-readonly KERNEL_RELEASE_VERSION=$(uname -r 2>/dev/null)
+# The general name of this script
+readonly SCRIPTNAME="${0##*/}"
+
+
+########################################
+# print version info
+print_version() {
+    cat <<EOF
+${SCRIPTNAME}: Debug information script version %S390_TOOLS_VERSION%
+Copyright IBM Corp. 2002, 2014
+EOF
+}
+
+
+########################################
+# print how to use this script
+print_usage()
+{
+    print_version
+
+    cat <<EOF
+
+
+Usage: ${SCRIPTNAME} [OPTIONS]
+
+This script collects runtime, configuration and trace information about
+your Linux on System z installation for debugging purposes.
+
+It also traces information about z/VM if the Linux runs under z/VM.
+
+
+The collected information is written to a TAR archive named
+
+    /tmp/DBGINFO-[date]-[time]-[hostname]-[processorid].tgz
+
+where [date] and [time] are the date and time when debug data is collected.
+[hostname] indicates the hostname of the system the data was collected from.
+The [processorid] is taken from the processor 0 and indicates the processor
+identification.
+
+Options:
+
+        -d|--directory     specify the directory where the data collection
+                           stores the temporary data and the final archive.
+        -h|--help          print this help
+        -v|--version       print version information
+
+
+Please report bugs to: linux390@de.ibm.com
+
+EOF
+}
+
+######################################
+# Verification to run as root
+#
+if test "$(/usr/bin/id -u 2>/dev/null)" -ne 0; then
+    echo "${SCRIPTNAME}: Error: You must be user root to run \"${SCRIPTNAME}\"!"
+    exit 1
+fi
+
+#######################################
+# Parsing the command line
+#
+paramWORKDIR_BASE="/tmp/"
+
+while [ ${#} -gt 0 ]; do
+    case ${1} in
+        --help|-h)
+            print_usage
+            exit 0
+            ;;
+        --version|-v)
+            print_version
+            exit 0
+            ;;
+        --directory|-d)
+            paramWORKDIR_BASE=${2}
+            shift
+            ;;
+        -*|--*|*)
+            echo
+            echo "${SCRIPTNAME}: invalid option \"${1}\""
+            echo "Try '${SCRIPTNAME} --help' for more information"
+            echo
+            exit 1
+            ;;
+    esac
+    shift
+done
+
+if test -z "${paramWORKDIR_BASE}"; then
+    echo "${SCRIPTNAME}: Error: No directory specified for data collection!"
+    echo
+    exit 1
+fi
+if test ! -d "${paramWORKDIR_BASE}"; then
+    echo "${SCRIPTNAME}: Error: The specified directory \"${paramWORKDIR_BASE}\" does ont exist!"
+    echo
+    exit 1
+fi
+
 
 ########################################
 # Global used variables
 #
 
-# The general name of this script
-readonly SCRIPTNAME="${0##*/}"
+# The base working directory
+readonly WORKDIR_BASE="$(echo "${paramWORKDIR_BASE}" | sed -e 's#/$##')/"
 
 # The terminal
 readonly TERMINAL=$(tty 2>/dev/null)
@@ -42,6 +142,9 @@ readonly TERMINAL=$(tty 2>/dev/null)
 # The hostname of the system
 readonly SYSTEMHOSTNAME=$(hostname -s 2>/dev/null)
 
+# The kernel release version as delivered from uname -r
+readonly KERNEL_RELEASE_VERSION=$(uname -r 2>/dev/null)
+
 # The processor ID for the first processor
 readonly PROCESSORID=$(grep -E ".*processor 0:.*" /proc/cpuinfo | sed 's/.*identification[[:space:]]*\=[[:space:]]*\([[:alnum:]]*\).*/\1/g')
 
@@ -51,9 +154,6 @@ readonly PROCESSORVERSION=$(grep -E ".*p
 # The current date
 readonly DATETIME=$(date +%Y-%m-%d-%H-%M-%S 2>/dev/null)
 
-# The base working directory
-readonly WORKDIR_BASE="/tmp/"
-
 # The current working directory for the actual script execution
 if test -z "${PROCESSORID}"; then
     readonly WORKDIR_CURRENT="DBGINFO-${DATETIME}-${SYSTEMHOSTNAME:-localhost}"
@@ -70,6 +170,9 @@ readonly WORKARCHIVE="${WORKDIR_BASE}${W
 # The log file of activities from this script execution
 readonly LOGFILE="${WORKPATH}dbginfo.log"
 
+# The file to indicate that another instance of the script is already running
+readonly LOCKFILE="/tmp/${SCRIPTNAME}.lock"
+
 # File that includes output of Linux commands
 readonly OUTPUT_FILE_CMD="${WORKPATH}runtime.out"
 
@@ -625,54 +728,6 @@ call_collect_file() {
 
 
 ########################################
-# print version info
-print_version() {
-    cat <<EOF
-${SCRIPTNAME}: Debug information script version %S390_TOOLS_VERSION%
-Copyright IBM Corp. 2002, 2014
-EOF
-}
-
-
-########################################
-# print how to use this script
-print_usage()
-{
-    print_version
-
-    cat <<EOF
-
-
-Usage: ${SCRIPTNAME} [OPTIONS]
-
-This script collects runtime, configuration and trace information about
-your Linux on System z installation for debugging purposes.
-
-It also traces information about z/VM if the Linux runs under z/VM.
-
-
-The collected information is written to a TAR archive named
-
-    /tmp/DBGINFO-[date]-[time]-[hostname]-[processorid].tgz
-
-where [date] and [time] are the date and time when debug data is collected.
-[hostname] indicates the hostname of the system the data was collected from.
-The [processorid] is taken from the processor 0 and indicates the processor
-identification.
-
-Options:
-
-        -h|--help          print this help
-        -v|--version       print version information
-
-
-Please report bugs to: linux390@de.ibm.com
-
-EOF
-}
-
-
-########################################
 # print that an instance is already running
 print_alreadyrunning() {
     print_version
@@ -682,42 +737,12 @@ print_alreadyrunning() {
 
 Please check the system if another instance of '${SCRIPTNAME}' is already
 running. If this is not the case, please remove the lock file
-'${WORKDIR_BASE}${SCRIPTNAME}.lock'.
+'${LOCKFILE}'.
 EOF
 }
 
 
 ########################################
-#
-commandline_parse()
-{
-    local cmdline_arg1=${1}
-    local cmdline_count=${#}
-
-    if test "${cmdline_count}" -eq 1; then
-	if test "${cmdline_arg1}" = '-h' || test "${cmdline_arg1}" = '--help'; then
-	    print_usage
-	elif test "${cmdline_arg1}" = '-v' || test "${cmdline_arg1}" = '--version'; then
-	    print_version
-	else
-	    echo
-	    echo "${SCRIPTNAME}: invalid option \"${cmdline_arg1}\""
-	    echo "Try '${SCRIPTNAME} --help' for more information"
-	    echo
-	    exit 1
-	fi
-	exit 0
-    elif test "${cmdline_count}" -ge 1; then
-	echo
-	echo "${SCRIPTNAME}: Error: Invalid number of arguments!"
-	echo
-	print_usage
-	exit 1
-    fi
-}
-
-
-########################################
 # Setup the environment
 environment_setup()
 {
@@ -729,11 +754,12 @@ environment_setup()
 	exit 1
     fi
 
-    if test -e "${WORKDIR_BASE}${SCRIPTNAME}".lock; then
+    if test -e "${LOCKFILE}"; then
 	print_alreadyrunning
         exit 1
     else
-	touch "${WORKDIR_BASE}${SCRIPTNAME}".lock
+	touch "${LOCKFILE}"
+        echo "${DATETIME}" > "${LOCKFILE}"
     fi
 
     if ! mkdir "${WORKPATH}" 2>/dev/null; then
@@ -777,7 +803,7 @@ environment_cleanup()
 	pr_stdout "       Please remove the directory manually"
 	pr_stdout " "
     fi
-    if ! rm -f "${WORKDIR_BASE}${SCRIPTNAME}".lock 2>/dev/null; then
+    if ! rm -f "${LOCKFILE}" 2>/dev/null; then
 	pr_stdout " "
 	pr_stdout "${SCRIPTNAME}: Warning: Deletion of \"${WORKDIR_BASE}${SCRIPTNAME}\" failed"
 	pr_stdout "       Please remove the file manually"
@@ -832,14 +858,6 @@ pr_syslog_stdout()
 ###############################################################################
 # Running the script
 
-commandline_parse "${@}"
-
-# Verification to run as root
-if test "$(/usr/bin/id -u 2>/dev/null)" -ne 0; then
-    echo "${SCRIPTNAME}: Error: You must be user root to run \"${SCRIPTNAME}\"!"
-    exit 1
-fi
-
 environment_setup
 print_version
 
--- a/scripts/dbginfo.sh.1
+++ b/scripts/dbginfo.sh.1
@@ -6,6 +6,8 @@ for debugging Linux on System z
 
 .SH SYNOPSIS
 .br
+\fBdbginfo.sh\fP [OPTIONS]
+.br
 \fBdbginfo.sh\fP {\-h|\-v}
 
 .SH DESCRIPTION
@@ -28,6 +30,10 @@ Print usage information, then exit.
 \fB\-v\fP, \fB\-\-version\fP
 Print version information, then exit.
 
+.TP
+\fB\-d <DIRECTORY>\fP, \fB\-\-directory <DIRECTORY>\fP
+Specify the DIRECTORY where the data collection stores the temporary data and the final archive. The specified directory must already exist. If this parameter is not specified, /tmp is used by default.
+
 .SH FILES
 A .tgz file of the form
 .PP
@@ -37,12 +43,12 @@ A .tgz file of the form
 
 .fam T
 .fi
-is generated and placed in the /tmp directory.
+is generated and placed in the /tmp directory or in the directory specified by the -d option.
 
 .SH EXAMPLE
 Sample invocation:
 .P
-[root@host]# dbginfo.sh
+[root@host]# dbginfo.sh \-d /data\-collection
 .br
 dbginfo.sh: Debug information script version %S390_TOOLS_VERSION%
 .br
@@ -72,7 +78,7 @@ Finalizing: Creating archive with collec
 .PP
 Collected data was saved to:
 .br
- >>  /tmp/DBGINFO\-2013\-10\-08\-10\-43\-16\-host\-012345.tgz  <<
+ >>  /data\-collection/DBGINFO\-2013\-10\-08\-10\-43\-16\-host\-012345.tgz  <<
 .SH HINTS
 Run the script with root authority.
 .br
