web123456

【Linux from basic to advanced】RAID and LVM configuration and management

RAID andLVMConfiguration and management

introduction

RAID(Redundant Arrayof Independent Disks and LVM (Logical Volume Management) areLinuxTwo commonly used storage management technologies in the system. RAID improves performance or data redundancy by combining multiple physical hard disks, while LVM provides flexible storage management methods, allowing system administrators to dynamically adjust disk space allocation. This article will introduce in detail how to configure and manage RAID and LVM in CentOS and Ubuntu systems, including RAID level selection, LVM creation and adjustment, and solutions to common problems.

1. RAID Overview

1.1 Introduction to RAID Level

RAID technology provides different performance, capacity and redundancy by combining multiple disks. Common RAID levels are:

  • RAID 0: Spread data across multiple disks, providing the highest performance but no redundancy.
  • RAID 1: Mirroring data on two disks provides data redundancy, but storage efficiency is 50%.
  • RAID 5: Stripe the data and store verification information on each disk to provide a balance of performance and redundancy.
  • RAID 6: Similar to RAID 5, but uses dual verification information, allowing two disks to be damaged simultaneously.
  • RAID 10: Combining RAID 0 with RAID 1 provides high performance and high redundancy.

1.2 Soft RAID vs Hard RAID

RAID can be passedhardware(hard RAID) or software (soft RAID) implementation. Soft RAID relies on the operating system for configuration and management, while hard RAID relies on dedicated RAID controllers. Soft RAID has the advantages of low cost and high flexibility, but its performance and reliability may be slightly inferior to hard RAID.

2. Configure RAID in Linux system

2.1 Installing the RAID management tool

existCentOSand Ubuntu system,mdadmTools are used to create and manage soft RAIDs. First, you need to install this tool:

# CentOS
sudo yum install -y mdadm

# Ubuntu
sudo apt install -y mdadm
  • 1
  • 2
  • 3
  • 4
  • 5

2.2 Creating a RAID array

The following example demonstrates how to create a RAID 1 array on two disks (/dev/sdb and /dev/sdc):

# Create a RAID 1 array
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

# After successful creation, you can view the array status
cat /proc/mdstat

# Save RAID configuration
sudo mdadm --detail --scan | sudo tee -a /etc/

# Update initramfs
sudo update-initramfs -u  # Ubuntu
sudo dracut -H -f /boot/initramfs-$(uname -r).img $(uname -r)  # CentOS
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.3 Format and Mount RAID Arrays

After creating a RAID array, it needs to be formatted and mounted:

# Format as ext4 file system
sudo mkfs.ext4 /dev/md0

# Create a mount point
sudo mkdir -p /mnt/raid1

# Mount RAID array
sudo mount /dev/md0 /mnt/raid1

# Mounting takes effect permanently
echo '/dev/md0 /mnt/raid1 ext4 defaults 0 0' | sudo tee -a /etc/fstab
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2.4 Monitoring RAID arrays

Regularly check the status of the RAID array to ensure it is functioning normally:

# Check RAID array status
sudo mdadm --detail /dev/md0
  • 1
  • 2

3. LVM Overview

3.1 Advantages of LVM

LVM provides a flexible disk management method that enables system administrators to dynamically adjust storage allocation without affecting system operation. The main LVMComponentsinclude:

  • Physical Volume (PV): A physical disk or partition contained in a volume group.
  • Volume Group (VG): A storage pool consisting of one or more physical volumes.
  • Logical Volume (LV): A logical storage unit allocated from a volume group that can be used to create a file system.

3.2 Common application scenarios of LVM

  • Dynamically resize partitions.
  • Snapshot function for creating consistent backups.
  • Manage multiple disks and provide flexible storage space.

4. Configure LVM in Linux system

4.1 Installing LVM Tools

By default, the LVM tool is installed in CentOS andUbuntuin the system. If not installed, you can install it through the following command:

# CentOS
sudo yum install -y lvm2

# Ubuntu
sudo apt install -y lvm2
  • 1
  • 2
  • 3
  • 4
  • 5

4.2 Creating a Physical Volume (PV)

usepvcreateCommand initializes a physical disk or partition to a physical volume:

# Initialize /dev/sdb and /dev/sdc as physical volumes
sudo pvcreate /dev/sdb /dev/sdc
  • 1
  • 2

4.3 Creating a Volume Group (VG)

usevgcreateCommand to create a volume group:

# Create a volume group named "vg_data"
sudo vgcreate vg_data /dev/sdb /dev/sdc
  • 1
  • 2

4.4 Creating a logical volume (LV)

uselvcreateThe command allocates space from the volume group to create a logical volume:

# Create a logical volume of 10G size "lv_data"
sudo lvcreate -L 10G -n lv_data vg_data
  • 1
  • 2

4.5 Format and mount logical volumes

After the logical volume is created, it needs to be formatted and mounted:

# Format logical volume to ext4 file system
sudo mkfs.ext4 /dev/vg_data/lv_data

# Create a mount point
sudo mkdir -p /mnt/lvm_data

# Mount logical volume
sudo mount /dev/vg_data/lv_data /mnt/lvm_data

# Mounting takes effect permanently
echo '/dev/vg_data/lv_data /mnt/lvm_data ext4 defaults 0 0' | sudo tee -a /etc/fstab
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

4.6 Dynamic adjustment of logical volumes

A significant advantage of LVM is that it can dynamically resize the logical volume. The following example demonstrates how to scale and shrink a logical volume:

4.6.1 Extended logical volumes
# Extend logical volumes to 15G
sudo lvextend -L +5G /dev/vg_data/lv_data

# Online extension ext4 file system
sudo resize2fs /dev/vg_data/lv_data
  • 1
  • 2
  • 3
  • 4
  • 5
4.6.2 Reducing the logical volume

Before shrinking the logical volume, you must shrink the file system:

# Zoom out ext4 file system to 8G
sudo resize2fs /dev/vg_data/lv_data 8G

# Reduce the logical volume to 8G
sudo lvreduce -L 8G /dev/vg_data/lv_data
  • 1
  • 2
  • 3
  • 4
  • 5

5. Use RAID and LVM in combination

RAID and LVM can be used in combination to achieve a balance of performance and flexibility. The following example shows how to create an LVM on a RAID array:

5.1 Creating a RAID array

First, create a RAID array, such asRAID 5

sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd
  • 1

5.2 Initialize the RAID array into a physical volume

sudo pvcreate /dev/md0
  • 1

5.3 Creating volume groups and logical volumes

sudo vgcreate vg_raid /dev/md0
sudo lvcreate -L 20G -n lv_raid vg_raid
  • 1
  • 2

5.4 Format and mount logical volumes

sudo mkfs.ext4 /dev/vg_raid/lv_raid
sudo mkdir -p /mnt/raid_lvm
sudo mount /dev/vg_raid/lv_raid /mnt/raid_lvm
  • 1
  • 2
  • 3

6. Troubleshooting FAQs for RAID and LVM

6.1 RAID failure recovery

A disk in the RAID array may fail and needs to be replaced and restored in time:

# Mark a failed disk
sudo mdadm --manage /dev/md0 --fail /dev/sdb

# Remove the failed disk
sudo mdadm --manage /dev/md0 --remove /dev/sdb

# Add a new disk and restore the array
sudo mdadm --manage /dev/md0 --add /dev/sdb
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

6.2 LVM snapshot management

LVM snapshots can be used to create instantaneous copies of the file system, making it easier to backup and restore data:

# Create a snapshot of 5G size
sudo lvcreate -L 5G -s -n lv_snapshot /dev/vg_data/lv_data

# Mount snapshot
sudo mount /dev/vg_data/lv_snapshot /mnt/snapshot
  • 1
  • 2
  • 3
  • 4
  • 5

Summarize

RAID and LVM are powerful storage management tools in Linux systems. Through reasonable configuration and management, the performance, reliability and flexibility of the storage system can be greatly improved. This article introduces in detail the basic concepts, configuration methods and common problems of RAID and LVM. Through these technologies, system administrators can better respond to complex storage needs and ensureData securityand efficient operation of the system. Whether in small systems or large enterprise environments, mastering the configuration and management skills of RAID and LVM will be of great benefit.