| CARVIEW |
This section steps through some basic command line partitioning. As an alternative to using the mighty GParted, the commands ‘ntfsresize’ and ‘fdisk’, when used in tandem, offer a command line interface method to resizing a disk’s existing partition. For this example, a single-partitioned, 512MB USB drive with an NTFS filesystem and approximately 40MB of saved data is used. (Important: The following actions will preserve the partition’s data, but please have a valid backup available to restore from in the event of an unexpected error.)
ntfsresize –size 100M /dev/sdb1
The command output should look like this:
ntfsresize v2.0.0 (libntfs 10:0:0) Device name : /dev/sdb1
NTFS volume version: 3.1
Cluster size : 4096 bytes
Current volume size: 509927936 bytes (510 MB)
Current device size : 509935104 bytes (510 MB)
New volume size : 99996160 bytes (100 MB)
Checking filesystem consistency …
100.00 percent completed
Accounting clusters …
Space in use : 43 MB (8.3%)
Collecting resizing constraints …
Needed relocations : 0 (0 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])
If you are sure you want to proceed, then answer with ‘y’ to continue and resize the filesystem. The rest of the output should then look like this:
Schedule chkdsk for 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 …
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!
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.
root@PartedMagic:~
2. Resizing the partition with Linux ‘fdisk’.
In the previous step, we shrunk the filesystem, not the partition. At this point, let’s review the partitions and filesystems on the USB drive with the following command entry:
disktype /dev/sdb
— /dev/sdb
Block device, size 493.5 MiB (517521408 bytes)
DOS/MBR partition map
Partition 1: 486.3 MiB (509935104 bytes, 995967 sectors from 63)
xType 0x07 (HPFS/NTFS)
xWindows NTLDR boot loader
xNTFS file system
xxVolume size 95.36 MiB (99995648 bytes, 195304 sectors)
root@PartedMagic:~#
As you can see, the volume size and partition value differ, with Partition 1 still filling the entire 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. Don’t worry, we are not deleting the data on the filesystem; we are deleting the partition only to create a new one (essentially, updating the partition table with new information).
Start ‘fdisk’ with the following command which will select the device and enter fdisk’s menu-driven, interactive mode:
fdisk /dev/sdb
Follow these sequence of menu command options to resize the partition.
Command (m for help):
For the curious, type ‘m’ to display and review all the fdisk menu command actions:
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help):
As mentioned earlier, it is necessary to delete the partition prior to creating a smaller one to match the resized NTFS filesystem. Typing ‘d’ will do just that:
Command (m for help): d Selected partition 1 Command (m for help):
Now type ‘n’ to create a new partition:
Command (m for help): n
Command action
e extended
p primary partition (1-4)
The original partition was a primary partition, so please type ‘p’ to continue and make this partition a primary partition:
p
Partition number (1-4):
Type ‘1’ because we want this to be the first partition:
Partition number (1-4): 1
First cylinder (1-31, default 1):
For this example, type ‘1’ for the first cylinder:
First cylinder (1-31, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-31, default 31):
From the beginning, it was decided to shrink the filesystem to 100MB. So make the partition equally 100MB, and enter the size like this:
Last cylinder or +size or +sizeM or +sizeK (1-31, default 31): +100M
Command (m for help):
The new partition now requires a system partition type (or id) to be assigned. To list all the known partition types that fdisk can create, type ‘L’. 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)
Finally, write the partition table to disk and exit fdisk with the command action ‘w’:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
root@PartedMagic:~#
That’s it. A quick look at the device once more from ‘disktype’, and you can see that the filesystem and partition have both been resized from 512MB to 100MB (and with all data intact). Nice.
disktype /dev/sdb
— /dev/sdb
Block device, size 493.5 MiB (517521408 bytes)
DOS/MBR partition map
Partition 1: 101.9 MiB (106896384 bytes, 208782 sectors from 63)
xType 0x07 (HPFS/NTFS)
xWindows NTLDR boot loader
xNTFS file system
xxVolume size 95.36 MiB (99995648 bytes, 195304 sectors)
root@PartedMagic:~#
