Simple script to list grub os's and boot one.

Post

Posted
Rating:
#1 (In Topic #882)
Guru
BruceSteers is in the usergroup ‘Guru’
here is a simple script the utilizes the grub-reboot command to select what os to boot into the next boot only. (useful if you have a few test OS's to choose from)

It reads grub.cfg and gets the main entries then lists them.
Double click an os to have it as the next boot os. then after rebooting out of that os it reverts to the default.

Note.
/etc/default/grub file MUST have the default line set to "saved" like this…

GRUB_DEFAULT=saved
if you changed the default to saved from something else then you have to run update-grub

then save this script somewhere, set it's exe flag and launch it.

Code (gambas)

  1. #!/usr/bin/env gbs3
  2. ' Gambas script file
  3.  
  4. Use "gb.gui"
  5. Use "gb.desktop"
  6.  
  7.  
  8. Private iTextLen As Integer
  9.  
  10. Public Sub Main()
  11.  
  12.   Dim sRet As String, aLines As String[]
  13.  
  14.   With fForm = New Form
  15.     .Spacing = True
  16.     .Margin = True
  17.     .Title = "Select OS to boot next boot"
  18.     .Arrangement = Arrange.Vertical
  19.     .Foreground = Color.Magenta
  20.    End With
  21.  
  22.   lList = New ListBox(fForm) As "List"
  23.   lList.Font = Font["Ubuntu,16"]
  24.   lList.Expand = True
  25.  
  26.   aLines = GetMainEntries()
  27.  
  28.   lList.List = aLines.Copy()
  29.   lList.Background = Color.SetAlpha(Color.Cyan, 220)
  30.   fForm.Width = iTextLen + (Desktop.Scale * 3)
  31.   fForm.Height = (lList.List.Count + 4) * lList.Font.Height
  32.  
  33.   With cBox = New CheckBox(fForm)
  34.   .Text = "Reboot after setting. (no confirmation)"
  35.  
  36.   fForm.Show
  37.  
  38.  
  39. Public Sub List_Activate()
  40.  
  41.   Desktop.RunAsRoot("grub-reboot \"" & Last.Text & "\"", True)
  42.   fForm.Hide()
  43.   If Not cBox.Value Then
  44.     If Message.Question("Next reboot set to...\n " & Last.Text, "Reboot now", "Reboot Later") <> 1 Then Return
  45.   Shell "reboot"
  46.   fForm.Close()
  47.  
  48.  
  49. Public Sub GetMainEntries() As String[]
  50.  
  51.   Dim sEntries As New String[]
  52.   Dim sLines As String[] = Split(File.Load("/boot/grub/grub.cfg"), "\n", Null, True)
  53.  
  54.   Dim bInSub As Boolean
  55.   Dim iDepth As Integer
  56.  
  57.   For Each s As String In sLines
  58.  
  59.     If bInSub Then
  60.       If LTrim(s) Begins "menuentry " Then
  61.         Inc iDepth
  62.       Else If Trim(s) = "}" Then
  63.         Dec iDepth
  64.         If iDepth < 0 Then bInsub = False
  65.  
  66.       Endif
  67.     Else
  68.       If LTrim(s) Begins "menuentry " Then
  69.         sEntries.Add(Split(s, If(InStr(s, "'"), "'", "\""))[1])
  70.         iTextLen = Max(iTextLen, lList.Font.TextWidth(sEntries.Last))
  71.       Else If LTrim(s) Begins "submenu " Then
  72.         Idepth = 0
  73.         bInSub = True
  74.       Endif
  75.  
  76.     Endif
  77.  
  78.   Next
  79.  
  80.   Return sEntries
  81.  
  82.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.