Fix ManjaroLinux in grub (fallback only), simple gambas script

Post Reply
Online
User avatar
BruceSteers
Posts: 210
Joined: Thursday 23rd July 2020 5:20pm

Fix ManjaroLinux in grub (fallback only), simple gambas script

Post by BruceSteers » Tuesday 17th November 2020 5:14pm

Here's a quick simple example of gambas scripting.
I was initially writing this script with bash but it has been much better to do it with gambas scripting.

What it does....
If you have ManjaroLinux on a disk but it's not your main disk and not the disk grub is installed from then the os-probe get's some details wrong about manjaro and makes a grub.cfg file that will only boot manjaro in "fallback" mode.

The reason is this...
On the Manjaro disk it's grub.cfg file will have a line like this at the end of the ManjaroLinux menu entry.
initrd /boot/intel-ucode.img /boot/initramfs-5.8-x86_64.img

But if your grub is set up on another drive it's grub.cfg will look like this..
initrd /boot/intel-ucode.img

I've made the simple fix before, more than once though as it reverts to the "wrong" config every time update-grub is run on the main OS.
So a simple script to fix it is born :) lol

So run this script from your main OS and it will auto-detect your Manjaro drive (if mounted) and read it's grub.cfg file to get the correct initrd line. (or you can manually select the Manjaro grub.cfg file if not detected.

It then reads the grub.cfg file from your running OS and Changes the initrd lines to be correct.
If run as root it will save the /boot/grub/grub.cfg automatically and job done.
If not run as root it will save a new modified grub.cfg file to your desktop for you to copy manually.

Wishing Well :)

(Ps. the Message() requesters show a gtk warning in the cli, ignore it. Benoit is looking into it.
change Use "gb.gui" to Use "gb.qt5" to use qt)

File Fix-manjaro-grub-entries.gbs
#!/usr/bin/env gbs3

' Gambas script file

Use "gb.gui"


Public sMGrubLine As String

Public Sub Main()

If Access("/boot/grub/grub.cfg", gb.Write) = False Then 
 If Message.Warning("Warning no write access!\n\nRun this script as root to save\nyour grub.cfg file directly.\nOtherwise the file will be saved to your Desktop", "Continue", "Cancel") <> 1 Then Quit 1
Endif

Dim sVar, sText As String
Shell "lsblk -o MOUNTPOINT,FSTYPE|grep /|grep ext4|awk '{print $1}'" To sVar
sVar = Trim(sVar)
Dialog.Path = ""
For Each sVar In Split(sVar, gb.Lf)
 If Exist(sVar &/ "etc/os-release") Then
 sText = File.Load(sVar &/ "etc/os-release")
  If InStr(sText, "Manjaro") Then
   Print "\nFound Manjaro on disk: " & sVar
   If Message.Question("Found Manjaro on..\n" & sVar & "\n\nUse /boot/grub/grub.cfg\nfrom this Drive?", "Yes", "Choose grub.cfg file") = 1 Then
   Dialog.Path = sVar &/ "boot/grub/grub.cfg"
   Goto SkipChooser
 Endif
  Endif
 Endif
Next


Print "Manjaro not auto-detected, opening file requester.."
Message("Select the\n/boot/grub/grub.cfg file\non your Manjaro harddrive")

With Dialog
.Title = "Choose grub.cfg"
.Path = "/media" &/ User.Name
.Filter = ["*.cfg", "Grub cfg Files"]
If .OpenFile() Then Return
End With

SkipChooser:

Print "\nUsing '" & Dialog.Path & "'"
sMGrubLine = GetManjaroLine(Dialog.Path)

If Not sMGrubLine Then
Message.Error("Error.\nCould not find initrd line from\n" & Dialog.Path)
Endif

EditGrub()

End


Public Sub GetManjaroLine(sFile As String) As String

Dim sLines As String[] = Split(File.Load(sFile), gb.Lf)
Dim bActive As Boolean, sLine As String
For iCount As Integer = 0 To sLines.Max
sLine = sLines[iCount]
If InStr(sLine, "--class manjaro") Then 
bActive = True
Continue
Endif
If Not bActive Then Continue
If InStr(sLine, "initrd") Then
Print "\nFound initrd on line " & (iCount + 1) & " in Manjaro grub.cfg\n" & sLine
 Return sLine
 Break
Endif
Next

End

Public Sub EditGrub()
If Not Exist("/boot/grub/grub.cfg") Then
Print "/bot/grub/grub.cfg not found, aborting."
Quit 1
Endif
Dim sLines As String[] = Split(File.Load("/boot/grub/grub.cfg"), gb.Lf)
Dim bActive As Boolean
Dim iLine As Integer = 0
Dim sLine As String

For iLine = 0 To sLines.Max

 If InStr(sLines[iLine], "menuentry 'Manjaro Linux") And (Not InStr(sLines[iLine], "fallback")) Then
  Print "\nFound menuentry: " & Split(sLines[iLine], "'")[1]
  bActive = True
  Continue
 Endif

If Not bActive Then Continue

If InStr(sLines[iLine], "initrd") Then 
Print "Changing grub.cfg Line: " & (iLine + 1)
sLines[iLine] = "\t" & sMGrubLine
bActive = False
Endif
Next

If Access("/boot/grub/grub.cfg", gb.Write) = True Then
Print "Root permissions granted, saving '/boot/grub/grub.cfg'"
File.Save("/boot/grub/grub.cfg", sLines.Join(gb.Lf))
Else
Print "Root permissions not granted, saving as " & User.Home &/ "Desktop/grub.cfg"
File.Save(User.Home &/ "Desktop/grub.cfg", sLines.Join(gb.Lf))
Endif
Catch
Print "An error occured..\n" & Error.Text
End
Attachments
Fix-manjaro-grub-entries.zip
(1.35 KiB) Downloaded 2 times
If at first you don't succeed , try it differently.

Post Reply