Monday 16 May 2011

LVM

Logical Volume Management

            Logical Volume Management (LVM) can allow to manage active partitions. Before LVM, we had no easy way to increase or reduce the size of a partition after Linux was installed. For example, if find that we have extra space on the /home directory partition and need more space on /var directory partition for log files, LVM will let reallocate the space. Alternatively, if we are managing a server on a growing network, new users will be common. May reach the point at which we need more room on our /home directory partition. With LVM, we can add a new physical disk and allocate its storage capacity to an existing /home directory partition.


          Install Red Hat Linux with LVM

            Boot with Red Hat Linux DVD. Click on “Create custom layout” to create partitions.
            1st create a “/boot” partition of 200 MB.
            2nd create LVM partition of 50 GB or as per requirement.
            Select newly created LVM (Physical Volume) > Create > Select LVM Volume Group > Create. Click on Add > Select Mount Point “/” and set its size approx 10 GB > Ok.
            Same as create -
            /home             5 GB
            /tmp                 5 GB
            /usr                 5 GB
            /var                  5 GB
            /usr/local        5 GB
            /opt                  5 GB
            Then click on ok.
            Create a Swap partition, Select its size to double of RAM space.
            Click on Ok > Click on Write changes on disk.
            Follow the instructions and click on next. The installation is starting.

         
Extend a Logical Volume:
1. Unmount the logical volume if it’s mounted.  (Note necessary)
# umount /home

2. Extend the logical volume with 50GB.
# lvextend -L +50G /dev/vg1/log2

3. Check the volume group.
#e2fsck -f /dev/vg1/log2

4. Resize the filesystem on the logical volume (ext3 in this case).
# resize2fs /dev/vg1/log2

            5. Mount the logical volume again.
# mount /dev/vg1/log2 /home                              (Note necessary)


            Reduce a Logical Volume: Make a backup of logical volume before reduction and to use around 5 GB of safe space to avoid data loss.

1. Umount the logical volume.
# umount /home

            2. Check the filesystem integrity.
# fsck -f -y -v /dev/vg1/log2

3. Resize the file system to something smaller than the final size (around 30GB in this case):
# resize2fs /dev/vg1/log2 30000M

4. Check the filesystem integrity again.
# fsck -f -y -v /dev/vg1/log2

5. Reduce the logical volume
# lvreduce -L 30G /dev/vg1/log2

6. Resize the filesystem to fit the logical volume.
# resize2fs /dev/vg1/log2

7. Check the filesystem to know whether the reduction went fine.
# fsck -f -y -v /dev/vg1/log2

8. Mount the logical volume.
# mount /dev/vg1/log2 /home

            9. Check the /home file system size:
# df -h | grep /home

            NOTE: In LVM we can increase the size of any partitions on live system without un-mounting the partition but we can't decrease all partitions on live system. Because for decreasing the size of any partition that is necessary to un-mount that partition and then decrease partition size otherwise data will be lost, and we can't un-mount these partitions /, /var, /usr, /tmp. So we can decrease only /home, /opt, /usr/local partitions. If we want to decrease the size of /, /var, /usr, /tmp partitions then boot system in Rescue mode with Linux DVD and do this.


            Create LVM Partitions after installing OS

            To create a new LVM system, we need to create a new PV (Physical Volume), using a command such as pvcreate, assign the space to a VG (Volume Group) with a command such as vgcreate, and allocate the space from some part of available VGs to an LV (Logical Volume) with a command such as lvcreate.

            Creating a Physical Volume: The first step in creating an LVM is to start with a physical disk. In that disk create to partition for example /dev/sda10 and /dev/sda11.After creating partition which id is 8e we can configure it like this.
# fdisk /dev/sda
            Command (m for help) : t
            Partition number (1-11): 10           
            Partition ID (L to list options): 8e
            Command (m for help) : w

            Save the partition table with “w” and run the command partprobe.
# partprobe

            Creating Physical Volume: Run these command to create Physical Volume.
# pvcreate /dev/sda10 /dev/sda11                      (This command creates the physical volume)
# pvs                                                              (It show the physical volumes)

            Creating a Volume Group: Once we have two or more PVs, we can create a volume group (VG) by  the following command, substitute the name of our choice for volume group name.
# vgcreate vg1 /dev/sdb10 /dev/sdb11
            Where vg1 is a volume group name. When Volume Group successfully create then use this command to see the volume group.
# vgs

            We can add more physical volume to any VG. Assume there's an existing /dev/sda12 partition, using a Linux LVM type, and the pvcreate command has been applied to that partition. We can then add that partition to an existing VG with the following command:
# vgextend vg1 /dev/sda12

            Creating a Logical Volume: However, a VG doesn't create and mount a filesystem on it. So we need to create a logical volume (LV) for this purpose. The following command creates an LV. We can add as many chunks of disk space as our need.
