Resizing Linux Logical Volumes With LVM

Expanding an Existing Disk

If you are using a virtualization platform such as VMWare/Hyper-V/Xen/KVM/Citrix Xen/etc you will need to shutdown the VM and extend the virtual drive. Once you have finished doing this power the machine back on and do the following:

pvdisplay

This will display the physical volume group and what is currently in it. If you are working with a system that has a single disk you will likely be working off of /dev/sda of /dev/xvda in Citrix Xenserver. We will need to create a new partition on this disk to extend the logical volume in question

fdisk /dev/sda
p
n
p
3
accept default begin block
accept default end block
t
3
8e
p
w
q

The sequence of steps above opens fdisk for /dev/sda. The p denotes the printing of the current partition table, which can be useful to see how many partition numbers there are, usually this will be /dev/sda1 for the boot partition and /dev/sda2 for the filesystem partition. Assuming this is the case pressing n will create a new partition, select p for primary, then select the next available digit (in this case 3 so we can create /dev/sda3). Then accept the default starting block and the default ending block. Once this is complete we need to set the type by pressing t then select the partition number (3) then choose 8e as the identifier for Linux LVM. Once this is done press p to confirm everything looks correct then w to write changes and q to quit.

Once you press w you have committed the changes if you make a mistake press q and try again, this will prevent any erroneous writes from occurring.

At this point we can proceed to extend the volume

partprobe

running partprobe will resync the partition table to the OS so that it can see the new partition, a reboot will do the same thing and may be required if the next step fails, however if possible to avoid a reboot it would be advisable.

pvcreate /dev/sda3

This creates /dev/sda3 as a new physical volume in the physical volume group. Next we will need to see what the volume group name is and then extend the volume group.

vgdisplay

Using the output of the above command we can then extend that volume group. For this example I’ll call it vg_example, yours will be different

vgextend vg_example /dev/sda3

Next we will need to identify the name of your logical volume you wish to extend by running the following command

lvdisplay

Once you have acquired this name we can proceed to the next example. Please note I’ve used lv-root as the name, yours may differ, and I have also used 50Gb as the amount to extend by, please use the correct amount for your use case

lvextend -L +50G /dev/mapper/vg_example/lv-root

Now we have arrived at the final step which is to resize the filesystem

resize2fs /dev/mapper/vg_example/lv-root

If you are using CentOS 7 or another OS that defaults to an XFS filesystem you will need to use xfs_growfs instead of resize2fs

Adding an Additional Disk

If you are working with a physical machine or a VM that you are adding another disk to the process is similar to the above but moderately easier. Assuming the new device we are adding is /dev/sdb, if you are doing this without restarting the machine run partprobe before attempting the below command. If this doesn’t work a reboot may be required.

pvcreate /dev/sdb

This creates /dev/sdb as a new physical volume in the physical volume group. Next we will need to see what the volume group name is and then extend the volume group.

vgdisplay

Using the output of the above command we can then extend that volume group. For this example I’ll call it vg_example, yours will be different

vgextend vg_example /dev/sdb

Next we will need to identify the name of your logical volume you wish to extend by running the following command

lvdisplay

Once you have acquired this name we can proceed to the next example. Please note I’ve used lv-root as the name, yours may differ, and I have also used 50Gb as the amount to extend by, please use the correct amount for your use case

lvextend -L +50G /dev/mapper/vg_example/lv-root

Now we have arrived at the final step which is to resize the filesystem

resize2fs /dev/mapper/vg_example/lv-root

If you are using CentOS 7 or another OS that defaults to an XFS filesystem you will need to use xfs_growfs instead of resize2fs

 

Removing Passphrase From SSH Key

So¬†generally speaking it’s always preferable to use a passphrase with your SSH Keys, however there are times when a passphrase may get in the way, particularly with service accounts that run cron jobs or connect out to other servers in your environment. Rotating keys semi-frequently would be a wise security precaution to prevent your key from becoming compromised. That said lets go through the process of stripping out the passphrase from an existing key.

Remove Passphrase From Key

openssl rsa -in ~/.ssh/id_rsa -out ~/.ssh/id_rsa_nopass

This will create a new copy of your key without a password

 

Modify Permissions Remove Old Key

First we need to remove the old key and rename the new one

rm id_rsa
mv id_rsa_nopass id_rsa

Now we need to fix permissions:

chmod 600 id_rsa

Enabling Nested Virtualization on Citrix XenServer

Create a VM

Go through the normal process of creating or cloning a VM. Make sure the virtual machine is in a powered off state

 

Edit Settings to Allow Nested Virtualization

Locate the UUID of the system, if you know the name of the box this is fairly straightforward:

xe vm-list name-label=<server name>

Once you have located the system you will be able to use the system’s UUID

xe vm-param-set UUID=<UUID> platform:exp-nested-hvm=true

 

Check CPU Info on Guest

Now that you have created a guest VM and enabled nested virtualization you will want to check that the CPU has the vmx (Intel) or svm (AMD) flag

cat /proc/cpuinfo