钢铁知识库,一个学习python爬虫、数据分析的知识库。在职运维,又喜欢开发。
什么是lvm逻辑卷
LVM(逻辑卷管理)是一种在Linux环境中广泛使用的磁盘管理技术。它允许管理员创建逻辑上的磁盘分区,这些分区可以跨越多个物理硬盘。使用LVM的主要优点包括:
- 弹性和灵活性:LVM允许在不中断服务的情况下动态地调整分区大小。这对于需要频繁调整存储空间的环境非常有用。
- 简化管理:通过LVM,管理员可以更容易地管理大量的硬盘空间,特别是在多硬盘系统中。
- 高级功能:LVM支持高级功能,如快照(用于备份或恢复)、镜像(增强数据可靠性)和条带化(提高性能)。
- 卷组和逻辑卷:在LVM中,物理硬盘被分成物理卷(PVs),这些物理卷被组合成卷组(VGs)。在卷组内部,可以创建多个逻辑卷(LVs),这些逻辑卷就像传统的磁盘分区,但它们提供了更多的灵活性。
简而言之,LVM提供了一种高效和灵活的方式来管理Linux系统上的磁盘空间,使得存储管理更加高效和灵活。
如何创建一个新逻辑卷
随着业务增长,现有的磁盘空间容量开始变得紧张,这时需要申请磁盘扩容。接下来我们通过主申请好的磁盘,来演示一次创建新逻辑卷的全过程。可以看到/dev/sdb是一块新的硬盘。
执行fdisk开始对这块硬盘进行格式化,创建一个主分区,并且格式化为Linux LVM,操作命令如下:
[root@localhost ~]# fdisk /dev/sdbWelcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xa89470a5.Command (m for help): n
Partition typep primary (0 primary, 0 extended, 4 free)e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857599, default 104857599): Created a new partition 1 of type 'Linux' and of size 50 GiB.Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): L00 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
01 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
02 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
03 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
04 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
05 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
06 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
07 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
08 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
09 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
0a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
0b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
0c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Linux extended
0e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
0f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT Aliases:linux - 83swap - 82extended - 05uefi - EFraid - FDlvm - 8Elinuxex - 85
Hex code or alias (type L to list all): 8e
Changed type of partition 'Linux' to 'Linux LVM'.Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
通过上面操作可以看到已经将这块硬盘进行了格式化,下一步开始建立物理卷、逻辑卷
## 向内核注册分区
partprobe /dev/sdb## 创建物理卷
[root@localhost ~]# pvcreate /dev/sdb1Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 56G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 55G 0 part ├─openeuler-root 253:0 0 33.2G 0 lvm /├─openeuler-swap 253:1 0 5.6G 0 lvm [SWAP]└─openeuler-home 253:2 0 16.2G 0 lvm /home
sdb 8:16 0 50G 0 disk
└─sdb1 8:17 0 50G 0 part
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 openeuler lvm2 a-- <55.00g 0 /dev/sdb1 lvm2 --- <50.00g <50.00g## 创建卷组,卷组名为data
[root@localhost ~]# vgcreate data /dev/sdb1Volume group "data" successfully created
[root@localhost ~]# vgsVG #PV #LV #SN Attr VSize VFree data 1 0 0 wz--n- <50.00g <50.00gopeneuler 1 3 0 wz--n- <55.00g 0 ## 创建逻辑卷,-l表示使用多少空间,-n表示逻辑卷名
[root@localhost ~]# lvcreate -l +100%FREE -n lv_data dataLogical volume "lv_data" created.
[root@localhost ~]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertlv_data data -wi-a----- <50.00g home openeuler -wi-ao---- 16.20g root openeuler -wi-ao---- 33.19g swap openeuler -wi-ao---- 5.60g
完成后就可以格式化,然后进行挂载
## 格式化文件系统并挂载
[root@localhost ~]# mkfs.ext4 /dev/data/lv_data
mke2fs 1.46.4 (18-Aug-2021)
Creating filesystem with 13106176 4k blocks and 3276800 inodes
Filesystem UUID: 29c9ec75-4410-47fd-be1b-6ebd2c50895a
Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424Allocating group tables: done
Writing inode tables: done
Creating journal (65536 blocks): done
Writing superblocks and filesystem accounting information: done [root@localhost ~]# lvsLV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convertlv_data data -wi-a----- <50.00g home openeuler -wi-ao---- 16.20g root openeuler -wi-ao---- 33.19g swap openeuler -wi-ao---- 5.60g ## 挂载
[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/data/lv_data /data
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.7G 0 3.7G 0% /dev
tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs tmpfs 1.5G 153M 1.4G 11% /run
tmpfs tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup
/dev/mapper/openeuler-root ext4 33G 2.4G 29G 8% /
tmpfs tmpfs 3.7G 0 3.7G 0% /tmp
/dev/sda1 ext4 974M 87M 820M 10% /boot
/dev/mapper/openeuler-home ext4 16G 203M 15G 2% /home
/dev/mapper/data-lv_data ext4 49G 24K 47G 1% /data## 加入到开机自启动
# vi /etc/fstab
/dev/data/lv_data /data ext4 defaults 0 0
以上就完成了对逻辑的创建,有需求的朋友直接按上面一步步跟着操作就可以了,对磁盘做逻辑卷主要还是方便后面扩容。
如何扩容逻辑卷
逻辑卷创建后,如果业务有增长后面肯定需要再进行扩容,下面就开始演示对现有的逻辑卷进行扩容。还是拿上面那块sdb盘来测试
## 创建物理卷
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 56G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 55G 0 part ├─openeuler-root 253:0 0 33.2G 0 lvm /├─openeuler-swap 253:1 0 5.6G 0 lvm [SWAP]└─openeuler-home 253:2 0 16.2G 0 lvm /home
sdb 8:16 0 50G 0 disk
└─sdb1 8:17 0 50G 0 part
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvsPV VG Fmt Attr PSize PFree /dev/sda2 openeuler lvm2 a-- <55.00g 0 /dev/sdb1 lvm2 --- <50.00g <50.00g## 将新增的pv加入到已有的vg
[root@localhost ~]# vgsVG #PV #LV #SN Attr VSize VFreeopeneuler 1 3 0 wz--n- <55.00g 0
[root@localhost ~]# vgextend openeuler /dev/sdb1 Volume group "openeuler" successfully extended## 加入vg后可以看到有VFree空间,通过逻辑卷扩容所有空闲分区
[root@localhost ~]# vgsVG #PV #LV #SN Attr VSize VFree openeuler 2 3 0 wz--n- 104.99g <50.00g[root@localhost ~]# lvextend -l +100%FREE /dev/mapper/openeuler-rootSize of logical volume openeuler/root changed from 33.19 GiB (8497 extents) to <83.19 GiB (21296 extents).Logical volume openeuler/root successfully resized.## 扩容成功后还需要执行以下命令都会生效
## 如果是xfs文件系统使用:xfs_growfs /dev/mapper/openeuler-root
[root@localhost ~]# resize2fs /dev/mapper/openeuler-root
resize2fs 1.46.4 (18-Aug-2021)
Filesystem at /dev/mapper/openeuler-root is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 11
The filesystem on /dev/mapper/openeuler-root is now 21807104 (4k) blocks long.
可以看到根分区已经由原来的33G扩容到82G,说明50G已经成功扩容。
[root@localhost ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 3.7G 0 3.7G 0% /dev
tmpfs tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs tmpfs 1.5G 153M 1.4G 11% /run
tmpfs tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup
/dev/mapper/openeuler-root ext4 82G 2.4G 76G 4% /
tmpfs tmpfs 3.7G 0 3.7G 0% /tmp
/dev/sda1 ext4 974M 87M 820M 10% /boot
/dev/mapper/openeuler-home ext4 16G 203M 15G 2% /home
---- 钢铁 648403020@qq.com 2024.01.23
参考链接
https://www.cnblogs.com/biyulin-bky/p/16144950.html