Skip to content

Instantly share code, notes, and snippets.

@jgrevich
Created April 9, 2019 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgrevich/624d687d8c323597561bf9f63b4d54eb to your computer and use it in GitHub Desktop.
Save jgrevich/624d687d8c323597561bf9f63b4d54eb to your computer and use it in GitHub Desktop.
Automate process of running bad blocks based on LBA number of errors found in smartctl
#!/usr/local/rvm/rubies/ruby-2.6.2/bin/ruby
# b = (int)((L-S)*512/B)
# where:
# b = File System block number
# B = File system block size in bytes
# L = LBA of bad sector
# S = Starting sector of partition as shown by fdisk -lu and (int) denotes the integer part.
lba = ARGV[0].to_i
dev = ARGV[1] || 'add_your_device_label_here'
block_range = ARGV[2].to_i || 64
iterations = ARGV[3].to_i || 100
block_number = ((lba - 2048)*512)/4096
bb_cmd = "sudo badblocks -b 4096 -p #{iterations} -s -vw /dev/#{dev} #{block_number+block_range} #{block_number-block_range}"
puts "execute command: #{bb_cmd}? [y/N]"
run_command = $stdin.gets.chomp
if run_command == ("y" || "Y")
`#{bb_cmd}`
else
puts "run: #{bb_cmd}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment