LVM - Merging Physical Drives

Logical Volume Management (LVM) is a nifty tool with many benefits. For me, there are two main benefits to using LVM.

  • Merging physical disks to appear as one volume when your drives aren't big enough.
  • Creating snapshots.

This tutorial aims to show you how to use LVM to quickly take advantage of the first benefit, pooling your drives together. Note that you can 'merge' volumes with RAID, but LVM is "easier" because you can mix and match different any size drives together and fully utilize them. With RAID, it is generally best match identical drive sizes together, but preferably with different amounts of usage/wear (i.e. Don't just stick two brand new drives into a raid array or they may fail at exactly the same time which defeats the purpose).

Note: This tutorial is written for guiding users through setting up thier first volume group and then to be re-used to add more drives to that volume group later. Thus, some of the steps may be need to be skipped depending on the scenario, and will clearly indicate to do so in the instructions, so please not just read the commands/code.

Steps

    For disks over 2TB we need to corrupt the GPT partition table.
    sudo dd if=/dev/zero of=/dev/sd[x] bs=512 count=1
    sudo partprobe
    Now we can create our physical volume on the drive with the following command:
    sudo pvcreate /dev/sd[x]
    If you are going to create a new volume group rather than add this to an existing one, perform the following command, otherwise please skip this step.
    sudo vgcreate [volume Group Name] /dev/sd[x]
    Example
    sudo vgcreate vgBackup /dev/sdc
    For all the other drives, or if you are adding this drive to an existing volume group:
    sudo vgextend [volume group name] /dev/sd[x]
    Example
    sudo vgextend vgBackup /dev/sdc
    If you are creating the LVM for the first time rather than extending an existing one:
    sudo lvcreate -L [size in GB]G -n lvBackup vgBackup
    Example:
    sudo lvcreate -L 3125G -n lvBackup vgBackup

    If you are adding a drive, or got the size slightly too low in the previous step, keep running the following command until you get an error message like
    "Insufficient free space: ___ extents needed, but only ___ available"

    sudo lvextend -L+[Number of GB to add]G -n /dev/[volume group name]/[logicial volume name]
    Example:
    sudo lvextend -L+1G -n /dev/vgBackup/lvBackup
    If you are creating a volume group rather than extending one then you will need to create a filesystem like so:
    sudo mkfs -t ext3 /dev/[volume group]/[volume name]
    Example:
    sudo mkfs -t ext3 /dev/vgBackup/lvBackup
    If you already have a filesystem on an existing volume group that you are adding to, then you need to extend that filesystem to utilize your newly added drive:
    resize2fs /dev/[volume group name]/[logical volume name]
    Example:
    resize2fs /dev/vgBackup/lvBackup
    Now lets mount your drive somewhere so you can store files on it. Make a directory somewhere (such as in your home folder or /mnt). Now mount the volume group manually:
    sudo mount /dev/[volume group name]/[logical volumen name] /path/to/new/directory
    Example
    sudo mkdir /mnt/backup
    sudo mount /dev/vgBackup/lvBackup /mnt/backup

References

No comments:

Post a Comment