How to use ntfsresize from the command line

Jephe Wu http://linuxtechres.blogspot.com/


In order to resize Windows ntfs partition for whatever reason(e.g. shrink it for installing Linux in free space for multi boot), you should get ntfsresize tool. I recommend you download RIP(Recovery Is Possible) rescue CD(non-X version is enough).

The following is the step by step guide. Actually, the original article is from http://mlf.linux.rulez.org/mlf/ezaz/ntfsresize.html but I couldn't access it anymore, so I also copy it on my blog for easier access for other people.

I'm not responsible for any data loss it could cause.

1. get RIP CD, boot up your Windows server with it, you might want to choose the second GRUB options to skip keyboard map

2. check your Windows ntfs partition first


# fdisk -l /dev/hda

Disk /dev/hda: 255 heads, 63 sectors, 2480 cylinders
Units = cylinders of 16065 * 512 bytes

Device Boot    Start       End    Blocks   Id  System
/dev/hda1   *         1      2479  19912536    7  HPFS/NTFS    
Only one partition, it's /dev/hda1 and NTFS, also marked as bootable

3. Find out where you could resize

# ./ntfsresize --info /dev/hda1
ntfsresize v1.9.4
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 20390432768 bytes (20391 MB)                  
Current device size: 20390436864 bytes (20391 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 7851 MB (38.5%)
Collecting shrinkage constrains ...
Estimating smallest shrunken size supported ...
You might resize at 7850958848 bytes or 7851 MB (freeing 12540 MB).


So we could free over 12 GB disk space using NTFS.

4. Make an ntfsresize test run, using the --no-action option

# ./ntfsresize --no-action --size 11000M /dev/hda1
ntfsresize v1.9.4
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 20390432768 bytes (20391 MB)                  
Current device size: 20390436864 bytes (20391 MB)
New volume size    : 10999996416 bytes (11000 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 7851 MB (38.5%)
Collecting shrinkage constrains ...
Needed relocations : 265947 (1090 MB)
Schedule chkdsk NTFS consistency check at Windows boot time ...
Resetting $LogFile ... (this might take a while)
Relocating needed data ...
100.00 percent completed
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...

The read-only test run ended successfully.


Everything looks good, let's go on

5. Resize NTFS
#
# ./ntfsresize --size 11000M /dev/hda1
ntfsresize v1.9.4
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 20390432768 bytes (20391 MB)
Current device size: 20390436864 bytes (20391 MB)
New volume size    : 10999996416 bytes (11000 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 7851 MB (38.5%)
Collecting shrinkage constrains ...
Needed relocations : 265947 (1090 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 NTFS consistency check at Windows boot time ...
Resetting $LogFile ... (this might take a while)
Relocating needed data ...
100.00 percent completed
Updating $BadClust file ...
Updating $Bitmap file ...
Updating Boot record ...
Syncing device ...
NTFS had been successfully resized on device '/dev/hda1'.
You can go on to resize 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!


6. Optionally you could check your NTFS integrity
#
./ntfsresize --info --force /dev/hda1
ntfsresize v1.9.4
NTFS volume version: 3.1
Cluster size       : 4096 bytes
Current volume size: 10999992320 bytes (11000 MB)                  
Current device size: 20390436864 bytes (20391 MB)
Checking filesystem consistency ...
100.00 percent completed
Accounting clusters ...
Space in use       : 7851 MB (71.3%)
Collecting shrinkage constrains ...
Estimating smallest shrunken size supported ...
You might resize at 7850958848 bytes or 7851 MB (freeing 3149 MB).


Notice that the volume (NTFS) size indeed became 11000 MB but
the device (partition) size is still 20399 MB
7. Repartition the disk
  • do backup first


# sfdisk -d /dev/hda > hda.pt                # saves the partition table
# dd if=/dev/hda of=hda.mbr bs=512 count=1   # saves the MBR



  • Follow the steps below in order.
    • List the partition table. If you should see the correct partition types (NTFS),
    • Temporarily delete the entry for the 1st partition. This is an in-memory operation only.
    • Recreate the 1st partition entry at the same starting cylinder (sector) and using a larger size than above. This is very important because partitioning tools, fdisk included, may round down slightly the provided value to cylinder boundary. This would cause unbootable Windows. The theoretical maximum cylinder size currently is less than 140 MB hence this extra size should be always enough. We also use 11140 MB, it must be safe.
    • Set the partition type to NTFS (type 7).
    • Mark it bootable if it was also marked before.
    • Print the partition table again to check everything is right.
    • Commit the whole process by writing it to disk.
    # fdisk /dev/hda
    
    Command (m for help): p
    
    Disk /dev/hda: 255 heads, 63 sectors, 2480 cylinders
    Units = cylinders of 16065 * 512 bytes
    
    Device Boot    Start       End    Blocks   Id  System
    /dev/hda1   *         1      2479  19912536    7  HPFS/NTFS
    
    Command (m for help): d
    Partition number (1-4): 1
    
    Command (m for help): n
    Command action
    e   extended
    p   primary partition (1-4)
    p
    Partition number (1-4): 1
    First cylinder (1-2480, default 1): 1
    Last cylinder or +size or +sizeM or +sizeK (1-2480, default 2480): +11140M
    
    Command (m for help): t
    Partition number (1-4): 1
    Hex code (type L to list codes): 7
    Changed system type of partition 1 to 7 (HPFS/NTFS)
    
    Command (m for help): a
    Partition number (1-4): 1
    
    Command (m for help): p
    
    Disk /dev/hda: 255 heads, 63 sectors, 2480 cylinders
    Units = cylinders of 16065 * 512 bytes
    
    Device Boot    Start       End    Blocks   Id  System
    /dev/hda1   *         1      1355  10884006    7  HPFS/NTFS
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
  • Next step is also optional but strongly recommended. We check again, using ntfsresize, if we can still access our NTFS. For this purpose one must use both the --info and --force options again.
    # ./ntfsresize --info --force /dev/hda1
    ntfsresize v1.9.4
    NTFS volume version: 3.1
    Cluster size       : 4096 bytes
    Current volume size: 10999992320 bytes (11000 MB)                  
    Current device size: 11145222144 bytes (11146 MB)
    Checking filesystem consistency ...
    100.00 percent completed
    Accounting clusters ...
    Space in use       : 7851 MB (71.3%)
    Collecting shrinkage constrains ...
    Estimating smallest shrunken size supported ...
    You might resize at 7850958848 bytes or 7851 MB (freeing 3149 MB).

    Excellent, still no error.
If you got error, you might need to give a bit more partition size for above fdisk operation.
  • Reboot to Windows to check everything is right (e.g. pressing [Ctrl]-[Alt]-[Del]). Don't get scared when during the boot process Windows will check the filesystem. It was scheduled by ntfsresize for extra safety. If the partition is a system partition then Windows will restart automatically after the filesystem check.
Now you should have unallocated disk space that all Linux distribution installations must be able to handle.

2 comments:

  1. Please not that ntfsresize calculates with powers of 10, not 2. For sufficiently large disks use +sizeMB instead of +sizeM in fdisk

    ReplyDelete
  2. Note if your original partition table reads a starting cylinder of 63, you -must- pass the -c=dos option to fdisk before repartitioning.

    # fdisk -c=dos /dev/sdb

    Otherwise, fdisk default behavior will limit the starting sector to 2048.

    ReplyDelete