A common problem that Linux users encounter when installing the operating system is how to correctly evaluate the size of each partition so as to divide the appropriate hard disk space. The general disk partition management method cannot change the size after each partition is divided. When a logical partition cannot store a file, this file is restricted by the upper-level file system, it cannot be stored across multiple partitions, so it cannot be stored on another disk at the same time. Or when a partition space is quickly exhausted, the solution is to use a symbolic link, or create a directory plug-in partition and use a tool to adjust the partition size.
A common problem that Linux users encounter when installing the operating system is how to correctly evaluate the size of each partition so as to divide the appropriate hard disk space. The general disk partition management method cannot change the size after each partition is divided. When a logical partition cannot store a file, this file is restricted by the upper-level file system, it cannot be stored across multiple partitions, so it cannot be stored on another disk at the same time. Or when a partition space is quickly exhausted, the solution is usually to use a symbolic link, or create a directory plug-in partition and use a tool to adjust the partition size, but this is only a temporary solution, the problem is not fundamentally solved. In fact, in Linux, the use of LVM can solve these problems, users can easily adjust the size of each partition without downtime.
I. Introduction to LVM commands
LVM (Logical Volume Manager) is a Logical Volume Manager applied to the Linux kernel. It is a mechanism for disk partition management in Linux.
Terms to be known in LVM:
1. PV (physical volume) can be a disk and a partition. Consists of PE (physical disk area), multiple PVS can form a VG (volume group ).
2. VG (volume group) is a group composed of multiple physical volumes, but the volume group cannot be used directly. You need to create an LV (logical volume) on it to use it. Multiple LVS can be created on VG.
3. PE (physical disk area) is like the block of the disk we learned earlier. The default value is 4 MB.
4. LV (logical volume) is an available space built on the volume group. There are two kinds of boundary: physical boundary and logical boundary.
Ii. Related commands
1. Physical Volume PV
Pvcreate create pv example: pvcreate/dev/sda1
Pvs: Enter pvs directly in the command line.
Pvdisplay: pvcreate/dev/sda1
Pvmove: Move pv data to another pv instance: pvmove/dev/sda1/dev/sda4
Pvremove: pvremove/dev/sda1
2. Volume group VG
How to create, view, and delete vg
Vgcreate create vg example: vgcreate myvg/dev/sda {m, n}
Vgs: Enter vgs directly in the command line.
Vgdisplay: vgdisplay myvg
Vgremove: vgremove myvg
How to expand vg
First, prepare a PV;
Then, use the vgextend command to complete the extension;
Format: vgextend VG_NAME/PATH/TO/PV
Vgextend testvg/dev/sdb2 extension myvg volume group, add/dev/sda2
How to Reduce vg
1) determine the PV to be removed;
2) transfer the data on this PV to other PVS;
Pvmove/path/to/pv
3) Remove the PV from the volume group;
Format: vgreduce VG_NAME/PATH/TO/PV
Vgreduce testvg/dev/sdb2: scale down the myvg volume group and remove/dev/sda2.
3. Logical Volume LV
How to create, view, and delete lv
Lvcreate create lv example: lvcreate lv1/dev/myvg
Lvs: Enter lvs directly in the command line.
Lvdisplay lv Details Example: vgdisplay lv1
Lvremove: lvremove/dev/myvg/lv1
How to expand lv
Determine how much space to expand
Determine whether the volume group of the Current Logical volume has sufficient free space;
Extension example:
1. physical boundaries
Lvextend-L [+] SIZE/path/to/lv
2. logical boundaries
Resize2fs/path/to/device
How to Reduce lv
Determine the size of the reduction, provided that at least all the original data can be accommodated.
Reduction:
1) uninstall and forcibly detect the file system;
Umount/path
E2fsck-f
2) logical boundaries
Resize2fs/path/to/device SIZE
3) physical boundaries
Lvreduce-L [-] SIZE/path/to/lv
4. snapshot volume
1) The lifecycle is the entire data duration. During this period, the data growth cannot exceed the snapshot volume size;
2) the snapshot volume should be read-only;
3) in the same volume group as the original volume;
Lvcreate
-L specify the snapshot volume size
-S: Create a snapshot volume
-The snapshot volume created by p r is read-only.
Format:
Lvcreate-l size-s-p r-n LV_NAME/path/to/lv
Iii. lv example
1. Create a partition
[Root @ test11 ~] # Fdisk-l // view partition information
[Root @ test11 ~] # Fdisk/dev/sda // Partition
Command (m for help): n // n is to add a partition, p view the partition
First cylinder (2898-10443, default 2898): // press Enter.
Using default value 2898 // select the default value for the start Cylinder
Last cylinder or + size or + sizeM or + sizeK (289810443, default 10443): + 2G // give 2G size
Command (m for help): t // change the partition type
Partition number (1-5): 5 // select a Partition
Hex code (type L to list codes): l // list of partition types
Hex code (type L to list codes): 8e // mark the newly added partition as lvm
Command (m for help): p // view the Partition Table
/Dev/sda5 2756 2999 1959898 + 8e Linux LVM
Command (m for help): w // save
[Root @ test11 ~] # Partprobe/dev/sda // is a system identification partition (instead of restarting)
[Root @ test11 ~] # Mkfs. ext4/dev/sda5 // format the newly added Partition
2. Create PV
[Root @ test11 ~] # Pvcreate/dev/sda5 // create pv
Writing physical volume data to disk "/dev/sda5"
Physical volume "/dev/sda5" successfully created
[Root @ test11 ~] # Pvs // view the current pv Information
Pv vg Fmt Attr PSize PFree
/Dev/sda5 lvm2 a -- 1.87G 1.87G
3. Create VG
[Root @ test11 ~] # Vgcreate vg0/dev/sda5 // vg0 is the name of the current vg
Volume group "vg0" successfully created
[Root @ test11 ~] # Vgs // view vg Information
VG # PV # LV # SN Attr VSize VFree
Vg0 1 0 0 wz -- n-1.87G 1.87G
4. lv Creation
[Root @ test11 ~] # Lvcreate-L 500 M-n lv01 vg0
Logical volume "lv01" created
[Root @ test11 ~] # Lvs // view lv Information
Lv vg Attr LSize Origin Snap % Move Log Copy % Convert
Lv01 vg0-wi-a-500.00 M
[Root @ test11 ~] # Mkfs. ext4/dev/vg0/lv01 // format
5. lv increase
[Root @ test11 ~] # Lvextend-L + 300 M/dev/vg0/lv01 // (original lv size 500 M, stretch partition to 800 M) Extending logical volume lv01 to 800.00 MB
Logical volume lv01 successfully resized
[Root @ test11 ~] # Resize2fs/dev/vg0/lv01 // stretch the File System
[Root @ test11 ~] # Lvs
Lv vg Attr LSize Origin Snap % Move Log Copy % Convert
Lv01 vg0-wi-ao 800.00 M // The size is changed to 800 M
6. lv reduction
# Umount/dev/vg0/lv01 // uninstall the logical volume/dev/vg0/lv01
# Df-h // View
# E2fsck-f/dev/vg0/lv01 // disk Verification
# Resize2fs/dev/vg0/lv01 500 M // the file system is reduced to 500 M
# Lvreduce-L 500 M/dev/vg0/lv01 // shrink the partition to 500 M
WARNING: switching active logical volume to 500.00 MB
This may destroy your data (filesystem etc .)
Do you really want to reduce lv01? [Y/n]: y
# Lvs
Lv vg Attr LSize Origin Snap % Move Log Copy % Convert
Lv01 vg0-wi-a-500.00 M // The value has changed to M.
# Mount/dev/vg0/lv01/mnt/lv01 // re-mount the partition
# Df-h // View
# Ls/mnt/lv01 // view the file
Iv. lvm Shutdown
[Root @ test11 ~] # Umount/mnt/lv01
[Root @ test11 ~] # Lvremove/dev/vg0/lv01 // Delete lv
Do you really want to remove active logical volume lv01? [Y/n]: y
Logical volume "lv01" successfully removed
[Root @ test11 ~] # Vgchange-a n vg0 // make vg0 not active
0 logical volume (s) in volume group "vg0" now active
[Root @ test11 ~] # Vgremove vg0 // Delete vg
Volume group "vg0" successfully removed
[Root @ test11 ~] # Pvremove/dev/sda5 // Delete pv
Labels on physical volume "/dev/sda5" successfully wiped
5. Expand the root partition and SWAP
[Root @ zkdb1 ~] # Pvs
Pv vg Fmt Attr PSize PFree
/Dev/sda3 vg_zkdb1 lvm2 a -- 557.46g 521.46g
[Root @ zkdb1 ~] # Lvs
Lv vg Attr LSize Pool Origin Data % Move Log Cpy % Sync Convert
Root vg_zkdb1-wi-ao ---- 20.00g
Swap vg_zkdb1-wi-ao ---- 16.00g
[Root @ zkdb1 ~] # Lvcreate-L 30720 M vg_zkdb1
Logical volume "lvol0" created
[Root @ zkdb1 ~] # Lvs
Lv vg Attr LSize Pool Origin Data % Move Log Cpy % Sync Convert
Lvol0 vg_zkdb1-wi-a ----- 30.00g
Root vg_zkdb1-wi-ao ---- 20.00g
Swap vg_zkdb1-wi-ao ---- 16.00g
[Root @ zkdb1 ~] # Lvextend-L + 30G/dev/mapper/vg_zkdb1-root
Extending logical volume root to 50.00 GiB
Logical volume root successfully resized
[Root @ zkdb1 ~] # E2fsck-f/dev/mapper/vg_zkdb1-root
E2fsck 1.41.12 (17-May-2010)
/Dev/mapper/vg_zkdb1-root is mounted.
E2fsck: Cannot continue, aborting.
[Root @ zkdb1 ~] # Lvs
Lv vg Attr LSize Pool Origin Data % Move Log Cpy % Sync Convert
Lvol0 vg_zkdb1-wi-a ----- 30.00g
Root vg_zkdb1-wi-ao ---- 50.00g
Swap vg_zkdb1-wi-ao ---- 16.00g
[Root @ zkdb1 ~] # Resize2fs/dev/mapper/vg_zkdb1-root
Resize2fs 1.41.12 (17-May-2010)
Filesystem at/dev/mapper/vg_zkdb1-root is mounted on/; on-line resizing required
Old desc_blocks = 2, new_desc_blocks = 4
Ming an on-line resize of/dev/mapper/vg_zkdb1-root to 13107200 (4 k) blocks.
The filesystem on/dev/mapper/vg_zkdb1-root is now 13107200 blocks long.
Expand SWAP
[Root @ zkdb1 ~] # Lvs
Lv vg Attr LSize Pool Origin Data % Move Log Cpy % Sync Convert
Lvol0 vg_zkdb1-wi-a ----- 30.00g
Root vg_zkdb1-wi-ao ---- 50.00g
Swap vg_zkdb1-wi-ao ---- 16.00g
[Root @ zkdb1 ~] # Lvextend-L + 16G/dev/mapper/vg_zkdb1-swap
Extending logical volume swap to 32.00 GiB
Logical volume swap successfully resized
[Root @ zkdb1 ~] # E2fsck-f/dev/mapper/vg_zkdb1-swap
E2fsck 1.41.12 (17-May-2010)
/Dev/mapper/vg_zkdb1-swap is mounted.
E2fsck: Cannot continue, aborting.
[Root @ zkdb1 ~] # Resize2fs/dev/mapper/vg_zkdb1-swap
Resize2fs 1.41.12 (17-May-2010)
Resize2fs: Bad magic number in super-block while trying to open/dev/mapper/vg_zkdb1-swap
Couldn't find valid filesystem superblock.
[Root @ zkdb1 ~] # Lvs
Lv vg Attr LSize Pool Origin Data % Move Log Cpy % Sync Convert
Lvol0 vg_zkdb1-wi-a ----- 30.00g
Root vg_zkdb1-wi-ao ---- 50.00g
Swap vg_zkdb1-wi-ao ---- 32.00g
Use LVM on Ubuntu to easily adjust partitions and make snapshot http://www.linuxidc.com/Linux/2015-09/122563.htm
Use LVM to create elastic disk storage-Part 1 http://www.linuxidc.com/Linux/2014-10/107697.htm
Extend/Contract LVM (Part 2) http://www.linuxidc.com/Linux/2014-10/107786.htm in Linux
Recording logical volume snapshot in LVM and restoring (part 3) http://www.linuxidc.com/Linux/2014-11/108867.htm
Set up a thin provisioning volume (part 4) http://www.linuxidc.com/Linux/2014-11/109757.htm in LVM
Managing multiple LVM disks with striped I/O (Part 5) http://www.linuxidc.com/Linux/2014-12/110532.htm
This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-09/122800.htm