Notice: Undefined index: order_next_posts in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 194

Notice: Undefined index: post_link_target in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 195

Notice: Undefined index: posts_featured_size in /nas/content/live/gadgetmag/wp-content/plugins/smart-scroll-posts/smart-scroll-posts.php on line 196

Resize your disks on the fly with LVM

Grow your disks like a true master and never reformat and restore your drive again

Often we see or hear of people running out of disk space on their Linux systems and resorting to resizing tools or worse. Linux has the ability to extend (or shrink) logical partitions across spare space or even across additional disks.

This guide walks you through the why and the how of resizing your disks.

We will be creating an additional new LVM- based partition on our virtual CentOS server, as well as the underlying structures that are called physical volumes (PVs) and volume groups (VGs) that make this awesome technology possible.

These three different parts together make up LVM (Logical Volume Manager). The best way to think about LVM setups is that they are like a layered sponge cake. On the bottom layer we have the physical volumes: the hard disks. Then we have the volume groups, the cake’s ‘cream’ that can in effect cement the disks together and provide a smooth contiguous surface to carve out our partitions. This means that a partition can be bigger than a single disk. Also, when you want to expand a disk, as long as you have space in your volume group, you can.


CentOS 6.4 ISO

Step by Step

Step 01

Get all your disks in a row

Essentially LVM separates the logical volumes from the physical disks with a glue in between called Volume Groups (or VGs for short). Firstly, you may actually be using LVM without realising it! Most newer distros use LVM when they do their install. To see if you are, just open a root console and type lvs.

If you see something like that shown in the picture below, then you are already using LVM. In our example we will be creating a new logical volume from scratch, but the same principles around resizing and reducing still apply.

All your disks in a row
All your disks in a row

Step 02

Building our new LVM setup

Use the command fdisk -l and find the disk we’re going to use as the first disk in our LVM setup. In our example, sdb is our new disk.

To tell Linux that the disk is going to be an LVM disk, we need to set the disk type (Label) to LVM. Use the command:

$ fdisk /dev/sdb

Press ‘n’ to add a new partition. The type of disk we are adding is a primary, so press ‘p’ and as we are only putting a single partition on the disk, we can select partition number 1. Accept the geometry the machine suggests by pressing Enter on the questions. Don’t quit out of fdisk yet as we have more to do!

Step 03

Preparing the bottom layer of our lVM cake: physical volumes

Once we have set up the partition, we need to identify it as an LVM device. To do this while still in fdisk, press ‘t’ to change the disk label, and enter the label ID, which in our case is 8e. Follow this by ‘w’ (to write the changes to disk). At this point we have created a partition and identified it as an LVM disk type. So now we need to ‘bless’ or prepare the physical disk upon which LVM will be based. To do this we use a command called pvcreate. This effectively prepares and marks the disk as an LVM disk. To do this, use the command:

$ pvcreate /dev/sdb1

Step 04

Followed by the lVM jam: volume groups

The bottom ‘cake layer’ is now created and we can make the jam in the middle: the volume groups. Each of these is like a chunk of storage that you can slice into one or several logical volumes. The difference is that you can expand and reduce disks within the volume group.

The vg_data bit is the name for the volume group:

$ vgcreate vg_data /dev/sdb1

If you want to span multiple disks, just add in the extra devices after /dev/sdb1. Remember to mark them as LVM disks using the pvcreate command and then marking them as LVM disks in fdisk!

Step 05

Adding the top of the cake: logical volumes

Now that we have created the second layer, we can add the logical volumes:

$ lvcreate -L 1G -n lv_data vg_data

This command will create a 1GB logical volume from the volume group we created. To clarify, lv_data is our new logical volume, while vg_data is the volume group to take it from. You can make the volume as small or large as you like, given a big enough volume group. We are using the entire volume group, rather than just a small portion of it. All the volume groups can be found under the /dev/mapper directory.

Now we can create a logical disk on top of it.

