Skip to content

Instantly share code, notes, and snippets.

@windwiny
Last active August 29, 2015 14:10
Show Gist options
  • Save windwiny/f47a628781853629bffa to your computer and use it in GitHub Desktop.
Save windwiny/f47a628781853629bffa to your computer and use it in GitHub Desktop.
fdisk -l output sector to CHS
=begin
fdisk -l output sector to CHS
=end
def sect2chs sec
sec = sec.to_i
return "" if sec == 0
c = sec / (255*63)
sec = sec % (255*63)
h = sec / 63
s = sec % 63 + 1
"#{c}/#{h}/#{s}"
end
def secs2sz size
sz = size.to_i
return "" if sz == 0
sz /= 2
if sz > 1024*1024
"#{sz/1024/1025}GB"
elsif sz > 1024
"#{sz/1024}MB"
else
"#{sz}KB"
end
end
DATA.each_line do |l|
l.strip!
break if l.empty?
dev, startp, endp, size = l.split
v1 = sect2chs(startp)
v2 = sect2chs(endp)
sz = secs2sz(size)
puts %{#{dev} #{'%12s' % startp} #{'%12s' % endp} |#{'%15s' % v1} #{'%15s' % v2} #{sz}}
end
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment