Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 3650  / 3 Years ago, fri, november 19, 2021, 10:19:07

For example:



00:00:00 to 06:00:00 -> Slitaz
06:00:01 to 13:00:00 -> Ubuntu
13:00:01 to 19:00:00 -> Fedora
19:00:01 to 23:59:59 -> openSUSE


Can grub change default 'entry' automatically?


More From » grub2

 Answers
6

First, run grep -E '(menuentry |submenu )' /boot/grub/grub.cfg to get a list of your grub menu entries. You should see something like:



menuentry 'Ubuntu' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-simple-7820cc72-dac4-447f-bba0-996ed1a12fa5' {
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-7820cc72-dac4-447f-bba0-996ed1a12fa5' {
menuentry 'Ubuntu, with Linux 3.16.0-28-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-28-generic-advanced-7820cc72-dac4-447f-bba0-996ed1a12fa5' {
menuentry 'Ubuntu, with Linux 3.16.0-28-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-28-generic-recovery-7820cc72-dac4-447f-bba0-996ed1a12fa5' {
menuentry 'Ubuntu, with Linux 3.16.0-25-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-25-generic-advanced-7820cc72-dac4-447f-bba0-996ed1a12fa5' {
menuentry 'Ubuntu, with Linux 3.16.0-25-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-3.16.0-25-generic-recovery-7820cc72-dac4-447f-bba0-996ed1a12fa5' {
menuentry 'Memory test (memtest86+)' {
menuentry 'Memory test (memtest86+, serial console 115200)' {
menuentry 'Windows 7 (loader) (on /dev/sda2)' --class windows --class os $menuentry_id_option 'osprober-chain-C84087BD4087B12C' {


Here you can see my first menu entry is Ubuntu, followed by a Advanced options for Ubuntu submenu (with four other entries), 2 memory tests and, at last, Windows 7.



If we create a file named /boot/grub/custom.cfg, it will be loaded after /boot/grub/grub.cfg, so we can easily change GRUB's default configuration.



I used GRUB's module datehook to get the current time.



/boot/grub/custom.cfg:



# This module creates special variables that return the current date/time
insmod datehook

# Add and extra 0 to minutes if it's less than 10 (force a 2-digit minute)
if [ $MINUTE -lt 10 ]; then PADDING="0"; else PADDING=""; fi
TIME=$HOUR$PADDING$MINUTE

# Boot "Ubuntu" from midnight to 5:59AM
if [ $TIME -ge 0 -a $TIME -lt 559 ]; then
set default="Ubuntu"
fi

# Boot "Windows 7" from 6AM to 4:59PM
if [ $TIME -ge 600 -a $TIME -lt 1659 ]; then
set default="Windows 7 (loader) (on /dev/sda2)"
fi

# If you want to boot an entry that's inside a submenu,
# you have to prepend its name with the submenu position, starting from 0.
# Boot "Ubuntu, with kernel 3.16.0-25-generic" from 5PM to 11:59PM
if [ $TIME -ge 1700 -a $TIME -lt 2359 ]; then
set default="1>Ubuntu, with Linux 3.16.0-25-generic"
fi


The module datehook makes available the variables: DAY, HOUR, MINUTE, MONTH, SECOND, WEEKDAY and YEAR, which return the actual date/time values based on hardware clock.



Let's take if [ $TIME -ge 600 -a $TIME -lt 1659 ]; then as an example. It means: if the current time is greater than or equal to 6AM and less than 4:59PM (16:59) then execute the next command (set default="Windows 7 (loader) (on /dev/sda2)"), which set the default variable with the Windows 7 menu entry name taken from that grep command above.



The last if block exemplifies the selection of a submenu entry. In that case "Ubuntu, with Linux 3.16.0-25-generic" lies inside a submenu that is the second entry in the main menu. As an entry position in a menu starts from 0, the menu entry named "Ubuntu" is 0 and the "Advanced options for Ubuntu" submenu is 1, that's why I had to add 1> before the entry name: set default="1>Ubuntu, with Linux 3.16.0-25-generic".



There no need to run update-grub.



The hardware clock may be unreliable, specially if the battery is dead. Also, enter BIOS setup and check the time. If it is UTC you'll have to change the time range in the script.


[#30418] Saturday, November 20, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ingssex

Total Points: 21
Total Questions: 122
Total Answers: 98

Location: Sweden
Member since Fri, Mar 26, 2021
3 Years ago
ingssex questions
;