Created
November 15, 2013 15:19
-
-
Save jordimassaguerpla/7485966 to your computer and use it in GitHub Desktop.
a script to change the default boot in grub 2. It parses the grub2 configuration to show you the options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
GRUB_CFG = "/mnt/boot_loader/boot/grub/grub.cfg" | |
def read_menu_entries(cfg_file) | |
menu_entries = [] | |
File.readlines(cfg_file).each do |line| | |
next if !line.include?("menuentry") | |
entry = line.match(/menuentry.*['"](.*)['"].*/)[1] | |
menu_entries << entry | |
end | |
menu_entries | |
end | |
def usage_and_exit(menu_entries) | |
puts "Usage: set_default_boot N" | |
puts " where N should be one of the numbers:" | |
menu_entries.length.times do |i| | |
puts "#{i}: #{menu_entries[i]}" | |
end | |
exit -1 | |
end | |
if !File.exists?(GRUB_CFG) | |
puts "I can't find configuration file for grub #{GRUB_CFG}" | |
exit -1 | |
end | |
option = ARGV[0].to_i | |
menu_entries = read_menu_entries(GRUB_CFG) | |
usage_and_exit(menu_entries) unless ARGV.length ==1 | |
usage_and_exit(menu_entries) unless option < menu_entries.length | |
grub_cfg = File.read(GRUB_CFG) | |
grub_cfg.gsub!(/set default="\d"/, "set default=\"#{option}\"") | |
File.open(GRUB_CFG, "w") { |file| file.write(grub_cfg) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment