如何创建 LVM 并扩容磁盘


1 创建分区

列出磁盘信息:

[root@blogtest ~]# 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 0x5d6e3023.

Command (m for help): p

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x5d6e3023

   Device Boot      Start         End      Blocks   Id  System

新增分区:

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-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set

修改分区类型为LVM格式:

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: 21.5 GB, 21474836480 bytes, 41943040 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: 0x5d6e3023

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   8e  Linux LVM

保存退出:

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

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

命令解释:

  • p: 列出磁盘信息
  • n: 新增分区
  • t: 修改类型
  • w: 保存退出

2 创建逻辑卷

2.1 创建 PV

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

[root@blogtest ~]# pvs
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--   49.80g      0 
  /dev/sdb1         lvm2 ---  <20.00g <20.00g

[root@blogtest ~]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               49.80 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12749
  Free PE               0
  Allocated PE          12749
  PV UUID               rOHvj1-Idbb-ttD4-OHf9-MI7f-30yt-rs4E2m
   
  "/dev/sdb1" is a new physical volume of "<20.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               <20.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               HH7fGx-5jWL-XR6j-sDtY-YxGj-CbYa-cNcaPk

2.2 创建 VG

[root@blogtest ~]# vgcreate vgdata /dev/sdb1
  Volume group "vgdata" successfully created

[root@blogtest ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   1   2   0 wz--n-  49.80g      0 
  vgdata   1   0   0 wz--n- <20.00g <20.00g

[root@blogtest ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               49.80 GiB
  PE Size               4.00 MiB
  Total PE              12749
  Alloc PE / Size       12749 / 49.80 GiB
  Free  PE / Size       0 / 0   
  VG UUID               DSxgUK-vsl8-Qynw-keQs-03sU-AIub-Cq9FLg
   
  --- Volume group ---
  VG Name               vgdata
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <20.00 GiB
  PE Size               4.00 MiB
  Total PE              5119
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5119 / <20.00 GiB
  VG UUID               qC62a5-Gsxm-B1WG-o0HU-ffMI-75qo-BvHekg

2.3 创建 LV

[root@blogtest ~]# lvcreate -l 100%FREE -n lvdata vgdata
  Logical volume "lvdata" created.

[root@blogtest ~]# lvs
  LV     VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root   centos -wi-ao----  45.80g                                                    
  swap   centos -wi-ao----   4.00g                                                    
  lvdata vgdata -wi-a----- <20.00g                                                    

[root@blogtest ~]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                7IHY6N-m0hO-fTJe-iOk2-n6rB-N1Bn-0iiFhz
  LV Write Access        read/write
  LV Creation host, time blogtest, 2018-06-17 16:56:48 +0800
  LV Status              available
  # open                 2
  LV Size                4.00 GiB
  Current LE             1024
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                p6DRr1-4tG9-bhY6-2Jm6-ZwT8-oyUj-x6lQh2
  LV Write Access        read/write
  LV Creation host, time blogtest, 2018-06-17 16:56:48 +0800
  LV Status              available
  # open                 1
  LV Size                45.80 GiB
  Current LE             11725
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/vgdata/lvdata
  LV Name                lvdata
  VG Name                vgdata
  LV UUID                L58Ra5-0MPx-ubhO-Fp2R-QldW-8ibJ-AIm0FJ
  LV Write Access        read/write
  LV Creation host, time blogtest, 2018-06-30 18:55:36 +0800
  LV Status              available
  # open                 0
  LV Size                <20.00 GiB
  Current LE             5119
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

2.4 格式化 LV

[root@blogtest ~]# mkfs.xfs /dev/vgdata/lvdata 
meta-data=/dev/vgdata/lvdata     isize=512    agcount=4, agsize=1310464 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5241856, 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

2.5 挂载 LV

[root@blogtest ~]# mkdir /data

[root@blogtest ~]# mount -t xfs /dev/vgdata/lvdata /data

[root@blogtest ~]# df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/centos-root     46G 1021M   45G   3% /
devtmpfs                   901M     0  901M   0% /dev
tmpfs                      912M     0  912M   0% /dev/shm
tmpfs                      912M  8.6M  904M   1% /run
tmpfs                      912M     0  912M   0% /sys/fs/cgroup
/dev/sda1                  197M  121M   77M  62% /boot
tmpfs                      183M     0  183M   0% /run/user/0
/dev/mapper/vgdata-lvdata   20G   33M   20G   1% /data

[root@blogtest ~]# blkid /dev/sdb1
/dev/sdb1: UUID="HH7fGx-5jWL-XR6j-sDtY-YxGj-CbYa-cNcaPk" TYPE="LVM2_member" 

[root@blogtest ~]# echo "UUID=HH7fGx-5jWL-XR6j-sDtY-YxGj-CbYa-cNcaPk     /data    xfs    defaults    1    2" | tee -a /etc/fstab 
UUID=HH7fGx-5jWL-XR6j-sDtY-YxGj-CbYa-cNcaPk     /data    xfs    defaults    1    2

3 扩容

3.1 扩容 VG

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

[root@blogtest ~]# vgdisplay 
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               49.80 GiB
  PE Size               4.00 MiB
  Total PE              12749
  Alloc PE / Size       12749 / 49.80 GiB
  Free  PE / Size       0 / 0   
  VG UUID               DSxgUK-vsl8-Qynw-keQs-03sU-AIub-Cq9FLg
   
[root@blogtest ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

[root@blogtest ~]# vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  centos   2   2   0 wz--n- <69.80g <20.00g

3.2 扩容 LV

[root@blogtest ~]# lvextend -l +100%FREE /dev/centos/root 
  Size of logical volume centos/root changed from 45.80 GiB (11725 extents) to <65.80 GiB (16844 extents).
  Logical volume centos/root successfully resized.

[root@blogtest ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3001600 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12006400, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=5862, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12006400 to 17248256

[root@blogtest ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   66G 1021M   65G   2% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.6M  904M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1                197M  121M   77M  62% /boot
tmpfs                    183M     0  183M   0% /run/user/0

文章作者: Jason Liu
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Jason Liu !
  目录