LVM is easy!

    Actually, I want to simply and easily talk about such a wonderful thing as Logical Volume Management or Logical Volume Management.
    Since I have been using LVM for a long time, I’ll tell you what it means for me, without looking at the manuals and pulling out quotes from the wiki, in my own words, so that it is clear to those who know nothing about it. I’ll try not to talk about all sorts of “advanced” functions like stripes, snapshots, etc.

    LVM is an additional layer of abstraction from iron, which allows you to collect heaps of heterogeneous disks into one, and then again break up this one exactly as we want.

    There are 3 levels of abstraction:
    1. PV (Physical Volume) - physical volumes (these can be partitions or entire “unbroken” disks)
    2. VG (Volume Group) - a group of volumes (we combine physical volumes (PV) into a group, create a single disk, which we will further partition as we want)
    3. LV (Logical Volume) - logical partitions, actually a section of our new “ a single disk ”aka the Group of Volumes, which we then format and use as a regular partition, a regular hard disk.
    this is perhaps the whole theory. :) now practice:
    to work, you need lvm2 packages and the ability to work with root privileges therefore:
    $ sudo bash
    # apt-get install lvm2

    Let's say we have a 40GB hard drive in our computer and we managed to scrape up some money and finally buy a TERBAYTnik! :))) The system is already up and running, and the first disk is divided into one partition (/ dev / sda1 as /), the second - the largest one that we just connected - is not broken / dev / sdb at all ... I
    suggest unloading the root disk a bit, and at the same time speed up (the new disk is faster than the old one) and "secure" the system using lvm.
    You can create partitions on the second disk and add them to volume groups (if we need several volume groups),
    or you can completely not make partitions on the disk and make the entire device a physical partition (PV)

    root @ ws: ~ # pvcreate / dev / sdb
    Physical volume "/ dev / sdb" successfully created

    Create a group of volumes with a talking name, for example, by the name of the machine “ws”, so that when we drag this disk to another machine there will be no conflict with the names of the volume groups:

    root @ ws: ~ # vgcreate ws / dev / sdb
    Volume group “vg0” successfully created

    it is advisable to make folders such as / usr / var / tmp / home from the root partition so as not to defragment the root partition once again and in any case not to overfill it, so create partitions:

    root @ ws: ~ # lvcreate -n usr -L10G ws # here we create a section called “usr”, 10Gb in size
    Logical volume “usr” created
    by analogy, do the same for / var, / tmp, / home:
    root @ ws: ~ # lvcreate -n var -L10G ws
    root @ ws : ~ # l vcreate -n tmp -L2G ws
    root @ ws: ~ # lvcreate -n home -L500G ws
    we still have a little free space in the volume group (for example, for the future partition for backup) to
    see exactly how much you can with the command:
    root @ ws: ~ # vgdisplay
    information on the created logical volumes
    root @ ws: ~ # lvdisplay
    information on the physical volumes
    root @ ws : ~ # pvdisplay

    sections that we created will appear in the / dev / [vg_name] / folder, more precisely there will be links to files,
    lrwxrwxrwx 1 root root 22 2009-08-10 18:35 swap -> / dev / mapper / ws-swap
    lrwxrwxrwx 1 root root 21 2009-08-10 18:35 tmp -> / dev / mapper / ws-tmp
    lrwxrwxrwx 1 root root 21 2009-08-10 18:35 usr -> / dev / mapper / ws-usr
    lrwxrwxrwx 1 root root 21 2009-08-10 18:35 var -> / dev / mapper / ws-var
    , etc ...

    then lvm is almost over ... we format our partitions into our favorite file systems:
    root @ ws: ~ # mkfs.ext2 -L tmp / dev / ws / tmp
    root @ ws: ~ # mkfs.ext4 -L usr / dev / ws / usr
    root @ ws: ~ # mkfs.ext4 -L var / dev / ws / var
    root @ ws: ~ # mkfs.ext4 -L home / dev / ws / home, by the

    way, it would be nice to make a swap partition:
    root @ ws: ~ # lvcreate -n swap -L2G ws
    root @ ws: ~ # mkswap -L swap / dev / ws / swap
    root @ ws: ~ # swapon / dev / ws / swap

    create a folder and connect the newly formed volumes in turn, copy them desired contents:
    root @ ws: ~ # mkdir / mnt / target
    root @ ws: ~ # mount / dev / ws / home / mnt / target
    copy everything there from the / home folder with your favorite file manager (while preserving access rights), for example So ;):
    root @ ws: ~ # cp -a / home / * / mnt / target /
    root @ ws: ~ # umount / mnt / target / by the
    way, for the temp folder you only need to fix the rights, copying something is optional there:
    root @ ws : ~ # mount / dev / ws / tmp / mnt / target && chmod -R a + rwx / mnt / target && umount / mnt / target /
    add the necessary lines to / etc / fstab, for example:
    / dev / mapper / ws -home / home ext4 relatime 0 2
    / dev / mapper / ws-tmp / tmp ext2 noatime 0 2
    / dev / mapper / ws-swap none swap sw 0 0
    and reboot ... (advanced gentlemen can do without rebooting;))

    In delicious I want to offer a more advanced thing:
    Let's say we have a system with a partition on LVM, and the hard drive started to fail, then we can move the entire system to another hard drive / partition without rebooting:

    # On-line adding / removing hard drives using LVM (example)

    root @ ws: ~ # pvcreate / dev / sda1 # our emulator for the failed disk
    Physical volume "/ dev / sda1" successfully created

    root @ ws: ~ # pvcreate / dev / sdb1 # our emulator for the rescue disk
    Physical volume "/ dev / sdb1" successfully created

    root @ ws: ~ # vgcreate vg0 / dev / sda1 # create a group of volumes vg0
    Volume group “vg0” successfully created

    root @ ws: ~ # lvcreate -n test -L10G vg0 # create a section for “important” information
    Logical volume “test” created

    root @ ws: ~ # mkfs.ext2 / dev / vg0 / test # create a file system on the
    root @ ws: ~ # mount / dev / mapper / vg0-test / mnt / tmp / # mount the partition
    ... # fill it with information, I open on it several files, etc.

    root @ ws: ~ # vgextend vg0 / dev / sdb1 # expand my volume group to the “rescue” disk
    Volume group “vg0” successfully extended

    root @ work: ~ # pvmove / dev / sda1 / dev / sdb1 # move the contents from the “dying” "Drive to the" rescue "
    / dev / sda1: Moved: 0.9%
    / dev / sda1: Moved: 1.8%
    ...
    / dev / sda1: Moved: 99.7%
    / dev / sda1: Moved: 100.0%

    root @ work: ~ # vgreduce vg0 / dev / sda1 # remove the "dying" disk from the volume group.
    Removed "/ dev / sda1" from volume group "vg0"

    Total:
    I created a logical partition, formatted it, mounted it and filled it with the necessary data, then moved it from one device to another, while the partition remained mounted and the data was always available!
    In a similar way, I was able to transfer the entire system from a dying disk to a reed array without rebooting. :)

    And this is my favorite link on LVM: xgu.ru/wiki/LVM

    PS Please forgive me for typos, I was constantly distracted =))

    PPS Ah, yes !!! The most important and the biggest minus of LVM is that it is not read by grub
    so the / boot partition must be outside LVM on a separate partition on the hard drive,
    otherwise the system will not boot.

    Also popular now: