Extending Disk Space using LVM

From girldick.hosting
This is the approved revision of this page, as well as being the most recent.
Jump to: navigation, search

If you've recently upgraded your VM with additional disk space, we'll most likely have extended the disk for you but not your partitions on the disk, because this requires modifying your filesystem which is a thing we avoid. This guide presents information about extending your disk using LVM - Logical Volume Manager. LVM is the default disk setup on most modern Linux distributions and, if we installed your OS for you, most likely what you'll be using.

Brief introduction to LVM

Note: It is not necessary to read/understand this to use the commmands on this page to extend your disk, but if you'd like to learn more about LVM, this could be a good starting point.

Broadly speaking, LVM divides disk management into a few different tiers:

  • Phyiscal Disks: these are the "physical" disks attached to your computer. In a traditional desktop computer, these would literally be physical disks plugged into the machine. In a virtual machine, however, these are individual virtual disks attached to the virtual machine - which the operating system interprets as physical disks.
  • Physical Volume (PV): these are partitions on your physical disks, which are laid out when you format the disk. You can either have one big volume/partition taking up the whole of the physical disk, or break it down into smaller ones.
  • Volume Group (VG): This is an LVM concept which allows you to combined multiple physical volumes (partitions) into one large group.
  • Logical volumes (LV): These are an LVM concept which are similar to traditional disk partitions - these become the partitions that your operating system can store data on. You can create multiple logical volumes out of one Volume Group. This allows you to present partitions to your operating system that are bigger than the actual partitions on your disk, by spanning the volume group across multiple physical volumes or even physical disks.
  • Filesystem: This is the actual formatted filesystem on the disk, and holds the data about where all your files are kept and how big they are. There is typically one filesystem to each logical volume.

For a visual breakdown, have a look at the following diagram showing an example of an LVM layout.

LVM diagram.jpg

On our systems, if you ask for additional disk space, we'll do this by extending the physical disk that your VM has attached, as opposed to adding another disk. The rest of this guide will assume that to be the case.

Extending a disk with no full-disk encryption

This will assume that you've upgraded from 30GB of disk space to 45GB. If these amounts are different, change these values while following these instructions. You'll need to have rebooted your server using the controls in our client area for your server to recognise your new disk, so if you haven't done so, do that now. You'll need to be logged in as root, or have root permissions by running sudo su before running these commands.

Create a new partition

First, we have to create a new partition on the physical disk. First, run fdisk /dev/vda, which opens up the partition editor for your virtual disk named "vda" (the default on our systems). This should look similar to the following:

[root@virtualmachine root]# fdisk /dev/vda
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.


Command (m for help):

Press n then return to add a new partition. When asked for the partition type, press p and return to create a new primary partition. When asked for the partition number, press return to accept the default - and remember what that default is for later. Press return again to accept the default first sector, and press return once more to accept the default last sector.

Now we've created the partition, we need to set the type to LVM. Press t then return to set the type, and then enter the partition number from earlier and press return. For the hex code, enter 8e and press return.

At the end, it should look something like this, where "15 GiB" is how much we extended your disk by:

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3):
First sector (62914560-94371839, default 62914560):
Using default value 62914560
Last sector, +sectors or +size{K,M,G} (62914560-94371839, default 94371839):
Using default value 94371839
Partition 3 of type Linux and of size 15 GiB is set

Press w and return to save your changes and exit - there may be some warnings regarding reading the partition table, these can be ignored. Now run partprobe to cause Linux to check for new partitions - this is necessary for Linux to detect the new partition you just created. It may generate some warnings about /dev/sr0 - these are safe to ignore.

Add the new LVM physical volume

Now we need to tell LVM about the new physical volume, so run pvcreate /dev/vdaX, where X is the partition number from earlier. If it prompts you regarding wiping a dos signature, from the disk, press y and return to allow it. It should report the volume has been created:

[root@virtualmachine root]# pvcreate /dev/vda3
WARNING: dos signature detected on /dev/vda3 at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/vda3.
  Physical volume "/dev/vda3" successfully created

Add it to your LVM volume group

Now we need to add it to our LVM volume group. First, we need to find out what our volume group is called, so run vgdisplay. It should output something similar to the following:

[root@virtualmachine root]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               29.51 GiB
  PE Size               4.00 MiB
  Total PE              7554
  Alloc PE / Size       7543 / 29.46 GiB
  Free  PE / Size       11 / 44.00 MiB
  VG UUID               WQSVtz-xyEi-aAAx-jWII-NM8c-IOIt-q9PteB

Look for the line that starts with VG Name - this is the name of your volume group. In this case, that's centos.

Now, to extend the volume group, run vgextend yyyyyy /dev/vdaX, where yyyyyy is your volume group name, and X is your partition number. For example:

[root@virtualmachine root]# vgextend centos /dev/vda3
  Volume group "centos" successfully extended

Extend the LVM logical volume

Now we need to extend our LVM logical volume. First, we need to find out what that's called. Run lvdisplay - for example:

[root@peridot root]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                wEzZXL-hEby-0eFX-RgO3-7qXP-Ou21-k5Ec5y
  LV Write Access        read/write
  LV Creation host, time virtualmachine.local, 2016-08-03 22:18:15 +0100
  LV Status              available
  # open                 1
  LV Size                27.46 GiB
  Current LE             7031
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                yJobUc-CKBy-Pnqq-5nZ6-9XvB-vCu8-IGzFnb
  LV Write Access        read/write
  LV Creation host, time virtualmachine.local, 2016-08-03 22:18:15 +0100
  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:1

The LV Path entry is your logical volume name. In this example, we have two logical volumes - one called /dev/centos/root and one called /dev/centos/swap. swap is a partition that's used for caching RAM to disk, so this is unlikely to be the one you want to extend. root is the root of the filesystem - your / directory, in effect. You may also have one referencing home in some cases, this will typically be for home directories, and will contain everything under the /home directory. It's likely that you'll want to extend your root volume, since this is where most files will end up, including log files, files that are served from your webserver, etc.

Next, we need to tell LVM to actually extend that volume, so run lvextend /dev/yyyyyy/zzzzzz /dev/vdaX, where yyyyyy is your volume group name, zzzzzz is your logical volume name and X is the partition number you added earlier. It should look something like this:

[root@virtualmachine root]# lvextend /dev/centos/root /dev/vda3
  Size of logical volume centos/root changed from 27.46 GiB (7031 extents) to 42.46 GiB (10870 extents).
  Logical volume root successfully resized.

Extend the filesystem

Finally, we have to extend the filesystem, so the OS can make use of the new space. We need to find out what filesystem is on the disk, so first run df -T. This will likely have output similar to the following:

[root@virtualmachine root]# df -T
Filesystem              Type     1K-blocks    Used Available Use% Mounted on
/dev/mapper/centos-root xfs       28784916 1625908  27159008   6% /
devtmpfs                devtmpfs    498476       0    498476   0% /dev
tmpfs                   tmpfs       508452       0    508452   0% /dev/shm
tmpfs                   tmpfs       508452    6672    501780   2% /run
tmpfs                   tmpfs       508452       0    508452   0% /sys/fs/cgroup
/dev/vda1               xfs         508588  168528    340060  34% /boot
tmpfs                   tmpfs       101692       0    101692   0% /run/user/1000

Look for the line starting with /dev/mapper/yyyyyy-zzzzzz, where yyyyyy is your volume group name, and zzzzzz is your logical volume name. The Type column tells you the filesystem type - in this case, xfs.

Finally, we can extend the filesystem - jump to the section below for the relevant filesystem.

ext2, ext3 or ext4

Run resize2fs /dev/mapper/yyyyyy-zzzzzz, where yyyyyy is your volume group name, and zzzzzz is your logical volume name.

xfs

Run xfs_growfs /dev/mapper/yyyyyy-zzzzzz, where yyyyyy is your volume group name, and zzzzzz is your logical volume name.

Checking you're done

Depending on you're operating system, you may now be done. Check the free disk space by running df -h:

[root@virtualmachine root]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   43G  1.6G   41G   4% /
devtmpfs                 487M     0  487M   0% /dev
tmpfs                    497M     0  497M   0% /dev/shm
tmpfs                    497M  6.6M  491M   2% /run
tmpfs                    497M     0  497M   0% /sys/fs/cgroup
/dev/vda1                497M  165M  333M  34% /boot
tmpfs                    100M     0  100M   0% /run/user/1000

As before, look for the line starting </code>/dev/mapper/yyyyyy-zzzzzz</code>, where yyyyyy is your volume group name, and zzzzzz is your logical volume name. The Size column shows the total disk space for that logical volume, and if that's increased to the expected size, you're already done! (Note: it may be +/- 3 or 4GB from what you expected, due to technicalities of how linux handles filesystems & calculates sizes - if it's close, it's right. In this case, it reads 43GB when we extended to 45GB, which is roughly right.)

If it hasn't updated the space, run reboot to reboot the server, and the space will be available after the server reboots. You can check it by using the df -h command again.