# lvcreate –L +250M –n /dev/vg1/log1
           
            The following command creates a device named log1 in /dev/vg1 of 250 MB size. We can format this device as if it were a regular disk partition, and then mount on a newly created directory. Using the “df -h” command to check the size of the volume after we've mounted on a directory on.
#lvs                (Show the logical volume)

            After creating a logical volume, format that partition by running this command.
# mke2fs –j /dev/vg1/log1

            Then mount that partition and add its entry in fstab file like this.
            dev/vg1/log1 /nilesh ext3 defaults 0 0

            Removing a Logical Volume: Removing an existing LV requires a straightforward command. The basic command is lvremove. If you've created an LV in the previous section and want to remove it, the basic steps are simple. However, it will work only from a rescue environment such as the linux rescue mode from a CD/DVD-based system. Save any data in directories that are mounted on the LV.
1. Unmount any directories associated with the LV. Based on the example in the previous section; you would use the following command:
# umount /dev/vg1/log1
            2. Apply the lvremove command to the LV with a command such as:
# lvremove /dev/vg1/log1
            You should now have the PV from this LV free for use in other LV.

            Resize logical volume and their filesystem.
            1. Unmount your mounted lvm partition.
            2. Run the command
# lvextend –L2G /dev/iiht/log1     (It will extend lvm partition to 2 Gb)
            3. Then run the command
# e2fsck –f /dev/vg1/log1              (It will check ext3 partition)
            4. Now run the command
# resize2fs /dev/iiht/log1               (This will resize lvm partition)

            Then mount it again and run the command “df –h” to see it is extended or not.
# df -h

            Remove Physical Volume:
# pvremove /dev/sda11



Commands         Description
partprobe             To notify kernel about the change in device
pvcreate               To create physical volume
pvdisplay              To display the size and other information about physical volume
pvremove             To remove the physical volume
pvresize               To modify the size of physical volume
pvscan                 To scan the physical volume
vgextend              To extend the size of volume group
vgreduce             To reduce the size of volume group
vgscan                 To scan the volume group for change
vgdisplay             To display the status and information about volume group
lvdisplay               To display the status of lvm
lvreduce               To reduce the size of lvm
lvextend               To extend the size of lvm
lvcreate                To create the lvm
lvremove              To remove the lvm partition
lvscan                  To scan lvm for change


1 comment:

  1. Hello all,

    I'm practicing with RAID on LVM (don't wand to use MDADM) on a virtual machine.

    I've created 18x5GB disks and I use the below script to create PV and VG (at the very bottom of this post) and VG is created correctly.

    Then I issue the command (that I belive correct) to create the RAID10 LV but i get the following error:

    sudo lvcreate --type raid10 -i 18 -m 1 -l 100%FREE --maxrecoveryrate 128 -n lv_RAID10 vg-for-raid

    Using default stripesize 64.00 KiB.

    Rounding size 44.96 GiB (11511 extents) up to stripe boundary size 45.00 GiB(11520 extents).

    Insufficient suitable allocatable extents found for logical volume lv_RAID10.

    Please tell me what's wrong with lvcreate command above because it's driving me mad... no problem with --type raid5 neither with --type raid6

    Thank you

    vgdisplay command show this:

    --- Volume group ---

    VG Name vg-for-raid

    System ID

    Format lvm2

    Metadata Areas 18

    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 18

    Act PV 18

    VG Size <89.93 GiB

    PE Size 4.00 MiB

    Total PE 23022

    Alloc PE / Size 0 / 0

    Free PE / Size 23022 / <89.93 GiB

    VG UUID YNtXrx-uaGR-qnsf-fmJA-Lq9h-7Ur1-mtpCi0

    ----------------------------------------------------------------------

    #!/bin/bash

    # Basic for loop

    disks='b c d e f g h i j k l m n o p q r s' # Available Physical Disk

    for disks in $disks

    do

    pvcreate -M2 --verbose -ff /dev/sd$disks

    done

    vgcreate --dataalignment 1152K --physicalextentsize 4096K -M2 --verbose vg-for-raid /dev/sdb /dev/sdc /dev/sdd /dev/sde /dev/sdf /dev/sdg /dev/sdh /dev/sdk /dev/sdi /dev/sdj /dev/sdl /dev/sdm /dev/sdn /dev/sdo /dev/sdp /dev/sdq /dev/sdr /dev/sds

    echo All done

    ---------------------------------------------------------------------

    ReplyDelete

Boot to UEFI Mode or legacy BIOS mode

Boot to UEFI Mode or legacy BIOS mode Choose UEFI or legacy BIOS modes while installing Windows. After Windows is installed, if you nee...