Wednesday 30 July 2014

Resize VirtualBox VM file system (and swap) under LVM

Quick overview of LVM

- Volume groups contain PV's and LV's, think of them as the glue between the two

- LV's are the things that are referred to in /etc/fstab (e.g thngs you out file systems on or use as swap space)

- You add space to VG's (by way of PV's) and you can then extend LV's which are members of these VG's

- The basic process is ... 1./ create disk partition, 2./ register it as a PV, 3./ add this PV to VG, 4./ extend LV

 

The high level process goes as follows....

1./ Shutdown VM

2./ Increase the size of the Virtual disk image

3./ Startup VM

4./ Create a new primary disk partition to fill the newly created VDI space

5./ Register the new partition as a PV (Physical Volume) with LVM

6./ Add the new PV to the existing Volume Group

7./ Extend the relevant Logical Volumes to use the newly available space in the Volume Group

8./ Resize the filesystem or swap accordingly depending on what you need to use the extra space for.

 

Here are the details....

First resize the Virtual disk image

Get your VM name - sudo VBoxManage list runningvms

Get the VDI path for your VM - sudo VBoxManage showvminfo <vm name> | grep -i vdi  (alternatively use VBoxManage list hdds)

- Power off the VM                                         - sudo VBoxManage controlvm <VM uuid> poweroff

- Now resize the VDI                                      - sudo VBoxManage modifyhd <path.vdi|VM uuid> --resize 32768 (for 32GB)

 

Create the primary disk partition

- Start up the VM                                             - sudo VBoxManage startvm  <uuid | name>     

- log on to the VM OS

- su to root

- fdisk /dev/sda

- u (twice) to change display units to cylinders

- p to print the partition table (you'll need this to obtain statring sector of the new partition)

- n to add a partion

- p to add primary partition

- select a partition number

- Select starting cylinder (the default should be correct)

- +16G (for adding 16GB)

- p to print the partition table to check it

- v to verify

- w to write the table

- reboot

 

Register the newply created partition with LVM

- running lvm with no args places you in the lvm prompt. Type help to see all commands

- We need to Create a physical volume (PV) for volume manager

- pvdisplay - will show you current PV’s

- pvcreate --verbose /dev/sda3  - add a new PV

 

Extend volume group

- vgdisplay - will show you which volume groups exist - we have one called "VolGroup" with a Free  PE / Size of 0/0

- vgextend <Vol group name> /dev/sda3 -  will add our new physical volume to this volume group

- if we now run vgdisplay again we'll see the Free  PE / Size has increased accordingly

 

Extend the logical volume

- lvextend --size +16G /dev/VolGroup/lv_root   - this adds 16GB to the volume the logical volume named lv_root

- Or we could have specified the target size    "lvextend --size 32G /dev/VolGroup/lv_root"

 

Display LV details

- lvdisplay

 

Now get the OS to actually use the added space by either resizing the filesystem or adding swap or both ...

 

To resize the filesystem

- resize2fs /dev/VolGroup/lv_root   (uses all available size)

- now take a look with df –k

 

To add swapspace

- swapoff /dev/VolGroup/lv_swap –v (or swapoff –a) to disable swap on all devices)

- lvextend /dev/VolGroup/lv_swap --size +1G  (assuming you have 1GB spare in your volume group)

- mkswap /dev/VolGroup/lv_swap

- swapon /dev/VolGroup/lv_swap

- now take a look with free -m

 

 


No comments:

Post a Comment