How to create PV/VG/LV → pvcreate /dev/sdX; vgcreate vg0 /dev/sdX; lvcreate -L 20G -n lv0 vg0

How to Create Physical Volumes, Volume Groups, and Logical Volumes in Linux LVM Table of Contents 1. [Introduction](#introduction) 2. [Prerequisites](#prerequisites) 3. [Understanding LVM Components](#understanding-lvm-components) 4. [Step-by-Step Implementation](#step-by-step-implementation) 5. [Practical Examples](#practical-examples) 6. [Advanced Configuration Options](#advanced-configuration-options) 7. [Troubleshooting Common Issues](#troubleshooting-common-issues) 8. [Best Practices](#best-practices) 9. [Monitoring and Maintenance](#monitoring-and-maintenance) 10. [Conclusion](#conclusion) Introduction Logical Volume Management (LVM) is a powerful disk management system in Linux that provides flexibility in managing storage space. Unlike traditional partitioning, LVM allows you to create, resize, and manage storage volumes dynamically without the constraints of fixed partition boundaries. This comprehensive guide will walk you through the process of creating Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV) using the fundamental LVM commands: `pvcreate`, `vgcreate`, and `lvcreate`. By the end of this tutorial, you'll have a thorough understanding of how to implement LVM from scratch, manage storage efficiently, and troubleshoot common issues that may arise during the process. Prerequisites Before diving into LVM creation, ensure you have the following requirements met: System Requirements - Linux operating system with LVM2 support (most modern distributions include this) - Root or sudo privileges - At least one available disk or partition - Basic understanding of Linux command line Required Packages Most Linux distributions come with LVM tools pre-installed. If not, install them using: Ubuntu/Debian: ```bash sudo apt update sudo apt install lvm2 ``` CentOS/RHEL/Fedora: ```bash sudo yum install lvm2 or for newer versions sudo dnf install lvm2 ``` Safety Precautions - Backup important data before proceeding - Verify disk devices using `lsblk` or `fdisk -l` - Ensure the target disk doesn't contain critical data - Test procedures in a virtual environment first Understanding LVM Components Physical Volumes (PV) Physical Volumes are the foundation of LVM. They represent actual storage devices (hard drives, partitions, or even files) that LVM can use. Think of PVs as the raw building blocks that provide storage space to the LVM system. Key characteristics: - Can be entire disks or partitions - Must be initialized with `pvcreate` before use - Contain metadata about the volume group they belong to Volume Groups (VG) Volume Groups act as storage pools that combine one or more Physical Volumes. They provide a unified storage space that can be divided into Logical Volumes. VGs abstract the underlying physical storage and present it as a single, manageable unit. Key characteristics: - Combine multiple PVs into a single storage pool - Can span multiple physical devices - Provide the space from which Logical Volumes are created Logical Volumes (LV) Logical Volumes are the equivalent of traditional partitions but with enhanced flexibility. They're created from the available space in Volume Groups and can be resized, moved, and managed independently of the underlying physical storage. Key characteristics: - Function like traditional partitions - Can be resized dynamically - Support various filesystem types - Can be created, deleted, and modified without affecting other LVs Step-by-Step Implementation Step 1: Identify Available Storage Before creating LVM components, identify available storage devices: ```bash List all block devices lsblk Alternative method using fdisk sudo fdisk -l Check for existing LVM components sudo pvs sudo vgs sudo lvs ``` Example output: ``` NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part / sdb 8:16 0 10G 0 disk sdc 8:32 0 15G 0 disk ``` Step 2: Create Physical Volume (pvcreate) The `pvcreate` command initializes a disk or partition for use with LVM: ```bash Basic syntax sudo pvcreate /dev/sdX Example: Initialize /dev/sdb sudo pvcreate /dev/sdb Initialize multiple devices at once sudo pvcreate /dev/sdb /dev/sdc Verify PV creation sudo pvs sudo pvdisplay ``` Important Options: - `--force`: Override safety checks (use with caution) - `--yes`: Automatically answer "yes" to prompts - `--verbose`: Provide detailed output Example with options: ```bash sudo pvcreate --verbose /dev/sdb ``` Expected output: ``` Wiping signatures on /dev/sdb Set up physical volume for "/dev/sdb" with 10737418240 bytes Physical volume "/dev/sdb" successfully created. ``` Step 3: Create Volume Group (vgcreate) The `vgcreate` command creates a Volume Group from one or more Physical Volumes: ```bash Basic syntax sudo vgcreate vg_name /dev/sdX Example: Create volume group 'vg0' using /dev/sdb sudo vgcreate vg0 /dev/sdb Create VG with multiple PVs sudo vgcreate vg0 /dev/sdb /dev/sdc Verify VG creation sudo vgs sudo vgdisplay vg0 ``` Important Options: - `-s`: Set physical extent size (default 4MB) - `--clustered`: Create clustered volume group - `--shared`: Create shared volume group Example with custom extent size: ```bash sudo vgcreate -s 8M vg0 /dev/sdb ``` Expected output: ``` Volume group "vg0" successfully created ``` Step 4: Create Logical Volume (lvcreate) The `lvcreate` command creates Logical Volumes within a Volume Group: ```bash Basic syntax with size specification sudo lvcreate -L size -n lv_name vg_name Example: Create 20GB logical volume named 'lv0' in 'vg0' sudo lvcreate -L 20G -n lv0 vg0 Alternative: Use percentage of VG sudo lvcreate -l 100%FREE -n lv0 vg0 Verify LV creation sudo lvs sudo lvdisplay /dev/vg0/lv0 ``` Size Specification Options: - `-L`: Specify size in bytes, KB, MB, GB, TB - `-l`: Specify size in logical extents or percentages - `100%FREE`: Use all available space - `50%VG`: Use 50% of volume group space Common lvcreate Options: - `-n`: Name of the logical volume - `-L`: Size in human-readable format - `-l`: Size in logical extents - `--type`: Specify LV type (linear, striped, mirror, etc.) Example creating multiple logical volumes: ```bash Create multiple LVs in the same VG sudo lvcreate -L 10G -n lv_data vg0 sudo lvcreate -L 5G -n lv_logs vg0 sudo lvcreate -l 100%FREE -n lv_backup vg0 ``` Step 5: Create Filesystem and Mount After creating the Logical Volume, create a filesystem and mount it: ```bash Create ext4 filesystem sudo mkfs.ext4 /dev/vg0/lv0 Create mount point sudo mkdir /mnt/lv0 Mount the logical volume sudo mount /dev/vg0/lv0 /mnt/lv0 Verify mount df -h /mnt/lv0 ``` For permanent mounting, add to `/etc/fstab`: ```bash echo '/dev/vg0/lv0 /mnt/lv0 ext4 defaults 0 2' | sudo tee -a /etc/fstab ``` Practical Examples Example 1: Single Disk LVM Setup Complete setup using a single 20GB disk: ```bash Step 1: Initialize physical volume sudo pvcreate /dev/sdb Step 2: Create volume group sudo vgcreate data_vg /dev/sdb Step 3: Create logical volumes sudo lvcreate -L 8G -n root_lv data_vg sudo lvcreate -L 4G -n home_lv data_vg sudo lvcreate -L 2G -n var_lv data_vg sudo lvcreate -l 100%FREE -n tmp_lv data_vg Step 4: Create filesystems sudo mkfs.ext4 /dev/data_vg/root_lv sudo mkfs.ext4 /dev/data_vg/home_lv sudo mkfs.ext4 /dev/data_vg/var_lv sudo mkfs.ext4 /dev/data_vg/tmp_lv Step 5: Create mount points and mount sudo mkdir -p /mnt/{root,home,var,tmp} sudo mount /dev/data_vg/root_lv /mnt/root sudo mount /dev/data_vg/home_lv /mnt/home sudo mount /dev/data_vg/var_lv /mnt/var sudo mount /dev/data_vg/tmp_lv /mnt/tmp ``` Example 2: Multi-Disk LVM Setup Setup using multiple disks for redundancy and performance: ```bash Initialize multiple physical volumes sudo pvcreate /dev/sdb /dev/sdc /dev/sdd Create volume group with all disks sudo vgcreate storage_vg /dev/sdb /dev/sdc /dev/sdd Create striped logical volume for better performance sudo lvcreate -L 30G -i 3 -I 64 -n striped_lv storage_vg Create standard logical volume sudo lvcreate -L 20G -n standard_lv storage_vg Display configuration sudo vgdisplay storage_vg sudo lvdisplay storage_vg ``` Example 3: Database Server Setup LVM configuration optimized for database workloads: ```bash Initialize physical volumes sudo pvcreate /dev/sdb /dev/sdc Create volume group sudo vgcreate db_vg /dev/sdb /dev/sdc Create logical volumes for database components sudo lvcreate -L 20G -n db_data_lv db_vg # Database files sudo lvcreate -L 10G -n db_logs_lv db_vg # Transaction logs sudo lvcreate -L 5G -n db_temp_lv db_vg # Temporary files sudo lvcreate -L 15G -n db_backup_lv db_vg # Backup storage Create appropriate filesystems sudo mkfs.ext4 -b 4096 /dev/db_vg/db_data_lv sudo mkfs.ext4 /dev/db_vg/db_logs_lv sudo mkfs.ext4 /dev/db_vg/db_temp_lv sudo mkfs.ext4 /dev/db_vg/db_backup_lv ``` Advanced Configuration Options Physical Extent Size Optimization Choose appropriate PE size based on volume group size: ```bash For large volume groups (>1TB), use larger PE size sudo vgcreate -s 32M large_vg /dev/sdb For small volume groups (<100GB), default 4M is fine sudo vgcreate small_vg /dev/sdc ``` Striped Logical Volumes Improve performance by striping across multiple PVs: ```bash Create striped LV across 2 disks with 64KB stripe size sudo lvcreate -L 20G -i 2 -I 64 -n striped_lv vg0 Stripe across all available PVs sudo lvcreate -L 20G -i $(sudo vgdisplay vg0 | grep "Cur PV" | awk '{print $3}') -n auto_striped_lv vg0 ``` Mirrored Logical Volumes Create redundant storage with mirroring: ```bash Create mirrored LV with 1 copy (2 total copies) sudo lvcreate -L 10G -m 1 -n mirrored_lv vg0 Create mirrored LV with specific log device sudo lvcreate -L 10G -m 1 --mirrorlog disk -n safe_lv vg0 ``` Snapshot Volumes Create point-in-time snapshots: ```bash Create snapshot of existing LV sudo lvcreate -L 2G -s -n lv0_snapshot /dev/vg0/lv0 Create snapshot using percentage sudo lvcreate -l 20%ORIGIN -s -n lv0_snap /dev/vg0/lv0 ``` Troubleshooting Common Issues Issue 1: "Device or resource busy" Error Symptoms: ``` Device /dev/sdb excluded by a filter. ``` Solutions: ```bash Check if device is mounted mount | grep sdb Check for existing filesystem signatures sudo wipefs -a /dev/sdb Force PV creation (use with caution) sudo pvcreate --force /dev/sdb ``` Issue 2: Volume Group Not Found Symptoms: ``` Volume group "vg0" not found ``` Solutions: ```bash Scan for volume groups sudo vgscan Activate volume groups sudo vgchange -ay Check if VG is exported sudo vgimport vg0 ``` Issue 3: Insufficient Space Symptoms: ``` Insufficient free space: 5120 extents needed, but only 4096 available ``` Solutions: ```bash Check available space sudo vgdisplay vg0 Add more physical volumes sudo pvcreate /dev/sdc sudo vgextend vg0 /dev/sdc Or reduce LV size sudo lvcreate -L 16G -n lv0 vg0 # Instead of 20G ``` Issue 4: LVM Commands Hanging Symptoms: Commands like `pvs`, `vgs`, `lvs` hang indefinitely. Solutions: ```bash Kill hanging processes sudo pkill -f lvm Clear LVM cache sudo rm -rf /etc/lvm/cache/.cache Rebuild cache sudo vgscan --cache ``` Issue 5: Metadata Corruption Symptoms: ``` Couldn't find device with uuid ``` Solutions: ```bash Backup current metadata sudo vgcfgbackup vg0 Restore from backup sudo vgcfgrestore vg0 Manual recovery (last resort) sudo vgreduce --removemissing vg0 ``` Best Practices Planning and Design 1. Capacity Planning: - Plan for future growth - Leave 10-20% free space in volume groups - Consider snapshot space requirements 2. Naming Conventions: - Use descriptive names (data_vg, app_lv) - Include purpose or application in names - Maintain consistency across environments 3. Physical Volume Layout: - Use entire disks when possible - Avoid mixing PV types (SSD/HDD) in same VG - Consider RAID underneath LVM for redundancy Security Considerations 1. Access Control: ```bash # Set appropriate permissions sudo chmod 640 /dev/vg0/lv0 sudo chown root:disk /dev/vg0/lv0 ``` 2. Encryption: ```bash # Create encrypted LV using LUKS sudo cryptsetup luksFormat /dev/vg0/lv0 sudo cryptsetup luksOpen /dev/vg0/lv0 encrypted_lv ``` Performance Optimization 1. Stripe Configuration: - Match stripe size to application I/O patterns - Use power-of-2 stripe sizes (64K, 128K, 256K) - Stripe across fast devices 2. Extent Size: - Use larger extents for large volume groups - Consider I/O alignment requirements 3. Monitoring: ```bash # Monitor LVM performance sudo iostat -x 1 sudo iotop # Check LVM statistics sudo dmsetup status sudo dmsetup table ``` Backup and Recovery 1. Metadata Backup: ```bash # Automatic backup (enabled by default) # Manual backup sudo vgcfgbackup vg0 # List available backups ls -la /etc/lvm/backup/ ``` 2. Snapshot-based Backup: ```bash # Create snapshot sudo lvcreate -L 2G -s -n backup_snap /dev/vg0/lv0 # Mount and backup sudo mkdir /mnt/backup_snap sudo mount /dev/vg0/backup_snap /mnt/backup_snap # Perform backup operations # Remove snapshot when done sudo umount /mnt/backup_snap sudo lvremove /dev/vg0/backup_snap ``` Documentation 1. Maintain Records: - Document LVM layout and purpose - Keep track of size allocations - Record configuration changes 2. Automation Scripts: ```bash #!/bin/bash # LVM status report script echo "=== Physical Volumes ===" sudo pvs echo "=== Volume Groups ===" sudo vgs echo "=== Logical Volumes ===" sudo lvs echo "=== Disk Usage ===" df -h | grep "/dev/mapper/" ``` Monitoring and Maintenance Regular Monitoring Tasks 1. Space Monitoring: ```bash # Check space usage sudo vgs sudo lvs df -h # Set up automated alerts # Add to crontab 0 /6 /usr/local/bin/lvm-space-check.sh ``` 2. Health Checks: ```bash # Verify LVM consistency sudo vgck vg0 sudo fsck /dev/vg0/lv0 # Check for errors in logs sudo journalctl -u lvm2-monitor ``` Maintenance Operations 1. Extending Logical Volumes: ```bash # Extend LV and filesystem sudo lvextend -L +5G /dev/vg0/lv0 sudo resize2fs /dev/vg0/lv0 ``` 2. Adding Storage: ```bash # Add new PV to existing VG sudo pvcreate /dev/sdd sudo vgextend vg0 /dev/sdd ``` 3. Cleanup Operations: ```bash # Remove unused snapshots sudo lvremove /dev/vg0/old_snapshot # Remove empty logical volumes sudo lvremove /dev/vg0/unused_lv ``` Conclusion Creating and managing LVM components using `pvcreate`, `vgcreate`, and `lvcreate` provides tremendous flexibility in Linux storage management. This comprehensive guide has covered the essential steps from initial setup through advanced configuration and troubleshooting. Key takeaways from this tutorial: 1. Foundation Understanding: LVM consists of three main components - Physical Volumes (PV), Volume Groups (VG), and Logical Volumes (LV) - each serving a specific purpose in the storage hierarchy. 2. Implementation Process: The basic workflow follows a logical sequence: initialize physical storage with `pvcreate`, group physical volumes with `vgcreate`, and create usable volumes with `lvcreate`. 3. Flexibility Benefits: LVM provides dynamic resizing, snapshot capabilities, and advanced features like striping and mirroring that traditional partitioning cannot match. 4. Best Practices: Proper planning, consistent naming conventions, regular monitoring, and maintaining backups are crucial for successful LVM deployment. 5. Troubleshooting Skills: Understanding common issues and their solutions helps maintain reliable storage systems. Next Steps After mastering basic LVM creation, consider exploring these advanced topics: - LVM Resizing: Learn to dynamically grow and shrink logical volumes - LVM Migration: Move data between physical volumes - Advanced Features: Explore thin provisioning, caching, and RAID integration - Automation: Develop scripts for automated LVM management - Integration: Combine LVM with containers, virtualization, and cloud storage Additional Resources For continued learning and reference: - Manual Pages: `man lvm`, `man pvcreate`, `man vgcreate`, `man lvcreate` - Red Hat Documentation: Comprehensive LVM administration guides - Ubuntu Documentation: LVM setup and management tutorials - Community Forums: Linux storage and LVM discussion groups Remember that LVM is a powerful tool that requires careful planning and understanding. Always test procedures in non-production environments before implementing them on critical systems. With proper implementation and maintenance, LVM provides a robust, flexible storage management solution that can adapt to changing requirements over time. The commands `pvcreate /dev/sdX; vgcreate vg0 /dev/sdX; lvcreate -L 20G -n lv0 vg0` represent just the beginning of what's possible with Linux LVM. Master these fundamentals, and you'll have a solid foundation for advanced storage management in enterprise Linux environments.