Expanding disk space via LVM partitions

The first step is to either A) add a new disk or B) expand an existing one. The steps below show each option in the context of a hyper-v VM and an Amazon EBS volume respectively.

After that is done, either C) create LVM partitions or D) extend existing ones to use the additional space.

A. Adding a new disk volume (Hyper-V)

Start by noting the devices that are already in the VM before the addition

ls /dev/sd* 

Add a new disk to the server:

Select the VM > Settings > IDE Controller 0 > Hard Drive > Add
New > VHDX > Dynamically expanding > accept the offered name or pick a new one > Next > Pick a size (depending on how much space there is left on the laptop), say 100Gb > Finish > Apply

2. Identify the device that was added

ls /dev/sd* 

Compare the list with what was shown before the addition to see which device was added, for example /dev/sdb

B. Adding space to an existing disk volume

The steps here describe how to expand to the full 30Gb of an EBS volume, of which just 8Gb are allocated by default. Additional steps may be needed if adding another volume. Adapted from https://www.rootusers.com/how-to-increase-the-size-of-a-linux-lvm-by-expanding-the-virtual-machine-disk/

Expand the EBS volume using one of the methods described here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-modify-volume.html. Then change the VM to make use of the addiitonal unpartitioned disk space.

Use fdisk to create a new primary partition to make use of the new expanded disk space. Note that we do not have 4 primary partitions already in place, making this method possible.

fdisk /dev/xvda

We are now using fdisk to create a new partition, the inputs I have entered in are shown below in bold. Note that you can press ‘m’ to get a full listing of the fdisk commands.

‘n’ was selected for adding a new partition.

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n

‘p’ is then selected as we are making a primary partition.

Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p

As I already have /dev/xda1, I have gone with using ‘2’ for this new partition which will be created as /dev/sda3

Partition number (1-4): 2

We just press enter twice above as by default the first and last cylinders of the unallocated space should be correct. After this the partition is then ready.

First cylinder (2611-3916, default 2611): "enter"
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-3916, default 3916): "enter"
Using default value 3916

‘t’ is selected to change to a partition’s system ID, in this case we change to ‘3’ which is the one we just created.

Command (m for help): t
Partition number (1-5): 2

The hex code ‘8e’ was entered as this is the code for a Linux LVM which is what we want this partition to be, as we will be joining it with the original /dev/sda5 Linux LVM.

Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

‘w’ is used to write the table to disk and exit, basically all the changes that have been done will be saved and then you will be exited from fdisk.

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

You will see a warning which basically means in order to use the new table with the changes a system reboot is required. If you can not see the new partition using “fdisk -l” you may be able to run “partprobe -s” to rescan the partitions. In my test I did not require either of those things at this stage (I do a reboot later on), straight after pressing ‘w’ in fdisk I was able to see the new /dev/sda3 partition of my 10gb of space as displayed in the below image.

For CentOS/RHEL run a “partx -a /dev/xvda2” to avoid rebooting later on.

That’s all for partitioning, we now have a new partition which is making use of the previously unallocated disk space.

C. Create LVM structure if not yet there

We use the pvcreate command which creates a physical volume for later use by the logical volume manager (LVM). In this case the physical volume will be our new /dev/sda3 partition. if pvcreate is not found, yum install lvm2.

pvcreate /dev/xvda2
  Device /dev/xvda2 not found (or ignored by filtering).

In order to get around this you can either reboot, or use partprobe/partx as previously mentioned to avoid a reboot, as in this instance the disk does not appear to be there correctly despite showing in “fdisk -l”. After a reboot or partprobe/partx use the same command which will succeed.

pvcreate /dev/xvda2
  Physical volume "/dev/xvda2" successfully created

Create a new volume group

vgcreate vg_var /dev/xvda2 

Create a new logical volume that takes the new space

lvcreate -l 100%FREE -nlv_var vg_var 

Move var to its own volume

Most of the data is stored by Anoto under /var. A good way to make more space for additional forms is therefore to relocate this folder to the location that now contains extra space

Use the logical volume instead of /dev/xvd*, for example /dev/vg_fdidevalf/lv_var

http://unix.stackexchange.com/questions/131311/moving-var-home-to-separate-partition

mkfs.ext4 /dev/vg_var/lv_var
mkdir /mnt/var
mount /dev/vg_var/lv_var /mnt/var
cd /var
cp -ax * /mnt/var
cd /
mv var var.old
mkdir var
umount /dev/vg_var/lv_var
mount /dev/vg_var/lv_var /var
vim /etc/fstab
/dev/vg_var/lv_var       /var     ext4    defaults    0 0

 

For reference only, LVM partitions can also be shrunk:

https://rbgeek.wordpress.com/2013/02/11/how-to-reduce-the-root-partition-in-lvm/

 

D. Extending already existing LVM partition

3. Find the logical volume to extend

lvdisplay

Look for the volume associated with the /var location. That's the one that will be extended, since that's where ALF data is stored. Note the VG NAME and LG PATH.

4. Create physical volume on the new disk

pvcreate /dev/sdb # or /dev/xdb - identified in step 2

5. Add the physical volume to the volume group

vgextend vg_fdidevalf /dev/sdb # use the VG NAME noted in 3 and the device identified in 2

6. Extend the Logical Volume to occupy the whole additional space and grow it

lvextend -l +100%FREE /dev/vg_fdidevalf/lv_var # use the LG PATH from 3
resize2fs /dev/vg_fdidevalf/lv_var
Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.
Powered by Zendesk