The previous article introduced some concepts of LVM. This article will teach you some daily LVM operations. After all, the theory must be applied in practice.
This blog article is divided into eight points to describe and operate LVM functions, hoping to help you.
1. Create a new volume
1. Prepare the Physical partition Physical Partions)
Use a partition tool such as fdisk to create an LVM partition in the same way as creating other common partitions. The difference is that the LVM partition type is 8e.
2. Create a physical volume PV
Suppose the partition/dev/sdb1 is created above. Here we use pvcreate to create the physical volume PV.
# Pvcreate/dev/sdb1 # Use pvcreate to convert sdb1 to a physical volume # pvdisplay # view the existing PV
3. Create a volume group VG
# Vgcreate-s 4 M vgname/dev/sdb1 # create a VG volume group with a PE of 4 M named vgname for PV (/dev/sdb1)
4. Create a logical Volume LV
# Lvcreate-l 10-n lvname vgname # create an LV logical volume named lvname on the vgname volume group that occupies the size of 10 PE blocks-l specify the number of PE blocks-L specify the size directly, for example,-L 40 M, directly specify the size of 40 M-n followed by the name of the LV to be created
5. format the logical volume
# Mkfs-t ext4/dev/vgname/lvname # format the lvname logical volume as the ext4 File System-t followed by the name of the file system to be formatted
6. Create a mount point and mount the logical volume
# Mkdir/file # create a mount directory/file # mount/dev/vgname/lvname/file # mount the logical volume lvname to the/file mount point
2. Add a volume group
The following test is performed when no space is available for pv and vg.
1. Prepare the Physical partition Physical Partions)
Use a partition tool such as fdisk to create an LVM partition in the same way as creating other common partitions. The difference is that the LVM partition type is 8e.
2. Create a physical volume PV
Suppose the partition/dev/sdb2 is created above. Here we use pvcreate to create the physical volume PV.
# Pvcreate/dev/sdb2 # Use pvcreate to convert sdb1 to a physical volume # pvdisplay # view the existing PV
3. Add a volume group VG.
# Vgextend vgname/dev/sdb2 # Add the physical volume of sdb2 to the vgname volume group # pvdisplay # view the PV information again after the physical volume is added.
3. Extract and delete PV
Solution 1:
1. View pv usage
# Pvdisplay # view existing PVS --- Physical volume --- PV Name/dev/sda1 VG Name vgname PV Size 1019.72 MiB/not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 254 Allocated PE 0 pv uuid TEiNQm-3H9i-OpmE-p5cX-KgRS-aGuf-HKaLON --- Physical volume --- PV Name/dev/sdb1 VG Name vgname PV Size 1019.72 MiB/not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 80 Allocated PE 174 pv uuid 27pvVQ-lERX-91ew-0XL2-Oxbk-NFKh-7l6c4M
2. Extract the empty pv based on the pv information.
# vgreduce vgname /dev/sda1
3. Delete the extracted pv.
# pvremove /dev/sda1
Solution 2:
When sda1 is used, the PV of sda1 is extracted.
1. View pv Information
# pvdisplay --- Physical volume --- PV Name /dev/sda1 VG Name vgname PV Size 1019.72 MiB / not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 80 Allocated PE 174 PV UUID TEiNQm-3H9i-OpmE-p5cX-KgRS-aGuf-HKaLON --- Physical volume --- PV Name /dev/sdb1 VG Name vgname PV Size 1019.72 MiB / not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 254 Allocated PE 0 PV UUID Duz6zM-GJwo-8cc5-5Jp2-vGiH-0j0L-gSvUtl
2. Move all PES in sda1 to sdb1.
# pvmove /dev/sdb1 /dev/sdd1 /dev/sda1: Moved: 0.0% /dev/sdb1: Moved: 100.0%
3. View Current pv usage
# Pvdisplay # view existing PVS --- Physical volume --- PV Name/dev/sda1 VG Name vgname PV Size 1019.72 MiB/not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 254 Allocated PE 0 pv uuid TEiNQm-3H9i-OpmE-p5cX-KgRS-aGuf-HKaLON --- Physical volume --- PV Name/dev/sdb1 VG Name vgname PV Size 1019.72 MiB/not usable 3.72 MiB Allocatable yes PE Size 4.00 MiB Total PE 254 Free PE 80 Allocated PE 174 pv uuid Duz6zM-GJwo-8cc5-5Jp2-vGiH-0j0L-gSvUtl
4. Extract the empty pv based on the pv information.
# vgreduce vgname /dev/sda1
5. Delete the extracted pv.
# pvremove /dev/sda1
Iv. Online stretch File System
1. Check that VG is available
# vgdisplay
2. Check the LV size.
# lvdisplay
3. Stretch the logical volume size
# Lvresize-l + 888/dev/vgname/lvname # Based on the remaining PES in the vg, set the number-l, followed by the plus sign (+), and followed by the minus sign, specify the size directly.
4. resize the File System
# Resize2fs/dev/vgname/lvname [nM/G] adjust the value to the maximum if the value is not increased.
5. view the LV size after stretching
# lvdisplay
V. Online File System reduction
1. unmount
# umount /dev/sda1
2. Use fsck to check
# e2fsck -f /dev/vgname/lvname
3. Reduce the file system size
# resize2fs /dev/vgname/lvname 200M
4. Reduce the LV size
# lvresize -L -40M /dev/vgname/lvname
5. Remount
# mount -a
Here, mount-a takes effect only when there are entries mounted to this partition in/etc/fstab. If it was previously on a temporary mount, and then unmount the file system and then reduce the file system size, then mount-a cannot be used to re-mount the file system.
Summary:To reduce the file system size on an lvm logical volume, you must first reduce the file system size and then the logical volume size. Just like the water in a bottle, you only need to first pour out the water and then shrink the size of the small bottle.
6. Delete LVM
1. Unmount and delete entries in/etc/fstab
# Umount/home # unmount # vim/etc/fstab # Delete the corresponding entries mounted to/home in the file
2. Remove the logical Volume LV
# Lvchange-an/dev/vgname/lvname # disable logical volume # lvremove/dev/vgname/lvname # Remove logical volume
3. Remove the volume group VG.
# Vgchange-an/dev/vgname # Close the volume group # vgremove/dev/vgname # Remove the volume group
4. Remove physical volume PV
# Pvremove/dev/sda1 # Remove a physical volume
5. Delete Disk Partitions
Use fdisk to delete disk partitions. This is not demonstrated # partx-d -- nr 1/dev/sda #. Remember to use this command to refresh the memory entries.
VII. LVM migration
The following operations are mainly performed to migrate VG, and the same is true for LV migration.
1. operations on the source host
① Cancel mounting the file system to be migrated
# umount /home
② Disable LV and VG
# lvchange -an /dev/vgname/lvname# vgchange -an /dev/vgname
③ Export VG
# vgexport vgname
2. operations on the target host
① Import VG
#vgimport vgname
② Activate VG and mount the File System
# vgchange -ay vgname
③ Mount the migrated File System
# Mkdir/home # mount/dev/vgname/lvname/home # df-h # view mounting information
VIII. LVM snapshot function
LVM snapshots are used to clone virtual machines. For example, when creating an xen virtual machine, you can create a new full Virtual Machine. Assume that the size is 10 Gb, then we can create a 3G snapshot on this 10g virtual machine, and then start the virtual machine through this 3G snapshot. After completing the experiment, you can directly Delete snapshots to keep the original virtual machine clean. Second, real-time backup of moment-in-time), that is, to maintain system consistency, we first create a snapshot to freeze the current system status, so that the content in the snapshot can be temporarily unchanged, the system itself continues to run, and uses the backup snapshot to back up services without interruption.
In addition, we recommend that you change the space when creating a snapshot larger than the lvname space. Otherwise, the snapshot will be damaged once the space exceeds the snapshot size.
Now, if we want to take a snapshot of/dev/vgname/lvname, lvname is now MB.
1. Create a snapshot with lvcreate
# lvcreate -L 5M -s -n lvname_snap /dev/vgname/lvname
2. Mount snapshots
# mount /dev/vgname/lvname_snap /snap
Command Summary
1. PV
Pvcreat creates partition as PV
Pvscan searches for any disk with PV in the system.
Pvdisplay displays the PV status on the system.
Pvremove remove the PV attribute of partition
2. VG
Vgcreate create VG
Whether VG exists in the vgscan Search System
Vgdisplay displays the status of VG ON THE SYSTEM
Vgextend adds PV in VG
Vgreduce removes PV from VG
Vgchange sets whether VG is active)
Vgremove Delete VG
3. LV
Lvcreate create LV
Lvscan
Lvdisplay displays the LV status in the system
Increase lvextend capacity in LV
Lvreduce reduces LV capacity
Lvremove
Lvresize
This blog post is here, and I will write a supplement later to supplement some features and features of LVM. Well, I will try again.
This article is from the Not Only Linux blog, please be sure to keep this source http://nolinux.blog.51cto.com/4824967/1350942