使用4块6TB SCSI硬盘 搭建LVM存储池

LVM 架构图

清除硬盘分区信息

1
2
3
4
5
6
[root@localhost ~]# dd if=/dev/zero bs=512K count=1 of=/dev/sdc   #清空硬盘的分区表
[root@localhost ~]# dd if=/dev/zero bs=512K count=1 of=/dev/sdd #清空硬盘的分区表
[root@localhost ~]# dd if=/dev/zero bs=512K count=1 of=/dev/sde #清空硬盘的分区表
[root@localhost ~]# dd if=/dev/zero bs=512K count=1 of=/dev/sdf #清空硬盘的分区表
[root@localhost ~]# sync #将缓存的数据刷新到磁碟上
[root@localhost ~]# partprobe #通知内核更新分区表

创建PV physical volume ,物理卷在逻辑卷管理系统最底层,可为整个物理硬盘或实际物理硬盘上的分区。

1
2
3
4
5
6
[root@localhost ~]# pvcreate /dev/sdc /dev/sdd /dev/sde /dev/sdf
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created.
Physical volume "/dev/sde" successfully created.
Physical volume "/dev/sdf" successfully created.
[root@localhost ~]#

如果想撤销对某块硬盘的操作

1
[root@localhost ~]# pvremove /dev/sd?    #删除物理卷

验证PV:

1
2
3
4
5
6
7
8
9
其中 /dev/sdc, /dev/sdd, /dev/sde, /dev/sdf 是我刚创建的
[root@localhost ~]# pvscan
PV /dev/sdb2 VG centos lvm2 [<464.76 GiB / 4.00 MiB free]
PV /dev/sdd lvm2 [<5.46 TiB]
PV /dev/sde lvm2 [<5.46 TiB]
PV /dev/sdf lvm2 [<5.46 TiB]
PV /dev/sdc lvm2 [<5.46 TiB]
Total: 5 [<22.29 TiB] / in use: 1 [<464.76 GiB] / in no VG: 4 [21.83 TiB]
[root@localhost ~]#

创建 卷组

Volume Groups

1
2
3
[root@localhost ~]# vgcreate data_pool /dev/sdc /dev/sdd /dev/sde /dev/sdf   #将4个PV纳入一个存储池, 池子名字为 data_pool
Volume group "data_pool" successfully created
[root@localhost ~]#

验证:

1
2
3
4
5
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 3 0 wz--n- <464.76g 4.00m
data_pool 4 0 0 wz--n- 21.83t 21.83t
[root@localhost ~]#

如果想删除卷组

1
[root@localhost ~]# vgremove data_pool # 指定卷组的名字

创建逻辑卷 logical volume

一个卷组可以分割为很多个逻辑卷, 此时我只需要一个逻辑卷, 占用卷组的全部空间

1
2
3
[root@localhost ~]# lvcreate --extents 100%FREE --name LV_Data data_pool
Logical volume "LV_Data" created.
[root@localhost ~]#

验证

1
2
3
4
5
6
7
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
home centos -wi-ao---- 407.00g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- 7.75g
LV_Data data_pool -wi-a----- 21.83t # 可以看到 逻辑卷LV_Data 隶属于data_pool卷组, 其容量为21.83TB
[root@localhost ~]#

如果创建的不够满意, 可以删除

1
[root@localhost ~]# lvremove -y  /dev/data_pool/LV_Data

使用逻辑卷 LV_Data

初始化 逻辑卷 LV_Data, 将其格式化为 ext4 文件系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@localhost ~]# mkfs.ext4 /dev/data_pool/LV_Data
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
366282752 inodes, 5860519936 blocks
293025996 blocks (5.00%) reserved for the super user
First data block=0
178849 block groups
32768 blocks per group, 32768 fragments per group
2048 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, 550731776, 644972544, 1934917632,
2560000000, 3855122432, 5804752896

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

[root@localhost ~]#

挂载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# mkdir /mnt/LV_Data
[root@localhost ~]# mount /dev/data_pool/LV_Data /mnt/LV_Data
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 50G 2.1G 48G 5% /
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 3.8G 14M 3.8G 1% /run
tmpfs tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/sdb1 xfs 1014M 143M 872M 15% /boot
/dev/mapper/centos-home xfs 407G 1.1G 406G 1% /home
tmpfs tmpfs 771M 0 771M 0% /run/user/1000
tmpfs tmpfs 771M 0 771M 0% /run/user/0
/dev/mapper/data_pool-LV_Data ext4 22T 20K 21T 1% /mnt/LV_Data
[root@localhost ~]#

开机自动挂载

1
2
3
4
5
6
7
8
[root@localhost ~]# umount /dev/data_pool/LV_Data
[root@localhost ~]# mkdir /mnt/sas_disk/
[root@localhost hexo]# blkid /dev/SAS_pool/SAS_LVM
/dev/SAS_pool/SAS_LVM: UUID="6ead9007-eceb-4b92-af74-bf376b9baa3a" TYPE="xfs"
[root@localhost hexo]#
[root@localhost ~]# echo "UUID=6ead9007-eceb-4b92-af74-bf376b9baa3a /mnt/sas_disk/ xfs defaults 0 0" >> /etc/fatab
[root@localhost ~]# mount -a
[root@localhost ~]# df -h

验证

1
2
[root@localhost ~]# reboot
[root@localhost ~]# df -h

缩小LVM

卸载已挂载的 LV

1
[root@localhost ~]# umount /dev/data_pool/LV_Data

EXT4 文件系统缩小(XFS文件系统不支持缩小操作)

1
2
3
4
[root@localhost ~]# resize2fs -f /dev/data_pool/LV_Data  15T            #速度极慢, 22T已用20K 缩小为15T 耗时10分钟, 大家可以参考一下
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/data_pool/LV_Data to 4026531840 (4k) blocks.
The filesystem on /dev/data_pool/LV_Data is now 4026531840 blocks long.

调整LV大小

1
[root@localhost ~]# lvresize -L 15T /dev/data_pool/LV_Data

查询操作

使用 lvs 查看 调整之后lv 大小

1
2
3
4
5
6
7
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
SAS_LVM SAS_pool -wi-ao---- 21.83t
home centos -wi-ao---- 407.00g
root centos -wi-ao---- 50.00g
swap centos -wi-ao---- 7.75g
[root@localhost ~]#

使用vgs 查看存储池 剩余

1
2
3
4
5
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
SAS_pool 4 1 0 wz--n- 21.83t 0
centos 1 3 0 wz--n- <464.76g 4.00m
[root@localhost ~]#

使用pvs 查看参与存储池的硬盘

1
2
3
4
5
6
7
8
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb2 centos lvm2 a-- <464.76g 4.00m
/dev/sdc SAS_pool lvm2 a-- <5.46t 0
/dev/sdd SAS_pool lvm2 a-- <5.46t 0
/dev/sde SAS_pool lvm2 a-- <5.46t 0
/dev/sdf SAS_pool lvm2 a-- <5.46t 0
[root@localhost ~]#