Clean "grub.cfg " with a script

First I have to say that there is a custom.cfg (thanks to @gohlip) in /boot/grub/ that leads me in any case to all startup screens of my Manjaro versions.

Text in grub.cfg must be completely identical!

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

Due to my multiboot system (5 versions of Manjaro) the grub.cfg is inflated after each β€œsudo update-grub” and extends the boot process over time.
With this script I remove the unnecessary entries of the β€œ30_os-prober” from grub.cfg.

I make it available here with the simultaneous request to ask what could be more improved. :slight_smile:

# - 05.10.2018 -
	echo "${0##*/} delete some unneeded stuff in grub.cfg."
	echo "Syntax: ${0##*/} hasn't a Syntax."
	echo "Properties for $FILENAME:"
	echo "Size is $(ls -lh $FILENAME | awk '{ print $5 }')"
	echo "Type is $(file $FILENAME | cut -d":" -f2 -)"
	echo "Inode number is $(ls -i $FILENAME | cut -d" " -f1 -)"
	echo "$(df -h $FILENAME | grep -v Mounted | awk '{ print "On",$1", \nwhich is mounted as the",$6,"partition."}')"
test "${1}" = "-h" -o "${1}" = "-help" -o "${1}" = "--help" && Help

# IFS stands for "internal field separator".

# Check if the current user is "root" otherwise restart the script with "sudo"...
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"
#[ "$(whoami)" != "root" ] && echo "You must be root, start new Terminal" && exit 1 # infinite loop

    echo "$0: $2" >&2
    exit "$1"

# set -o pipefail causes a pipeline (for example, curl -s | grep foo) to produce a failure return
#   code if any command errors. Normally, pipelines only return a failure if the last command errors. In combination
#   with set -e, this will make your script exit if any command in a pipeline errors.
# set -u Treat unset variables as an error, and immediately exit.
# set -e Instruct the Script to Abort for any command error...
# Give some output as it runs (-x), fail on errors (-e), and return an error code from the failure of a subcommand instead of the script itself (-o pipefail)
set -euo pipefail
#set -x

#clean and format terminal
echo -e "\e[8;66;67t"
# Last petrol station in front of the Highway ;)
	echo -e "\e[0m"
    echo -e "\e[32m"
    echo -e " β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”"
    echo -e " β”‚                                                               β”‚"
    echo -e " β”‚                                                               β”‚"
    echo -e " β”‚                \e[32mThis Script will \e[31m\e[5mdelete\e[0m\e[32m                        β”‚"
    echo -e " β”‚           the '30_os-proper' entries in grub.cfg.             β”‚"
    echo -e " β”‚                                                               β”‚"
    echo -e " β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜\e[0m"
	echo -e "       \e[7m "  Press [Enter] to execute, for exit press [CTRL+C]  " \e[0m                     "
    read -p ""

# Backup grub.cfg
cd $DIRname
cp grub.cfg grub.cfg.bck

# Delete entry between "30_os-prober"
sed '/\### BEGIN \/etc\/grub.d\/30_os-prober \###/,/\### END \/etc\/grub.d\/30_os-prober \###/d' $PATHname > new2

# Search - replace, possibly not necessary?
sed 's/### END \/etc\/grub.d\/20_linux_xen ###/### END \/etc\/grub.d\/20_linux_xen ###\n### BEGIN \/etc\/grub.d\/30_os-prober ###\n### END \/etc\/grub.d\/30_os-prober ###/g' new2 > $FILEname

# Clean /boot/grub/
rm new2

echo "Hello $USER!"
echo "It's all taken care of."
echo ""

exit 0

Name it how you like and put in ~/bin/ , set execution rights chmod u=rwx ~/bin/SCRIPTNAME.

It’s already very late / early :slight_smile: in Germany, which I’ll expand then.

Todo, boot mountpoints and clean all grub.cfg file in one pass.
/dev/sda2: UUID="9030b9ef-9aab-4578-92c7-b085e2b31cbb" XFCE   | sgs@mx
/dev/sdb1: UUID="4aaa187d-9945-433d-8bd2-309afbb8e717" Deepin | mint@deepin
/dev/sdb2: UUID="04d0077e-4517-4d2c-86d6-f3f60acc4712" XFCE   | mint@manjaro
/dev/sdb3: UUID="52cb2b74-6161-4bf2-859f-9934ffeda8b8" i3wm   | mint@i3
/dev/sdc3: UUID="4c6b4732-c901-4836-92ac-b0e96a71cea0" i3wm   | mint@i3wm

Boot Mountpoints
df | grep -q /mnt/4aaa187d-9945-433d-8bd2-309afbb8e717 || sudo mount /mnt/4aaa187d-9945-433d-8bd2-309afbb8e717
df | grep -q /mnt/04d0077e-4517-4d2c-86d6-f3f60acc4712 || sudo mount /mnt/04d0077e-4517-4d2c-86d6-f3f60acc4712
df | grep -q /mnt/52cb2b74-6161-4bf2-859f-9934ffeda8b8 || sudo mount /mnt/52cb2b74-6161-4bf2-859f-9934ffeda8b8
df | grep -q /mnt/4c6b4732-c901-4836-92ac-b0e96a71cea0 || sudo mount /mnt/4c6b4732-c901-4836-92ac-b0e96a71cea0

Thank you for your interest.

Solution sought

# infinite loop
[ "$(whoami)" != "root" ] && echo "You must be root, start new Terminal" && exit 1 

