LVM 原理
LVM (Logical volume Manager): 虚拟设备驱动,是在内核中块设备和物理设备之间添加的一个新的抽象层次, LVM 可以弹性的调整 文件系统的容量
LVM的实现原理:LVM 将几个实体的 partitions/disk 通过软件组合成一块独立的大磁盘VG,之后将这块大磁盘再经过分割称为可使用分割槽 LV后,将其格式化和挂载后即可以使用
LVM的具体实现 (ref: linux鸟哥的私房菜):
- 通过 fdisk 将 partition system id 改为 8e (LVM的标识符)
- 通过 pvcreate 将 partition 转换成 LVM 底层的 PV (Physical Volume)
- 通过 vgcreate 将PV 整合成 VG (Volume Group)
- 通过 lvcreate 将 VG 切分成 LV (Logical Volume)
- 通过 mkfs 将 LV 格式化、挂载 即可使用
- PE (Physical Extend),LVM的最小储存区块,LVM预设的 PE 为 4MB,PE 组成 LV 和 VG,因此 LV 和 VG 的大小和其包含的 PE 总数相关。LVM 的每个 VG 最多包含 65534 个 PE, 因此最大为 256GB
- LVM 的可弹性变更的 filesystem 容量是通过交换 PE 实现的,通过移除 LV 内部的 PE 以降低 LV 的容量,通过加入 PE 到 LV 中以加大容量
VMware 虚拟机中配置 LVM
-
在 vmware 中添加硬盘,用于需要配置的 LVM,详细可参见: https://docs.vmware.com/cn/VMware-Workstation-Pro/14.0/com.vmware.ws.using.doc/GUID-FB6F0D1B-6517-4F27-9AB5-93BF834D684D.html
添加后可以通过 lsblk 进行查看,e.g. 新添加了 10G 的 sdb 磁盘,如下:
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.7G 0 part /
sdb 8:16 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
- 通过 fdisk 将新加入的磁盘标记为 LVM
1. 指定需要分区的磁盘
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.32.1).
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 0xac408cac.
Command (m for help): m
Help:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition
F list free unpartitioned space
l list known partition types
n add a new partition
p print the partition table
t change a partition type
v verify the partition table
i print information about a partition
Misc
m print this menu
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
2. 添加一个分区
Command (m for help): n
Select, Partition number,First sector,Last sector 使用的均为默认值
Created a new partition 1 of type 'Linux' and of size 10 GiB.
3. 修改分区的类型
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden or c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f 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
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'.
4. 将所做的分区修改进行保存
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
5. 使用 fdisk -l 查看分区是否成功
[root@localhost ~]# fdisk -l
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xac408cac
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 20971519 20969472 10G 8e Linux LVM
- 通过 pvcreate 创建 PV
1. 通过 lsblk 查看分区表结构,分区类型为 LVM 但目前该磁盘仍属于分区状态
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.7G 0 part /
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part
sr0 11:0 1 1024M 0 rom
2. 创建 PV
[root@localhost ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
3. 查看创建的 PV
[root@localhost ~]# pvdisplay
"/dev/sdb1" is a new physical volume of "<10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size <10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID vdjNL2-O1Kq-vwdI-YnjQ-GSCS-gpoR-GwzjBB
- 创建 VG
[root@localhost ~]# vgcreate vgtest /dev/sdb1
Volume group "vgtest" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vgtest 1 0 0 wz--n- <10.00g <10.00g
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb1
VG Name vgtest
PV Size <10.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2559
Free PE 2559
Allocated PE 0
PV UUID vdjNL2-O1Kq-vwdI-YnjQ-GSCS-gpoR-GwzjBB
- 创建 LV
[root@localhost ~]# lvcreate -L 1G -n lvtest vgtest Logical volume "lvtest" created.
[root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lvtest vgtest -wi-a----- 1.00g
- 格式化 LV 并挂载使用
[root@localhost ~]# mkfs.xfs /dev/vgtest/lvtest
meta-data=/dev/vgtest/lvtest isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
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=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 300M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17.7G 0 part /
sdb 8:16 0 10G 0 disk
└─sdb1 8:17 0 10G 0 part
└─vgtest-lvtest 253:0 0 1G 0 lvm
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# mkdir -p /lvtest
[root@localhost /]# mount /dev/vgtest/lvtest /lvtest/
[root@localhost lvtest]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs 1.4G 0 1.4G 0% /dev/shm
tmpfs 1.4G 9.7M 1.4G 1% /run
tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda3 18G 12G 6.4G 65% /
/dev/sda1 295M 174M 122M 59% /boot
tmpfs 280M 1.2M 279M 1% /run/user/42
tmpfs 280M 5.7M 275M 3% /run/user/0
/dev/mapper/vgtest-lvtest 1014M 40M 975M 4% /lvtest
ref:
- 《linux 鸟哥私房菜》
- 虚拟机添加虚拟硬盘 https://docs.vmware.com/cn/VMware-Workstation-Pro/14.0/com.vmware.ws.using.doc/GUID-FB6F0D1B-6517-4F27-9AB5-93BF834D684D.html
- LVM 逻辑卷的创建 https://www.cnblogs.com/liumou-site/p/12118527.html