log-collector: cmd for collecting logs at pre-install stage

This will dump logs and kdumps, and generate a tarball

Tested:
>log_collector > mytar.tar.gz
> tar -xvzf mytar.tar.gz
tmp/tmp.PyYXS0/journal.log
mnt/luks-mmcblk0_fs/kdump/kcore.3
run/log/kdump/kcore.1
run/log/kdump/kcore.2

Google-Bug-Id: 317372429
Change-Id: I71410affa0e972f2c864963270e055399e81739a
Signed-off-by: Yuxiao Zhang <yuxiaozhang@google.com>
(cherry picked from commit 7a62cd625f663ea95ec6c4d6b336ef0f67ac9ffa)
diff --git a/recipes-google/gbmc-collect-logs/files/gbmc-collect-logs b/recipes-google/gbmc-collect-logs/files/gbmc-collect-logs
new file mode 100644
index 0000000..6c92188
--- /dev/null
+++ b/recipes-google/gbmc-collect-logs/files/gbmc-collect-logs
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+# Used by installer to dump logs and other
+# artifacts before reinstall
+#
+
+exec 2>/dev/null
+
+journal_name='journal.log'
+
+function collect_journal(){
+  # Save the last 1000 lines of logs for most related events
+  journalctl -n 1000 > $1/$journal_name
+}
+
+TMPDIR=$(mktemp -d)
+trap 'rm -rf "$TMPDIR"' EXIT TERM INT
+
+collect_journal $TMPDIR
+
+kdump_file_list=$(kdumpctl list-path)
+
+# a workaround of writing tarball to stdout
+tar -czO $TMPDIR/$journal_name $kdump_file_list | cat
diff --git a/recipes-google/gbmc-collect-logs/gbmc-collect-logs.bb b/recipes-google/gbmc-collect-logs/gbmc-collect-logs.bb
new file mode 100644
index 0000000..2d21c14
--- /dev/null
+++ b/recipes-google/gbmc-collect-logs/gbmc-collect-logs.bb
@@ -0,0 +1,18 @@
+SUMMARY = "logs collecting command on gBMC before reinstall"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+SRC_URI:append = " \
+  file://gbmc-collect-logs \
+  "
+S = "${WORKDIR}"
+
+RDEPENDS:${PN} += " \
+  bash \
+  "
+
+do_install:append() {
+  install -d -m0755 ${D}${bindir}
+  install -m0755 ${WORKDIR}/gbmc-collect-logs ${D}${bindir}/
+}