Learn Linux Series (#11) - Mechanism for disk space management LVM

in utopian-io •  7 years ago  (edited)

Learn Linux Series (#11) - Mechanism for disk space management LVM

What Will I Learn?

  • What is LVM
  • How does LVM work?
  • How to use LVM commands
  • Zooming LVM
  • Decreasing LVM
  • Disks in Linux systems
  • Specific formatting - mkfs

Requirements

  • Linux system installed
  • Basic knowledge of terminal operation
  • Average knowledge of linux commands
  • Motivation when something goes wrong

Difficulty

  • Intermediate



Learn Linux Series (#11) - Mechanism for disk space management LVM



  • What is LVM

    LVM (Logical Volume Manager) it is a very flexible mechanism for managing disk space.
    Thanks to this tool, we can fly - during the operation of a service / system, eg a web server, increase the disk space per page. Other users will not be able to "feel" anything. You will not have to shut down the system or interrupt the operation of the service.



    LVM can:

    change on-line ("hot") volume group size (VG) by absorbing or removing physical volumes (PV),

    change on-line size of logical volumes (LV), by attaching or shortening, so-called areas (logical extent),

    create copies (logical snapshots) of logical volumes,

    connect together (stripe) whole or fragments of logical volumes coming from different physical volumes, similarly as in RAID 0,

    create mirroring of logical volumes on a few PV (physical volume), similar to what happens in RAID 1,

    to move logical volumes on-line between physical volumes,

    to divide or combine volume groups (VG).



    It can not:

act like RAID 5; for this reason, it is recommended to create LVM volumes on disks previously piled in RAID or disk array.



  • How does LVM work?

    I found a picture that should make everyone aware of the LVM principle in a very simple way, which is why I will not describe the action and I will make it available to you.




  • How to use LVM commands

    I will present to you in my opinion the most useful commands, including a few different ones that you will not find on the internet.

    Available partitions/or independent disks eg /dev/sda /dev/sdb
/dev/sda1 /dev/sda2 /dev/sda3

Initializing physical partitions for using LVM:

pvcreate /dev/sda1
pvcreate /dev/sda2
pvcreate /dev/sda3

Displaying physical attributes of volumes:

pvdispaly & psv

Creating a volume group:

vgcreate vdysk /dev/sda1 /dev/sda2 /dev/sda3

or

(where PE - Physical Extents will be 32M, default 4M)

vgcreate -s 32 vdysk /dev/sda1 /dev/sda2 /dev/sda3

Displaying the attributes of volume groups:

vgdisplay & vgs

Creating a logical volume in a volume group:

lvcreate -L 32M -n part1 vdysk

Creating a peak system on a logical volume:

mkfs.ext4 /dev/vdysk/part1

Displaying logical volume attributes:

lvdisplay & lvs

Catalog creation and mounting:

mkdir /lvm
mount /dev/vdysk/part1 /lvm

or

mount /dev/mapper/vdysk-part1



  • Zooming LVM

    Displaying the attributes of volume groups:
vgs

Expansion of logical volumes by 12M:

lvextend -L +12M /dev/vdysk/part1

or

Expansion of logical volumes by 4 PE units:

lvextend -l +4 /dev/vdysk/part1

or 50%

lvextend -l +50%FREE /dev/vdysk/part1

Then to expand our logical disk you should:

Expanding filesystems:

resize2fs /dev/vdysk/part1

Checking the partition size:

df -h



  • Decreasing LVM

    Unmounting a logical volume mounted in the lvm directory:
umount /lvm

Reducing the file system size to 32M

resize2fs /dev/vdysk/part1 32M

Reduce the partition size up to 32M

lvreduce -L 32 /dev/vdysk/part1
mount /dev/vdysk/part1 /lvm

Adding an additional partition / space to the group volume

pvcreate /dev/sda4
vgextend vdysk /dev/sda4

Reducing the size of partitions

pvresize /dev/sda2 --setphysicalvolumesize 50G

Disconnecting the disk from the group
pvmove - Moves data to other partitions available in the volum group

pvmove /dev/sda1

Unlinking /dev/sda1 from the vdysk group:

vgreduce vdysk /dev/sda1

Remove the LVM designation from the physical partition

pvremove /dev/sda1

Snapshot

lvcreate -L 12M -n snapszot -s /dev/vdysk/part1
  • Disks in Linux systems



    In this section I will quickly remind you to use the linux disk utility.


    Start the console as an administrator and check what disks and partitions are currently connected by issuing the command:
fdisk -l

Unmount the partition you want to modify with the command:

umount /dev/partition_number

Then use "fdisk" to access this partition, for example:

fdisk /dev/sdb

Now you can make changes to the selected partition using the command:

  • "d" delete the partition

  • "n" create a new one

  • select the partition type: "p" - main or "e" - extended

  • enter the number of the first and last cylinder

  • select the system type of files: "l" will display a list of partition codes

  • save changes in "in"

  • you can always view help "m".



    lvm.png

  • Formatting mkfs


    If you want to format the partition in text mode, you can do it with the "mkfs" program by issuing the command:

mkfs.type_of_file_system /dev/partition_number

You can choose the type of file system at your own discretion, eg ext3, ext4, reiserfs, xfs, vfat (fat32), ntfs, jfs, if you format the hard disk partition, select "vfat" for the USB stick by issuing the command:

mkfs.vfat /dev/sdb1





Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

This tutorial contains only set of commands without more details and proper teaching that is crucial for quality tutorial.

Used images are not cited and not described at all.

You can contact us on Discord.
[utopian-moderator]