From 87302e2b39bee979988923e5da7c837c6c58c0ff Mon Sep 17 00:00:00 2001
From: Sergio Lopez <slp@redhat.com>
Date: Thu, 2 Mar 2023 07:34:49 +0100
Subject: [PATCH 01/11] krunfw: Don't panic when init dies

In libkrun, the isolated process runs as PID 1. When it exits,
trigger an orderly reboot instead of panic'ing.

Also, silence reboot messages for the moment. Eventually we should
play with the printk levels instead.

Signed-off-by: Sergio Lopez <slp@redhat.com>
---
 kernel/exit.c   | 5 +++++
 kernel/reboot.c | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/kernel/exit.c b/kernel/exit.c
index edb50b4c9972..01f18a262895 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -69,6 +69,7 @@
 #include <linux/rethook.h>
 #include <linux/sysfs.h>
 #include <linux/user_events.h>
+#include <linux/reboot.h>
 
 #include <linux/uaccess.h>
 #include <asm/unistd.h>
@@ -840,8 +841,12 @@ void __noreturn do_exit(long code)
 		 * immediately to get a useable coredump.
 		 */
 		if (unlikely(is_global_init(tsk)))
+#ifdef VERBOSE_LIBKRUN
 			panic("Attempted to kill init! exitcode=0x%08x\n",
 				tsk->signal->group_exit_code ?: (int)code);
+#else
+            orderly_reboot();
+#endif
 
 #ifdef CONFIG_POSIX_TIMERS
 		hrtimer_cancel(&tsk->signal->real_timer);
diff --git a/kernel/reboot.c b/kernel/reboot.c
index 3bba88c7ffc6..a08a8a9b4d88 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -268,10 +268,12 @@ void kernel_restart(char *cmd)
 	do_kernel_restart_prepare();
 	migrate_to_reboot_cpu();
 	syscore_shutdown();
+#ifdef VERBOSE_LIBKRUN
 	if (!cmd)
 		pr_emerg("Restarting system\n");
 	else
 		pr_emerg("Restarting system with command '%s'\n", cmd);
+#endif
 	kmsg_dump(KMSG_DUMP_SHUTDOWN);
 	machine_restart(cmd);
 }
-- 
2.40.0

