首页 > 解决方案 > 从 GRUB 菜单项运行脚本

问题描述

我按照出色的指南https://willhaley.com/blog/custom-debian-live-environment/创建了 Debian Live DVD 。

I would like to be able to great two grub menu enteries when selected auto login and run a script.

    menuentry "Run Script 1" {
        linux /vmlinuz boot=live quiet nomodeset
        initrd /initrd
    }

    menuentry "Run Script 2" {
        linux /vmlinuz boot=live quiet nomodeset
        initrd /initrd
    }

当我以 root 身份自动登录时,如何从 grub 菜单条目传递要运行的脚本的绝对路径?

/lib/live/mount/medium/scripts/script1.bash

要自动登录,我已将 /lib/systemd/system/getty@.service 修改为使用上述菜单项以 root 身份自动登录。

标签: linux-kernelgrub

解决方案


使用自定义参数启动:

linux /vmlinuz .... my_dummy_param=/lib/live/mount/medium/scripts/script1.bash

然后稍后在您的登录 shell 启动文件中读取/proc/cmdline并解析它,例如。用于.bashrcbash shell 或.profile

#!/bin/bash
. /proc/cmdline
echo "Running $my_dummy_param"
"$my_dummy_param"

推荐阅读