Step 06

Making our volumes

Now we can format the disk using the following command:

$ mkfs.ext4 /dev/mapper/vg_data-lvdata

To test the partition, try mounting it.

Now this is where things get interesting. This could be a 60GB or 600GB disk. We just chose the 1GB as an example. Now, say we have filled this disk with all our our ISOs and such, how do we fix it and add that shiny new disk into the logical disk? This is where we see the joy of LVM. We can expand our volume over the new disk and make our volume bigger!

Expanding Volumes
Expanding Volumes

Step 07

Expanding our volumes

To do this, it is a very similar method to creating an LVM; we need to in effect prepare the new disk as an LVM member. To do so, repeat the process we performed earlier to prepare the disks (with fdisk and pvcreate).

Now that the partition is prepared, we can add it into the volume group we just created. To do this we use the vgextend command, as shown above. The syntax is quite straightforward with the first argument being the volume group you want to add the disk to and secondly, the disk you wish to add.

$ vgextend vg_data /dev/sdc1

Now that the VG is extended, we need to perform a similar process to increase the logical disk to use the underlying storage we expanded before. Below is a simple example to just expand our VG with a 1GB partition.

lvextend -L +1G /dev/mapper/vg_data /dev/sdc1

Step 08

Generation game

And lastly we will need to extend the actual file system. There is a command called resizefs that we can use. As the name implies, it is used to resize the filesystem.

[root@localhost ~]# resize2fs /dev/ mapper/vg_data-lvdata
resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/ vg_data-lvdata to 2219008 (4k) blocks. The filesystem on /dev/mapper/vg_data- lvdata is now 2219008 blocks long.

Step 09

Managing LVMs the GUI way

There is actually a GUI tool, system- config-lvm. We purposely didn’t mention this earlier because you need to understand how all the components of how the system fits together. Not only that but also not all systems, especially servers have a GUI installed. To install the package install system-config-lvm.noarch. This will install the graphical LVM tool.

To try it out, lets try with a basic server with an additional 1 GB drive.

Step 10

Creating our physical volumes (again!)

StartbyloadingtheLVMtool.Ifyoulooktotheleft- hand side of the application you will see Volume Groups, Unallocated Volumes, and Uninitialized Entities. It shows the volume groups that we work with, volumes and raw devices that are not members of volume groups.

Doing it this way can be very useful in that it will set up the disk label and such automatically, so no terminal and fdisk are required.

Before you add your unused partition, you will need to initialise the partition, as you can see. Do this by clicking Initalize Entity. This is equivalent to preparing a volume using fdisk and pvcreate as we did before.

Step 11

…And the volume group

Once this has completed, you will see that the disk has moved from Uninitialized to Unallocated – so, basically, it is prepped to add to our volume group.

You will also notice that if you click on the unallocated volumes you will get a group of buttons below, each stating its purpose.

You could, if you wanted, create a new volume group with this disk – or, as we did before, add the disk to an existing volume. Remember, a VG is just a chunk of space that you can split up as you wish; so when you are presented with the sizing screen, carve up that space as you wish.

When creating a new volume group, you will get a selection, as shown below.

The Volume Group
The Volume Group

Step 12

Creating a new volume group

You can leave these options as they are as they’re reasonable defaults. At this point you will see the make-up of your new volume group, the physical disks and the logical make-up.

Step 13

Carving out storage from our LVM

The last stage is to carve up a chunk of space, so select the Logical view for the VG you want to carve data out of. Hit the Create Logical Volume button. Now you can specify the logical disks.

So, it’s easy enough. Give it a good name. Again for LV properties, just go with the defaults. Size is easy enough and obvious enough. If you want to use the entire disk (most people do), click Use Remaining.

Now we have set the size, we need to decide what file system to use. As we are using CentOS 6, our default file system is ext4. Unless you have a compelling reason to change to it, leave the default selection as it is.
All file systems need a mount point to access them, so let’s put in the mount points.