How to expand a partition on the fly

http://commons.wikimedia.org/wiki/File:Crystal_128_harddrive.pngI’ve just had some troubles with my Odroid XU4 complaining that there was no more space left on the micro SD card. It’s a 16 GB card so I was wondering what caused this. As it turned out I simply forgot to expand the root partition to the size of the SD card, so I thought I’ll write a post about how to easily and safely expand a mounted linux partition on the fly without having to reboot the system.

Before you start you should backup your data, just in case anything goes wrong during the process. It is usually safe to expand a partition on the fly, but just a single typo can cause loss of data.

Basically, there are three steps to be taken:

Step 1: Expand the partition

Run fdisk /dev/mmcblk0 to get access to the device with the partition you want to expand (in this case it’s /dev/mmcblk0p2) and print the partition layout with p

$ fdisk /dev/mmcblk0

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/mmcblk0: 15 GiB, 16037969920 bytes, 31324160 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
Disklabel type: dos
Disk identifier: 0x0008aae4

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        2048   264191   262144  128M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      264192  7620514  7356321  3.8G 83 Linux

As you can see, the SD card has a size of about 16 GB, but the second partition only uses 7356321 * 512 = 3766436352 Bytes. To expand the partition to use all free space, you first need to note down the start sector of the partition (i.e. 264192) and the number of sectors on the SD card (i.e. 31324160). Now you need to delete the old partition and create a new one that uses all free sectors. Don’t worry, no data will be deleted during this process.

Command (m for help): d
Selected partition: 2

Command (m for help): p

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        2048   264191   262144  128M  c W95 FAT32 (LBA)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4, default 2): 2
First sector (264192-31324159, default 264192):
Using default value 264192
Last sector, +sectors or +size{K,M,G} (264192-31324159, default 31324159):
Using default value 31324159

Command (m for help): p
Disk /dev/mmcblk0: 15 GiB, 16037969920 bytes, 31324160 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
Disklabel type: dos
Disk identifier: 0x0008aae4

Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        2048   264191   262144  128M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      264192 31324159 31059968 14.8G 83 Linux


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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Delete the partition with d and enter the number of the partition you want to delete (i.e. 2). You can use p to check if it worked out. Next, create a new primary partition with the same partition number and starting sector (n, p, 2, 264192). fdisk should automatically use these values as defaults, but double check them, just in case. The last sector should be the number of sectors on the SD card minus 1, i.e. 31324159. Again, use p to check if everything worked out ok. If the new partition layout looks good, use w to apply the changes and exit fdisk.

Step 2: Inform the kernel about partition table changes

As fdisk points out, the kernel still uses the old partition table. If you don’t want to reboot the system you can use $ partprobe to update the information. Depending on your disk configuration this may take a moment.

Step 3: Expand the file system

When the partition table update is finished, you need to expand the file system on the partition in question. This can easily be done with $ resize2fs /dev/mmcblk0p2. After a few moments the command prompt should return and you can check the new partition size with $ df

$ df | grep mmcblk
/dev/mmcblk0p2                15290952    3946764   10668500  28% /
/dev/mmcblk0p1                  130798       7102     123696   6% /media/boot

If everything went well, enjoy your new expanded partition and have a nice weekend!

Advertisements

About h0nk3ym0nk3y

h0nk3ym0nk3y is one of the three LinuxM0nk3ys from Linux M0nk3ys @ WordPress Linux M0nk3ys @ YouTube Linux M0nk3ys @ Twitter

Posted on March 18, 2017, in Command-Line and tagged , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: