lvm-guide

Ubuntu LVM Guide

This guide shows how to work with LVM (Logical Volume Management) on Linux. This guide was originally written by Falko Timme <ft [at] falkotimme [dot] com> (here), but I’ve re-organized and adapted it to be used as more of a reference for those looking for something a little more abstract.

Abstract:

LVM is a logical volume manager for the Linux kernel. It was originally written in 1998 by Heinz Mauelshagen, who based its design on that of the LVM in HP-UX.

Notes:

I adapted this guide for use on Ubuntu 7.04, so I can’t guarantee it will work exactly as advertised on anything else. With that in mind, if you install all the correct packages, it should work on any distribution.

I know there are formatting problems – but I’ll get to them. It took me nearly 4 months to actually get around to completing the guide, but I hope to fix up the formatting sometime in the near future.

1. Installing necessary tools

  1. sudo apt-get install lvm2 dmsetup mdadm reiserfsprogs xfsprogs

2. Raid Setup

Please refer to some of my other guides regarding setting up various raid levels. You can, and I would highly recommend, setting up your drives in a raid configruation prior to adding them to a logical volume. By doing so, you’ll have properly redundant drives to keep all of your precious data safe.3. LVM Layout

Very basically an LVM consists of multiple drives, which make up a “Volume Group”. This Volume Group, can then be divided up into Logical Volumes, and expanded / contracted at will. Any Logical Volume can be larger than the physical size of any one disk, but the total size of all Logical Volumes, obviously, cannot exceed the total space your disks afford you.4. LVM Setup

Lets start by determining what disks are attached to our system:

  1. $ sudo fdisk -l

  2. Disk /dev/hdb: 60.0 GB, 60040544256 bytes

  3. 255 heads, 63 sectors/track, 7299 cylinders

  4. Units = cylinders of 16065 * 512 = 8225280 bytes Device Boot Start End Blocks Id System

  5. /dev/hdb1 * 1 6996 56195338+ 83 Linux

  6. /dev/hdb2 6997 7299 2433847+ 5 Extended

  7. /dev/hdb5 6997 7299 2433816 82 Linux swap / SolarisDisk /dev/sda: 160.0 GB, 160041885696 bytes

  8. 255 heads, 63 sectors/track, 19457 cylinders

  9. Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sda: 160.0 GB, 160041885696 bytes

  10. 255 heads, 63 sectors/track, 19457 cylinders

  11. Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sda doesn't contain a valid partition tableDisk /dev/sdb: 160.0 GB, 160041885696 bytes

  12. 255 heads, 63 sectors/track, 19457 cylinders

  13. Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdb doesn't contain a valid partition tableDisk /dev/sdc: 500.1 GB, 500107862016 bytes

  14. 255 heads, 63 sectors/track, 60801 cylinders

  15. Units = cylinders of 16065 * 512 = 8225280 bytesDisk /dev/sdc doesn't contain a valid partition tableDisk /dev/sdd: 500.1 GB, 500107862016 bytes

  16. 255 heads, 63 sectors/track, 60801 cylinders

  17. Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdd doesn’t contain a valid partition table

As you can see, I have 5 disks in the system, one of which is a 60 GB boot drive, and the other four are 2x 160GB drives and 2x 500GB drives.

First thing you’ll notice is that the 4 drives I want to add to my lvm: /dev/sda – /dev/sdd do not contain partition tables. So, the first thing we’ll do is partition the drives to their maximum capacity:

  1. $ sudo fdisk /dev/sda

  2. The number of cylinders for this disk is set to 19457.

  3. There is nothing wrong with that, but this is larger than 1024,

  4. and could in certain setups cause problems with:

  5. 1) software that runs at boot time (e.g., old versions of LILO)

  6. 2) booting and partitioning software from other OSs

  7. (e.g., DOS FDISK, OS/2 FDISK)Command (m for help): <strong>&lt;-- m</strong>

  8. Command action

  9. a toggle a bootable flag

  10. b edit bsd disklabel

  11. c toggle the dos compatibility flag

  12. d delete a partition

  13. l list known partition types

  14. m print this menu

  15. n add a new partition

  16. o create a new empty DOS partition table

  17. p print the partition table

  18. q quit without saving changes

  19. s create a new empty Sun disklabel

  20. t change a partition's system id

  21. u change display/entry units

  22. v verify the partition table

  23. w write table to disk and exit

  24. x extra functionality (experts only)Command (m for help): <strong>&lt;-- n</strong>

  25. Command action

  26. e extended

  27. p primary partition (1-4)

  28. <strong>&lt;-- p</strong>

  29. Partition number (1-4): <strong>&lt;-- 1</strong>

  30. First cylinder (1-19457, default 1): <strong>&lt;-- &lt;enter&gt;</strong>

  31. Using default value 1

  32. Last cylinder or +size or +sizeM or +sizeK (1-19457, default 19457): <strong>&lt;-- &lt;enter&gt;</strong>

  33. Command (m for help): <strong>&lt;-- t</strong>

  34. Selected partition 1Hex code (type L to list codes): <strong>&lt;-- 8e</strong>

  35. Changed system type of partition 1 to 8e (Linux LVM)

  36. Command (m for help): <strong>&lt;-- w</strong>

  37. The partition table has been altered!

  38. Calling ioctl() to re-read partition table.

  39. Syncing disks.

Now repeat the process for the other 3 disks:

  1. $ sudo fdisk /dev/sdb

  2. $ sudo fdisk /dev/sdc

  3. $ sudo fdisk /dev/sdd

Now you’ll want to prepared the partitions for use in out Volume Group:

  1. $ sudo pvcreate /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1

  2. Physical volume "/dev/sda1" successfully created

  3. Physical volume "/dev/sdb1" successfully created

  4. Physical volume "/dev/sdc1" successfully created

  5. Physical volume "/dev/sdd1" successfully created

Now run:

  1. $ sudo pvdisplay

  2. $ sudo pvdisplay

  3. --- NEW Physical volume ---

  4. PV Name /dev/sda1

  5. VG Name

  6. PV Size 149.05 GB

  7. Allocatable NO

  8. PE Size (KByte) 0

  9. Total PE 0

  10. Free PE 0

  11. Allocated PE 0

  12. PV UUID **

  13. --- NEW Physical volume ---

  14. PV Name /dev/sdb1

  15. VG Name

  16. PV Size 149.05 GB

  17. Allocatable NO

  18. PE Size (KByte) 0

  19. Total PE 0

  20. Free PE 0

  21. Allocated PE 0

  22. PV UUID **

  23. --- NEW Physical volume ---

  24. PV Name /dev/sdc1

  25. VG Name

  26. PV Size 465.76

  27. Allocatable NO

  28. PE Size (KByte) 0

  29. Total PE 0

  30. Free PE 0

  31. Allocated PE 0

  32. PV UUID **

  33. --- NEW Physical volume ---

  34. PV Name /dev/sdd1

  35. VG Name

  36. PV Size 465.76

  37. Allocatable NO

  38. PE Size (KByte) 0

  39. Total PE 0

  40. Free PE 0

  41. Allocated PE 0

  42. PV UUID **

You can now see that our physical volumes are all set up. I removed the PV UUID since they will be different for any and all systems.

Now its time to create our volume group:

  1. $ sudo vgcreate fileserver /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1

Now you should be able to list the contents of your volume group:

  1. $sudo vgdisplay

  2. --- Volume group ---

  3. VG Name fileserver

  4. System ID

  5. Format lvm2

  6. Metadata Areas 4

  7. Metadata Sequence No 1

  8. VG Access read/write

  9. VG Status resizable

  10. MAX LV 0

  11. Cur LV 0

  12. Open LV 0

  13. Max PV 0

  14. Cur PV 4

  15. Act PV 4

  16. VG Size 1229.62 GB

  17. PE Size 4.00 MB

  18. Total PE 314780

  19. Alloc PE / Size 0 / 0

  20. Free PE / Size 314780 / 1229.62 GB

  21. VG UUID

Finally we’ll create some logical volumes on the volume group:

  1. $ sudo lvcreate --name share --size 100G fileserver

  2. $ sudo lvcreate --name backup --size 400G fileserver

  3. $ sudo lvcreate --name filestore --size 400G fileserver

You’ll notice that I didn’t use up the full amount of space on the logical volume. I did this for two reasons, first, its always good to leave yourself some room to grow. Second, I find LVMs tend to get really slow once they’re getting filled up.

Now, lets throw a filesystem on our logical volumes.

  1. $ sudo mkfs.reiserfs /dev/fileserver/share

  2. $ sudo mkfs.reiserfs /dev/fileserver/backup

  3. $ sudo mkfs.reiserfs /dev/fileserver/filestore

Finally, we’re ready to mount those volumes. Obviously you’ll need somewhere to mount them to:

  1. $ sudo mkdir /var/share /var/backup /var/filestore

And now we mount them:

  1. $ sudo mount /dev/fileserver/share /var/share

  2. $ sudo mount /dev/fileserver/backup /var/backup

  3. $ sudo mount /dev/fileserver/media /var/media

And thats it…

Dave Lachapelle © 2012 All Rights Reserved. We Love WordPress 3.4.2 .