From 13eb9829fae7e5e78eef1c78a983016ccbae4404 Mon Sep 17 00:00:00 2001 From: longpanda Date: Sun, 14 Aug 2022 23:10:46 +0800 Subject: [PATCH] ALT Linux multiple initrd (#1784) --- .../grub-2.04/grub-core/ventoy/ventoy_linux.c | 91 +++++++++++++++++++ README.md | 2 +- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c index 94befa7a..e32ff02f 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_linux.c @@ -333,12 +333,84 @@ end: VENTOY_CMD_RETURN(GRUB_ERR_NONE); } +static int ventoy_linux_initrd_collect_hook(const char *filename, const struct grub_dirhook_info *info, void *data) +{ + int len; + initrd_info *img = NULL; + + (void)data; + + if (0 == info->dir) + { + if (grub_strncmp(filename, "initrd", 6) == 0) + { + len = (int)grub_strlen(filename); + if (grub_strcmp(filename + len - 4, ".img") == 0) + { + img = grub_zalloc(sizeof(initrd_info)); + if (img) + { + grub_snprintf(img->name, sizeof(img->name), "/boot/%s", filename); + + if (ventoy_find_initrd_by_name(g_initrd_img_list, img->name)) + { + grub_free(img); + } + else + { + if (g_initrd_img_list) + { + img->prev = g_initrd_img_tail; + g_initrd_img_tail->next = img; + } + else + { + g_initrd_img_list = img; + } + + g_initrd_img_tail = img; + g_initrd_img_count++; + } + } + } + } + } + + return 0; +} + +static int ventoy_linux_collect_boot_initrds(void) +{ + grub_fs_t fs; + grub_device_t dev = NULL; + + dev = grub_device_open("loop"); + if (!dev) + { + debug("failed to open device loop\n"); + goto end; + } + + fs = grub_fs_probe(dev); + if (!fs) + { + debug("failed to probe fs %d\n", grub_errno); + goto end; + } + + fs->fs_dir(dev, "/boot", ventoy_linux_initrd_collect_hook, NULL); + +end: + return 0; +} + static grub_err_t ventoy_grub_cfg_initrd_collect(const char *fileName) { int i = 0; int len = 0; int dollar = 0; int quotation = 0; + int initrd_dollar = 0; grub_file_t file = NULL; char *buf = NULL; char *start = NULL; @@ -421,6 +493,19 @@ static grub_err_t ventoy_grub_cfg_initrd_collect(const char *fileName) debug("Remove quotation <%s>\n", img->name); } + /* special process for /boot/initrd$XXX.img */ + if (dollar == 1) + { + if (grub_strncmp(img->name, "/boot/initrd$", 13) == 0) + { + len = (int)grub_strlen(img->name); + if (grub_strcmp(img->name + len - 4, ".img") == 0) + { + initrd_dollar++; + } + } + } + if (dollar == 1 || ventoy_find_initrd_by_name(g_initrd_img_list, img->name)) { grub_free(img); @@ -458,6 +543,12 @@ static grub_err_t ventoy_grub_cfg_initrd_collect(const char *fileName) grub_free(buf); grub_file_close(file); + if (initrd_dollar > 0 && grub_strncmp(fileName, "(loop)/", 7) == 0) + { + debug("collect initrd variable %d\n", initrd_dollar); + ventoy_linux_collect_boot_initrds(); + } + VENTOY_CMD_RETURN(GRUB_ERR_NONE); } diff --git a/README.md b/README.md index e52c24ff..750a7291 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Debian, Ubuntu, CentOS(6/7/8/9), RHEL(6/7/8/9), Deepin, Fedora, Rocky Linux, Alm DragonFly FreeBSD pfSense GhostBSD FreeNAS TrueNAS XigmaNAS FuryBSD OPNsense HardenedBSD MidnightBSD ClonOS EmergencyBootKit **ChromeOS** -FydeOS, CloudReady +FydeOS, CloudReady, ChromeOS Flex **Other** VMware ESXi, Citrix XenServer, Xen XCP-ng