Expand an existing Linux partition

13 10 2009

I’ve to do this from time to time – but never regularly enough to remember all the commands and the order in which to go about it.  This is just one way of doing it (and by the way this does not cover logical volume groups, just plain vanilla EXT2/3/4 partitions).  In this example I’ll extend the root partition / which just happens to be at /dev/sda2 on my box.

Whilst in the OS check free space on partition.

#df -h /

Now check the partition table using fdisk (this should let you know which disk device / is on, as well as the partition number it mounted at eg; /dev/sda2 or /dev/hdb5)

#fdisk -l

To change the partition size the disk needs to be unmounted, I think the easiest way of doing this is just booting to a Linux live CD/USB.  I keep a small non-GUI one handy.

Check the disk hasn’t moved ID by running fdisk again.  At this stage it is necessary to note the starting cylinder of the partition you want to expand – this is in the start column.

#fdisk -l

Now use fdisk to delete the relevant partition (yes I did say delete, don’t panic if we start the new partition on the same cylinder and resize the partition all data will be intact provided our new partition is larger than the original).

#fdisk /dev/sda

>d (ENTER)

>2 (ENTER)  …delete the second partition, ie; /dev/sda2

>n (ENTER) …create a new partition

>p (ENTER) …create a new primary partition

>2 (ENTER)  …the number of the new partition

>(ENTER) …the start cylinder of the new partition, it should default to the same cylinder id recorded earlier

><last_cylinder>(ENTER)  …the last cylinder of the new partition, the default is the end of the disk but you can specify a size, for example +40G for a 40GB partition

>w(ENTER)  …write the changes to the disk and exit

Now we’ve deleted and re-created our partition (only this time larger) all we have to do is run a file check on it and tell our partition it’s been resized.

Check and if necessary fix the new partition.

#e2fsck -f /dev/sda2

Resize the partition.

#resize2fs /dev/sda2

Hopefully if all has gone well up to this point we can restart the original OS and check disk free using the command.

#df -h /

Advertisements

Actions

Information

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: