LVM: merge hard drives as if they were only one

LVM usage examples

GNU Linux is extremely versatile, no one doubts that. But perhaps some users do not know some tools or possibilities that it offers us and that could facilitate our day to day or do incredible things. In this article we will talk about LVM (Logical Volume Manager), a tool that, although initially it was not created for Linux, later it was ported and now Linux users can enjoy its possibilities.

LVM is a logical volume manager as its name suggests, initially it was cCreated by Heinz Mauelshagen in 1998 for the HP-UX operating system, HP's UNIX. But later it would be implemented in the Linux kernel. With it you can resize logical groups, as well as logical volumes, read-only snapshots, manage RAID, etc. But the feature that interests us for this article is to merge several hard drives.

LVM can "see" disk groups and partitions as a whole instead of handling many independent spaces. That is why we can join several partitions as one, expand certain partitions on other different physical disks, play with several disks in RAID mode, add "hot" or "hot swap" hard drives, without forgetting the "snapshot" function to create backups. .

How can you do this? Well, basically thanks to the handling of three concepts:

  • PV (Physical Volume): are the physical volumes, that is, hard drives or partitions of a computer.
  • VG (Volume Group): volume group, is the area where the PVs and VLs meet.
  • LV (Logical Volume): logical volumes or devices where file systems or FS can be created.

To work with LVM, we can work from the terminal with three main tools:

  • pvccreate: You can create physical volumes by joining several different hard drives or partitions. For example, let's join the partition / dev / sda3 and / dev / sdb1:
pvcreate /dev/sda3 /dev/sdb1

  • vgcreate: You can create volume groups, that is, partitions or physical disks belong to a group. For example, to create a group called "data":
vgcreate datos /dev/sdb1

  • lvcreate: defines the logical volumes that will be within the group. For example, imagine that you want to create a volume called "new" within the group "data" and 8GB in size:
lvcreate --name nuevo --size 8G datos

Let's see a practical exampleImagine that you have a computer with an x ​​GB hard drive and you decide to expand the capacity by including another hard drive. In that case, the operating system treats it as such, another new hard drive in which you must create one or more partitions on it to use it. I'm going to be even more specific, imagine that your hard drive is 120GB first and that you have a series of partitions, including / home that occupies 80GB of those 120 and is called / dev / sda3, where / dev / sda1 is the root partition. / and / dev / sda2 the SWAP ...

Now you find your new hard drive with 500GB (/ dev / sdb1) of additional space, but instead of creating another partition, you want your / home to have 580GB. This is possible with LVM, making / dev / sda and / dev / sdb be viewed by the operating system as a single device, a single partition that is physically located on two different hard drives. And this is only a small possibility of the many that LVM allows and it would be done like this:

—BEFORE ANYTHING, MAKE A BACKUP COPY OF / HOME AS IT WILL BE FORMATTED -

sudo -i

unmount /dev/sda3

unmount /dev/sdb1

vgcreate lvm /dev/sda3 /dev/sdb1

modprobe dm-mod

lvcreate -n home -l 100% VG lvm

mkfs.ext4 /dev/lvm/home

mount /dev/lvm/home /home

All that remains is to edit the / etc / fstab file So do not mount the pratitions / dev / sda3 and / dev / sdb1 at system startup, failing that, mount / dev / lvm / home / home. If we type the following (use gedit, nano or whatever text editor you want ...):

sudo gedit /etc/fstab

We can see the content to edit it, we will see that there are comments # and other lines to mount the partitions that we currently have in our distro. Be careful, spaces are not normal spaces, when you edit, use TAB to space the content! You will see that you can put something like UUID = XXX-XXX-XXX-XXX, but you can substitute this gibberish for / dev / sdx without problems ... that is, for the name of the partition as is. In our case you would have to remove (or better than delete, put a # at the beginning of the line to make a comment, so if there is a problem or it does not work, we could edit fstab and simply delete our new line and remove the # to return to the previous configuration ...) the two lines corresponding to / dev / sda3 and / dev / sdb1 and add:

/ dev / lvm / home / home ext4 defaults 0 1

Restart and now we would have a / home of 580GB, joining the two hard drives as if they were only one. Of course you can vary the parameters to your liking, use the partitions that you want, the FS you want (here we have used EXT4, but you can use whatever you need), etc. Please, leave your messages, questions, comments, etc..


8 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: AB Internet Networks 2008 SL
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   George said

    Hey.
    Very good tutorial, but they believe that they do not mention that we must make a backup of information, since when joining the partitions of the / home disks, they are formatting both disks: mkfs.ext4 / dev / lvm / home, as you do to not delete the information you already have in the first / home partition to get the same results?

    1.    David-G said

      It is that the two partitions are formatted, so you have to make a backup before proceeding to join them (I recommend deja-dup / sudo snap install deja-dup –classic (this for the stable version through snap) / sudo apt install leaves -dup).
      I hope it has been helpful for you.

  2.   Saul said

    On my previous laptop I had a hard drive with quite a few bad sectors scattered all over it. I partitioned the good and bad sections (around 16 partitions). I joined the good sections as a group volume with LVM and on top of that new "storage unit" I installed my fedora without major inconvenience. With LVM I made usable that hard drive that would otherwise go to the trash, it saved me some pesos.

  3.   Nestor R. Arango said

    Thanks for the info, it's something I needed to do. Thanks

  4.   Juan Jose Lopez Maglione said

    I have tried following this tutorial, but found that LVM is no longer installed by default on Debian 9.5.0. And when you want to install it, with [sudo apt install lvm2], the application is installed, but when you want to activate it, it is not activated. It gives the following error:
    $ sudo service lvm2 start
    Failed to start lvm2.service: Unit lvm2.service is masked.

  5.   Yoel - Mr. support said

    Good afternoon.
    interesting article.
    I have a question, how many discs can you use as a single unit? that is, if I have 4 discs, could I join the 4 as a single unit?

    Thanks in advance (:

    Mr. suporty

  6.   Manuel Nevado Santos said

    Hello:

    I'm going to build a totally new desktop. I only want Ubuntu 18.04 as the operating system. I'm going to put a Western Digital Black SN750 NVMe 500GB SSD M.2 PCI Express 3.0, which is supposed to be very fast. But also, I have at home a Samsung 860 EVO Basic SSD 500GB SATA3, which I bought at the time and in the end I did not use, thinking about this new desktop moment. I want to add it too. I have read your article, and I would like, if it is not too much trouble, if you would advise me as to what partitions to create and how, to obtain good performance. Thanks.

  7.   Pedro said

    Hello, thanks for your valuable contribution, I don't know much about linux, I just mounted a server with 2 980gb solid state hard drives, I made a logical drive, but I see that linux created a 200gb partition which I am already filling and not I know how to activate the rest of the space that tells me is 1.7 Tb, hopefully you can help me how to activate it and so that I can use it for audio, I am the recent webmin interface. Greetings.