← Back to the Blog

How to repair bad sectors on HDD in OpenBSD (Part I)

Relocate bad sectors:

 

The IDE or SATA disk will not automatically reallocate any bad sector unless it is overwritten. Any attempt to read it will fail with an "uncorrectable data error" message.

Obviously the workaround is to overwrite the bad sector by hand. so make sure to backup everything you can from the partition where the bad sector is located. get the partition and the bad sector number from the log (wd0g, 29041232 from below example)

 

I'm getting following errors in /var/log/messsages:

Jul  4 17:06:37 odin /bsd: wd0g: uncorrectable data error reading fsbn 29041232 of 29041216-29041247 (wd0 bn 102442224; cn 6376 tn 187 sn 3)

 

stop, kill corresponding services and unmount /dev/wd0g

(and by the way, running fsck -f does nothing here)

 

now let's reallocate the bad sector, using dd utility

Dec  7 16:56:53 odin /bsd: wd0g: uncorrectable data error reading fsbn 31099744 of 31099744-31099775 (wd0 bn 104500736; cn 6504 tn 221 sn 53), retrying
Dec  7 16:56:53 odin /bsd: wd0g: uncorrectable data error reading fsbn 31099751 of 31099744-31099775 (wd0 bn 104500743; cn 6504 tn 221 sn 60), retrying
umount /dev/wd0g
check that it is really a bad sector:
dd if=/dev/rwd0g skip=31099751 of=/dev/null count=1

you are supposed to get more uncorrectable data error messages. If you do't, then probably the sector number is wrong. Try again until you find the bad sector number.

 

now let's go ahead and overwrite it:

dd if=/dev/zero of=/dev/rwd0g seek=31099751 count=1

 

try to read again:

dd if=/dev/rwd0g skip=29041232 of=/dev/null count=1

 

repeat for all bad sectors reported in /var/log/messages

dd if=/dev/zero of=/dev/rwd0g seek=29041235 count=1
dd if=/dev/rwd0g skip=29041235 of=/dev/null count=1
dd if=/dev/zero of=/dev/rwd0g seek=29041235 count=1

the read attempt will now succeed and we should get output like this:

root@odin:~ # dd if=/dev/zero of=/dev/rwd0g seek=29041232 count=1
1+0 records in
1+0 records out
512 bytes transferred in 0.022 secs (22485 bytes/sec)
root@odin:~ # dd if=/dev/rwd0g skip=29041232 of=/dev/null count=1 
1+0 records in
1+0 records out
512 bytes transferred in 0.014 secs (34323 bytes/sec)

now run fsck -f , mount the partition, check /var/log/messages

 

check older messages files to confirm that no other blocks/sectors where affected:

zcat /var/log/messages.*.gz | grep -v 31099744 | grep -v 31099751
fsck -f /dev/wd0g
badblocks -v /dev/wd0g

 

restart services, check the logs

Be the first to reply