# repair bad block



## douglasfim (Nov 5, 2011)

how do I repair bad sectors in FreeBSD?

I use SATAII


----------



## T-Daemon (Nov 5, 2011)

http://forums.freebsd.org/showthread.php?t=1823



			
				SirDice said:
			
		

> Filesystem errors are fixed by using fsck. A real bad sector however isn't. If you have an IDE or S-ATA disk and it's showing bad sectors replace the disk as soon as possible.


----------



## jb_fvwm2 (Nov 5, 2011)

I'd go so far as to copy critical data to a thumbdrive before shutdown. (A disk just failed here that, while totally unreadable, still shows smartctl data with only the
spinup time unreasonable. ) The next boot and maybe no access to it. (Just restored
99 percent of the data yesterday, thanks to 
	
	



```
rsync, fdisk with parameters from a file,
sysinstall, escape to loader prompt, bsdlabel, shell-saved-history, newfs, mkdir -p, chflags,
```
 too complex to writeup except locally. (Not from the unreadable drive though.)


----------



## douglasfim (Nov 6, 2011)

is to resolve this before installing FreeBSD?


----------



## kpa (Nov 6, 2011)

Bad blocks should be mapped out by drive firmware pretty much automatically. If you see a real bad block on a modern hard drive it means that the drive firmware has run out of spare blocks to use in place of bad blocks, throw the drive away and buy a new one.


----------



## douglasfim (Nov 6, 2011)

I already have FreeBSD installed on my notebook, I want to test the new FreeBSD on my desktop, even though I lost 100GB, no problem.

I just want to test.


----------



## wblock@ (Nov 6, 2011)

Are you saying you have a desktop drive with bad sectors?  Write all zeros to it with dd until *smartctl -a* doesn't report any new reallocated sectors.  Or just install FreeBSD.  When a bad block is written, the drive should swap it out.  Of course the drive could fail entirely at any moment.


----------



## douglasfim (Nov 6, 2011)

When I install FreeBSD, there is a lot of error and restarts. Finally managed to install, I typed 
	
	



```
[FILE]fsck -y[/FILE]
```
 and is running many days.


----------



## wblock@ (Nov 6, 2011)

Sounds like the drive is failing.
Run smartctl to check reallocated_sector_count:
`# smartctl -a /dev/ad0`

Then wipe the whole drive with zeros:
`# dd if=/dev/zero of=/dev/ad0 bs=64k`

Repeat that until reallocated_sector_count stops going up, or the drive fails.

Just out of curiosity, what brand of drive is this?


----------



## douglasfim (Nov 7, 2011)

Western digital. This will erase the entire hard drive?
[cmd=]dd if=/dev/zero of=/dev/ad0 bs=64k[/cmd]

Is the same driver that is installed FreeBSD. I have to use a live CD? I'm not getting the system starts, it asks to do the fsck. *fsck* never ends.


----------



## wblock@ (Nov 7, 2011)

douglasfim said:
			
		

> western digital
> 
> this will erase the entire hard drive?
> 
> ...



Yes.

The drive might be a different number, ad4 or ad6 or even ada0.  That depends on the computer and version of FreeBSD.



> is the same driver that is installed FreeBSD



Yes.



> I have to use a live CD?
> 
> I'm not getting the system starts, it asks to do the fsck.



The first time, boot in single user mode.  dd is in /bin and should be available.  After you run it, everything will be gone and you'll have to use a live CD anyway.  I recommend mfsBSD.

Realize that the fsck is showing that all those spots went bad after installing FreeBSD.  That drive is probably not going to work much longer.


----------



## douglasfim (Nov 7, 2011)

zfs is better to use this driver? mfsBSD is to install or to use only as liveCD. When installing FreeBSD have to format, or just install.


----------



## wblock@ (Nov 7, 2011)

If the drive is failing, no filesystem can make up for that.  If it's not failing, UFS is fine.

mfsBSD is a liveCD meant to be used for this kind of thing, not to install.


----------



## douglasfim (Nov 12, 2011)

I'm confused.

dd > format > install > fsck

or

dd > fsck > format > install


----------



## FestusHagen (Nov 12, 2011)

If ya must.

Boot LiveCD > dd > Boot Install CD > Install
If fails > repeat until your heart is content that the drive is truly JUNK!!

-Enjoy
fh : )_~


----------



## Beastie (Nov 12, 2011)

Running *fsck* on a brand new filesystem that was created on a disk that has just been wiped out entirely is pretty much useless.
Right after *dd*-ing the disk, run *smartctl* again (available on the *mfsBSD* disc) and see if things have changed (especially Reallocated_Sector_Ct, Current_Pending_Sector, Offline_Uncorrectable).
Do what wblock@ says in post #7.

Now if you only want to test FreeBSD and don't care about your disk potentially failing at any time, then just *dd* the disk and install FreeBSD. "Formatting" (i.e. running *newfs*) is done automatically when you install FreeBSD.
Note that the installation may return errors or completely fail depending on the status of the disk and the location of the bad blocks.


----------



## wblock@ (Nov 12, 2011)

douglasfim said:
			
		

> I'm confused.
> 
> dd > format > install > fsck
> 
> ...



Neither.  fsck is to repair a filesystem, and there won't be anything left on the disk after dd finishes.

Run *smartctl* and look at the reallocated sector count.

dd zeros to the whole drive.
Check the reallocated sector count with smartctl again.  If it has increased, use dd again.

Repeat until the reallocated sector count doesn't go up.  Or until the drive dies, which is fairly likely.

If you manage to "fix" it, the reallocated sector count will stop going up.  Then just reinstall as normal.


----------



## douglasfim (Nov 12, 2011)

Linux has the command badblocks would have an equivalent in FreeBSD?

badsect is similar to the badblocks?


----------



## wblock@ (Nov 12, 2011)

Yes, both are appropriate for 25-year-old drives that don't have error correction, and already have a filesystem on them.  Is there some reason you can't run dd?


----------



## douglasfim (Nov 12, 2011)

I am running dd â€‹â€‹is so far running.

The smartctl saves everything in HD? after formatting will lose everything?


----------



## wblock@ (Nov 12, 2011)

smartctl just reports the drive SMART information.  Formatting (or dd) does not erase that information.


----------



## douglasfim (Aug 11, 2012)

I had forgotten about this topic.

I removed the HD and buy another.

Thank you all.


----------

