Thursday, August 8, 2013

HOWTO LVM

LVM stands for Logical Volume Manager.

With LVM, we can create Logical Partitions that can span across one or more Physical hard drives. The hard drives are divided into Physical Volumes (PV), then those Physical Volumes are combined together to create the Volume Group (VG) and finally the Logical Volumes (LV) are created from Volume Group to, at the end, be able to create your Filesystem.

Before proceed, we need install the lvm2 package, which mostly is not installed by default on a basic Debian distribution.

ximena@anneke:~$ sudo apt-get intall lvm2

To create a LVM, we need to run through the following steps:
  •     Select and create the Physical Partitions (PV)
  •     Create the Volume Group
  •     Create Logical Volumes

Select and Create the Physical Storage Devices

In this step, we need to choose the Physical Volumes that will be used to create the LV. We can create the physical volumes using pvcreate command, but before, we will check our current partitions:

ximena@anneke:~$ sudo fdisk -l

Disk /dev/sda: 600.1 GB, 600093712384 bytes
255 heads, 63 sectors/track, 72957 cylinders, total 1172058032 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x000c8553

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *         512      292863      146176   83  Linux
/dev/sda2          292864    15918079     7812608   82  Linux swap / Solaris
/dev/sda3        15918080   211230719    97656320   83  Linux
/dev/sda4       211231230  1172057599   480413185    5  Extended
Partition 4 does not start on physical sector boundary.
/dev/sda5       211231232   406543359    97656064   83  Linux
/dev/sda6       406543872  1172057599   382756864   83  Linux

ximena@anneke:~$

Now I know that /dev/sda6 is the free space that will be used for my PV.

I can also check the structure of my HDD by running:

ximena@anneke:~$ sudo lsblk
NAME                           MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda                              8:0    0 558.9G  0 disk
├─sda1                           8:1    0 142.8M  0 part /boot
├─sda2                           8:2    0   7.5G  0 part [SWAP]
├─sda3                           8:3    0  93.1G  0 part /
├─sda4                           8:4    0     1K  0 part
├─sda5                           8:5    0  93.1G  0 part /var
└─sda6                           8:6    0   365G  0 part
ximena@anneke:~$


Which give us a more "friendly" output.

Now, it's time to create our PV

ximena@anneke:~$ sudo pvcreate /dev/sda6
Physical volume "/dev/sda6" successfully created
ximena@anneke:~$


As shown above one PV (physical Volume) is created: /dev/sda6.

If the physical volumes is already created, you can view it using the pvscan and pvdisplay command:

ximena@anneke:~$ sudo pvscan
  PV /dev/sda6   VG lb01   lvm2 [365.02 GiB / 350.02 GiB free]
  Total: 1 [365.02 GiB] / in use: 0 [0] / in no VG: 1 [365.02 GiB]
ximena@anneke:~$


You can view the list of PVs with attributes like Size, Physical Extent Size, Total Physical Extent Size, the Free Space, etc., using pvdisplay command:

ximena@anneke:~$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda6
  VG Name               lb01
  PV Size               365.03 GiB / not usable 2.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              93446
  Free PE               89606
  Allocated PE          3840
  PV UUID               Q0e3tN-McgT-FBqI-2MAh-BZjr-Rvoj-57FuG9
  
ximena@anneke:~$


Note: PE stands for: Physical Extents, and they are nothing but equal-sized chunks. The default size of extent is 4MB.

Creating a Volume Group


Volume groups are a pool of storage that consists of one or more physical volumes. Once you create the PV, you can create the Volume Group (VG).

ximena@anneke:~$ sudo vgcreate vg01 /dev/sda6
  Volume  group "vg01" successfully created
ximena@anneke:~$


LVM processes the storage in terms of extents. We can also change the extent size (from the default size 4MB) using -s flag.

The following command lists the created VG:

ximena@anneke:~$ sudo vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               365.02 GiB
  PE Size               4.00 MiB
  Total PE              93446
  Alloc PE / Size       0 / 0 GiB
  Free  PE / Size       93446 / 365.02 GiB
  VG UUID               5HwOD6-cqKP-2xvm-B90G-2RMq-0mfv-Pl3G8d
  
ximena@anneke:~$


Creating Logical Volumes


Now, everything is ready to create the logical volumes from the volume groups. lvcreate command creates the logical volume with the size of 80MB.

ximena@anneke:~$ sudo lvcreate -L10G -n lv01 vg01
  Logical volume "lv01" created

ximena@anneke:~$


Using the command lvdisplay we can view the available logical volumes with its attributes:

ximena@anneke:~$ sudo lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                QAtkyi-HoMx-s1t1-NEYB-2r0S-IjY0-HmE1tV
  LV Write Access        read/write
  LV Creation host, time anneke, 2013-08-08 14:57:48 +0200
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             2560
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0

ximena@anneke:~$


After that, we are able to create our Filesystem

ximena@anneke:~$ sudo mkfs.ext4 /dev/vg01/lv01
mke2fs 1.42.5 (29-Jul-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=64 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                           
Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

ximena@anneke:~$ 


Increasing the Size of the LVs


We can extend the size of the LVs by using lvextend utility. For examlpe:

ximena@anneke:~$ sudo lvextend -L15G /dev/vg01/lv01
  Extending logical volume lv01 to 15.00 GiB
  Logical volume lv01 successfully resized
ximena@anneke:~$


We can also add additional size to a specific LV as shown below.

ximena@anneke:~$ sudo lvextend -L+20G /dev/lb02/lv01
  Extending logical volume lv01 to 35.00 GiB
  Logical volume lv01 successfully resized
ximena@anneke:~$


Creating a Snapshot (Backup) LV


Sometimes to backup our own filesystems and/or LVs, we can use this great tool as Backup. It just simple as run this command:

ximena@anneke:~$ lvcreate -L5G -s -n lv01-SNAP vg01/lv01
  Logical volume "lv01-SNAP" created
ximena@anneke:~$


Where "-s" stands for "snapshot".

And now, our LVs will look like:

ximena@anneke:~$ lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg01/lv01
  LV Name                lv01
  VG Name                vg01
  LV UUID                QAtkyi-HoMx-s1t1-NEYB-2r0S-IjY0-HmE1tV
  LV Write Access        read/write
  LV Creation host, time anneke, 2013-08-08 14:57:48 +0200
  LV snapshot status     source of
                         lv01-SNAP [active]
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             8960
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:0
  
  --- Logical volume ---
  LV Path                /dev/vg01/lv01-SNAP
  LV Name                lv01-SNAP
  VG Name                vg01
  LV UUID                i1LP4O-xEeB-3UNd-eQf3-OMKK-L9ug-C5cEj5
  LV Write Access        read/write
  LV Creation host, time anneke, 2013-08-08 15:18:15 +0200
  LV snapshot status     active destination for lv01
  LV Status              available
  # open                 0
  LV Size                10.00 GiB
  Current LE             8960
  COW-table size         5.00 GiB
  COW-table LE           1280
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KiB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           254:1

ximena@anneke:~$


Hope this helps!