Here comes a memo to resize an NTFS partition using commandline with ntfsresize
(from the ntfs-3g / ntfsprogs
package) and fdisk
, that should work for Windows XP-to-8 versions. Note that GParted does all the following for MBR/DOS as well as for EFI/GPT drives if ntfs-3g / ntfsprogs is installed. My references are at the end.
OK in this scenario I have a MBR-partitionned 149 GB disk with one single NTFS partition (Windows C:). I'll resize it to 20 GB (As @sourcejedi said it well: BEWARE UNITS) in order to make room for new partitions, eg. Data and/or another OS.
Pre-requisite
- Clean NTFS filesystem: The linux-based NTFS tools will refuse to touch a NTFS filesystem unless it is already perfectly consistent, so as to minimize the chance of data loss. If not, one has to use
ntfsfix
(from Linux), orchkdsk /f
(from Windows). - Backup the important data (eg. using ntfsclone as @peterph wrote above)
- Know which device and partition I want to resize, /dev/sdb1 in this case.
A. Shrink the filesystem
Check the device to ensure it's ready to be resized:
~ ntfsresize --check /dev/sdb
Any warning at this point should be addressed before processing further.
Check the size this filesystem can be resized to:
~ ntfsresize --info /dev/sdb > ... > You might resize at 16865632256 bytes or 16866 MB (freeing 112060 MB). > ...
Test how shrinking to 18 GB will work (we'll make that 20 GiB in a minute):
~ ntfsresize --no-action --size 18G /dev/sdb1 > ... > The read-only test run ended successfully.
Resize to 18 GB :
~ ntfsresize -v --size 18G /dev/sdb1 > ... > Successfully resized NTFS on device '/dev/sdb1'. > 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! > ...
Tip: if the disk has known problems such as bad sectors, add the '--bad-sectors
' option.
B. Resize the partition
In the previous step, we shrunk the filesystem, not the partition as we can check with:
~ disktype /dev/sdb
> Block device, size 149.1 GiB (160041885696 bytes)
> DOS/MBR partition map
> Partition 1: 149.1 GiB (138602266624 bytes, 8459611 clusters of 16 KiB, bootable)
> Volume size 18.37 GiB (20799750144 bytes, 1269516 clusters of 16 KiB)
As you can see, the volume size and partition value differ, with Partition 1 still filling the drive. At this stage, it is necessary to delete the partition and create a smaller one to match the new size of the NTFS filesystem. As my disk has a DOS/MBR partition table, I'll do that with fdisk
(@peterph has an answer above for GPT):
~ fdisk /dev/sdb
Delete the partition
Type 'p' to display the partitions on that disk, and note which one contains the resized filesystem.
Typing ‘d’ will ask which partition you wish to delete
> Command (m for help): d > partition number (1-3, last one by default) : 1
Re-create the partition
Now recreate the partition smaller, keeping it primary and first in order:
> Command (m for help): n > Command action > e extended > p primary partition (1-4) p > Partition number (1-4): 1 > First cylinder (1-31, default 1): 1
Since I shrunk the filesystem to 18GB, let's make the partition 20GiB:
> Last cylinder or +size or +sizeM or +sizeK (1-31, default 31): +20G
We know the filesystem is NTFS, so use the command action ‘t’ for type and the listed hex code ‘7’ for NTFS:
> Command (m for help): t > Selected partition 1 > Hex code (type L to list codes): 7 > Changed system type of partition 1 to 7 (HPFS/NTFS)
Next I want to make sure the partition is still marked as bootable:
> Command (m for help): p Device Boot Start End Blocks Id System /dev/sdb1 63 40644449 40644387 7 HPFS/NTFS
Make the new partition bootable by adding the boot flag:
> Command (m for help): a > Partition number (1-4): 1
Write the altered partition table to disk and exit fdisk with the command action ‘w':
> Command (m for help): w
Now a final resize of the filesystem
ntfsresize without the 'size' switch will make sure it uses the whole partition's available space (whole of it and no more - thanks @sourcejedi)
~ ntfsresize -v /dev/sdb1
Now we can check whether the partitions and filesystems on the drive are on pair:
~ disktype /dev/sdb
> ...
> Partition 1: 20.0 GiB (21474836480 bytes, 40644387 sectors from 63, bootable)
> Type 0x07 (HPFS/NTFS)
> Windows NTLDR boot loader
> NTFS file system
> Volume size 19.98 GiB (21453361644 bytes, 1269516 clusters of 16 KiB)
References