linux lvm

发布时间 2023-11-29 09:45:17作者: xiaoheiDTF

1 LVM是什么
1.1 概念解释
LVM(Logical Volume Manager), 逻辑卷管理, 是一种将一至多个硬盘的分区在逻辑上进行组合, 当成一个大硬盘来使用.

当硬盘空间不足时, 可以动态地添加其它硬盘的分区到已有的卷组中 —— 磁盘空间的动态管理.

1.2 为什么用LVM
LVM通常用于装备大量磁盘的系统, 比如服务器中的磁盘阵列.

但LVM同样适用于仅有一、两块硬盘的小系统.

1.2.1 不使用LVM时的扩容思路
传统的文件系统是基于分区的, 一个文件系统对应一个分区, 这种方式比较直观, 但不易改变:

(1) 不同的分区相互独立, 单独的文件不能跨分区存储, 容易出现硬盘的利用率不均衡;
(2) 当一个文件系统/分区装满时, 是不能对其进行扩容的, 只能采用重新分区/建立文件系统, 重新分区会丢失数据, 就要:

① 做数据的迁移和备份;
② 或者把分区中的数据移到另一个更大的分区中;
③ 或者采用符号连接的方式使用其它分区的空间 —— 都非常麻烦;

(3) 如果要把硬盘上的多个分区合并在一起使用, 只能采用重新分区的方式, —— 需要做好数据的备份与恢复.

1.2.2 使用LVM时的扩容思路
使用LVM时技术时, 情况有所不同:

(1) 硬盘的多个分区由LVM统一管理为卷组, 可以很轻松地加入或移走某个分区 —— 也就是扩大或减小卷组的可用容量, 充分利用硬盘空间;
(2) 文件系统建立在逻辑卷上, 而逻辑卷可以根据需要改变大小(在卷组容量范围内)以满足要求;
(3) 文件系统建立在LVM上, 可以跨分区存储访问, 更加方便;

强烈建议对拥有多个磁盘的系统, 使用LVM管理磁盘.

1.3 名词解释
PV(Physical Volume): 物理卷, 处于LVM最底层, 可以是物理硬盘或者分区;

PP(Physical Extend): 物理区域, PV中可以用于分配的最小存储单元, 可以在创建PV的时候指定, 如1M, 2M, 4M, 8M…..组成同一VG中所有PV的PE大小应该相同;

VG(Volume Group): 卷组, 建立在PV之上, 可以含有一个到多个PV;

LV(Logical Volume): 逻辑卷, 建立在VG之上, 相当于原来分区的概念, 不过大小可以动态改变.

2 普通的挂载磁盘方法
2.1 创建分区的主要操作
(1) 查看分区情况 - fdisk -l

[root@localhost ~]# fdisk -l

Disk /dev/sda: 299.0 GB, 298999349248 bytes # 磁盘/dev/sda
255 heads, 63 sectors/track, 36351 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4d69fe0e

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux # 分为2个区, sda1
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 36352 291785728 8e Linux LVM # sda2

磁盘/dev/sdb没有分区

Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

......
(2) 查看已有磁盘 - lsblk

[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 278.5G 0 disk
├─sda1 8:1 0 200M 0 part /boot
└─sda2 8:2 0 278.3G 0 part
└─VolGroup-LogVol (dm-0) 253:0 0 1.9T 0 lvm / # LVM类型的分区
sdb 8:32 0 3.7T 0 disk # 还没有分区的新磁盘
(3) 对新磁盘进行分区 - fdisk /dev/sdb

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf91f8c4c.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: The size of this disk is 4.0 TB (4000225165312 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
partition table format (GPT).

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n # n 表示新建分区
Command action
e extended
p primary partition (1-4)
p # p 表示分区类型为主分区, 主分区只有1-4种选择
Partition number (1-4): 1 # 主分区的编号
First cylinder (1-486333, default 1): # 开始扇区号, 直接回车, 使用默认值1
Using default value 1

结束扇区号, 使用默认值 --- 这里只加载了新磁盘的一半(2T), 所以还需要再次创建分区/dev/sdb2使用剩下的一半.

Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349):
Using default value 267349

Command (m for help): w # 将上述设置写入分区表并退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
(4) 再次查看分区情况 - fdisk -l

多出来一个/dev/sdb1的区, 这个1就是之前主分区之后指定的分区编号.

[root@localhost ~]# fdisk -l

Disk /dev/sda: 299.0 GB, 298999349248 bytes
255 heads, 63 sectors/track, 36351 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4d69fe0e

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 36352 291785728 8e Linux LVM

/dev/sdb磁盘:

Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8f3043b5

多出来的分区/dev/sdb1

Device Boot Start End Blocks Id System
/dev/sdb1 1 267349 2147480811 83 Linux

......
(5) 查看当前分区表中的分区信息 - cat /proc/partitions

[root@localhost ~]# cat /proc/partitions
major minor #blocks name

8 0 291991552 sda
8 1 204800 sda1
8 2 291785728 sda2
8 32 3906249728 sdb # 添加的新磁盘
8 33 2147480811 sdb1 # 创建的新分区
253 0 2046660608 dm-0
如果创建完之后,cat /proc/partitions 查看不到对应的分区, 使用 parprobe 刷新命令即可:

[root@localhost ~]# partprobe /dev/sdc
2.2 格式化新分区
(1) 格式化新分区 - mkfs -t

这里建议将新分区格式化为ext4文件类型, 还有ext2, ext3等文件类型, 区别请参考博客 ext2、ext3与ext4的区别 .

[root@localhost ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
134217728 inodes, 536870202 blocks
26843510 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
16384 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000

Writing inode tables: 8874/16384
(2) 等待一小会后, 将出现下述提示, 说明格式化完成:

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
2.3 挂载新分区
(1) 创建目录, 并将 /dev/sdb1挂在到该目录下:

[root@localhost /]# mkdir data && cd /data
[root@localhost data]# mount /dev/sdc1 /data
(2) 查看挂载是否成功:

[root@localhost data]# df -l
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VolGroup-LogVol 286901696 18601728 253726196 7% /
tmpfs 66020980 0 66020980 0% /dev/shm
/dev/sda1 495844 33476 436768 8% /boot

挂载成功:

/dev/sdb1 2113784984 202776 2006208168 1% /data
2.4 设置开机自动挂载
编辑文件 /etc/fstab:

[root@localhost data]# vim /etc/fstab

文件内容如下:

/etc/fstab

Created by anaconda on Wed Sep 12 10:41:40 2018

Accessible filesystems, by reference, are maintained under '/dev/disk'

See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

/dev/mapper/VolGroup-LogVol / ext4 defaults 1 1
/dev/sdb1 /data ext4 defaults 1 1
UUID=22b1d425-d050-43c3-a735-06d48bbb9051 /boot ext4 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0