From 8a6630007780a9454a15c9a086ca3f77b5ba0873 Mon Sep 17 00:00:00 2001 From: wangzhaohui Date: Sat, 12 Oct 2024 15:52:02 +0800 Subject: [PATCH] feat: use a custom grub message --- debian/changelog | 6 + .../deepin-use-custom-grub-message.patch | 127 ++++++++++++++++++ debian/patches/series | 3 +- 3 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 debian/patches/deepin-use-custom-grub-message.patch diff --git a/debian/changelog b/debian/changelog index 149c982..297a701 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +ostree (2024.6-1deepin4) unstable; urgency=medium + + * use a custom grub message + + -- wangzhaohui Sat, 12 Oct 2024 15:21:52 +0800 + ostree (2024.6-1deepin3) unstable; urgency=medium * use last boot as default if GRUB_SAVEDEFAULT set in grub diff --git a/debian/patches/deepin-use-custom-grub-message.patch b/debian/patches/deepin-use-custom-grub-message.patch new file mode 100644 index 0000000..b0ec7c7 --- /dev/null +++ b/debian/patches/deepin-use-custom-grub-message.patch @@ -0,0 +1,127 @@ +diff --git a/src/libostree/ostree-bootloader-grub2.c b/src/libostree/ostree-bootloader-grub2.c +index cbe7605..2015bf5 100644 +--- a/src/libostree/ostree-bootloader-grub2.c ++++ b/src/libostree/ostree-bootloader-grub2.c +@@ -25,6 +25,8 @@ + #include + + #include ++#include ++#include + + // Written by bootupd + #define BOOTUPD_CONFIG "boot/bootupd-state.json" +@@ -166,6 +168,67 @@ _ostree_bootloader_grub2_get_name (OstreeBootloader *bootloader) + return "grub2"; + } + ++static char * ++ostree_deployment_get_time(OstreeSysroot *sysroot, OstreeDeployment *deployment) ++{ ++ if (sysroot == NULL || deployment == NULL) ++ return NULL; ++ ++ g_autofree char *deploy_path = ++ g_file_get_path(ostree_sysroot_get_deployment_directory(sysroot, deployment)); ++ ++ struct stat target_info; ++ if (stat(deploy_path, &target_info) != 0) ++ return NULL; ++ ++ char deploy_time[20]; ++ struct tm *tm_info; ++ ++ tm_info = localtime(&target_info.st_mtime); ++ strftime(deploy_time, sizeof(deploy_time), "%Y/%m/%d %H:%M:%S", tm_info); ++ ++ return g_strdup(deploy_time); ++} ++ ++static const char * ++ostree_update_grub_boot_message(OstreeSysroot *sysroot, const char *stage_deploy_time, ++ const char *title, guint index) ++{ ++ GPtrArray *deployments = sysroot->deployments; ++ ++ if (index >= deployments->len) ++ return title; ++ ++ g_autofree char *deploy_time = NULL; ++ /* When ostree has a staged deployment, a subprocess is spawned to call grub-mkconfig. ++ * However, the subprocess does not include the staged deployment in its deployment state, ++ * so we need to retrieve the deployment time from the OSTREE_STAGE_DEPLOY_TIME environment ++ * variable. ++ */ ++ if (stage_deploy_time) ++ deploy_time = index == 0 ? g_strdup_printf("%s", stage_deploy_time) ++ : ostree_deployment_get_time(sysroot, deployments->pdata[index - 1]); ++ else ++ deploy_time = ostree_deployment_get_time(sysroot, deployments->pdata[index]); ++ ++ if (deploy_time) ++ { ++ setlocale(LC_ALL, ""); ++ bindtextdomain("ostree", "/usr/share/locale"); ++ textdomain("ostree"); ++ ++ // remove substring " (ostree" ++ char *p = strstr(title, " (ostree"); ++ if (p) ++ *p = '\0'; ++ ++ return index ? g_strdup_printf("%s %s (%s)", gettext("Roll back to"), title, deploy_time) ++ : g_strdup_printf("%s (%s)", title, deploy_time); ++ } ++ ++ return title; ++} ++ + /* This implementation is quite complex; see this issue for + * a starting point: + * https://github.com/ostreedev/ostree/issues/717 +@@ -180,6 +243,7 @@ _ostree_bootloader_grub2_generate_config (OstreeSysroot *sysroot, int bootversio + static const char hardcoded_insmods[] = "insmod gzio\n"; + const char *grub2_boot_device_id = g_getenv ("GRUB2_BOOT_DEVICE_ID"); + const char *grub2_prepare_root_cache = g_getenv ("GRUB2_PREPARE_ROOT_CACHE"); ++ const char *stage_deploy_time = g_getenv ("OSTREE_STAGE_DEPLOY_TIME"); + + /* We must have been called via the wrapper script */ + g_assert (grub2_boot_device_id != NULL); +@@ -214,6 +278,7 @@ _ostree_bootloader_grub2_generate_config (OstreeSysroot *sysroot, int bootversio + + kernel = ostree_bootconfig_parser_get (config, "linux"); + ++ title = ostree_update_grub_boot_message(sysroot, stage_deploy_time, title, i); + quoted_title = g_shell_quote (title); + uuid = g_strdup_printf ("ostree-%u-%s", (guint)i, grub2_boot_device_id); + quoted_uuid = g_shell_quote (uuid); +@@ -285,6 +350,8 @@ typedef struct + { + const char *root; + const char *bootversion_str; ++ /* Used for child grub2-mkconfig to get deploy time of staged one */ ++ const char *stage_deploy_time; + gboolean is_efi; + } Grub2ChildSetupData; + +@@ -299,6 +366,8 @@ grub2_child_setup (gpointer user_data) + if (cdata->is_efi) + setenv ("_OSTREE_GRUB2_IS_EFI", "1", TRUE); + ++ if (cdata->stage_deploy_time) ++ setenv ("OSTREE_STAGE_DEPLOY_TIME", cdata->stage_deploy_time, TRUE); + /* Everything below this is dealing with the chroot case; if + * we're not doing that, return early. + */ +@@ -425,6 +494,11 @@ _ostree_bootloader_grub2_write_config (OstreeBootloader *bootloader, int bootver + g_autofree char *bootversion_str = g_strdup_printf ("%u", (guint)bootversion); + cdata.bootversion_str = bootversion_str; + cdata.is_efi = self->is_efi; ++ /* The first deployment of new_deployments should be the staged one, pass its ++ deploy time to child grub-mkconfig to update grub boot message. ++ */ ++ if (new_deployments->len) ++ cdata.stage_deploy_time = ostree_deployment_get_time (self->sysroot, new_deployments->pdata[0]); + /* Note in older versions of the grub2 package, this script doesn't even try + to be atomic; it just does: + diff --git a/debian/patches/series b/debian/patches/series index 7d901ee..6cab176 100644 --- a/debian/patches/series +++ b/debian/patches/series @@ -3,4 +3,5 @@ debian/test-sysroot-Skip-on-s390x-by-default.patch debian/Skip-test-admin-deploy-uboot.sh-on-s390x.patch deepin-deploy-when-root-is-overlay.patch make-socket-callback-during-cleanup-into.patch -deepin-save-last-boot-as-defalt-for-grub.patch \ No newline at end of file +deepin-save-last-boot-as-defalt-for-grub.patch +deepin-use-custom-grub-message.patch