Extending the Logical Volume Part 1 – fdisk

Learn how to extend existing logical volumes on your cloud server using fdisk

The cloud has revolutionised data storage, by providing gargantuan amounts of hard disk drive (HDD) and solid-state disk (SSD) space. You can very quickly increase or decrease your storage allocation based on business requirements, with this data being easily accessible by anyone over the internet.

When increasing your storage allocation, existing servers will not recognise this new storage space without additional configuration. There are two ways to do this, using either fdisk or parted in Linux. For reference, fdisk is best used when your total storage space is less than 2 terabytes (TB). This is due to fdisk using the Master Boot Record (MBR) partitioning method, which was designed with smaller drives in mind. We will be covering fdisk on Linux in this guide.

For total storage allocations exceeding 2TB, we recommend reading Part 2 of this series, where you can learn how to use parted on Linux to extend your logical volume.

Verification steps before using fdisk

Please note that this guide was created using CentOS 7, with the ISO repository located here.

Before you get started with fdisk, you should first check your current allocated storage space by typing the following command into your Terminal window:

df -h

This will use the disk free utility to scan your filesystem for directories. You should get an output similar to below:

[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 38G 21G 18G 54% /
devtmpfs 1.2G 0 1.2G 0% /dev
tmpfs 1.2G 0 1.2G 0% /dev/shm
tmpfs 1.2G 24M 1.2G 2% /run
tmpfs 1.2G 0 1.2G 0% /sys/fs/cgroup
/dev/sda1 497M 189M 308M 39% /boot
tmpfs 234M 0 234M 0% /run/user/0

Your logical volume (LVM) is listed as the root of the drive, indicated by a / under the Mounted on column.

In this case, /dev/mapper/centos-root is your LVM. We will be extending this.

Next, you need to determine the filesystem structure that is currently in use. You can do this by typing the following into your Terminal window:

fdisk -l

In this case, we get the following output:

[root@localhost ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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 label type: dos
Disk identifier: 0x000a2b1e

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 83886079 41430016 8e Linux LVM

Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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 /dev/mapper/centos-root: 40.3 GB, 40273707008 bytes, 78659584 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

NOTICE – If you see either gparted or parted listed anywhere in the above output, please use our guide Extending the Logical Volume Part 2parted [add hyperlink here] instead of this one.

As you can see above, the current Device Boot is set to /dev/sda1 as indicated by the *.

The new volume we want to extend to is labelled as Linux LVM under the System column. Make a note of the Device Boot which is /dev/sda2, as you will need this shortly.

The new volume is already linked to our existing storage drive. This is true because sda1 has an End of 1026047, with sda2 having a Start of 1026048. Make a note of the sda2 Start, as you will need this when extending the LVM with fdisk. Also make note of the Id number for the partition you are extending to, which is 8e in this example.

Extending the partition with fdisk

Now we have all the information needed to extend our partition.

Choose the drive of which you want to modify the parameters, which in this case is /sda1, by typing the following into Terminal:

fdisk /dev/sda

You should see the following output:

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

This means we are now in fdisk, and connected to our existing /sda1 partition.

WARNING – These next steps will format the partition, meaning you will lose all saved data on that partition. Please back up any critical files before going any further.

This is an interactive shell, so you can type commands without invoking fdisk each time. Next, type the following:

d

This is the Delete command and will delete your partition. You should see the similar output in Terminal:

Partition number (1,2, default 2):

Our default was marked by the * earlier in this guide, which was /sda1. This ensures that we are deleting the correct partition using fdisk.

Next type 2 to delete the default partition in Terminal and press Enter on the keyboard. You should see the following output:

Partition 2 is deleted

Now /sda1 has been deleted and has been released as free partitioning space.

Next, you can add a partition with the following command:

n

This will generate the following output:

Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended

As you can see, there are 3 free partitions in our example. We want to create a Primary partition, so type the following:

p

You will see:

Partition number (2-4, default 2):

Use the same partition number as the one we just deleted, which was:

2

Now, you be prompted for the First Sector as shown below:

First sector (1026048-209715199, default 1026048):

Our /sda2 Start was 1026048, so type it as the First sector here.

Next, you will be prompted for the Last sector:

Last sector, +sectors or +size{K,M,G} (1026048-209715199, default 209715199):

You can simply press Enter on the keyboard to use the default number. This will use all available space on the disk for that partition.

Now we need to change the partition type. Input the following into Terminal:

t

When the following appears:

Partition number (1,2, default 2):

Type:

2

This will select the partition we just created, partition 2.

Next, you will be prompted for a Hex code:

Hex code (type L to list all codes):

The Id listed earlier in this guide was 8e. Type the Id you got earlier, and you will see a similar output:

Changed type of partition 'Linux' to 'Linux LVM'

This confirmation shows that our partition type has been changed to a Linux Logical Volume. Confirm this has happened by typing the following into Terminal:

p

This will print the new partition table layout:

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 209715199 104344576 8e Linux LVM

The Start and End should correspond with what we typed earlier in Terminal. After checking that the values match, you can then write these changes by typing:

w

Then you will see the following output:
The partition table has been altered!
Calling ioctl() to re-read partition table.

NOTE – If you get an error stating Re-reading the partition table failed with error 16, restart your server before continuing. The changes have applied, the disk is just busy due to a running process and cannot be updated in real time.

Upon reboot or finishing these steps, type:

fdisk -l

Confirm that the changes have applied, and then you can move to extend the logical volume.

Extending the logical volume

Now you can extend the logical volume.

First, you will need to check that the partition is listed as a Physical Volume.

Use the following command to display all existing Physical Volumes on the disk:

pvdisplay

You should see a similar output to this:

--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 39.51 GiB / not usable 2.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 10114
Free PE 0
Allocated PE 10114
PV UUID NP5SVJ-xEhi-hEd9-sMZs-6DGe-VDOi-zuIetu

You can now extend the Physical Volume by typing:

pvresize /dev/sda2

/dev/sda2 applies to our example, but replace this with your Physical Volume name, as listed next to PV Name.

You should get an output saying:

Physical volume "/dev/sda2" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

Now display the Logical Volumes on the drive by typing:

lvdisplay

In our example, we saw the following output:

--- Logical volume ---
LV Path /dev/centos/swap
LV Name swap
VG Name centos
LV UUID w7zRnQ-0TCm-IIJF-dMR4-ypNL-HxZV-tO90iu
LV Write Access read/write
LV Creation host, time localhost, 2014-10-06 08:56:38 +0000
LV Status available

# open 2

LV Size 2.00 GiB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto


- currently set to 8192
Block device 253:0


--- Logical volume ---
LV Path /dev/centos/root
LV Name root
VG Name centos
LV UUID gaqFxq-HmdB-Msdc-DH32-qgz0-BafH-xERhST
LV Write Access read/write
LV Creation host, time localhost, 2014-10-06 08:56:39 +0000
LV Status available


# open 1

LV Size 37.51 GiB
Current LE 9602
Segments 1
Allocation inherit
Read ahead sectors auto
currently set to 8192
Block device 253:1

NOTE – The swap is similar to the page file on Windows, and should be left as is to allow for disk-based caching when programs have been idle for extended periods of time. This helps reduce RAM usage, and dramatically improves system stability.

Root is the logical volume we want to extend. That means we should use /dev/centos/root in the next step.

Now you can extend the volume by typing:

lvextend -l+100%FREE /dev/centos/root

lvextend stands for logical volume extend.

-l+100%FREE tells lvextend to extend the volume using 100% of the available free space. If you wish to use a specific number, just change 100% to 95%, and so on. This may be useful if you want to assign some space to another partition or volume.

You should get a similar output to this:

Size of logical volume centos/root changed from 37.51 GiB (9602 extents) to 97.51 GiB (24962 extents).

Logical volume root successfully resized.

Now, you need to expand the OS filesystem to accommodate the increased volume size.

Please choose the correct type of server, by checking both of the options below.

For Fasthosts CloudNX or VPS servers, type the following:

xfs_growfs /dev/centos/root

The output should be:

meta-data=/dev/mapper/centos-root isize=256 agcount=9, agsize=1147392 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=9832448, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 9832448 to 25561088

On Fasthosts Bare Metal, Legacy Cloud, and Legacy Virtual servers, type the following:

resize2fs /dev/centos/root

The output will be:

resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/centos/root is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 16
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 25561088 (4k) blocks.
The filesystem on /dev/centos/root is now 25561088 blocks long.

After choosing the relevant command for your server type, your Logical Volume should now be extended, and the process complete.

Double-check that the Logical Volume extended by typing:

df -h

If your volume has increased by the additional storage space amount you intended, then the process was successful.