Converting GRUB entries (Linux)

GRUB device entries are interesting. They’re nothing like what’d you see in Linux or otherwise, and one could wonder what kind of voodoo goes on in the back-end to map GRUB entries to /dev/hda1..2..3.. and so forth.

When I started using GRUB, I used tab completion, and I was fortunately blessed by some sort of Linux/Unix voodoo that i’d usually get things right. Up until now, I used the guess what partition it is game for when things didn’t work out so well.

Thanks to a comment posted here, the naming scheme became more apparent. And so did a simple script I was able to hack up in less than 5 minutes time*. It is far from perfect (and i’m not a real expert in Ruby, i’m just a poser–honest), but here it is:

#!/usr/bin/ruby -w

class GrubConvert
	def initialize(basedir,device,subletter,number)
		@lookup = { 'a' => '0', 'b' => '1', 'c' => '2', 'd' => '3', 'e' => '4', 
'f' => '5', 'g' => '6', 'h' => '7', 'i' => '8', 'j' => '9', 'k' => '10', 'l' => '11', 
'm' => '12', 'n' => '13', 'o' => '14', 'p' => '15', 'q' => '16', 'r' => '17', 
's' => '18', 't' => '19', 'u' => '20', 'v' => '21', 'w' => '22', 'x' => '23', 
'y' => '24', 'z' => '25' }	
		@basedir = basedir
		@device = device
		@subletter = subletter
		@number = number
	end

	def convert_to_grub
		first_g_number = @lookup[@subletter]	
		second_g_number = @number.to_i - 1
		puts "(#{@device}#{first_g_number},#{second_g_number})"
	end

	def convert_to_drive
		sub_num_to_l = @lookup.index(@subletter)
		second_g_number = @number.to_i + 1
		puts "#{@basedir}#{@device}#{sub_num_to_l}#{second_g_number}"
	end	
end

if ARGV[0] =~ /(.*)\/(..)(.)(.*)/ 
	entry = GrubConvert.new($1,$2,$3,$4)
	entry.convert_to_grub
elsif ARGV[0] =~ /(..)(.),(.)/ # the cmd line arg may need to be quoted
	todrive = GrubConvert.new('/dev/',$1,$2,$3)
	todrive.convert_to_drive
else
	puts "Is that really a disk drive? (couldn't find out if what you entered: 
#{ARGV[0]} matched a disk device)"
end

* This also means that it hasn’t been throughly checked and flogged of bugs and/or appearances.

Advertisements

One response to “Converting GRUB entries (Linux)

  1. Pingback: Dual booting between Windows and Linux (on separate hard disks) « Coding + computing = fun

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: