Extending partitions via command line


Posted by Diego Assencio on 2013.12.09 under Linux (Management)

If you want to extend the size of one of your partitions via command line, you should use fdisk. Please notice that fdisk can only manipulate the partition table of a disk. To actually extend the file system from a partition, you will need a different tool (for ext2, ext3 and ext4 you can use resize2fs; I will show how at the end of this post). Roughly speaking, the partition table contains a map of the partitions of your disk while each partition can contain a file system which stores a map of the files and directories on that partition.

I have made the assumption that you are using Ubuntu/Debian; if not, you might have to adapt some of the commands below to have them work on your distribution.

For illustration purposes, I used a disk with 1000MB of space whose device node is /dev/sda. To change the partition table of your disk, just replace all occurrences of /dev/sda with the device node of your disk in the commands below. Please backup your data before doing anything shown here!

Open the partition table of your disk with fdisk:

sudo fdisk /dev/sda

Now list all partitions on the disk:

Command (m for help): p

In my case, this is what I see:

Disk /dev/sda: 1000 MB, 1000000000 bytes
255 heads, 63 sectors/track, 121 cylinders, total 1953125 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
Disk identifier: 0xbfd8408f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048      400000      198976+  83  Linux
/dev/sda2         1500000     1953124      226562+  83  Linux

There are two partitions on this disk. The "Start" and "End" columns show the disk sectors where each of these partitions start and end respectively. For instance, the first partition starts at sector 2048 and ends at sector 400000. Since each sector is 512 bytes long (see the line starting with "Units"), this means the first partition has 512 × (400000 - 2048) = 203751424 bytes (approximately 200MB). All sectors numbered in the range 400001-1499999 are unused since the second partition starts at sector 1500000, so we can extend the first partition up to the 1499999-th sector. To do that, we must first delete this partition:

Command (m for help): d
Partition number (1-4): 1

Listing all partitions again (p) shows now only the second partition:

Disk /dev/sda: 1000 MB, 1000000000 bytes
255 heads, 63 sectors/track, 121 cylinders, total 1953125 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
Disk identifier: 0x283b0c39

   Device Boot      Start         End      Blocks   Id  System
/dev/sda2         1500000     1953124      226562+  83  Linux

Now we can recreate the first partition with a bigger size. If there is data on that partition, be careful in this step as you must recreate the partition starting on the same sector as it started before (2048 in my case) or you might permanently lose the stored data:

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-1953124, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-1499999, default 1499999): 1499999

If you list the partitions again, you will see the first one occupies all sectors in the range 2048-1499999:

Disk /dev/sda: 1000 MB, 1000000000 bytes
255 heads, 63 sectors/track, 121 cylinders, total 1953125 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
Disk identifier: 0xbfd8408f

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     1499999      748976   83  Linux
/dev/sda2         1500000     1953124      226562+  83  Linux

Now write the changes to the partition table of the disk:

Command (m for help): w
The partition table has been altered!

Syncing disks.

If you have an ext2/ext3/ext4 filesystem on /dev/sda1, you can extend it with resize2fs. Before doing this, reboot your device to have the kernel recognize the changes to the partition table of your disk. Then run:

sudo resize2fs /dev/sda1

The filesystem should now occupy the entire size of the partition /dev/sda1.

Comments

No comments posted yet.

Leave a reply

NOTE: A name and a comment (max. 1024 characters) must be provided; all other fields are optional. Equations will be processed if surrounded with dollar signs (as in LaTeX). You can post up to 5 comments per day.