xref: /aosp_15_r20/external/ltp/testcases/kdump/runkdump.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/sh -xe
2*49cdfc7eSAndroid Build Coastguard Worker
3*49cdfc7eSAndroid Build Coastguard WorkerSetupCrontab ()
4*49cdfc7eSAndroid Build Coastguard Worker{
5*49cdfc7eSAndroid Build Coastguard Worker    echo "Setup crontab."
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker    set +e
8*49cdfc7eSAndroid Build Coastguard Worker    crontab -r
9*49cdfc7eSAndroid Build Coastguard Worker    set -e
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker    # crontab in some distros will not read from STDIN.
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker    cat <<EOF >kdump.cron
14*49cdfc7eSAndroid Build Coastguard WorkerSHELL=/bin/sh
15*49cdfc7eSAndroid Build Coastguard WorkerPATH=/usr/bin:/usr/sbin:/sbin:/bin
16*49cdfc7eSAndroid Build Coastguard WorkerMAILTO=root
17*49cdfc7eSAndroid Build Coastguard Worker@reboot cd "$(pwd)"; cd ..; ${0} >>/tmp/kdump-$(date +%F-%T).log 2>&1
18*49cdfc7eSAndroid Build Coastguard WorkerEOF
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker    crontab kdump.cron
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker    echo "Enable cron daemon by default."
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker    if [ -f /etc/init.d/crond ]; then
25*49cdfc7eSAndroid Build Coastguard Worker        cron=crond
26*49cdfc7eSAndroid Build Coastguard Worker    else
27*49cdfc7eSAndroid Build Coastguard Worker        # SUSE
28*49cdfc7eSAndroid Build Coastguard Worker        cron=cron
29*49cdfc7eSAndroid Build Coastguard Worker    fi
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker    # Red Hat and SUSE.
32*49cdfc7eSAndroid Build Coastguard Worker    if [ -x "/sbin/chkconfig" ]; then
33*49cdfc7eSAndroid Build Coastguard Worker        /sbin/chkconfig "${cron}" on
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker    # Debian and Ubuntu.
36*49cdfc7eSAndroid Build Coastguard Worker    elif [ -x "/sbin/update-rc.d" ]; then
37*49cdfc7eSAndroid Build Coastguard Worker        /sbin/update-rc.d "${cron}" defaults
38*49cdfc7eSAndroid Build Coastguard Worker    fi
39*49cdfc7eSAndroid Build Coastguard Worker}
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard WorkerSetupKdump ()
42*49cdfc7eSAndroid Build Coastguard Worker{
43*49cdfc7eSAndroid Build Coastguard Worker    echo "Start kdump daemon."
44*49cdfc7eSAndroid Build Coastguard Worker    /etc/init.d/kdump restart
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker    echo "Enable kdump daemon by default."
47*49cdfc7eSAndroid Build Coastguard Worker    # Red Hat and SUSE.
48*49cdfc7eSAndroid Build Coastguard Worker    if [ -x "/sbin/chkconfig" ]; then
49*49cdfc7eSAndroid Build Coastguard Worker        /sbin/chkconfig kdump on
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker    # Debian and Ubuntu.
52*49cdfc7eSAndroid Build Coastguard Worker    elif [ -x "/sbin/update-rc.d" ]; then
53*49cdfc7eSAndroid Build Coastguard Worker        /sbin/update-rc.d kdump defaults
54*49cdfc7eSAndroid Build Coastguard Worker    fi
55*49cdfc7eSAndroid Build Coastguard Worker}
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard WorkerPrepareVerify ()
59*49cdfc7eSAndroid Build Coastguard Worker{
60*49cdfc7eSAndroid Build Coastguard Worker    if [ "${last}" = "KLEXT" ]; then
61*49cdfc7eSAndroid Build Coastguard Worker        # If not mountable, skip it, and continue doing the test.
62*49cdfc7eSAndroid Build Coastguard Worker        set +e
63*49cdfc7eSAndroid Build Coastguard Worker        mount "${EXT3_PART}" /mnt
64*49cdfc7eSAndroid Build Coastguard Worker        set -e
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker        COREDIR=/mnt"${COREDIR}"
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker    elif [ "${last}" = "KLLBL" ]; then
69*49cdfc7eSAndroid Build Coastguard Worker        # If not mountable, skip it, and continue doing the test.
70*49cdfc7eSAndroid Build Coastguard Worker        set +e
71*49cdfc7eSAndroid Build Coastguard Worker        mount -L "${EXT3_LABEL}" /mnt
72*49cdfc7eSAndroid Build Coastguard Worker        set -e
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker        COREDIR=/mnt"${COREDIR}"
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker    elif [ "${last}" = "KLUID" ]; then
77*49cdfc7eSAndroid Build Coastguard Worker        # If not mountable, skip it, and continue doing the test.
78*49cdfc7eSAndroid Build Coastguard Worker        set +e
79*49cdfc7eSAndroid Build Coastguard Worker        mount "/dev/disk/by-uuid/${EXT3_UID}" /mnt
80*49cdfc7eSAndroid Build Coastguard Worker        set -e
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker        COREDIR=/mnt"${COREDIR}"
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker    elif [ "${last}" = "KLRAW" ]; then
85*49cdfc7eSAndroid Build Coastguard Worker        mkdir -p "${COREDIR}/${last}"
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker        # If not dumpable, skip it, and continue doing the test.
88*49cdfc7eSAndroid Build Coastguard Worker        set +e
89*49cdfc7eSAndroid Build Coastguard Worker        dd if="${RAW_PART}" of="${COREDIR}/${last}/vmcore" bs=1024
90*49cdfc7eSAndroid Build Coastguard Worker        set -e
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker    elif [ "${last}" = "KNSCP" ]; then
93*49cdfc7eSAndroid Build Coastguard Worker        if [ -z "${SCP_PATH}" ]; then
94*49cdfc7eSAndroid Build Coastguard Worker            echo "Fail: network destination not defined."
95*49cdfc7eSAndroid Build Coastguard Worker            exit 1
96*49cdfc7eSAndroid Build Coastguard Worker        fi
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker        file=$(ssh -i ~/.ssh/kdump_id_rsa "${SCP_PATH}" "ls -t ${COREDIR}/*/vmcore* \
99*49cdfc7eSAndroid Build Coastguard Worker         2>/dev/null | head -1")
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker        mkdir -p "${COREDIR}/${last}"
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker        if [ "${file}" ]; then
104*49cdfc7eSAndroid Build Coastguard Worker            # Not fatal error.
105*49cdfc7eSAndroid Build Coastguard Worker            set +e
106*49cdfc7eSAndroid Build Coastguard Worker            scp  -i ~/.ssh/kdump_id_rsa "${SCP_PATH}:${file}" "${COREDIR}/${last}"
107*49cdfc7eSAndroid Build Coastguard Worker            set -e
108*49cdfc7eSAndroid Build Coastguard Worker        fi
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker    elif [ "${last}" = "KNNFS" ]; then
111*49cdfc7eSAndroid Build Coastguard Worker        # Not fatal error.
112*49cdfc7eSAndroid Build Coastguard Worker        set +e
113*49cdfc7eSAndroid Build Coastguard Worker        mount "${NFS_PATH}" /mnt
114*49cdfc7eSAndroid Build Coastguard Worker        set -e
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker        COREDIR=/mnt"${COREDIR}"
117*49cdfc7eSAndroid Build Coastguard Worker    fi
118*49cdfc7eSAndroid Build Coastguard Worker
119*49cdfc7eSAndroid Build Coastguard Worker    vmcore=$(ls -t "${COREDIR}"/*/vmcore* 2>/dev/null | head -1)
120*49cdfc7eSAndroid Build Coastguard Worker}
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard WorkerVerifyTest ()
123*49cdfc7eSAndroid Build Coastguard Worker{
124*49cdfc7eSAndroid Build Coastguard Worker    # Should not be here.
125*49cdfc7eSAndroid Build Coastguard Worker    if [ -z "${last}" ]; then
126*49cdfc7eSAndroid Build Coastguard Worker        echo "Should not be here!"
127*49cdfc7eSAndroid Build Coastguard Worker	echo "There must be something wrong with the test setup."
128*49cdfc7eSAndroid Build Coastguard Worker	exit 1
129*49cdfc7eSAndroid Build Coastguard Worker    fi
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker    echo "Verifying the result of previous test ${last}."
132*49cdfc7eSAndroid Build Coastguard Worker    ldir=$(ls -td "../${log}/$(hostname)."* | head -1)
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker    if [ -f "${vmcore}" ]; then
135*49cdfc7eSAndroid Build Coastguard Worker        echo "$(date +%F-%T): verification of test ${last} passed." \
136*49cdfc7eSAndroid Build Coastguard Worker        >>"${ldir}/status"
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker        ./verify.sh "../${conf}" "${vmcore}" "${CRASH}" \
139*49cdfc7eSAndroid Build Coastguard Worker        >>"${ldir}/${ITERATION}.${last}.$(date +%F-%T)"
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker        # Be careful to define COREDIR.
142*49cdfc7eSAndroid Build Coastguard Worker        rm -rf "${COREDIR}"/*
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker    else
145*49cdfc7eSAndroid Build Coastguard Worker        echo "$(date +%F-%T): verification of test ${last} failed:\
146*49cdfc7eSAndroid Build Coastguard Worker vmcore NOT FOUND." >>"${ldir}/status"
147*49cdfc7eSAndroid Build Coastguard Worker        echo "vmcore NOT FOUND." \
148*49cdfc7eSAndroid Build Coastguard Worker        >>"${ldir}/${ITERATION}.${last}.$(date +%F-%T)"
149*49cdfc7eSAndroid Build Coastguard Worker    fi
150*49cdfc7eSAndroid Build Coastguard Worker}
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard WorkerRunTest ()
153*49cdfc7eSAndroid Build Coastguard Worker{
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker    sed -i "s/\(^REBOOT\)=.*/\1=$((count + 1))/" \
156*49cdfc7eSAndroid Build Coastguard Worker     "../${conf}"
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker    echo "Running current test ${i}."
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker    echo "$(date +%F-%T): running current test ${i}." \
161*49cdfc7eSAndroid Build Coastguard Worker    >> "${ldir}/status"
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker    # Save STDIO buffers.
164*49cdfc7eSAndroid Build Coastguard Worker    sync
165*49cdfc7eSAndroid Build Coastguard Worker    ./test.sh "../${conf}" "${i}" "../${log}"
166*49cdfc7eSAndroid Build Coastguard Worker}
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker# Start test.
169*49cdfc7eSAndroid Build Coastguard Workerconf="./runkdump.conf"
170*49cdfc7eSAndroid Build Coastguard Workerlib="lib"
171*49cdfc7eSAndroid Build Coastguard Workerlog="log"
172*49cdfc7eSAndroid Build Coastguard Worker
173*49cdfc7eSAndroid Build Coastguard Worker# Read test configuration file.
174*49cdfc7eSAndroid Build Coastguard Worker. "${conf}"
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker# Check mandatory variables.
177*49cdfc7eSAndroid Build Coastguard Workerif [ -z "${ITERATION}" ] || [ -z "${REBOOT}" ] || [ -z "${COREDIR}" ]
178*49cdfc7eSAndroid Build Coastguard Workerthen
179*49cdfc7eSAndroid Build Coastguard Worker    echo "Fail: some mandatory variables are missing from\
180*49cdfc7eSAndroid Build Coastguard Worker configuration file."
181*49cdfc7eSAndroid Build Coastguard Worker    exit 1
182*49cdfc7eSAndroid Build Coastguard Workerfi
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Workercd "${lib}"
185*49cdfc7eSAndroid Build Coastguard Worker
186*49cdfc7eSAndroid Build Coastguard Workerwhile [ "${ITERATION}" -ge 1 ]; do
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker# Reboot the machine first to take advantage of boot parameter
189*49cdfc7eSAndroid Build Coastguard Worker# changes.
190*49cdfc7eSAndroid Build Coastguard Workerif [ -z "${REBOOT}" ] || [ "${REBOOT}" -eq 0 ]; then
191*49cdfc7eSAndroid Build Coastguard Worker    echo "Setup test environment."
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker    SetupCrontab
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker    ./setup.sh "../${conf}"
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard Worker    sed -i 's/\(^REBOOT\)=.*/\1=1/' "../${conf}"
198*49cdfc7eSAndroid Build Coastguard Worker
199*49cdfc7eSAndroid Build Coastguard Worker    echo "System is going to reboot."
200*49cdfc7eSAndroid Build Coastguard Worker    /sbin/shutdown -r now
201*49cdfc7eSAndroid Build Coastguard Worker    sleep 60
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Workerelse
204*49cdfc7eSAndroid Build Coastguard Worker    count=1
205*49cdfc7eSAndroid Build Coastguard Worker
206*49cdfc7eSAndroid Build Coastguard Worker    for i in ${CRASHER} ${BASIC_LKDTM} ${EXTRA_LKDTM} ${EXTRA_DUMP} \
207*49cdfc7eSAndroid Build Coastguard Worker             END; do
208*49cdfc7eSAndroid Build Coastguard Worker
209*49cdfc7eSAndroid Build Coastguard Worker        if [ "${count}" -eq "${REBOOT}" ]; then
210*49cdfc7eSAndroid Build Coastguard Worker            # Wait for machine fully booted.
211*49cdfc7eSAndroid Build Coastguard Worker            sleep 60
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker            # First Test.
214*49cdfc7eSAndroid Build Coastguard Worker            if [ "${REBOOT}" -eq 1 ]; then
215*49cdfc7eSAndroid Build Coastguard Worker                echo "First test..."
216*49cdfc7eSAndroid Build Coastguard Worker                echo "Verify Boot Loader."
217*49cdfc7eSAndroid Build Coastguard Worker                if ! grep 'crashkernel=' /proc/cmdline; then
218*49cdfc7eSAndroid Build Coastguard Worker                    echo "Fail: error changing Boot Loader, no crashkernel=."
219*49cdfc7eSAndroid Build Coastguard Worker                    exit 1
220*49cdfc7eSAndroid Build Coastguard Worker                fi
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker                SetupKdump
223*49cdfc7eSAndroid Build Coastguard Worker
224*49cdfc7eSAndroid Build Coastguard Worker                # Creat log directory.
225*49cdfc7eSAndroid Build Coastguard Worker                mkdir -p "../${log}/$(hostname).$(date +%F-%T)"
226*49cdfc7eSAndroid Build Coastguard Worker
227*49cdfc7eSAndroid Build Coastguard Worker                echo "Gather system information."
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker                ldir=$(ls -td "../${log}/$(hostname)."* | head -1)
230*49cdfc7eSAndroid Build Coastguard Worker                ./sysinfo.sh >"${ldir}/system.info"
231*49cdfc7eSAndroid Build Coastguard Worker
232*49cdfc7eSAndroid Build Coastguard Worker            else
233*49cdfc7eSAndroid Build Coastguard Worker                PrepareVerify
234*49cdfc7eSAndroid Build Coastguard Worker
235*49cdfc7eSAndroid Build Coastguard Worker                VerifyTest
236*49cdfc7eSAndroid Build Coastguard Worker
237*49cdfc7eSAndroid Build Coastguard Worker		if [ "${i}" = END ]; then
238*49cdfc7eSAndroid Build Coastguard Worker		    # We are done.
239*49cdfc7eSAndroid Build Coastguard Worker		    break
240*49cdfc7eSAndroid Build Coastguard Worker		fi
241*49cdfc7eSAndroid Build Coastguard Worker
242*49cdfc7eSAndroid Build Coastguard Worker            fi
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker            RunTest
245*49cdfc7eSAndroid Build Coastguard Worker
246*49cdfc7eSAndroid Build Coastguard Worker            # Some tests could not reboot target. They can hung up
247*49cdfc7eSAndroid Build Coastguard Worker            # machine or leave it working. But we need to do all
248*49cdfc7eSAndroid Build Coastguard Worker            # tests. So we are going to reboot if we are in wrong
249*49cdfc7eSAndroid Build Coastguard Worker            # place.
250*49cdfc7eSAndroid Build Coastguard Worker
251*49cdfc7eSAndroid Build Coastguard Worker            sleep 3600
252*49cdfc7eSAndroid Build Coastguard Worker            echo "$(date +%F-%T): manually reboot for test ${i}." >>"${ldir}/status"
253*49cdfc7eSAndroid Build Coastguard Worker            /sbin/shutdown -r now
254*49cdfc7eSAndroid Build Coastguard Worker            sleep 60
255*49cdfc7eSAndroid Build Coastguard Worker        fi
256*49cdfc7eSAndroid Build Coastguard Worker
257*49cdfc7eSAndroid Build Coastguard Worker        # No test is scheduled to run.
258*49cdfc7eSAndroid Build Coastguard Worker        count=$((count + 1))
259*49cdfc7eSAndroid Build Coastguard Worker	last=${i}
260*49cdfc7eSAndroid Build Coastguard Worker    done
261*49cdfc7eSAndroid Build Coastguard Workerfi
262*49cdfc7eSAndroid Build Coastguard Worker
263*49cdfc7eSAndroid Build Coastguard Workerif [ "${ITERATION}" -eq 1 ]; then
264*49cdfc7eSAndroid Build Coastguard Worker    # We are done.
265*49cdfc7eSAndroid Build Coastguard Worker    break
266*49cdfc7eSAndroid Build Coastguard Worker
267*49cdfc7eSAndroid Build Coastguard Workerelse
268*49cdfc7eSAndroid Build Coastguard Worker    # Run the next iteration.
269*49cdfc7eSAndroid Build Coastguard Worker    sed -i "s/\(^ITERATION\)=.*/\1=$((ITERATION - 1))/" \
270*49cdfc7eSAndroid Build Coastguard Worker     "../${conf}"
271*49cdfc7eSAndroid Build Coastguard Workerfi
272*49cdfc7eSAndroid Build Coastguard Worker
273*49cdfc7eSAndroid Build Coastguard Workerdone
274*49cdfc7eSAndroid Build Coastguard Worker
275*49cdfc7eSAndroid Build Coastguard Worker# We are done.
276*49cdfc7eSAndroid Build Coastguard Worker# Reset.
277*49cdfc7eSAndroid Build Coastguard Workersed -i "s/\(^REBOOT\)=.*/\1=0/" "../${conf}"
278*49cdfc7eSAndroid Build Coastguard Workercrontab -r
279*49cdfc7eSAndroid Build Coastguard Workerldir=$(ls -td "../${log}/$(hostname)."* | head -1)
280*49cdfc7eSAndroid Build Coastguard Workerecho "$(date +%F-%T): test run complete." >>"${ldir}/status"
281*49cdfc7eSAndroid Build Coastguard Worker
282*49cdfc7eSAndroid Build Coastguard Workerexit 0
283