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

Post

Posted
Rating:
#1 (In Topic #500)
Banned
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

Code (gambas)

  1. #!/usr/bin/env gbs3
  2.  
  3. ' Gambas script file
  4.  
  5. Use "gb.gui"
  6.  
  7.  
  8. Public sMGrubLine As String
  9.  
  10. Public Sub Main()
  11.  
  12. If Access("/boot/grub/grub.cfg", gb.Write) = False Then
  13.  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
  14.  
  15. Dim sVar, sText As String
  16. Shell "lsblk -o MOUNTPOINT,FSTYPE|grep /|grep ext4|awk '\{print $1}'" To sVar
  17. sVar = Trim(sVar)
  18. Dialog.Path = ""
  19. For Each sVar In Split(sVar, gb.Lf)
  20.  If Exist(sVar &/ "etc/os-release") Then
  21.  sText = File.Load(sVar &/ "etc/os-release")
  22.   If InStr(sText, "Manjaro") Then
  23.    Print "\nFound Manjaro on disk: " & sVar
  24.    If Message.Question("Found Manjaro on..\n" & sVar & "\n\nUse /boot/grub/grub.cfg\nfrom this Drive?", "Yes", "Choose grub.cfg file") = 1 Then
  25.    Dialog.Path = sVar &/ "boot/grub/grub.cfg"
  26.    Goto SkipChooser
  27.  
  28.  
  29. Print "Manjaro not auto-detected, opening file requester.."
  30. Message("Select the\n/boot/grub/grub.cfg file\non your Manjaro harddrive")
  31.  
  32. .Title = "Choose grub.cfg"
  33. .Path = "/media" &/ User.Name
  34. .Filter = ["*.cfg", "Grub cfg Files"]
  35. If .OpenFile() Then Return
  36.  
  37. SkipChooser:
  38.  
  39. Print "\nUsing '" & Dialog.Path & "'"
  40. sMGrubLine = GetManjaroLine(Dialog.Path)
  41.  
  42. If Not sMGrubLine Then
  43. Message.Error("Error.\nCould not find initrd line from\n" & Dialog.Path)
  44.  
  45. EditGrub()
  46.  
  47.  
  48.  
  49. Public Sub GetManjaroLine(sFile As String) As String
  50.  
  51. Dim sLines As String[] = Split(File.Load(sFile), gb.Lf)
  52. Dim bActive As Boolean, sLine As String
  53. For iCount As Integer = 0 To sLines.Max
  54. sLine = sLines[iCount]
  55. If InStr(sLine, "--class manjaro") Then
  56. bActive = True
  57. If InStr(sLine, "initrd") Then
  58. Print "\nFound initrd on line " & (iCount + 1) & " in Manjaro grub.cfg\n" & sLine
  59.  Return sLine
  60.  
  61.  
  62. Public Sub EditGrub()
  63. If Not Exist("/boot/grub/grub.cfg") Then
  64. Print "/bot/grub/grub.cfg not found, aborting."
  65. Dim sLines As String[] = Split(File.Load("/boot/grub/grub.cfg"), gb.Lf)
  66. Dim bActive As Boolean
  67. Dim iLine As Integer = 0
  68. Dim sLine As String
  69.  
  70. For iLine = 0 To sLines.Max
  71.  
  72.  If InStr(sLines[iLine], "menuentry 'Manjaro Linux") And (Not InStr(sLines[iLine], "fallback")) Then
  73.   Print "\nFound menuentry: " & Split(sLines[iLine], "'")[1]
  74.   bActive = True
  75.  
  76.  
  77. If InStr(sLines[iLine], "initrd") Then
  78. Print "Changing grub.cfg Line: " & (iLine + 1)
  79. sLines[iLine] = "\t" & sMGrubLine
  80. bActive = False
  81.  
  82. If Access("/boot/grub/grub.cfg", gb.Write) = True Then
  83. Print "Root permissions granted, saving '/boot/grub/grub.cfg'"
  84. File.Save("/boot/grub/grub.cfg", sLines.Join(gb.Lf))
  85. Print "Root permissions not granted, saving as " & User.Home &/ "Desktop/grub.cfg"
  86. File.Save(User.Home &/ "Desktop/grub.cfg", sLines.Join(gb.Lf))
  87. Print "An error occured..\n" & Error.Text
  88.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.