Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Managing LVM Logical Volumes and Snapshots on Linux

Tech Jul 26 2

Understanding and Managing LVM Components

LVM (Logical Volume Manager) introduces an abstraction layer over physical storage, enabling flexible volume management.

  • Physical Volume (PV): A block device—such as a disk partition or RAID array—formatted for use by LVM.
  • Volume Group (VG): A pool of storage created by combining one or more PVs. The VG is subdivided into fixed-size units called Physical Extents (PEs).
  • Logical Volume (LV): A virtual partition carved from the VG’s PEs. LVs can be formattted with a filesystem and mounted like regular partitions. Their size can be resized dynamically without data loss, provided sufficient space exists in the VG.
  • Physical Extent (PE): The smallest allocatable unit within a VG. All LVs are composed of PEs.

Key Management Commands

Physical Volume Tools

  • pvs: Brief PV summary
  • pvdisplay: Detailed PV info
  • pvcreate /dev/device: Initialize a device as a PV

Volume Group Tools

  • vgs: Brief VG summary
  • vgdisplay: Detailed VG info
  • vgcreate [-s PE_size] vg_name /dev/device...: Create a VG (default PE = 4 MiB)
  • vgextend vg_name /dev/device: Add a PV to a VG
  • pvmove /dev/device_to_remove: Migrate data off a PV before removal
  • vgreduce vg_name /dev/device: Remove a PV from a VG
  • vgremove vg_name: Delete a VG

Logical Volume Tools

  • lvs: Brief LV summary
  • lvdisplay: Detailed LV info (path: /dev/vg_name/lv_name)
  • lvcreate -L size -n lv_name vg_name: Create an LV
  • lvremove /dev/vg_name/lv_name: Delete an LV

Resizing Logical Volumes

Extending an LV (ext4 example):

umount /dev/vg_name/lv_name
lvextend -L +1G /dev/vg_name/lv_name
resize2fs /dev/vg_name/lv_name
mount /dev/vg_name/lv_name /mount/point

Shrinking an LV (ext4 only; insure data fits in new size):

umount /dev/vg_name/lv_name
e2fsck -f /dev/vg_name/lv_name
resize2fs /dev/vg_name/lv_name 2G
lvreduce -L 2G /dev/vg_name/lv_name
mount /dev/vg_name/lv_name /mount/point

Note: resize2fs works only with ext2/3/4. For XFS, use xfs_growfs; XFS cannot be shrunk.

Practical Example

Assume three 5 GiB partitions (/dev/sda5, /dev/sda6, /dev/sda7) with type 8e (Linux LVM):

  1. Initialize PVs:

    pvcreate /dev/sda5 /dev/sda6 /dev/sda7
    
  2. Create a VG named storage_vg with 8 MiB PEs:

    vgcreate -s 8M storage_vg /dev/sda5 /dev/sda6
    
  3. Add another PV later:

    vgextend storage_vg /dev/sda7
    
  4. Create a 2 GiB LV named data_lv:

    lvcreate -L 2G -n data_lv storage_vg
    
  5. Format and mount:

    mkfs.ext4 /dev/storage_vg/data_lv
    mkdir -p /mnt/data
    mount /dev/storage_vg/data_lv /mnt/data
    
  6. Enable automatic mounting at boot: Add to /etc/fstab:

    /dev/storage_vg/data_lv  /mnt/data  ext4  defaults  0 0
    

Using dd for Low-Level Operations

The dd command copies and converts files at the byte level.

Common uses:

  • Disk cloning: dd if=/dev/sda of=/dev/sdb
  • Backup MBR: dd if=/dev/sda of=mbr.img bs=512 count=1
  • Wipe bootloader: dd if=/dev/zero of=/dev/sda bs=446 count=1

Special devices:

  • /dev/zero: Produces a continuous stream of null bytes.
  • /dev/null: Discards all data written to it.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.