首页 > Linux Shell > Linux命令大全 > linux下lvm操作详解
2019
01-05

linux下lvm操作详解

环境

操作系统:CentOS Linux release 7.6.1810 (Core)
硬盘设备:3块100G硬盘(/dev/sdb、/dev/sdc、/dev/sdd)
过程:/dev/sdb和/dev/sdb合成一块lvm挂载在/data/目录,之后加入/dev/sdd,扩容/data/目录大小。

操作过程
/dev/sdb分区

[root@laofuxi.com ~]# parted -s /dev/sdb mklabel gpt
[root@laofuxi.com ~]# parted -s /dev/sdb mkpart primary 0% 100%
[root@laofuxi.com ~]# parted -s /dev/sdb toggle 1 lvm
[root@laofuxi.com ~]# parted -s /dev/sdb print
Model: VMware Virtual disk (scsi)
Disk /dev/sdb: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  107GB  107GB               primary  lvm

不重启读取分区信息

[root@laofuxi.com ~]# partprobe

/dev/sdc分区

[root@laofuxi.com ~]# parted -s /dev/sdc mklabel gpt
[root@laofuxi.com ~]# parted -s /dev/sdc mkpart primary 0% 100%
[root@laofuxi.com ~]# parted -s /dev/sdc toggle 1 lvm
[root@laofuxi.com ~]# parted -s /dev/sdc print
Model: VMware Virtual disk (scsi)
Disk /dev/sdc: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  107GB  107GB               primary  lvm

不重启读取分区信息

[root@laofuxi.com ~]# partprobe

LVM创建部分
使用命令pvcreate创建物理卷

