experimental objective
- Create and manage an LVM volume group (VG).
- Create, expand, and shrink logical volumes (LVs) in volume groups.
- Create and use snapshots.
experimental environment
- Linux servers
- Three available disk partitions.
/dev/sdb1
,/dev/sdb2
, and/dev/sdb3
experimental step
Step 1: Install the LVM Tool
Make sure the LVM tool is installed. If not, install it:
yum install lvm2 -y
Step 2: Create a Physical Volume (PV)
utilization/dev/sdb1
, /dev/sdb2
, and/dev/sdb3
Create a physical volume.
pvcreate /dev/sdb1
pvcreate /dev/sdb2
pvcreate /dev/sdb3
Step 3: Viewing Physical Volume Information
pvs
pvdisplay
Step 4: Create a Volume Group (VG)
Create a volume group with all three physical volumesmyvg
。
vgcreate myvg /dev/sdb1 /dev/sdb2 /dev/sdb3
Step 5: Viewing Volume Group Information
vgs
vgdisplay
Step 6: Volume Group Expansion and Reduction
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 3 0 0 wz--n- <14.99g <14.99g
rhel 1 2 0 wz--n- <19.00g 0
[root@localhost ~]# pvcreate /dev/sdb{5,6}
Physical volume "/dev/sdb5" successfully created.
Physical volume "/dev/sdb6" successfully created.
[root@localhost ~]# vgextend myvg /dev/sdb{5,6}
Volume group "myvg" successfully extended
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 5 0 0 wz--n- 18.98g 18.98g
rhel 1 2 0 wz--n- <19.00g 0
[root@localhost ~]# vgreduce myvg /dev/sdb6
Removed "/dev/sdb6" from volume group "myvg"
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 4 0 0 wz--n- 16.98g 16.98g
rhel 1 2 0 wz--n- <19.00g 0
[root@localhost ~]#
Step 7: Create a Logical Volume (LV)
Create two logical volumes:mylv1
cap (a poem)mylv2
。
[root@localhost ~]# lvcreate -l 10 -n mylv1 myvg
Logical volume "mylv1" created.
[root@localhost ~]# lvcreate -L 100M -n mylv2 myvg
Logical volume "mylv2" created.
Step 8: Viewing Logical Volume Information
lvs
lvdisplay
Step 9: Formatting Logical Volumes
Format the newly created logical volume asext4
File System.
mkfs.ext4 /dev/myvg/mylv1
mkfs.ext4 /dev/myvg/mylv2
Step 10: Mounting a Logical Volume
Create a mount point and mount the logical volume.
[root@localhost ~]# mkdir -p /mydir/mylv1
[root@localhost ~]# mkdir -p /mydir/mylv2
[root@localhost ~]# mount /dev/myvg/mylv1 /mydir/mylv1
[root@localhost ~]# mount /dev/myvg/mylv2 /mydir/mylv2
Step 11: Verify the Mount
[root@localhost ~]# df -Th
Filesystem Type Capacity Used Available Used % Mount Points
/dev/mapper/rhel-root xfs 17G 3.8G 14G 22% /
devtmpfs devtmpfs 897M 0 897M 0% /dev
tmpfs tmpfs 912M 0 912M 0% /dev/shm
tmpfs tmpfs 912M 9.0M 903M 1% /run
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 179M 836M 18% /boot
tmpfs tmpfs 183M 20K 183M 1% /run/user/0
/dev/mapper/myvg-mylv1 ext4 35M 782K 32M 3% /mydir/mylv1
/dev/mapper/myvg-mylv2 ext4 93M 1.6M 85M 2% /mydir/mylv2
Step 12: Extending a Logical Volume (LV)
extensionsmylv1
The size of the
[root@localhost ~]# lvextend -L +60M /dev/myvg/mylv1
[root@localhost ~]# resize2fs /dev/myvg/mylv1
Step 13: Reduce Logical Volume (LV)
First unmount the logical volume, then check and resize the file system, and finally resize the logical volume.
[root@localhost ~]# umount /mydir/mylv1
[root@localhost ~]# e2fsck -f /dev/myvg/mylv1
[root@localhost ~]# resize2fs /dev/myvg/mylv1 50M
[root@localhost ~]# lvreduce -L -50M /dev/myvg/mylv1
[root@localhost ~]# mount /dev/myvg/mylv1 /mydir/mylv1
[root@localhost ~]# lvs
Step 14: Creating a Snapshot
establishmylv2
snapshotsmysnop
。
[root@localhost ~]# echo "test data" > /mydir/mylv2/newfile
[root@localhost ~]# lvcreate -L 20M -s -n mysnop /dev/myvg/mylv2
[root@localhost ~]# lvs
Step 15: Recovering Data Using Snapshots
Assuming that themylv2
Some data has been written to it and now it needs to be restored to the state it was in at the time of the snapshot.
[root@localhost ~]# ls /mydir/mylv2/
[root@localhost ~]# rm -rf /mydir/mylv2/newfile
[root@localhost ~]# umount /mydir/mylv2
[root@localhost ~]# lvconvert --merge /dev/myvg/mysnop
[root@localhost ~]# mount /dev/myvg/mylv2 /mydir/mylv2
[root@localhost ~]# ls /mydir/mylv2/
[root@localhost ~]# lvs
Step 16: Logical Volume Volume Group Deletion
When the experiment is complete, clean up all resources created.
umount /mydir/mylv1
umount /mydir/mylv2
lvremove /dev/myvg/mylv1
lvremove /dev/myvg/mylv2
vgremove myvg
pvremove /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb5
caveat
- Make sure to back up important data before performing any operations involving data.
- Be sure to unmount the logical volume first when performing an expansion or reduction operation on the logical volume.
- Before shrinking a logical volume, make sure you perform a file system check first (
e2fsck
) and resize the file system (resize2fs
)。
After completing the above steps, you should be able to master the basic management and operation techniques of LVM. Please make sure to record important output results and observed phenomena during the experiment for subsequent analysis and learning.