Linux磁盘分区————逻辑卷
目录
逻辑卷的概念
PV的创建与删除
查看分区
创建pv
删除pv
VG的创建、删除和扩容
创建vg
删除vg
扩容vg
lv的创建、删除和扩容
创建lv
逻辑卷的路径
删除lv
扩容lv
格式化逻辑卷
查看uuid
进行挂载
在线扩容lv
如果对磁盘直接使用fdisk
分区,那么这种分区,我们叫做Linux的标准分区,Linux的标准分区如果格式化成文件系统之后,挂载使用,那么一旦文件系统的空间满了,是无法扩容的。如果一定要“扩容”,需要将数据备份出来,在换一个更大的文件系统,再将数据还原回去。这种方法效率特别低。
上面的问题促使了逻辑卷的诞生
逻辑卷的概念
逻辑卷是个抽象的概念,有很对抽象的概念
-
pv(physical volume):物理卷
-
vg(volume group):卷组
-
lv(logical volume):逻辑卷
PV的创建与删除
查看分区
[root@bogon ~]# fdisk -l /dev/sda
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: 0xcf227196
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 2099199 2097152 1G 83 Linux
/dev/sda2 2099200 4196351 2097152 1G 83 Linux
/dev/sda3 4196352 6293503 2097152 1G 83 Linux
/dev/sda4 6293504 41943039 35649536 17G 5 Extended
/dev/sda5 6295552 8392703 2097152 1G 83 Linux
/dev/sda6 8394752 10491903 2097152 1G 83 Linux
/dev/sda7 10493952 12591103 2097152 1G 83 Linux
在这个磁盘中有7个分区,三个主分区,三个扩展分区
创建pv
pv可用直接添加一整块磁盘,在这里我们使用分区进行演示
[root@bogon ~]# pvcreate /dev/sda1
WARNING: ext2 signature detected on /dev/sda1 at offset 1080. Wipe it? [y/n]: y
Wiping ext2 signature on /dev/sda1.
Physical volume "/dev/sda1" successfully created.
[root@bogon ~]# pvcreate /dev/sda2
WARNING: xfs signature detected on /dev/sda2 at offset 0. Wipe it? [y/n]: y
Wiping xfs signature on /dev/sda2.
Physical volume "/dev/sda2" successfully created.
[root@bogon ~]# pvcreate /dev/sda3
WARNING: dos signature detected on /dev/sda3 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
[root@bogon ~]# pvcreate /dev/sda5
WARNING: dos signature detected on /dev/sda5 at offset 510. Wipe it? [y/n]: y
Wiping dos signature on /dev/sda5.
Physical volume "/dev/sda5" successfully created.
添加了四个分区,分别是sda1,sda2,sda3,sda,sda5
使用pvs和pvdisplay分别进行查看
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rl_bogon lvm2 a-- <49.00g 0
/dev/sda1 lvm2 --- 1.00g 1.00g
/dev/sda2 lvm2 --- 1.00g 1.00g
/dev/sda3 lvm2 --- 1.00g 1.00g
/dev/sda5 lvm2 --- 1.00g 1.00g
[root@bogon ~]# pvdisplay /dev/sda1 # pvdisplay可以查看详细信息
"/dev/sda1" is a new physical volume of "1.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda1
VG Name
PV Size 1.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID PQ3VVY-BRQl-e64m-QCln-YxDj-9hDv-ik6Pn3
删除pv
如果vg已经创建,pv则无法删除
使用pvremove进行删除pv
[root@bogon ~]# pvremove /dev/sda5
Labels on physical volume "/dev/sda5" successfully wiped.
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rl_bogon lvm2 a-- <49.00g 0
/dev/sda1 lvm2 --- 1.00g 1.00g
/dev/sda2 lvm2 --- 1.00g 1.00g
/dev/sda3 lvm2 --- 1.00g 1.00g
[root@bogon ~]#
使用pvs查看,可以看到在pv中已经没有sda5
VG的创建、删除和扩容
创建vg
创建vg,前提是该磁盘或者分区已经加入到了pv中,如果将没有加入pv的磁盘或分区进行创建,该磁盘或者分区会自动加入到pv中(在rhel8和rhel9中是这个样子的,其他的不清楚)
先创建个vg1,使用pv中的分区进行创建
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rl_bogon lvm2 a-- <49.00g 0
/dev/sda1 lvm2 --- 1.00g 1.00g
/dev/sda2 lvm2 --- 1.00g 1.00g
/dev/sda3 lvm2 --- 1.00g 1.00g
[root@bogon ~]# vgcreate vg1 /dev/sda1
Volume group "vg1" successfully created
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rl_bogon lvm2 a-- <49.00g 0
/dev/sda1 vg1 lvm2 a-- 1020.00m 1020.00m
/dev/sda2 lvm2 --- 1.00g 1.00g
/dev/sda3 lvm2 --- 1.00g 1.00g
在创建个vg2,使用不在pv中的分区进行创建
[root@bogon ~]# vgcreate vg2 /dev/sda5
Physical volume "/dev/sda5" successfully created.
Volume group "vg2" successfully created
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rl_bogon lvm2 a-- <49.00g 0
/dev/sda1 vg1 lvm2 a-- 1020.00m 1020.00m
/dev/sda2 lvm2 --- 1.00g 1.00g
/dev/sda3 lvm2 --- 1.00g 1.00g
/dev/sda5 vg2 lvm2 a-- 1020.00m 1020.00m
sda5虽然不在pv中,但是创建了vg后,sda5直接被添加到了pv中,这种方式不建议
创建vg是指定pe的大小
PE:物理区域(PE)是物理卷中可用于分配的最小存储单元
-s:指定pe的大小
[root@bogon ~]# vgcreate vg1 /dev/sda{2..3} -s 8M
Volume group "vg1" successfully created
[root@bogon ~]# vgdisplay vg1
--- Volume group ---
...
PE Size 8.00 MiB
...
[root@bogon ~]# vgdisplay vg2
--- Volume group ---
...
PE Size 4.00 MiB
...
可以看到vg1
创建的时候使用-s
指定PE
的为8M
,而vg2
没有指定,则pe
的大小为4M
删除vg
如果vg上有逻辑卷在使用,那么vg无法删除
[root@bogon ~]# vgremove vg1
Volume group "vg1" successfully removed
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rl_bogon 1 2 0 wz--n- <49.00g 0
vg2 1 0 0 wz--n- 1020.00m 1020.00m
可用看到vg1被删除
扩容vg
[root@bogon ~]# vgextend vg2 /dev/sda1
Volume group "vg2" successfully extended
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rl_bogon 1 2 0 wz--n- <49.00g 0
vg2 2 0 0 wz--n- 1.99g 1.99g
[root@bogon ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/nvme0n1p2 rl_bogon lvm2 a-- <49.00g 0
/dev/sda1 vg2 lvm2 a-- 1020.00m 1020.00m
/dev/sda2 lvm2 --- 1.00g 1.00g
/dev/sda3 lvm2 --- 1.00g 1.00g
/dev/sda5 vg2 lvm2 a-- 1020.00m 1020.00m
可以看到,在刚才sda1后面显示的是vg1,把vg1删除之后,在使用扩容,将sda1分给了vg2,现在sda1和sda2都属于了vg2
lv的创建、删除和扩容
创建lv
[root@bogon ~]# vgs
VG #PV #LV #SN Attr VSize VFree
rl_bogon 1 2 0 wz--n- <49.00g 0
vg1 2 0 0 wz--n- 1.98g 1.98g
[root@bogon ~]# lvcreate -n lv1 -L 1G vg1
Logical volume "lv1" created.
[root@bogon ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rl_bogon -wi-ao---- <45.09g
swap rl_bogon -wi-ao---- 3.91g
lv1 vg1 -wi-a----- 1.00g
-
-n:表示逻辑卷的名字
-
-L:表示逻辑卷的大小
-
vg1:表示卷组vg1
[root@bogon ~]# lvcreate -n lv2 -L 1G vg1
Volume group "vg1" has insufficient free space (126 extents): 128 required.
创建lv2没有成功,这里表示剩余的PE为126个,需要的PE为128个,这里可以给vg1扩容来增加来解决问题
[root@bogon ~]# vgextend vg1 /dev/sda2
Volume group "vg1" successfully extended
[root@bogon ~]# lvcreate -n lv2 -L 1G vg1
Logical volume "lv2" created.
[root@bogon ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rl_bogon -wi-ao---- <45.09g
swap rl_bogon -wi-ao---- 3.91g
lv1 vg1 -wi-a----- 1.00g
lv2 vg1 -wi-a----- 1.00g
逻辑卷的路径
逻辑卷创建完成之后,可以直接格式化成为文件系使用,如果卷组下面创建了一个新的逻辑卷,那么该逻辑卷就会自动在设备文件路径出现/dev/vgname/lvname
[root@bogon ~]# ll /dev/vg1/lv*
lrwxrwxrwx. 1 root root 7 Mar 11 19:44 /dev/vg1/lv1 -> ../dm-2
lrwxrwxrwx. 1 root root 7 Mar 11 19:51 /dev/vg1/lv2 -> ../dm-3
可以看到lv1和lv2都是软连接,他们实际是dm-2和dm-3
删除lv
[root@bogon ~]# lvremove /dev/vg1/lv2
Do you really want to remove active logical volume vg1/lv2? [y/n]: y
Logical volume "lv2" successfully removed.
需要指定一个完整的目录,这个操作很危险(凡是涉及到删除,都需要谨慎操作)
扩容lv
格式化逻辑卷
格式化的方式和标准分区的一样,使用mkfs命令选择需要的文件系统进行格式化
[root@bogon ~]# mkfs.ext4 /dev/vg1/lv1
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 579c7761-640d-43a9-a459-a8319424edc2
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
[root@bogon ~]# mkfs.xfs /dev/vg1/lv2
meta-data=/dev/vg1/lv2 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
在这里lv1使用了ext4文件系统,lv2使用了xfs文件系统
查看uuid
[root@bogon ~]# ls /dev/disk/by-uuid/ -l
total 0
lrwxrwxrwx. 1 root root 10 Mar 11 19:05 0d703ec7-1bac-436c-a2ee-548e1325e6d4 -> ../../dm-0
lrwxrwxrwx. 1 root root 9 Mar 11 19:05 2023-11-12-21-32-45-00 -> ../../sr0
lrwxrwxrwx. 1 root root 10 Mar 11 20:00 579c7761-640d-43a9-a459-a8319424edc2 -> ../../dm-2
lrwxrwxrwx. 1 root root 10 Mar 11 19:05 e143f66c-be64-4ca4-8898-baaf9cfaa163 -> ../../dm-1
lrwxrwxrwx. 1 root root 15 Mar 11 19:05 ff7ef47f-0497-4981-a78b-12021b9bebb4 -> ../../nvme0n1p1
进行挂载
这里演示的是临时挂载,如果需要永久性挂载,则需要写入fstab中(这里就不演式了,如果需要可以看,可以看上一篇文章Linux磁盘分区————标准分区-CSDN博客)
[root@bogon ~]# mkdir /mnt/vg1_ext4 /mnt/vg1_xfs
[root@bogon ~]# mount /dev/vg1/lv1 /mnt/vg1_ext4/
[root@bogon ~]# mount /dev/vg1/lv2 /mnt/vg1_xfs/
[root@bogon ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 724M 9.1M 715M 2% /run
/dev/mapper/rl_bogon-root xfs 46G 1.8G 44G 4% /
/dev/nvme0n1p1 xfs 960M 257M 704M 27% /boot
tmpfs tmpfs 362M 0 362M 0% /run/user/0
/dev/mapper/vg1-lv1 ext4 974M 24K 907M 1% /mnt/vg1_ext4
/dev/mapper/vg1-lv2 xfs 960M 39M 922M 5% /mnt/vg1_xfs
在线扩容lv
lv在使用的情况下,文件系统中有内容,就不能进行卸载,就需要在线进行lv的扩容
[root@bogon ~]# lvextend /dev/vg1/lv1 -L 1.5G
Size of logical volume vg1/lv1 changed from 1.00 GiB (128 extents) to 1.50 GiB (192 extents).
Logical volume vg1/lv1 successfully resized.
[root@bogon ~]# lvextend /dev/vg1/lv2 -L 1.2G
Rounding size to boundary between physical extents: 1.20 GiB.
Size of logical volume vg1/lv2 changed from 1.00 GiB (128 extents) to 1.20 GiB (154 extents).
Logical volume vg1/lv2 successfully resized.
[root@bogon ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 1.8G 0 1.8G 0% /dev/shm
tmpfs tmpfs 724M 9.1M 715M 2% /run
/dev/mapper/rl_bogon-root xfs 46G 1.8G 44G 4% /
/dev/nvme0n1p1 xfs 960M 257M 704M 27% /boot
tmpfs tmpfs 362M 0 362M 0% /run/user/0
/dev/mapper/vg1-lv1 ext4 974M 24K 907M 1% /mnt/vg1_ext4
/dev/mapper/vg1-lv2 xfs 960M 39M 922M 5% /mnt/vg1_xfs
在扩容之后,扩容的部分仍然不能用,这是因为扩容的部分没有进行格式化,所以在文件系统上检测不到
每种文件系统的扩容方式可能会有所不同
## ext4将扩容的空间加入到文件系统
[root@bogon ~]# resize2fs /dev/vg1/lv1
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/vg1/lv1 is mounted on /mnt/vg1_ext4; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vg1/lv1 is now 393216 (4k) blocks long.
## xfs将扩容的空间加入到文件系统,只能指定挂载点
[root@bogon ~]# xfs_growfs /mnt/vg1_xfs/
meta-data=/dev/mapper/vg1-lv2 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1 bigtime=1 inobtcount=1 nrext64=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=16384, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 262144 to 315392