几个启动脚本?

我希望kubuntu在启动时问我 – 我想使用哪个配置文件。 应该有一个默认答案,应该在3秒内自动运行。 好像Grub问你想要启动哪个操作系统。

有几个启动选项的理性:通常我默认启动很多东西:krusader,firefox,konsole,emacs ……所以完全加载大约需要1分钟。 但有时候 – 我正在奔跑,只需要做一些事情 – 所以我只需要,比如krusader。 我已经制作了几个启动脚本……所以我想知道 – 是否可以在启动时选择它们?

也许这可以用Grub完成……

编辑

Grub根本没有问我关于启动配置文件 – 甚至不是“正常模式”或“恢复模式”。 我让他用Grub编辑器(包名为kcm-grub2kcm-grub2

 sudo sed -i '$ a\deb http://download.opensuse.org/repositories/home:ksmanis/xUbuntu_11.10/ /' /etc/apt/sources.list wget -q http://download.opensuse.org/repositories/home:ksmanis/xUbuntu_11.10/Release.key -O- | sudo apt-key add - sudo apt-get update sudo apt-get install kcm-grub2 

安装并重新启动后,在System settings – > Startup and Shutdown中出现了新项目: Grub2 Bootloader 。 在那里 – 我选中了“在3秒内自动选择默认启动项”。

之后,在lumbric解决方案之后,我将gnu-linux menuentry记录复制到了/etc/grub.d/40_custom的末尾,并在那里添加了“生产力配置文件”。 这是修改后的40_custom的文本:

 #!/bin/sh exec tail -n +3 $0 # This file provides an easy way to add custom menu entries. Simply type the # menu entries you want to add after this comment. Be careful not to change # the 'exec tail' line above. menuentry 'Full' --class ubuntu --class gnu-linux --class gnu --class os { recordfail set gfxpayload=$linux_gfx_mode insmod gzio insmod part_msdos insmod ext2 set root='(hd0,msdos1)' search --no-floppy --fs-uuid --set=root 392f79ba-0f1f-422f-ac76-11860e0f4869 linux /boot/vmlinuz-3.0.0-16-generic root=UUID=392f79ba-0f1f-422f-ac76-11860e0f4869 ro quiet splash vt.handoff=7 productivity-profile=1 initrd /boot/initrd.img-3.0.0-16-generic } menuentry 'Public' --class ubuntu --class gnu-linux --class gnu --class os { recordfail set gfxpayload=$linux_gfx_mode insmod gzio insmod part_msdos insmod ext2 set root='(hd0,msdos1)' search --no-floppy --fs-uuid --set=root 392f79ba-0f1f-422f-ac76-11860e0f4869 linux /boot/vmlinuz-3.0.0-16-generic root=UUID=392f79ba-0f1f-422f-ac76-11860e0f4869 ro quiet splash vt.handoff=7 productivity-profile=2 initrd /boot/initrd.img-3.0.0-16-generic } menuentry 'Fastest' --class ubuntu --class gnu-linux --class gnu --class os { recordfail set gfxpayload=$linux_gfx_mode insmod gzio insmod part_msdos insmod ext2 set root='(hd0,msdos1)' search --no-floppy --fs-uuid --set=root 392f79ba-0f1f-422f-ac76-11860e0f4869 linux /boot/vmlinuz-3.0.0-16-generic root=UUID=392f79ba-0f1f-422f-ac76-11860e0f4869 ro quiet splash vt.handoff=7 productivity-profile=3 initrd /boot/initrd.img-3.0.0-16-generic } 

productivity-profile由启动bash脚本获取。

而已。 现在我有5个启动配置文件:Clean,Recovery,Full,Public,Fastest。

在@Lekensteyn的帮助下,我创建了一个版本,您可以在GRUB中选择配置文件。 可能仍然可以改进自定义菜单的创建。

第1步:创建自定义GRUB菜单

您需要使用其他参数创建GRUB菜单项。 您可以按照此说明操作 。 因此,将/boot/grub/grub.cfg (带有menuentry ... { ... } )的默认条目复制到/etc/grub.d/40_custom

然后编辑(在/etc/grub.d/40_custom复制的条目)它所说的linux /boot...的行,并添加一个用空格分隔的行:

 productivity-profile=1 

您还可以编辑菜单条目的名称,因此将部分menuentry 'Ubuntu, with...更改为menuentry 'Ubuntu, with...类似于menuentry 'Ubuntu (Profile 1), with...

为每个配置文件添加这样的菜单条目(每个配置文件都有一个唯一的编号)。

您可以从/etc/grub.d/中的其他文件中删除执行权限,以删除这些菜单项。 不要忘记之后运行sudo update-grub

请注意,在内核更新后,如果您按照此说明操作,则必须手动更改grub菜单! (您可以改进此答案并描述如何相应地编辑/etc/grub.d/的脚本。’)

第2步:检查步骤1是否成功

重新启动并选择其中一个配置文件。 然后在终端中运行:

 cat /proc/cmdline 

你应该得到类似的东西:

 BOOT_IMAGE=/boot/vmlinuz-2.6.... root=... productivity-profile=2 

…行末尾的数字必须是所选的配置文件。

第3步:根据您的需要采用脚本并在启动时运行它

然后使用此脚本运行启动脚本:

 #!/bin/bash profile=$(cat /proc/cmdline |sed 's/.*productivity-profile=\([0-9]\).*/\1/g') echo Running profile $profile ... case $profile in 1) # Run script for profile 1 ./script-profile1.sh ;; 2) # Run script for profile 1 ./script-profile2.sh ;; 3) # Run script for profile 1 ./script-profile3.sh ;; *) echo Error ;; esac 

您可以在启动时运行以下脚本:

 #!/bin/bash echo 'Press a key...' echo '[1] Full profile (default)' echo '[2] whatever 2' echo '[3] another one' read -t 3 -n 1 -p "Choose profile: " profile echo case $profile in 1) # Run script for profile 1 ./script-profile1.sh ;; 2) # Run script for profile 1 ./script-profile2.sh ;; 3) # Run script for profile 1 ./script-profile3.sh ;; *) echo Invalid choice ;; esac 

如果需要,可以包含一个循环,以便在选择无效时再次询问您,或者您可以用您喜欢的编程语言完全编程。

但我同意,在启动过程开始时选择配置文件会更好。 您可以启动计算机,选择配置文件,然后在一切都启动时返回。 但我不知道如何使用GRUB中的参数。