BUG: GRUB creates many duplicate comments in grub.cfg when running update-grub

I notice there are a lot of duplicate comments in /boot/grub/grub.cfg : :point_down:

...
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### BEGIN /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
### END /etc/grub.d/41_snapshots-btrfs ###
...

More than 200 same comments in it.

That causes GRUB processing to slow down.

How to reproduce the issue:

For grub 2.06.r499 and grub-btrfs 4.12 users.

  1. You have at least one btrfs root snapshot.
  2. You run with root privileges.
  3. Create grub.cfg backup
# cp /boot/grub/grub.cfg  /boot/grub/grub.cfg_backup
  1. Update grub config
# update-grub

OR

# grub-mkconfig -o /boot/grub/grub.cfg
  1. See diff between two files: grub.cfg vs. grub.cfg_backup
# diff grub.cfg_backup grub.cfg

Difference output:

197a198,199
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###
  1. Run update-grub 5 times again:
    Difference output:
197a198,209
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### BEGIN /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###
> ### END /etc/grub.d/41_snapshots-btrfs ###

Most likely an issue with grub-btrfs. Maybe check how that script creates the comment. You may disable it.

The issue would be the GRUB header /etc/grub.d/00_header which would create the same comment with NO snapshot-entry , but all snapshot-entries are in /boot/grub/grub_btrfs.cfg that is outside of /boot/grub/grub.cfg.

I created a new executable GRUB script:

  1. $ sudo vim /etc/grub.d/00_clear_grub_cfg to add
#!/bin/sh

if [ -f "/boot/grub/grub.cfg" ]; then
  rm /boot/grub/grub.cfg
fi

The script name “00_clear_grub_cfg” has the highest priority in GRUB order.

  1. $ sudo chmod +x /etc/grub.d/00_clear_grub_cfg
  2. $ sudo update-grub

It removes the old grub.cfg then create a new grub.cfg when running update-grub, that does a trick

This trick is unreliable. I get the warning when the grub.cfg was initially deleted.

grep: /boot/grub/grub.cfg: No such file or directory

WARNING: 'grub-mkconfig' needs to run at least once to generate the snapshots (sub)menu entry in grub the main menu. After that this script can run alone to generate the snapshot entries.

The reliable solution:

Create a new executable GRUB script:

  1. $ sudo vim /etc/grub.d/39_clear_grub_btrfs_comments to add
#!/bin/sh

file=/boot/grub/grub.cfg

# Find all duplicate lines and store in array
duplicates=$(grep -n "41_snapshots-btrfs ###" $file | cut -d: -f1)

count=0
i=0
# Loop through duplicates and remove all but the first two comments
for line_num in $duplicates; do
  if [ "$i" -gt 1 ]; then
    # Delete the line if it contains "41_snapshots-btrfs ###"
    line_num=$((line_num - count))
    sed -i "$line_num"d "$file"
    count=$((count + 1))
  fi
  i=$((i + 1))
done
  1. $ sudo chmod +x /etc/grub.d/39_clear_grub_btrfs_comments
  2. $ sudo update-grub

It removes all duplicates, leaving the first two comments.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.