[root@laofuxi.com ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created.
[root@laofuxi.com ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.

查看创建好的物理卷

[root@laofuxi.com ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               data
  PV Size               <100.00 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              25599
  Free PE               25599
  Allocated PE          0
  PV UUID               K0D5uu-WAPJ-7bGy-RKfd-AvUL-MlbA-oRdkRe
   
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               data
  PV Size               <100.00 GiB / not usable 2.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              25599
  Free PE               25599
  Allocated PE          0
  PV UUID               t4WxmX-G7LA-Dr8i-7GSs-f6LF-9fMl-ax61Xg
   

创建卷组名为data,将物理卷加入卷组

[root@laofuxi.com ~]# vgcreate data /dev/sdb1 /dev/sdc1
  Volume group "data" successfully created

后面新加物理卷的时候也可以通过vgextend向data组添加物理卷

查看创建好的卷组

[root@laofuxi.com ~]# vgdisplay 
  --- Volume group ---
  VG Name               data
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               199.99 GiB
  PE Size               4.00 MiB
  Total PE              51198
  Alloc PE / Size       0 / 0   
  Free  PE / Size       51198 / 199.99 GiB
  VG UUID               gxRJ2l-94W4-mFgt-s5Iv-qiyO-V2mx-eV8B6i

创建完卷组后,就可以创建逻辑卷,所有卷组空间大小全部用于创建逻辑卷

[root@laofuxi.com ~]# lvcreate -n data01 -l 100%VG data 
  Logical volume "data01" created.

查看创建好的逻辑卷

[root@laofuxi.com ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/data/data01
  LV Name                data01
  VG Name                data
  LV UUID                Kxf3GX-XdeX-Q2ok-Zgpp-yLeU-k173-IjrIMV
  LV Write Access        read/write
  LV Creation host, time laofuxi.com, 2018-12-22 15:47:31 +0800
  LV Status              available
  # open                 0
  LV Size                199.99 GiB
  Current LE             51198
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
   

格式化分区

[root@laofuxi.com ~]# mkfs.xfs /dev/data/data01 
meta-data=/dev/data/data01       isize=512    agcount=4, agsize=13106688 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=52426752, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=25599, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@laofuxi.com ~]# 

创建挂载目录/data/

[root@laofuxi.com ~]# mkdir -pv /data/
mkdir: created directory ‘/data/’

查看存储设备UUID

[root@laofuxi.com ~]# blkid 
/dev/mapper/centos-root: UUID="400a6d43-fa78-480a-bfc2-4b7bdc13ed46" TYPE="xfs" 
/dev/sda2: UUID="J6yOLO-2TqX-VOU8-eZN4-0igH-3fQd-SKlccd" TYPE="LVM2_member" 
/dev/sr0: UUID="2018-11-25-21-21-31-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="ed02c768-3d81-42dd-ba0b-63a435a499f8" TYPE="xfs" 
/dev/mapper/centos-swap: UUID="5621c520-307f-4c68-9da6-feb8e6347ce1" TYPE="swap" 
/dev/mapper/centos-home: UUID="5fad35e8-a5be-40bd-8a0b-e2a9fce6075b" TYPE="xfs" 
/dev/sdb1: UUID="K0D5uu-WAPJ-7bGy-RKfd-AvUL-MlbA-oRdkRe" TYPE="LVM2_member" PARTLABEL="primary" PARTUUID="f1e6c9e9-af03-4806-a9ae-fc60bc40929f" 
/dev/sdc1: UUID="t4WxmX-G7LA-Dr8i-7GSs-f6LF-9fMl-ax61Xg" TYPE="LVM2_member" PARTLABEL="primary" PARTUUID="d8aa30cd-f7d5-47f4-8f12-7f1ac34748af" 
/dev/mapper/data-data01: UUID="16e7dd6c-8946-4998-980f-da07b8ec7374" TYPE="xfs" 

添加开机自动挂载信息

[root@laofuxi.com ~]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat Apr 15 05:43:07 2000
#
# 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/centos-root /                       xfs     defaults        0 0
UUID=ed02c768-3d81-42dd-ba0b-63a435a499f8 /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

# /dev/mapper/data-data01
UUID=16e7dd6c-8946-4998-980f-da07b8ec7374       /data   xfs     defaults        0 0

保存退出。

执行挂载操作

[root@laofuxi.com ~]# mount -a

查看挂载后的状态,总容量是200G

[root@laofuxi.com ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  2.1G   48G   5% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G   17M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  145M  870M  15% /boot
/dev/mapper/centos-home   42G   33M   42G   1% /home
tmpfs                    783M     0  783M   0% /run/user/0
/dev/mapper/data-data01  200G   33M  200G   1% /data

把/dev/sdd硬盘空间扩展到/data/目录里
/dev/sdd分区

[root@laofuxi.com ~]# parted -s /dev/sdd mklabel gpt
[root@laofuxi.com ~]# parted -s /dev/sdd mkpart primary 0% 100%
[root@laofuxi.com ~]# parted -s /dev/sdd toggle 1 lvm
[root@laofuxi.com ~]# parted -s /dev/sdd print
Model: VMware Virtual disk (scsi)
Disk /dev/sdd: 107GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End    Size   File system  Name     Flags
 1      1049kB  107GB  107GB               primary  lvm


使用命令pvcreate创建物理卷

[root@laofuxi.com ~]# pvcreate /dev/sdd1
  Physical volume "/dev/sdd1" successfully created.

查看创建好的物理卷

[root@laofuxi.com ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               data
  PV Size               <100.00 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              25599
  Free PE               0
  Allocated PE          25599
  PV UUID               K0D5uu-WAPJ-7bGy-RKfd-AvUL-MlbA-oRdkRe
   
  --- Physical volume ---
  PV Name               /dev/sdc1
  VG Name               data
  PV Size               <100.00 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              25599
  Free PE               0
  Allocated PE          25599
  PV UUID               t4WxmX-G7LA-Dr8i-7GSs-f6LF-9fMl-ax61Xg
   
  --- NEW Physical volume ---
  PV Name               /dev/sdd1
  VG Name               
  PV Size               <100.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               KQJM7w-1pU1-WCKo-mwzS-ygXZ-0x5U-RmSNWo
   

扩展VG:data
[root@laofuxi.com ~]# vgextend data /dev/sdd1
Volume group “data” successfully extended

查看扩展后的VG:data

[root@laofuxi.com ~]# vgdisplay 
  --- Volume group ---
  VG Name               data
  System ID             
  Format                lvm2
  Metadata Areas        3
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                3
  Act PV                3
  VG Size               <299.99 GiB
  PE Size               4.00 MiB
  Total PE              76797
  Alloc PE / Size       51198 / 199.99 GiB
  Free  PE / Size       25599 / <100.00 GiB
  VG UUID               gxRJ2l-94W4-mFgt-s5Iv-qiyO-V2mx-eV8B6i
   

扩展LV:data01

[root@laofuxi.com ~]# lvextend -l +100%FREE /dev/data/data01
  Size of logical volume data/data01 changed from 199.99 GiB (51198 extents) to <299.99 GiB (76797 extents).
  Logical volume data/data01 successfully resized.

查看扩展后的LV:data01

[root@laofuxi.com ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/data/data01
  LV Name                data01
  VG Name                data
  LV UUID                Kxf3GX-XdeX-Q2ok-Zgpp-yLeU-k173-IjrIMV
  LV Write Access        read/write
  LV Creation host, time laofuxi.com, 2018-12-22 15:47:31 +0800
  LV Status              available
  # open                 1
  LV Size                <299.99 GiB
  Current LE             76797
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3
   

xfs文件系统:同步文件系统

[root@laofuxi.com ~]# xfs_growfs /dev/data/data01 
meta-data=/dev/mapper/data-data01 isize=512    agcount=4, agsize=13106688 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=52426752, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=25599, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 52426752 to 78640128

附:
resize2fs命令 针对的是ext2、ext3、ext4文件系统
xfs_growfs命令 针对的是xfs文件系统

查看LV扩展后的容量

[root@laofuxi.com ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  2.1G   48G   5% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  8.9M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1               1014M  145M  870M  15% /boot
/dev/mapper/centos-home   42G   33M   42G   1% /home
/dev/mapper/data-data01  300G   33M  300G   1% /data
tmpfs                    783M     0  783M   0% /run/user/0

最后编辑:
作者:劳福喜博客
这个作者貌似有点懒,什么都没有留下。

留下一个回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据