LVM挂载数据盘

PV 物理卷

  • 物理卷,Physical Volume,是LVM机制的基本存储设备,通常对应一个普通分区或是整个硬盘。
  • 创建物理卷时,会在分区或磁盘头部创建一个用于记录LVM属性的保留区块,并把存储空间分割成默认大小为4MB的基本单元(Physical Extend,PE),从而构成物理卷。
  • 普通分区先转换分区类型为8e;整块硬盘,可以将所有的空间划分为一个主分区再做调整。

VG 卷组

卷组,Volume Group,是由一个或多个物理卷组成的一个整体。可以动态添加、移除物理卷,创建时可以指定PE大小。

LV 逻辑卷

逻辑卷,Logical Volume,建立在卷组之上,与物理卷没有直接关系。格式化后,即可挂载使用。

三者关系


通过以上对三者的解释可以看出,建立LVM的过程。首先,将普通分区或整个硬盘创建为物理卷;然后,将一个或多个物理卷创建为卷组;最后,在卷组上分割不同的数据存储空间形成逻辑卷。有了逻辑卷,就可以格式化、挂载使用了。

LVM 管理

常用命令

功能 PV 管理命令 VG 管理命令 LV 管理命令
Scan(扫描) pvscan vgscan lvscan
Create(建立) pvcreate vgcreate lvcreate
Display(显示) pvdisplay vgdisplay lvdisplay
Remove(移除) pvremove vgremove lvremove
Extend(扩展) / vgextend lvextend
Reduce(减少) / vgreduce lvreduce

查看硬盘分区
fdisk -l

使用 fdisk 工具对硬盘进行分区,如果磁盘分区大于2T,需要使用 parted 工具进行分区

[root@localhost /]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.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
Building a new DOS disklabel with disk identifier 0x4da2e52d.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-209717247, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-209717247, default 209717247): 107375230976
Partition 1 of type Linux and of size 511 MiB is set

Command (m for help): t         //修改分区格式
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107375230976 bytes, 209717248 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
Disk label type: dos
Disk identifier: 0x4da2e52d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     1048576      523264+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

使新分区生效,无需重启:[root@localhost /]# partprobe

使用新的分区来创建PV

[root@localhost /]# pvcreate /dev/sdb1    
  Physical volume "/dev/sdb1" successfully created.

删除PV:pvremove /dev/sdb1

创建卷组VG,给VG添加成员

[root@localhost /]# vgcreate data-group /dev/sda1  //data-group 为卷组名字
[root@localhost /]# pvs
  PV         VG         Fmt  Attr PSize    PFree   
  /dev/sda1  data-group lvm2 a--  <300.00g 1020.00m
  /dev/sdb1             lvm2 ---   100.00g  100.00g
  /dev/vda2  centos     lvm2 a--   <99.00g       0 
//给VG添加成员,将 /dev/sdb1加入卷组data-group
[root@localhost /]# vgextend data-group  /dev/sdb1   
 Volume group "data-group" successfully extended

删除成员: vgreduce data-group /dev/sdb1

创建逻辑卷 LV

[root@localhost /]# lvcreate -L 100G -n data1 data-group
  Logical volume "data1" created.
[root@localhost /]# 
[root@localhost /]# mkfs.xfs /dev/data-group/data1     //将逻辑卷格式化并指定文件类型xfs
meta-data=/dev/data-group/data1  isize=512    agcount=4, agsize=6553600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=26214400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=12800, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

取消逻辑卷:lvremove /dev/vg0/lv0

挂载逻辑卷->文件系统

[root@localhost /]#
[root@localhost /]# mkdir data1
[root@localhost /]# mount /dev/data-group/data1 /data1 取消挂载:umount /mnt/lv0


删除硬盘上的LVM分区

1、fdisk /dev/sdb
2、Command (m for help): p
3、Command (m for help): d
Selected partition 1
4、Command (m for help): p <--在显示分区情况,发现已经没了
5、 Command (m for help): w

把root 分区的空间扩大

逻辑卷扩容:lvextend –L +100g /dev/vol_name/lv_name (给/dev/vol_name/lv_name扩容100g)


将vg空间划入分区:lvextend -L +10G 分区名

lvextend -l +100%FREE /dev/centos/root

挂载报错问题解决:

[root@hyp-2 data]# mount /dev/mapper/data--group-data /data
mount: /dev/mapper/data--group-data is already mounted or /data busy
![](https://upload-images.jianshu.io/upload_images/11810968-f9f77cd6042c7510.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

[root@hyp-2 /]# mount /dev/mapper/data--group-data /data
mount: /dev/mapper/data--group-data is write-protected, mounting read-only
mount: unknown filesystem type '(null)'

解决方法:
[root@hyp-2 /]# mkfs.xfs /dev/data-group/data
mkfs.xfs: /dev/data-group/data appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[root@hyp-2 /]# mkfs.xfs -f /dev/data-group/data 注意:需要加上-f

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容