Linux_day05_01_LVM logical volume

LVM logical volume

LVM logical volume management: A layer of abstraction on the physical device allows the generation of logical storage volumes, which is more flexible than physical storage management.
LVM virtualizes storage, is not limited to physical storage, shields hardware-related storage parameters, and does not need to unload the file system to adjust volume size or data migration.

advantage:

  • Flexible container
  • Scalable storage pool
  • Online data redistribution
  • Convenient device commands
  • Disk striping
  • Image volume
  • Volume snapshot

Basic terms:

  • Physical storage media (PhysicalStorageMedia): the physical storage devices of the system, such as /dev/hda, /dev/sda, etc., are the storage units at the bottom of the storage system.
  • Physical Volume: PV, refers to a disk partition or a device that has the same function as a disk partition (such as RAID). It is the basic storage logical block of LVM, but it is compatible with basic physical storage media (such as partitions, disks, etc.). ) Comparison, but contains management parameters related to LVM.
  • Volume Group: VG, a storage pool composed of one or more physical volumes, on which one or more logical volumes can be created.
  • Logical Volume (Logical Volume): LV, similar to hard disk partitions in non-LVM systems, is built on a volume group and is a standard block device. A file system can be built on logical volumes.
1. PV management tools
  • Basic command
    • pvs: Briefly display physical volume information
    • pvdisplay: Detailed display of physical volume information
    • pvcreate /dev/sda: Create physical volume
  • Other commands
    • pvremove: means to erase the data
    • pvmove: Move PV containing data to other physical PV
2. VG Management Tool
  • Basic command
    • vgs: Briefly display volume group information
    • vgdisplay: Display volume group information in detail
    • vgcreate (-s PE size) volume group name physical volume path: create a volume group
  • Other commands
    • vgextend volume group name physical volume: extended volume group
    • vgremove: erase data
    • vgreduce: Reduce the volume group, which is actually the process of removing PV, and vgremove first
3. LV management tools
  • Basic command
    • lvs: Briefly display logical volume information
    • lvdisplay: Detailed display of logical volume information
    • lvcreate -L size[nMgGtT] -n name volume group path: create logical volume
  • Other commands
    • lvremove logical volume path: remove logical volume
  • Extend logical volume
    1. vgs: view volume group status and remaining space
    2. lvextend -L +size[mMgGtT] (-r) logical volume (the use of -r parameter does not require step 3)
    3. resize2fs logical volume path: reload the logical volume size
  • Reduce logical volume
    1. umount logical volume path: unmount the logical volume
    2. e2fsck -f logical volume path: detect file system
    3. resize2fs logical volume path size[mMgGtT]: shrink the file system
    4. lvreduce -L -size[mMgGtT] logical volume path: shrink the logical volume
    5. mount
4. Snapshot and Restore
  1. lvcreate -s -L size[mMgGtT] -n snapshot name logical volume path: create a snapshot (-s specifies to create a snapshot) (xfs system)

lvcreate -s -L size[mMgGtT] -pr -n snapshot name logical volume path: create a snapshot (ext system)

  1. umount logical volume path: unmount source
  2. lvconvert --merge snapshot file path: restore snapshot
  3. mount Logical volume path Original mount path: remount source
  • The snapshot volume is valid only once and is automatically deleted after the restore.
  • If the snapshot logical volume becomes full, it will be discarded and become unusable.
  • Two files, one is created before the snapshot is created, and the other is created after the snapshot is created. If the restore snapshot function is executed successfully, the file created after the snapshot is created will not exist.

Case 1
creates a 4G VG named testvg consisting of two PVs; requires a PE size of 16MB, and then creates a 3G logical volume testlv in the volume group; mounts it to the /users directory.

  • Step 1: Prepare two partitions with a size of 2G, and use the lsblk command to view
lsblk
  • Step 2: Create a physical volume
pvcreate /dev/sdb{
    
    1,2}
  • Step 3: Create testvg volume group, specify PE as 16MB
vgcreate -s 16M testvg /dev/sdb{
    
    1,2}
  • Step 4: Create a logical volume
lvcreate -L 3G -n testlv testvg
  • Step 5: Format the logical volume and create a mount point
mkfs.ext4 /dev/testvg/testlv
mkdir /users
  • Step 6: Use blkid to view UUID and write to /etc/fstab
blkid
vim /etc/fstabs
  • Step 7: Mount and view
mount -a
df -Th

Case 2
Create a new user archlinux, require its home directory to be /users/archlinux, then su switch to the archlinux user, copy the /etc/pam.d directory to your home directory

  • Step 1: Create a new user
useradd -d /user/archlinux archlinux
  • Step 2: Switch to archlinux user
su - archlinux
  • Step 3: Copy
cp -av /etc/pam.d ./

cp -a is equivalent to cp -pdr

Case 3
Extend testlv to 3.6G, requiring archlinux users’ files not to be lost

  • The first step: vgs view volume group status
vgs
  • Step 2: Expansion
lvextend -L +600M -r /dev/testvg/testlv

Case 4
shrink testlv to 3G, require archlinux users' files not to be lost, unmount the logical volume, be sure to check whether the unmount is successful.

  • Step 1: Unmount the logical volume before shrinking
umount -a
  • Step 2: Check the file system
e2fsck -f -y /dev/testvg/testlv
  • Step 3: Shrink the file system
resize2fs /dev/testvg/testlv 3G
  • Step 4: Shrink the logical volume
lvreduce -L 3G /dev/testvg/testlv

Case 5
Create a snapshot of testlv and try to back up data based on the snapshot to verify the function of the snapshot

  • Step 1: Create a snapshot with a size of 1G
lvcreate -s -L 1G -p r -n testsnap /dev/testvg/testlv
  • Step 2: Uninstall the source
umount /dev/testvg/testlv
  • Step 3: Restore the snapshot
lvconvert --merge /dev/testvg/testsnap:
  • Step 4: Mount the source
mount -a

Guess you like

Origin blog.csdn.net/qq_44924544/article/details/108896852