Quantcast
Viewing all articles
Browse latest Browse all 2

Resizing a partition with Linux on the command line

If you have an existing partition layout, for instance an image, and want to transfer it to a larger disk / SD Card, you might want to resize the last partition after writing the image, for using all available space.

Please note that this tutorial probably needs to be adjusted for dealing with extended partitions (if you have more than 4 partitions it is very probable that you have extended partitions).

You can do this from Linux using the command line:

Listing available block devices

lsblk

Will show you the available block devices. If your device is not listed, you may need to trigger USB / device recognition before re-running the command.

udevadm trigger

Note which /dev/sdx your drive is located at. (lsblk will also list the available partitions)

Repartition the SD card

Let’s assume that your SD card is located at /dev/sdc.

fdisk -u=sectors -l /dev/sdc

This command will list the partitions currently on the drive and exit. Example output (16 GB card with 8 GB image, last partition to be resized):

Disk /dev/sdc: 15.8 GB, 15819866112 bytes
4 heads, 32 sectors/track, 241392 cylinders, total 30898176 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: 0×00003638

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            4096      147455       71680    c  W95 FAT32 (LBA)
/dev/sdc2          151552    15353855     7601152   83  Linux

 

Note the number of available partitions, the start (151552 in this case) of the last partition, the Id of the partition (83).

Now run the following command, to enter interactive mode of fdisk:

fdisk -u=sectors /dev/sdc

Pressing “m”  + Enter will show you the available options. “q” + Enter will exit the application. No changes to the actual partition table will be done, until you write it (in the last step).

We will now delete and recreate the last partition, in our case it is partition number 2.

Press “d” + Enter

Enter “2” + Enter

Press “p” + Enter to list the result (not written yet to the SD, don’t worry – if you deleted the wrong partition, press “q” + Enter)

To recreate the last partition press “n” + Enter.

It will ask you for the partition type. Here you should press “p” + Enter (needs to be adjusted for extended partitions!)

On the second question, you can simply press enter – it will use “2” by default (or a different number if you have more partitions).

Now it will ask you for the first sector. Here you have to enter the exactly same value as the one you obtained with the first command. In our case it is 151552 . Press Enter.

Now it will ask you for the last sector. To resize to the maximum available space, simply press Enter to use the suggested default value.

Press “p” + Enter to check the result. It looks like this for us:

Command (m for help): p

Disk /dev/sdc: 15.8 GB, 15819866112 bytes
4 heads, 32 sectors/track, 241392 cylinders, total 30898176 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: 0×00003638

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            4096      147455       71680    c  W95 FAT32 (LBA)
/dev/sdc2          151552    30898175    15373312   83  Linux

Check that the Id is the same as before, and that Start is also the same.

If you are satisfied with the result, press “w” and Enter to write the table. It will indicate the sucessful write by:

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

 

You can now exit the application by pressing “q” + Enter.

If you are NOT satisfied with the result, press “q” + Enter to exit the application without writing the new partition table.

At this point you can also use lsblk to see the success of the resize in human readable format.

NAME                MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdc                   8:32   1  14.8G  0 disk
├─sdc1                8:33   1    70M  0 part
└─sdc2                8:34   1  14.7G  0 part

Resizing File System

As the last step we need to resize the file system on the partition. For a Linux partition this is easily done in the following way:

resize2fs -p /dev/sdc2

will resize the partition to the maximum available space. It will acknowledge the success by:

resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on /dev/sdc2 to 3843328 (4k) blocks.
Begin pass 1 (max = 60)
Extending the inode table     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The filesystem on /dev/sdc2 is now 3843328 blocks long.

 

That’s it.

Easy, once you know how to do it.

Did you like this article? Support us by sharing it, or buying Raspberry Pi gear from our store.


Viewing all articles
Browse latest Browse all 2

Trending Articles