Translated with :slight_smile:


After a nice cup of coffee :wink:

# Clean /boot/grub/
rm new2

what’s an os proper?

1 Like

Look in grub.cfg

### BEGIN /etc/grub.d/30_os-prober ###
### END /etc/grub.d/30_os-prober ###

Something like help/config files to create grub.cfg
I don’t want to know it’s just supposed to be gone. :smiley:

Ahhhhhhh, prober :smiley: , thank you :slight_smile:

Fixed in first post

 β”‚                                                               β”‚
 β”‚                                                               β”‚
 β”‚                This Script will delete                        β”‚
 β”‚           the '30_os-proper' entries in grub.cfg.             β”‚
 β”‚                                                               β”‚
         Press [Enter] to execute, for exit press [CTRL+C]                       

Hello root!
It's all taken care of.

I wasn’t root. But issued a sudo before. Not a good idea. You must request explicitly the access rights.

# Check if the current user is "root" otherwise restart the script with "sudo"...
[ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@"

yes, this is what iam talking about. The script must exit with the hint that it can only run as root. What I did was

$ ./

and the script grabbed the root rights granted shortly before to another command. This is what I mean with β€œnot good”.

change it to something like this:

[ "$(whoami)" != "root" ] && echo "you must be root" && exit 1
1 Like

Hmm, it has cut my grub.cfg in half. Let me reboot. If you do not hear anything from me in the next hr, be prepared for a drone homing in on your smartphone :wink:

look for the grub.cfg.bck backup :slight_smile:

β€œYou must be root, start new Terminal” dos not work.

yes, i have seen that. I made another backup before hand. But i am back. Cool thing :+1:

1 Like

I probably didn’t think about the fact that the text with the 30_os-prober doesn’t have to be the same for all of them.

Guten Morgen! Hello again.

A few more things to share with you. Some you may like, some you may not (I like cheesecake! :smile: ).

  1. I have and keep more than Manjaro iso files; some other distro iso’s. Some I keep for a longer time and boot up more often, like gparted iso and one or two remastered iso’s which I boot up very often and remastered again (same name) after some kernel or apps upgrades.
    So it makes sense for me to have a partition just for these iso’s and nothing else.
    Best format instead of ext4 would be ext2. Using f2fs now to test and it works very well for Manjaro iso files, but f2fs may not be suitable for other distro iso boots and doesn’t work for gparted iso, for example.
  2. Looking at your post above, it removes some entries in os-prober.
    Interesting, but I confess I do not know much of it - not having studied on or having any career in computer systems (but in both have to use computers, as a user and have to learn by myself programming languages (now obsoleted) to do so). But I want you to know I disable os-prober in all of my OS’s (and re-enabled for testing purposes).
  3. The reason I disable os-prober is that I use my own grub that is OS-independent since the days of grub-legacy and in all my computer systems. I also understand (from some of the topics here in this forum) our os-prober (and hence update-grub) especially to non-lsb-release OS’s like Antergos takes a much longer time than other OS’s update-grub. Perhaps it is the way we modify os-prober, but I have not checked. Not having os-prober enabled is much faster as well.
  4. In the search line for grub entries, we can use 'label’s instead of uuid. In the 3rd and 4th posts of the above link, it explains how we can do that. Perhaps your script can be made easier with it? As I said, I don’t know but maybe you can take a look at it.

Thanks for your script and appreciate if you can explain further.
Cheers. Take care.

ps: you wake up early from a late night.
Seems we all Manjaro Muntou’s need very sleep. :sweat_smile:

[EDIT] - seems I missed the 1st thing I want to tell you. Sorry and heh heh!
I do not have many entries (like you do) for manjaro entries or others. I just have one.
I comment out the possible options like this. And uncomment if I want to use it.
Just a note to tell you what I did.

menuentry "Manjaro iso" --class dvd {
#    set isofile="/miso/manjaro-lxqt-17.1.12-stable-minimal-x86_64.iso"
    set isofile="/miso/manjaro-kde-18.0-beta-8-testing-x86_64.iso"
#    set isofile="/miso/manjaro-gnome-17.1.6-1-testing-x86_64.iso"
#    set isofile="/miso/manjaro-openbox-minimal-17.1.12-stable-x86_64.iso"
    set dri="free"
    insmod ext2 
    search --no-floppy -f --set=root $isofile
    probe -u $root --set=abc
    set pqr="/dev/disk/by-uuid/$abc"
    loopback loop $isofile
    linux  (loop)/boot/vmlinuz-x86_64  img_dev=$pqr img_loop=$isofile driver=$dri tz=Europe/Berlin keytable=de lang=de_DE
    initrd  (loop)/boot/intel_ucode.img (loop)/boot/initramfs-x86_64.img

1 Like

I do not know where?
Read now β€œmy own grub” , maybe then my script is obsolete. :smiley:

I got grub.cfg over 1.000 Kb (10.000 lines?) and boot time was crazy.

See you typing and could not sleep :smiley:

1 Like

To disable os-prober, At /etc/default grub, add a line


ps: I have a new edit to my previous post. Hope you read it.
You did not sleep yet? Ha Ha. You are the king mantao then!

ps: taking my dog for a walk now.
Gute Nacht to you.

1 Like

I’m glad we talked about it.
Again something learned,
should be enough for me. :slight_smile:

Many ways lead to Rome, but yours is much, much shorter :wink:

Thanks again.

If I have slept in, I look again in peace your


1 Like

Forum kindly sponsored by Bytemark