How can I resize partitions from the command line? I've heard of GParted, but I don't want to use a GUI program.
-
if you're considering to do this because the GUI doesn't work, then maybe it's better that you fix the latter problem instead ;) http://unix.stackexchange.com/questions/206320/gparted-livecd-fails-to-start – knocte May 20 '16 at 10:30
1 Answers
Use parted instead, possibly coupled with your filesystem's resizing command.
parted is the engine underneath the GParted GUI. You can use it in either interactive command mode or directly from the command line.
Before parted 3.0, the following command does what you are probably expecting, having learned about GParted:
$ sudo parted /dev/sdb resize 1 1 200M
That will resize the first partition on the second hard disk to 200 MiB, and make sure it starts 1 MiB into the disk so as to avoid alignment problems with Advanced Format drives.
This functionality was removed in v3.0, the regression being justified by comparison to removing a gangrenous toe. Partial functionality was restored in v3.1, covering only FAT and HFS+.
v3.2 is where things really got interesting, from a Unix/Linux file system perspective. It replaces the resize command with resizepart. The new name is due to the fact that it can only change the partition size; it doesn't even attempt to rearrange its contents first.
In the case of growing an existing filesystem, this is a low-risk operation as long as you're using a filesystem that can be grown on the fly, like ext4 or XFS. For example, if we start with a 200 MiB ext4 partition on /dev/sdb1, we can double its size:
$ sudo parted /dev/sdb resizepart 1 400M
$ sudo resize2fs /dev/sdb1 400M
The same command pair also works for shrinking ext[234] filesystems, except that you give them in reverse: shrink the actual filesystem, then chop the empty space off the end of the partition.
Some filesystems (e.g. XFS) can be grown only; they can't be shrunk. This is why XFS's equivalent to resize2fs is called xfs_growfs.
RAID and LVM systems complicate all of this. They have their own restrictions and capabilities.
A common situation is to have an XFS filesystem on top of a multi-drive LVM-managed disk array, to which you add some drives, then expand the LVM, and finally expand the XFS filesystem into the new space.
- 71,107
- 16
- 178
- 168
-
-
Is there a `resize2fs` equivalent for FAT32/vfat? I found `fatresize` but it appears to be a wrapper for parted, which doesn't appear to do this any more... – jacobq Oct 27 '20 at 22:08
-
1@jacobq: This is a really old question. It's best if you just ask a new question to focus on this specific element, given current software realities. – Warren Young Oct 27 '20 at 22:28
-
1be aware that size in the parted command is NOT the size of the partition but the number relative to the beginning of the disk, so 400M will mean the partition will end on the 400th megabyte of the disk, which will *not* be a partition of 400M, unless your partition starts at 0, and it basically never does. – Aleksandar Ivanisevic May 26 '23 at 07:03