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