Wednesday 29 December 2010

Adding Disks via LVM

In my last post, I mentioned that I like to use a 4GB disk for my root volume on any Linux VM's... this is mainly for local / dev environments, where you want to cram as many VM's as possible into a small space. But sometimes you need more space, in which case you can add another disk. This isn't as straight forward with lvm as it is without, so here's a nice easy step by step to help you get there after you have added a new disk to the VM. In this case, we are adding a new 2GB disk that will be configured as a new logical volume (you could of course just add to an existing volume if you wanted). We'll then format it with ext3 and mount it at /usr/local/pgsql/data.


1. Determine the device name using fdisk

> fdisk -l

Disk /dev/sda: 4294 MB, 4294967296 bytes
...
Disk /dev/sdb: 2147 MB, 2147483648 bytes
...
Disk /dev/sdb doesn't contain a valid partition table

2. Create a physical volume using the entire device using pvcreate

> pvcreate /dev/sdb

Physical volume "/dev/sdb" successfully created

3. Check your pv was created using pvdisplay

> pvdisplay

...

"/dev/sdb" is a new physical volume of "2.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 2.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID U5qcEM-udqZ-fDBF-qc2w-sWVR-Smpf-LIREms

4. Check current volume groups using vgdisplay

> vgdisplay
--- Volume group ---
VG Name vg00
System ID
Format lvm2
Metadata Areas 1
...
VG UUID fFWVQl-937b-chRg-8s6v-Nd4E-H3m6-o8Lxbr

5. Create a new volume group with vgcreate

> vgcreate vg01 /dev/sdb
Volume group "vg01" successfully created

6. Check vg was created using vgdisplay

> vgdisplay
--- Volume group ---
VG Name vg01
System ID
Format lvm2
Metadata Areas 1
...
Alloc PE / Size 0 / 0
Free PE / Size 511 / 2.00 GiB
VG UUID ZK7Cqf-zdJh-1ibo-djqm-JQw1-BgHe-vjXFaE


--- Volume group ---
VG Name vg00
System ID
Format lvm2
Metadata Areas 1
...
VG UUID fFWVQl-937b-chRg-8s6v-Nd4E-H3m6-o8Lxbr

7. Create a logical volume named 'data' using 100% of free space on the volume group vg01 using lvcreate

> lvcreate -l 100%FREE -n data vg01
Logical volume "data" created

8. confirm lv was created using lvdisplay

> lvdisplay
--- Logical volume ---
LV Name /dev/vg01/data
VG Name vg01
...
LV Size 2.00 GiB
Current LE 511
...
Block device 251:2

--- Logical volume ---
LV Name /dev/vg00/swap
VG Name vg00
...
Block device 251:0

--- Logical volume ---
LV Name /dev/vg00/root
VG Name vg00
...
Block device 251:1

9. Create a filesystem on the logical volume using mkfs.ext4

> mkfs.ext3 -m 0 /dev/vg01/data
mke2fs 1.41.11 (14-Mar-2010)
...
This filesystem will be automatically checked every 31 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

10. Create the desired mount point

> mkdir /usr/local/pgsql/data

11. Add entry to /etc/fstab

/dev/vg01/data /usr/local/pgsql/data ext3 defaults 0 0

12. Mount everything in fstab

> mount -a

12. Check you can change into your new volume

> cd /usr/local/pgsql/data

And you're done!