How do I resize a windows partition without using gparted?
Suppose I have a native (i.e. coming from manufacturers) windows 7 installation on a laptop (with an SSD device, BIOS/MBR partition table if that matters). The partition on the device is completely allocated and dedicated to windows. I now want to install a linux system alongside windows, and to do that I need to first shrink the windows partition. While I can find ways to do that from within windows or using gparted, how can I do this using only command line programs, like parted or fdisk?
3 Answers 3
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), or chkdsk /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
~ ntfsresize --info /dev/sdb > . > You might resize at 16865632256 bytes or 16866 MB (freeing 112060 MB). > .
~ ntfsresize --no-action --size 18G /dev/sdb1 > . > The read-only test run ended successfully.
~ 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):
- 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
> 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
> 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)
> Command (m for help): p Device Boot Start End Blocks Id System /dev/sdb1 63 40644449 40644387 7 HPFS/NTFS
> Command (m for help): a > Partition number (1-4): 1
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)
Nice demo! You left some margin but only 5%, if you’re trying to cover GB/GiB unit error I believe that’s around 7%. I also recommended a final resize with no explicit size, to automatically take up the margin — at least it would exclude some future puzzlement.
Yeah, adding adding a final ntfsresize with no explicit size would be safer. The damned 7% GB/GiB difference! You’re right on both @sourcejedi, thanks!
i’ve submitted an edit to put the second ntfsresize after the fdisk write, the other way round didn’t make sense. looking great now + upvoted :).
GParted is often worth using because it helps avoid several nasty mistakes. I guess the main advantage of command-line tools here is to have more visibility of details. This can be useful in unexpectedly fragile situations (at least once it’s broken, the details might help you realize why). However I wouldn’t recommend using them to others unless they want to be able to learn from mistakes up to «my disk is now full of zeros and I need to start from scratch».
Also a desktop Linux install process should provide a user-friendly tool for resizing the Windows partition. (Or official documentation). It’s the common case. This would be my first recommendation in general.
All of these options will recommend making backups in case of any error.
Confusingly you should not use the parted command-line tool. It used to be a convenient option, but the developers no longer support resizing filesystems with it.
Otherwise, you use ntfsresize , then delete and re-create the partition ( fdisk ) with the same details except for the size. BEWARE UNITS — SOME TOOLS USE MB; OTHERS MAY SAY MB BUT MEAN MiB. fdisk uses MiB and ntfsresize uses MB. The lazy way is to ntfsresize to much smaller than you need (e.g. 2x), then after recreating the partition you run ntfsresize a second time with no explicit size.
For the hard way, to convert units, you can run numeric expressions in bash. E.g. to see 10GiB in bytes: echo $((10 * 1024 * 1024 * 1024)) . You can use those expressions as arguments to command-line tools like ntfsresize .
The partition name for ntfsresize will look like /dev/sda1 . lsblk -f will list all partitions (including your boot disc) with their size, and tell you about the filesystem. fdisk will want the name of the disk, like /dev/sda .
For MBR, the partition details to recreate are: partition type and «active»/bootable flag, as well as starting offset.[1] fdisk should show the partition offset in sectors by default. (If not, there may be fractions which are not shown — possibly indicated by a + on the end, but there might be a trap there — you should be sure to always use fdisk in sectors mode).
To avoid typing errors inside fdisk , I sometimes select numbers + paste them with the middle mouse button. That requires either X Windows, or in text mode you need gpm. I think it’s less common to provide gpm on the console by default now, but it’s there when I use Clonezilla Live. It’s convenient, but you could probably lose the number first. So you should probably write the original partition offset down before you delete it.
[1] GPT uses a different format for the type, adds some more flags and a partition UUID. I don’t think they’d usually be important; flags wouldn’t apply to the main Windows partition and the partition UUID isn’t used by much yet.
Linux parted resize ntfs
This manual page documents briefly the parted command. Complete documentation is distributed with the package in GNU Info format; see below.
parted is a disk partitioning and partition resizing program. It allows you to create, destroy, resize, move and copy ext2, ext3, linux-swap, FAT, FAT32, and reiserfs partitions. It can create, resize and move Macintosh HFS partitions, as well as detect jfs, ntfs, ufs, and xfs partitions. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks.
OPTIONS
-h, —help displays a help message. -i, —interactive prompts for user intervention. -s, —script never prompt the user. -v, —version displays the version.
COMMANDS
[device] The block device to partition. [command [options]] Specifies a command to parted. If no command is given, parted will give you a command prompt. Commands are: check partition does a simple check on partition . cp [source-device] source dest copies the source partition’s filesystem on source-device (or the current device if no other device was specified) to the dest partition on the current device. help [command] prints general help, or help on command if specified. mkfs partition fs-type make a filesystem fs-type on partition . fs-type can be one of «fat16», «fat32», «ext2», «linux-swap» or «reiserfs». mklabel label-type Creates a new disklabel (partition table) of label-type . label-type should be one of «bsd», «dvh», «gpt», «loop», «mac», «msdos», «pc98» or «sun». mkpart part-type [fs-type] start end make a part-type partition with filesystem fs-type (if specified), beginning at start and ending at end (in megabytes). fs-type can be one of «fat16», «fat32», «ext2», «HFS», «linux-swap», «NTFS», «reiserfs» or «ufs». part-type should be one of «primary», «logical» or «extended» mkpartfs part-type fs-type start end make a part-type partition with filesystem fs-type beginning at start and ending at end (in megabytes) move partition start end move partition to start at start and end at end . Note: move never changes the minor number name partition name set the name of partition to name . This option works only on Mac, PC98, and GPT disklabels. The name can be placed in quotes, if necessary print displays the partition table quit exits parted resize partition start end resize the filesystem on partition to start at start and end at end megabytes rm partition deletes partition select device choose device as the current device to edit. device should usually be a Linux hard disk device, but it can be a partition, software raid device or a LVM logical volume if that is necessary set partition flag state change the state of the flag on partition to state . Flags supported are: «boot», «root», «swap», «hidden», «raid», «lvm», «lba» and «palo». state should be either «on» or «off»
REPORTING BUGS
SEE ALSO
fdisk (8), mkfs (8), The parted program is fully documented in the info(1) format GNU partitioning software manual.
AUTHOR
This manual page was written by Timshel Knoll , for the Debian GNU/Linux system (but may be used by others).