Bad Sector Recovery on NTFS
ATTENTION: This information is out of date. Inspired by this article, the NTFS tools (see below) now have a built-in function to reset bad clusters.
The Problem:
I wanted to install WinXP SP1 on my harddrive. Unfortunately my partition C: was too small (of course). So I decided to resize the partition (which meant I had to lose some space on the subsequent drive D:). Unfortunately my disk had a bad sector and as one might know, partitioning without data loss is not allowed by any partitioning tool in the presence of bad sectors. So I thought: Bad sectors are really bad anyway and there’s still warranty on the drive so let’s have it changed… and I got myself a new drive.
I copied the whole content using DriveImage which worked marvelous, well even too well: It not just copied all the blocks but also the information about which blocks were defect
That’s not a problem of the tool but rather a problem of the file format. Anyway: Now my new hard drive had sector tagged as “bad” which actually were perfectly alright. This now really p***ed me off because that meant that I was not able to repartition without data loss again! So my task for the week was to find out how to remove this wrong entry about wrong blocks from my drive. I had to find one bit or so out of 6,400,000,000,000!
The solution
I mean normally that’s not a problem because nowadays somebody has tools, powerful tools like e.g. Partition Magic, that for FAT or FAT32 partitions respectively, can do the job for you:
- For FAT or FAT32 Partitions just install Partition Magic (Version 4.0 is enough) and perform a “bad sector retest”.
However – and that’s the part about Microsoft now – NTFS is a protected format. Microsoft provides no documentation about it and so there are no tools available on the marked which could do the same for NTFS. Some smart people gave me the hint: Well but that’s not a problem because WinXP has chkdsk and using chkdsk /R should reset such blocks. If you read the documentation about this parameter you will find that it is a bit ambiguous: Locates bad sectors and recovers readable information. One could read that as recovers actually readable blocks. But it doesn’t! It only tries to find real bad blocks and tags them as bad. So
- chkdsk /R does not work in any case for this problem!
So if there is apparently not Win32 tool support what should one do? Of course: Look for Unix tools. Unfortunately NTFS support is still very rudimentary on Unix at the moment because one has to reverse engineer the format. But however I was lucky enough to find the Linux-NTFS Project which provides the Linux Power Tools. And I was lucky enough that they were open source. That’s because I actually had to patch one line of code to make it work for my purpose (thanks to szaka at this point). So here is how it works:
- For NTFS partitions:
- If you have also a Unix installed you are lucky – go to 2. If not, go to e.g. Knoppix.de and get yourself a Linux that boots entirely from CD-ROM. (Though it’s a German domain, the site and software are also available in English.)
- Download a pre-patched version of the ntfs tools here and extract it.
- or -
Download the original NTFS Tools source code from here.
Extract the archive to a <source directory>, edit the file \ntfsprogs\ntfsresize.c with a text editor and add a
return 0;
right at the beginning of the method has_bad_sectors. This will tell the tool that there are just no bad sectors – regardless what there is written on the disk. - Start Unix/Linux and copy the <source directory> to a location that is editable (e.g. /ramdisk on Knoppix).
- Start a terminal and change to the directory containing the <source directory> and make all the contents writable and executable:
chmod -R 777 <source directory> - Now perform the following to compile the patched version:
cd <source directory>
./configure
make
cd ntfsprogs/
su - Now run the tool and write down the volume size (marked italic). Substitute <part> with the name of your actual partition.
./ntfsresize –info <part>
ntfsresize v1.8.2
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 69561159680 bytes (69562 MB)
Current device size: 69561160704 bytes (69562 MB)
Checking filesystem consistency …
100.00 percent completed
Accounting clusters …
Space in use : 19915 MB (28,6%)
Calculating smallest shrunken size supported …
File feature Last used at By inode
$MFT : 15415 MB 0
$MFTMirr : 13794 MB 1
Compressed : 24367 MB 75538
Sparse : 1179 MB 25545
Multi-Record : 24368 MB 7879
Ordinary : 24369 MB 66633
You could resize at 24368844800 bytes or 24369 MB (freeing 45193 MB). - Unmount the partition:
umount <part> - Perform the action:
./ntfsresize -s <written down volume size> <part>
ntfsresize v1.8.2
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 69561159680 bytes (69562 MB)
Current device size: 69561160704 bytes (69562 MB)
New volume size : 69561159680 bytes (69562 MB)
Checking filesystem consistency …
100.00 percent completed
Accounting clusters …
Space in use : 19915 MB (28,6%)
Needed relocations : 0 (0 MB)
WARNING: Every sanity check passed and only the DANGEROUS operations left.
Please make sure all your important data had been backed up in case of an
unexpected failure!
Are you sure you want to proceed (y/[n])? y
Schedule chkdsk for NTFS consistency check at Windows boot time …
Resetting $LogFile … (this might take a while)
Updating $BadClust file …
Updating $Bitmap file …
Updating Boot record …
Syncing device …
Successfully resized NTFS on device ‘/dev/hdg5′.
You can go on to shrink the device e.g. with ‘fdisk’.
IMPORTANT: When recreating the partition, make sure you
1) create it with the same starting disk cylinder
2) create it with the same partition type (usually 7, HPFS/NTFS)
3) do not make it smaller than the new NTFS filesystem size
4) set the bootable flag for the partition if it existed before
Otherwise you may lose your data or can’t boot your computer from the disk!
(a full console log of the session is available here)
Done! That was easy, wasn’t it ?
Probably somebody should write a script for that…





