I have successfully used ntfsresize and fdisk to shrink my Windows partition on a hard drive that has bad sectors. I am new to partitioning, but the following steps worked for me, so maybe they will help someone else. Many sites warn that these operations are risky and may result in losing all the data on your hard drive, so be sure to back up whatever you want to save if you try this.
My goal was to dual boot with Ubuntu 9.04 and Windows XP (I had only Windows XP). I followed the instructions at https://help.ubuntu.com/9.04/switchi...procedure.html. On Step 6 of that page, I wanted to reduce the size of my Windows partition, but size was not given as an option. (The partitioner listed the "space in use" on my Windows partition as "unknown".) I exited the installation process.
Seeking a more informative partitioning tool, I downloaded pmagic-4.3.iso from partedmagic.com. This Live CD contains a number of useful partitioning tools such as GParted, ntfsresize, and fdisk(8), which I used. Booting from the CD, I opened GParted, right-clicked on my Windows partition, and selected the "information" option. It told me that GParted had detected bad sectors on this partition, but if I wanted to resize this partition anyway, I could return to Windows, run 'chkdsk /f /r', reboot twice, and then use ntfsresize with the --bad-sectors option.
The first time I ran CHKDSK, it seemed to do nothing and found no bad sectors. But then I ended up running it again later (after compressing old files and defragging), and CHKDSK ran for maybe an hour, so long that I didn't watch the whole thing. All I could find afterward was a summary of its log (stored in the "Event Manager"), in which it reported there were indeed 4 bad sectors, but didn't say anything about whether it had "repaired" them. I decided to proceed with the resize.
The man page of ntfsresize (http://man.linux-ntfs.org/ntfsresize.8.html) tells me that the proper way to do this is shrink the filesystem with ntfsresize, and then use fdisk(8) to change the size of the partition to match the new filesystem, but doesn't say how.
The best source I found about how to use ntfsresize+fdisk to resize a partition was in the Parted Magic documentation: http://partedmagic.com/documentation...titioning.html. Another good (if awfully formatted) guide is at http://crashrecovery.org/CrashRecove...OWTO.ntfs.html. I also benefitted from http://ubuntuforums.org/archive/inde...t-1069748.html. Now here is what I did.
Boot the machine from the Parted Magic CD. Open a Terminal (find it in the main menu). I want to know what Parted Magic calls my hard drive so I type:
Code:
root@PartedMagic:~# fdisk -l
Disk /dev/hda: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x40000000
Device Boot Start End Blocks Id System
/dev/hda1 1 10 80293+ de Dell Utility
/dev/hda2 * 11 4864 38989755 7 HPFS/NTFS
The answer is "/dev/hda". Now in my case there are two partitions, and the Windows one is called: "/dev/hda2". I ask ntfsresize how much it thinks I could shrink the Windows partition, including the --badsectors option since I know my disk has that problem:
Code:
root@PartedMagic:~# ntfsresize -i --bad-sectors /dev/hda2
ntfsresize v2.0.0 (libntfs 10:0:0)
Device name : /dev/hda2
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 39925506560 bytes (39926 MB)
Current device size: 39925509120 bytes (39926 MB)
WARNING: This software has detected that the disk has at least 1 bad sector.
WARNING: Bad sectors can cause reliability problems and massive data loss!!!
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use : 16866 MB (42.2%)
Collecting resizing constraints ...
You might resize at 16865632256 bytes or 16866 MB (freeing 23060 MB).
Please make a test run using both the -n and -s options before real resizing!
In theory, I could resize to 16866 MB, but this is just an estimate. Playing it safe, I decide to resize to 19000MB. I want to make sure there are no unexpected problems so I do a test run of the filesystem resize operation (the --no-action prevents it from changing anything, the -b is short for --bad-sectors):
Code:
root@PartedMagic:~# ntfsresize --no-action -b --size 19000M /dev/hda2
ntfsresize v2.0.0 (libntfs 10:0:0)
Device name : /dev/hda2
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 39925506560 bytes (39926 MB)
Current device size: 39925509120 bytes (39926 MB)
New volume size : 18999992832 bytes (19000 MB)
WARNING: This software has detected that the disk has at least 1 bad sector.
WARNING: Bad sectors can cause reliability problems and massive data loss!!!
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use : 16866 MB (42.2%)
Collecting resizing constraints ...
Needed relocations : 972361 (3983 MB)
Schedule chkdsk for NTFS consistency check at Windows boot time ...
Resetting $LogFile ... (this might take a while)
Relocating needed data ...
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
The read-only test run ended successfully.
Since the process was successful in theory, now I do it for real. This is the step that is dangerous to my data:
Code:
root@PartedMagic:~# ntfsresize -b --size 19000M /dev/hda2
ntfsresize v2.0.0 (libntfs 10:0:0)
Device name : /dev/hda2
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 39925506560 bytes (39926 MB)
Current device size: 39925509120 bytes (39926 MB)
New volume size : 18999992832 bytes (19000 MB)
WARNING: This software has detected that the disk has at least 1 bad sector.
WARNING: Bad sectors can cause reliability problems and massive data loss!!!
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use : 16866 MB (42.2%)
Collecting resizing constraints ...
Needed relocations : 972361 (3983 MB)
WARNING: Every sanity check passed and only the dangerous operations left.
Make sure that important data has been backed up! Power outage or computer
crash may result major data loss!
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)
Relocating needed data ...
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
Syncing device ...
Successfully resized NTFS on device '/dev/hda2'.
You can go on to shrink the device for example with Linux fdisk.
IMPORTANT: When recreating the partition, make sure that you
1) create it at the same disk sector (use sector as the unit!)
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 won't be able to access NTFS or can't boot from the disk!
If you make a mistake and don't have a partition table backup then you
can recover the partition table by TestDisk or Parted's rescue mode.
Now the filesystem has been resized to 19000MB. (I can type 'disktype /dev/hda' for information confirming this). I ask fdisk for the partition table so I can read the current settings:
Code:
root@PartedMagic:~# fdisk -l
Disk /dev/hda: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x40000000
Device Boot Start End Blocks Id System
/dev/hda1 1 10 80293+ de Dell Utility
/dev/hda2 * 11 4864 38989755 7 HPFS/NTFS
For my Windows partition, the starting cylinder is 11, the partition type is 7 (NTFS), and the bootable flag is on. I am going to edit the partition table and replace the entry for the Windows partition with a new entry that makes the partition smaller, but otherwise identical to the old one. I enter the fdisk dialogue:
Code:
root@PartedMagic:~# fdisk /dev/hda
The number of cylinders for this disk is set to 4864.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
I delete the Windows partition, in my case partition 2:
Code:
Command (m for help): d
Partition number (1-4): 2
Then I make a new partition. It is a primary partition, partition number 2 (like the old one). The default starting cylinder was the correct value in this case, 11. For the size, I specify 19000M, the same value I shrank the filesystem to. (I saw ntfsresize round that requested size down, and fdisk round it up, so the filesystem was correctly contained inside the partition.):
Code:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (11-4864, default 11): 11
Last cylinder, +cylinders or +size{K,M,G} (11-4864, default 4864): +19000M
Set the type to 7, as it was before:
Code:
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 7
Changed system type of partition 2 to 7 (HPFS/NTFS)
Next I want to make sure the partition is still marked as bootable, so I check the table:
Code:
Command (m for help): p
Disk /dev/hda: 40.0 GB, 40007761920 bytes
255 heads, 63 sectors/track, 4864 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x40000000
Device Boot Start End Blocks Id System
/dev/hda1 1 10 80293+ de Dell Utility
/dev/hda2 11 2433 19462747+ 7 HPFS/NTFS
It isn't listed as bootable, so I use the 'a' command to toggle the bootable flag:
Code:
Command (m for help): a
Partition number (1-4): 2
Now I type 'w' to save the new partition table and exit.
Code:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
Then I shut down Parted Magic and rebooted into Windows to let it run its checks and make sure everything was ok. All was well, so I tried again with the Ubuntu install process, and the install CD was able to find the newly freed space on my drive and automatically put Ubuntu there and set things up for dual boot. Now I have a successfully dual booting machine despite bad sectors.
Thanks to everyone out there who put up enough information that I was able to do it eventually! I hope this post might speed up the process for someone else.