- Giới thiệu
LVM (Logical Volume Manager) là trình quản lý lôgic volume, cho phép quản lý linh hoạt các phân vùng lưu trữ trên hệ thống.
Volume vật lý (PV - Physical Volume): Là các thiết bị lưu trữ thực tế như ổ cứng hoặc phân vùng.
Nhóm volume (VG - Volume Group): Kết hợp nhiều PV thành một nhóm duy nhất, có thể xem như một ổ đĩa lôgic lớn.
Volume lôgic (LV - Logical Volume): Là các "phân vùng" được tạo ra từ VG, có thể định dạng và sử dụng như các phân vùng thông thường.
Đơn vị mở rộng vật lý (PE - Physical Extent): Là đơn vị lưu trữ nhỏ nhất trong LVM, mặc định kích thước là 4MB.
- Các bước thiết lập LVM
Đầu tiên, cần chuẩn bị các thiết bị lưu trữ (có thể là phân vùng hoặc toàn bộ ổ đĩa).
Tiếp theo, chuyển đổi các thiết bị thành volume vật lý (PV).
Sau đó, kết hợp các PV thành một nhóm volume (VG). VG có thể điều chỉnh kích thước linh hoạt bằng cách thêm hoặc bớt PV.
Cuối cùng, tạo các volume lôgic (LV) từ VG để sử dụng. LV cũng có thể thay đổi kích thước linh hoạt và cần được định dạng trước khi sử dụng.
- Quản lý Volume Vật lý
3.1 Phân vùng ổ đĩa
Sử dụng lệnh fdisk để phân vùng, cần thay đổi ID phân hệ thành 8e (ID cho LVM).
3.2 Tạo Volume Vật lý
pvcreate [tên_thiết_bị]
Có thể tạo PV từ toàn bộ ổ đĩa:
[root@server ~]# pvcreate /dev/sdc
Hoặc tạo từ các phân vùng:
[root@server ~]# pvcreate /dev/sdc1
Physical volume "/dev/sdc1" successfully created.
[root@server ~]# pvcreate /dev/sdc2
Physical volume "/dev/sdc2" successfully created.
3.3 Xem thông tin Volume Vật lý
[root@server ~]# pvscan
PV /dev/sda2 vg_system lvm2 [<50.00 GiB / 0 free]
PV /dev/sdc1 lvm2 [10.00 GiB]
PV /dev/sdc2 lvm2 [20.00 GiB]
Total: 3 [<80.00 GiB] / in use: 1 [<50.00 GiB] / in no VG: 2 [<30.00 GiB]
Lệnh pvdisplay cung cấp thông tin chi tiết hơn:
[root@server ~]# pvdisplay /dev/sdc1
--- Physical volume ---
PV Name /dev/sdc1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID aBcDeF-12Gh-34Ij-56Kl-mNoP-qRSt-uVwXyZ
3.4 Xóa Volume Vật lý
[root@server ~]# pvremove /dev/sdc2
- Quản lý Nhóm Volume
4.1 Tạo Nhóm Volume
vgcreate [tùy_chọn] tên_nhóm_volume tên_pv
-s kích_thước_PE: Chỉ định kích thước PE (MB, GB, TB), mặc định 4MB
[root@server ~]# vgcreate -s 16MB data_group /dev/sdc1 /dev/sdc2
Volume group "data_group" successfully created
4.2 Xem thông tin Nhóm Volume
[root@server ~]# vgscan
Reading volume groups from cache.
Found volume group "data_group" using metadata type lvm2
Found volume group "vg_system" using metadata type lvm2
[root@server ~]# vgdisplay data_group
--- Volume group ---
VG Name data_group
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 29.90 GiB
PE Size 16.00 MiB
Total PE 1918
Alloc PE / Size 0 / 0
Free PE / Size 1918 / 29.90 GiB
VG UUID zYxWvU-tSrQp-OnmL-kJih-gFed-cBaZyX
4.3 Mở rộng Nhóm Volume
[root@server ~]# vgextend data_group /dev/sdd1
Volume group "data_group" successfully extended
4.4 Thu hẹp Nhóm Volume
[root@server ~]# vgreduce data_group /dev/sdd1
Removed "/dev/sdd1" from volume group "data_group"
4.5 Xóa Nhóm Volume
[root@server ~]# vgremove data_group
Volume group "data_group" successfully removed
- Quản lý Volume Lôgic
5.1 Tạo Volume Lôgic
lvcreate [tùy_chọn] [-n tên_volume_lôgic] tên_nhóm_volume
-L dung_lượng: Chỉ định kích thước volume lôgic (MB, GB, TB)
-l số_lượng_PE: Chỉ định kích thước theo số lượng PE
-n tên_volume_lôgic: Đặt tên cho volume lôgic
[root@server ~]# lvcreate -L 5G -n storage_lv data_group
Logical volume "storage_lv" created.
Sau khi tạo xong, cần định dạng và mount volume lôgic:
[root@server ~]# mkfs -t ext4 /dev/data_group/storage_lv
[root@server ~]# mkdir /mnt/storage
[root@server ~]# mount /dev/data_group/storage_lv /mnt/storage/
5.2 Xem thông tin Volume Lôgic
[root@server ~]# lvscan
ACTIVE '/dev/data_group/storage_lv' [5.00 GiB] inherit
[root@server ~]# lvdisplay /dev/data_group/storage_lv
--- Logical volume ---
LV Path /dev/data_group/storage_lv
LV Name storage_lv
VG Name data_group
LV UUID mNoPqR-sTuV-wXyZ-aBcDeF-12Gh-34Ij-56Kl
LV Write Access read/write
LV Creation host, time server, 2023-05-15 10:30:22 +0700
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 320
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
5.3 Điều chỉnh kích thước Volume Lôgic
lvresize [tùy_chọn] đường_dẫn_volume_lôgic
-L dung_lượng: Điều chỉnh theo dung lượng, dùng + để tăng, - để giảm
-l số_lượng_PE: Điều chỉnh theo số lượng PE
[root@server ~]# lvresize -L +3G /dev/data_group/storage_lv
Size of logical volume data_group/storage_lv changed from 5.00 GiB (320 extents) to 8.00 GiB (512 extents).
Logical volume data_group/storage_lv successfully resized.
Sau khi thay đổi kích thước volume lôgic, cần điều chỉnh hệ thống tập tin:
[root@server ~]# resize2fs /dev/data_group/storage_lv
resize2fs 1.44.1 (18-Mar-2022)
Filesystem at /dev/data_group/storage_lv is mounted on /mnt/storage; on-line resizing required
The filesystem on /dev/data_group/storage_lv is now 2097152 blocks long.
[root@server ~]# df -h /mnt/storage
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/data_group-storage_lv 8.0G 1.2G 6.3G 16% /mnt/storage
5.4 Xóa Volume Lôgic
lvremove đường_dẫn_volume_lôgic
Trước khi xóa, cần umount volume:
[root@server ~]# umount /mnt/storage
[root@server ~]# lvremove /dev/data_group/storage_lv
Do you really want to remove active logical volume data_group/storage_lv? [y/n]: y
Logical volume "storage_lv" successfully removed