Pages

Sunday 3 August 2014

Using resize2fs for resizing Linux partition (Centos/Ubuntu)

In vitualization world, sometime, when you create a VM, you assign certain amount of disk space and then you realize that you need more. Resizing a partition is not a easy task, especially is boot partition, you have to reboot the machine to take effect. This memories will take me through some basically step to help me resize linux partition online/offline (this step apply for both Centos and Ubuntu).

Before go through the guidance, let talk a bit to make clear my concept about Linux disk, there include 3 type of disk(or partition) will be mention in this document is physical disk, underlying partition and parttion. Physical disk is the this that we attach to the server (hard disk), underlying partition is just a partition that occupies a whole physical disk (ext: /dev/sda), and partition is a parted disk (/dev/sda1; /dev/sda2).

First, on hypervisor layer, try to extend the physical disk and check by fdisk -l, we should see the disk has ready to extend the partition.

Please NOTE, 
1) We can not extend root disk on-line, this will not take effect after we reboot the server. So just extend the physical disk and reboot the server (on Centos, resize2fs is automatically run after boot, otherwise we have to run resize2fs manually with Ubuntu).

2) resize2fs or grow_xfs utilities, as their document, can extend partition without un-mount it, but after test, i realize it can be only done when we using underlying partition. Not effect to other partition.


For offline re-size: (resize partition such as /dev/sda1, /dev/sda2)

In this example, partition will be resized using ext4 format, first, umount the partition and check for error and disable journal as well.
# umount <device> 
# fsck -n <device> (ext: fsck /dev/sda1)
# tun2fs -O ^has_journal <device>
# e2fsck -f <device>

The next step will be stress you up with delete partition steps, we have to delete the partition which we want  to extend the capacity, don't worry, we are not going to lose the data, this step just make sure the partition table is updated with new size
> cd /home/<username>/svn/repository
> svnadmin create myfirstproject
# fdisk /dev/<underlyung partition, ext: /dev/sda>
Type p for showing the device we want to delete
Command (m for help): d
Partition number (1-4) : <device ID> (ext: 1, ext number 1 is /dev/sda1)
Command (m for help): n
Command action: p
Partition number (1-4): <same device ID we just deleted> (ext: 1)
First cylinder (<number> - <number>): [enter]
Last cyliinder, +cylinder or +size{K, M, G} (<number> - <number>): [enter]
Command (m for help): w




Now, recheck the partition and resize it
# fsck -n <device>
# resize2fs <device> (ext: resize2fs /dev/sda1)
We almost done here, re-enable something that we throw away from our first step
# tune2fs -j <device> (ext: tune2fs -j /dev/sda1)
Now, the partition is ready to mount and use.

For online re-size: (resize underlying partition /dev/sda)

Just run command:
# resize2fs <device_name>












No comments:

Post a Comment