名词解析
-
PV(Physical volume):物理卷
A Volume Group (VG) is a collection of one or more physical devices, each called a Physical Volume (PV).
-
VG(Volume group):卷组
Each block of data in an LV is stored on one or more PV in the VG, according to algorithms implemented by Device Mapper (DM) in the kernel.
-
LV(Logical volume):逻辑卷
A Logical Volume (LV) is a virtual block device that can be used by the system or applications.
先从感官上来认识PV/VG/LV
# 查看物理设备,当前环境中存在两个盘/dev/nvme0n1p1和/dev/sda
[root@192 home]# fdisk -l
Disk /dev/nvme0n1: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VMware Virtual NVMe Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7c9360
Device Boot Start End Sectors Size Id Type
/dev/nvme0n1p1 * 2048 2099199 2097152 1G 83 Linux
/dev/nvme0n1p2 2099200 104857599 102758400 49G 8e Linux LVM
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x94c9d806
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 10500000 10497953 5G 83 Linux
/dev/sda2 10500096 21000000 10499905 5G 83 Linux
# 查看物理卷
[root@192 home]# pvdisplay
--- Physical volume ---
PV Name /dev/nvme0n1p2
VG Name cs_192
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12543
Free PE 0
Allocated PE 12543
PV UUID eUvMTe-qQVn-Jq28-QIDH-HLH8-5lbY-1b9sOh
[root@192 home]# vgdisplay
--- Volume group ---
VG Name cs_192
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 12543 / <49.00 GiB
Free PE / Size 0 / 0
VG UUID 1GLOYb-51tp-sG78-cZcl-VQkc-dq6r-q0J7k3
[root@192 home]# lvdisplay
--- Logical volume ---
LV Path /dev/cs_192/swap
LV Name swap
VG Name cs_192
LV UUID tpUazx-0ikg-27P0-hKvs-7Are-62t1-WH1EkA
LV Write Access read/write
LV Creation host, time 192.168.0.100, 2023-03-08 07:45:16 +0800
LV Status available
# open 2
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
--- Logical volume ---
LV Path /dev/cs_192/root
LV Name root
VG Name cs_192
LV UUID H8Vzp4-KtbR-Fms7-hI8U-IVjz-5ofl-LMt1H1
LV Write Access read/write
LV Creation host, time 192.168.0.100, 2023-03-08 07:45:16 +0800
LV Status available
# open 1
LV Size <44.00 GiB
Current LE 11263
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
创建和管理LVM(Logic Volume Manage)
什么是LVM
The Logical Volume Manager (LVM) provides tools to create virtual block devices from physical devices.
两种管理方式
-
基于分区的方式
基于分区的方式,需要事先通过fdisk或者其他方式对磁盘分区,分区之后不格式化文件系统,然后再用pvcreate来创建物理卷
-
基于磁盘的方式
基于磁盘的方式,直接对磁盘创建物理卷(PV),然后分配卷组(VG),再在卷组里面添加逻辑卷(LV)
本次着重描述第二种方式,一般适用于专属服务器,比如数据库服务器,整个磁盘都是为数据库服务而设的,可以根据数据库服务的需求,直接划分出数据盘,备份盘,归档盘等等。
查看物理盘
查看服务器挂载的块设备,对应于实际的物理磁盘,也就是pv命令操作的对象(physical devices)。
[root@192 curtis]# fdisk -l
Disk /dev/nvme0n1: 50 GiB, 53687091200 bytes, 104857600 sectors
Disk model: VMware Virtual NVMe Disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x6d7c9360
Device Boot Start End Sectors Size Id Type
/dev/nvme0n1p1 * 2048 2099199 2097152 1G 83 Linux
/dev/nvme0n1p2 2099200 104857599 102758400 49G 8e Linux LVM
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x94c9d806
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 10500000 10497953 5G 83 Linux
/dev/sda2 10500096 21000000 10499905 5G 83 Linux
# 以/dev/sdb为示例创建物理卷
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual S
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
创建物理卷
# 格式:pvcreate 物理磁盘对应的块设备名
[root@sndsdevdb01 ~]#pvcreate /dev/vda
# 查看已创建的物理卷
[root@192 curtis]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created.
[root@192 curtis]# pvscan
PV /dev/nvme0n1p2 VG cs_192 lvm2 [<49.00 GiB / 0 free]
PV /dev/sdb lvm2 [10.00 GiB]
Total: 2 [<59.00 GiB] / in use: 1 [<49.00 GiB] / in no VG: 1 [10.00 GiB]
[root@192 curtis]# pvdisplay
--- Physical volume ---
PV Name /dev/nvme0n1p2
VG Name cs_192
PV Size <49.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12543
Free PE 0
Allocated PE 12543
PV UUID eUvMTe-qQVn-Jq28-QIDH-HLH8-5lbY-1b9sOh
"/dev/sdb" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb # 物理卷名
VG Name
PV Size 10.00 GiB # 物理卷大小
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID GawNUd-Cd2Z-ikP0-CA9Z-Pny1-Jzg9-c81sTv
创建卷组
# 格式:vgcreate 卷组名 物理卷名
[root@192 curtis]# vgcreate scsi_vg /dev/sdb
Volume group "scsi_vg" successfully created
[root@192 curtis]# vgscan
Found volume group "scsi_vg" using metadata type lvm2
Found volume group "cs_192" using metadata type lvm2
[root@192 curtis]# vgdisplay
--- Volume group ---
VG Name scsi_vg
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 0 / 0
Free PE / Size 2559 / <10.00 GiB
VG UUID x596pX-kWJ1-RCNp-B372-P4kN-rWwC-nOQ8N2
--- Volume group ---
VG Name cs_192
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size <49.00 GiB
PE Size 4.00 MiB
Total PE 12543
Alloc PE / Size 12543 / <49.00 GiB
Free PE / Size 0 / 0
VG UUID 1GLOYb-51tp-sG78-cZcl-VQkc-dq6r-q0J7k3
创建逻辑卷
# 格式: lvcreate -n 逻辑卷名 -L 逻辑卷大小 卷组名
[root@192 curtis]# lvcreate -n scsi_vl_01 -L 5G scsi_vg
Logical volume "scsi_vl_01" created.
[root@192 curtis]# lvdisplay
--- Logical volume ---
LV Path /dev/scsi_vg/scsi_vl_01
LV Name scsi_vl_01
VG Name scsi_vg
LV UUID PjwnV4-30q2-AWC1-6uZ5-q1KJ-iPDm-qDonaM
LV Write Access read/write
LV Creation host, time 192.168.0.100, 2023-03-09 07:55:05 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
...
# fdisk -l也可以查看对应的逻辑卷可以直接使用了
[root@192 curtis]# fdisk -l
...
Disk /dev/mapper/scsi_vg-scsi_vl_01: 5 GiB, 5368709120 bytes, 10485760 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
...
上述LV还没有被格式化文件系统,属于裸设备,如需要存储数据,还需要对LV进行mkfs操作
格式化文件系统
以ext4文件系统类型为例
# 格式: mkfs.ext3 LV对应的块设备名
[root@192 curtis]# mkfs.ext4 /dev/mapper/scsi_vg-scsi_vl_01
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: c4043f33-d2e9-46e1-b637-0d99cb52b127
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
挂载使用
# mount [-t vfstype] [-o options] [设备名称] [挂载点]
-
挂载点:
必须是一个已经存在的目录,这个目录可以不为空,但挂载后这个目录下以前的内容将不可用,umount以后会恢复正常。
-
设备名称:
可以是一个分区,一个usb设备,光驱,软盘,网络共享等
-
-t
指定文件系统的类型,通常不必指定。mount 会自动选择正确的类型。
-
-o
主要用来描述设备或档案的挂接方式。常用的参数有:
loop:用来把一个文件当成硬盘分区挂接上系统;
ro:采用只读方式挂接设备;
rw:采用读写方式挂接设备;
iocharset:指定访问文件系统所用字符集
[root@192 curtis]# cd /mnt/
[root@192 mnt]# mkdir storage
[root@192 mnt]# ls
hgfs storage
[root@192 mnt]# mount -o rw /dev/mapper/scsi_vg-scsi_vl_01 /mnt/storage/
[root@192 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.5G 9.7M 1.5G 1% /run
/dev/mapper/cs_192-root 44G 4.3G 40G 10% /
/dev/nvme0n1p1 1014M 292M 723M 29% /boot
tmpfs 767M 100K 766M 1% /run/user/1000
/dev/mapper/scsi_vg-scsi_vl_01 4.9G 24K 4.6G 1% /mnt/storage
扩展LV的大小
系统使用一段时间后,可能LV的空间已经满了,或者一开始操作有误,空间给小了,需要扩展大小,可以使用lvextend命令
首先先确认卷组中是否有剩余空间
[root@192 mnt]# vgdisplay
--- Volume group ---
VG Name scsi_vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <10.00 GiB
PE Size 4.00 MiB
Total PE 2559
Alloc PE / Size 1280 / 5.00 GiB # 已分配空间 (scsi_vl_01)
Free PE / Size 1279 / <5.00 GiB # 剩余空间
VG UUID x596pX-kWJ1-RCNp-B372-P4kN-rWwC-nOQ8N2
- 如果
Free PE / Size
还有空间,并且剩余的空间能满足扩展需求,那么可以直接用lvextend
命令。
# 格式: lvextend -L +10G lv块设备名
# 注意要有加号,否则就表示将该lv扩展至10G
[root@192 mnt]# umount /mnt/storage
[root@192 mnt]# lvextend -L +2G /dev/mapper/scsi_vg-scsi_vl_01
Size of logical volume scsi_vg/scsi_vl_01 changed from 5.00 GiB (1280 extents) to 7.00 GiB (1792 extents).
Logical volume scsi_vg/scsi_vl_01 successfully resized.
[root@192 mnt]# fdisk -l
Disk /dev/mapper/scsi_vg-scsi_vl_01: 7 GiB, 7516192768 bytes, 14680064 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
扩展完成后,还需补充格式化新扩展的卷的文件系统,否则df -hl
是看不到扩展的大小。
[root@192 mnt]# mount -o rw /dev/mapper/scsi_vg-scsi_vl_01 /mnt/storage/
[root@192 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.5G 9.7M 1.5G 1% /run
/dev/mapper/cs_192-root 44G 4.3G 40G 10% /
/dev/nvme0n1p1 1014M 292M 723M 29% /boot
tmpfs 767M 100K 766M 1% /run/user/1000
/dev/mapper/scsi_vg-scsi_vl_01 4.9G 24K 4.6G 1% /mnt/storage
[root@192 mnt]# umount /mnt/storage
[root@192 mnt]# resize2fs /dev/mapper/scsi_vg-scsi_vl_01
resize2fs 1.46.5 (30-Dec-2021)
Please run 'e2fsck -f /dev/mapper/scsi_vg-scsi_vl_01' first.
[root@192 mnt]# e2fsck -f /dev/mapper/scsi_vg-scsi_vl_01
e2fsck 1.46.5 (30-Dec-2021)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/scsi_vg-scsi_vl_01: 11/327680 files (0.0% non-contiguous), 42078/1310720 blocks
[root@192 mnt]# resize2fs /dev/mapper/scsi_vg-scsi_vl_01
resize2fs 1.46.5 (30-Dec-2021)
Resizing the filesystem on /dev/mapper/scsi_vg-scsi_vl_01 to 1835008 (4k) blocks.
The filesystem on /dev/mapper/scsi_vg-scsi_vl_01 is now 1835008 (4k) blocks long.
[root@192 mnt]# mount -o rw /dev/mapper/scsi_vg-scsi_vl_01 /mnt/storage/
[root@192 mnt]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.5G 9.7M 1.5G 1% /run
/dev/mapper/cs_192-root 44G 4.3G 40G 10% /
/dev/nvme0n1p1 1014M 292M 723M 29% /boot
tmpfs 767M 100K 766M 1% /run/user/1000
/dev/mapper/scsi_vg-scsi_vl_01 6.9G 24K 6.5G 1% /mnt/storage
- VG中已经没有剩余空间了,那么就需要追加挂载磁盘,然后重新创建新的PV,然后加到当前的VG中(vgextend),然后才是LV的扩展。