VM Adding Disk Space

This is a walkthrough of manageing storage using LVM under Ubuntu

Basic MAchine

I built a Basic Ubuntu 14.03 machine - nothing fancy.

In VirtualBox I choose to allocate 8Gb of storage.

I chose

- Install LVM (Just about the 1st screen)

Wait 5 mins and then the server has been created.

Looking in the Machine

The machine reboots - and I check the disk space...

df -kh

I can see 57% of the disk is already used - but interestingly I can see RHEL is already using Logical Volumes.

Practical Tests

I will do the following 3 tests - there are more tests shown using the RHEL LVM page.

-Add a new disk drive and add to Volume Group

Shut the machine down - and on command prompt do the following.

Create a Disk

If your storage device is VMDK - then you need to convert that to VDI

 VBoxManage createmedium --format /srv/d1/VirtualBox/HardDisks/disk2.vdi

I now add this (cheating throught the GUI) else

vboxmanage storageattach 01bcc3cd-341a-46dc-bf7f-23f6b1bf51a3 --storagectl "SATA" --port 2  --device 0 --type hdd --medium /srv/d1/VirtualBox/HardDisks/disk2.vdi

Adding The Storage

Boot the machine - and as root

df Take note of the logical volume mapping (ex. /dev/mapper/vg_lvm/lv_root)

fdisk -l

Take note of the filesystem partition of your physical volume sits (ex. /dev/sda2)

fdisk /dev/sdb We need to organise the partitions

Select your partition (we are creating from new from /dev/sdb) n Create a new partition p Make it a primary partition 1 The partition number to make it on (same as we deleted) Set the starting block (keep the default as it is usually correct) Set the ending block (keep the default as it is fine for our use case) w Write the partition (will also exit fdisk shell)

reboot We must reboot in order to have the new partition table loaded

vgextend my_volume_group /dev/sdb1 Add Disk to Volume Group

pvresize /dev/sdb1 Resizes the physical volume pvscan Use to verify the new size lvextend -l +100%FREE /dev/vg_lvm/lv_root Extend the logical volume to take all free space resize2fs /dev/vg_lvm/lv_root Resize the file system df See your newly sized volume

And that is it! With the last df command, you should see that your volume increased!

df
fdisk -l
fdisk /dev/sda
d
2
n
p
2
<return>
<return>
w
reboot
pvresize /dev/sda2
pvscan
lvextend -l +100%FREE /dev/vg_lvm/lv_root
resize2fs /dev/vg_lvm/lv_root

Add Storage

Shut the VM down and create a new drive.

vboxmanage createmedium disk --filename ~/VirtualBox\ VMs/RH_LVM_test/disk3.vdi --format VDI --size 2500
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: c5be9a2b-478f-4556-b078-6c6fbf0e33b6

Add Storage to VM

Now add this Disk to the VM

 VBoxManage storageattach 01bcc3cd-341a-46dc-bf7f-23f6b1bf51a3 --storagectl "SATA" --port 2  --device 0 --type hdd --medium ~/VirtualBox\ VMs/RH_LVM_test/disk3.vdi

Note u may need to alter the port value depending how many other Disks you have.

Boot Machine

We should now have our existing disk available - but the new drive is currently now used.

fdisk -l

Shows /dev/sdb and is 2.6 Gb is size.

fdisk /dev/sdb

o create new Partition Table

n New Partition

p Primary

1 Partition No

return Start Block

return End Block

w Write and Exit

Add Physical Volume to a Volume Group

Check what VG there are

 vgdisplay

Then

vgextend vg_lvm /dev/sdb1

At this point the new Disk in in the new Volume Group (But now the Logical Volume)

Add to Logical Volume

lvextend /dev/vg_lvm/lv_root /dev/sdb1

You should now see that the storage available has increased.

Resize the LV

If you are using RHEL/Fedora etc then you can increase the LV with

resize2fs /dev/mapper/vg_lvm-lv-root

Conclusion

Add New Disk is easy, expand existing disk is easy - but ONLY if you are using Logical Volumes.