Resize an NTFS partition using Linux
Today I spent some time resizing an existing ntfs partition, in order to make space for an LFS build.
Here's the existing partition structure:
# fdisk -l -u /dev/sdb Disk /dev/sdb: 465.8 GiB, 500106780160 bytes, 976771055 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xa7cea7ce Device Boot Start End Sectors Size Id Type /dev/sdb1 * 2048 206847 204800 100M 7 HPFS/NTFS/exFAT /dev/sdb2 206848 976766975 976560128 465.7G 7 HPFS/NTFS/exFAT
Partition sdb2 contains a previous Windows installation, which I no longer require, except for approximately 350Gb of user data. We can delete unneccessary files and use ntfsresize to shrink the filesystem.
Resize the filesystem
First, check the filesystem for errors:
# ntfsresize --check /dev/sdb2 ntfsresize v2016.2.22AR.1 (libntfs-3g)
Next, run ntfsresize --info to see what size the filesystem can shrink to:
# ntfsresize --info /dev/sdb2 ntfsresize v2016.2.22AR.1 (libntfs-3g) Device name : /dev/sdb2 NTFS volume version: 3.1 Cluster size : 4096 bytes Current volume size: 500000870912 bytes (500001 MB) Current device size: 500000873984 bytes (500001 MB) Checking filesystem consistency ... 100.00 percent completed Accounting clusters ... Space in use : 351792 MB (70.4%) Collecting resizing constraints ... You might resize at 351791267840 bytes or 351792 MB (freeing 148209 MB). Please make a test run using both the -n and -s options before real resizing!
Test whether shrinking will work:
# ntfsresize --no-action --size 352G /dev/sdb2 ntfsresize v2016.2.22AR.1 (libntfs-3g) Device name : /dev/sdb2 NTFS volume version: 3.1 Cluster size : 4096 bytes Current volume size: 500000870912 bytes (500001 MB) Current device size: 500000873984 bytes (500001 MB) New volume size : 351999996416 bytes (352000 MB) Checking filesystem consistency ... 100.00 percent completed Accounting clusters ... Space in use : 351792 MB (70.4%) Collecting resizing constraints ... Needed relocations : 488282 (2001 MB) 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 ... The read-only test run ended successfully.
Resize the filesystem:
# ntfsresize -v --size 352G /dev/sdb2 ntfsresize v2016.2.22AR.1 (libntfs-3g) Device name : /dev/sdb2 NTFS volume version: 3.1 Cluster size : 4096 bytes Current volume size: 500000870912 bytes (500001 MB) Current device size: 500000873984 bytes (500001 MB) New volume size : 351999996416 bytes (352000 MB) Checking for bad sectors ... Checking filesystem consistency ... 100.00 percent completed Accounting clusters ... Space in use : 351792 MB (70.4%) Collecting resizing constraints ... Needed relocations : 488282 (2001 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 ... Updating $BadClust file ... Updating $Bitmap file ... Updating Boot record ... Syncing device ... Successfully resized NTFS on device '/dev/sdb2'. 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.
After resizing the filesystem will be flagged dirty, run ntfsfix to fix this:
# ntfsfix -d /dev/sdb2 Mounting volume... OK Processing of $MFT and $MFTMirr completed successfully. Checking the alternate boot sector... OK NTFS volume version is 3.1. NTFS partition /dev/sdb2 was processed successfully.
Resize the partition
Once the filesystem has been shrunk, the partition can be resized. Use disktype to check the volume and partition size:
# apt-get install disktype
# disktype /dev/sdb --- /dev/sdb Block device, size 465.8 GiB (500106780160 bytes) DOS/MBR partition map Partition 1: 100 MiB (104857600 bytes, 204800 sectors from 2048, bootable) Type 0x07 (HPFS/NTFS) NTFS file system Volume size 100.0 MiB (104857088 bytes, 204799 sectors) Partition 2: 465.7 GiB (499998785536 bytes, 976560128 sectors from 206848) Type 0x07 (HPFS/NTFS) NTFS file system Volume size 329.7 GiB (353999998976 bytes, 691406248 sectors)
We can see the volume size shrunk to 329.7GiB, therefore we can shrink partition sdb2 to 330Gb. Resizing a partition consists of deleting the existing partition and replacing it with a new starting at exactly the same location.
First, make a backup of the disk's mbr and partitioning data:
# dd if=/dev/sdb bs=512 count=1 of=500_sdb_mbr.dat
# fdisk -l -u /dev/sdb > 500_sdb_1.txt
Note, this method works for the older DOS/MBR partition table. For newer EFI/GPT drives, you must use another method to back up the partition table.
# fdisk /dev/sdb (p) - to display exisiting partition table (d) - to delete a partition (n) - to create a new partition: - primary partition (p) - first cylinder (see the output of fdisk -l) - last cylinder: +330G (t) - set partition type to 7 (HPFS/NTFS) (w) - write the changes to mbr
Now as a final step we can run ntfsresize without the --size switch to make sure we use all available partition space:
# ntfsresize -v /dev/sdb2
Now there is room to create an LFS partition:
# fdisk -l -u /dev/sdb Disk /dev/sdb: 465.8 GiB, 500106780160 bytes, 976771055 sectors Units: sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disklabel type: dos Disk identifier: 0xa7cea7ce Device Boot Start End Sectors Size Id Type /dev/sdb1 * 2048 206847 204800 100M 7 HPFS/NTFS/exFAT /dev/sdb2 206848 692267007 692060160 330G 7 HPFS/NTFS/exFAT /dev/sdb3 692267008 976771054 284504047 135.7G 83 Linux
Comments
Display comments as Linear | Threaded