背景:

当前系统home分区空间不足,新增一块磁盘,对home分区进行在线扩容,前提是home分区是逻辑卷。

实施步骤:用虚拟机新增一块磁盘,然后对磁盘进行分区,对分区好的磁盘进行更改为lvm类型,进行格式化,然后创建物理卷,添加卷组,扩展逻辑卷组,同步到文件系统,至此扩容完成。

1、新增磁盘,重启系统,通过lsblk可以看见刚刚添加好的磁盘sdc,容量为5G,用于测试

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 18.5G  0 lvm  /
  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
sdb               8:16   0   10G  0 disk 
└─centos-home   253:2    0  9.5G  0 lvm  /home
sdc               8:32   0    5G  0 disk 
sr0              11:0    1 1024M  0 rom  

2、通过fdisk -l查看 /dev/sdc还未进行分区

[root@localhost ~]# fdisk -l

Disk /dev/sda: 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: 0x000021f1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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 /dev/mapper/centos-root: 19.9 GB, 19872612352 bytes, 38813696 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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 /dev/mapper/centos-home: 10.2 GB, 10200547328 bytes, 19922944 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

3、通过fdisk /dev/sdc进行分区,分配第一个区为2G,第二个为3G

[root@localhost ~]# fdisk /dev/sdc
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 0xe05d36b5.

Command (m for help): m      此处是列出相关的执行操作 
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): p    此处是打印当前分区信息,可以看出sdc无分区信息

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xe05d36b5

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n           此处n代表新建分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):                      此处默认主分区分区,不用输入直接回车
Using default response p
Partition number (1-4, default 1):        此处默认主分区分区数为1,不用输入直接回车
First sector (2048-10485759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-10485759, default 10485759): +2G  此处指定分区大小
Partition 1 of type Linux and of size 2 GiB is set


磁盘分第二个区

Command (m for help): p      打印当前磁盘分区信息

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xe05d36b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   83  Linux     磁盘已完成第一个分区

Command (m for help): n      创建分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p):    默认主分区,不用输入直接回车
Using default response p
Partition number (2-4, default 2):      默认分区数2,不用输入直接回车
First sector (4196352-10485759, default 4196352):     直接回车
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-10485759, default 10485759):     直接回车,默认将剩余空间分配第二个区
Using default value 10485759
Partition 2 of type Linux and of size 3 GiB is set

Command (m for help): p      打印当前磁盘分区信息

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xe05d36b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   83  Linux
/dev/sdc2         4196352    10485759     3144704   83  Linux     第二个区已分好

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# fdisk -l              查看磁盘信息

Disk /dev/sda: 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: 0x000021f1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xe05d36b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   83  Linux
/dev/sdc2         4196352    10485759     3144704   83  Linux  

Disk /dev/mapper/centos-root: 19.9 GB, 19872612352 bytes, 38813696 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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 /dev/mapper/centos-home: 10.2 GB, 10200547328 bytes, 19922944 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

[root@localhost ~]# lsblk    查看磁盘信息
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0  500M  0 part /boot
└─sda2            8:2    0 19.5G  0 part 
  ├─centos-root 253:0    0 18.5G  0 lvm  /
  └─centos-swap 253:1    0    1G  0 lvm  [SWAP]
sdb               8:16   0   10G  0 disk 
└─centos-home   253:2    0  9.5G  0 lvm  /home
sdc               8:32   0    5G  0 disk 
├─sdc1            8:33   0    2G  0 part 
└─sdc2            8:34   0    3G  0 part    此时sdc2还不是lvm类型

sr0              11:0    1 1024M  0 rom  

3、将sdc2更改为lvm类型

[root@localhost ~]# fdisk /dev/sdc        对sdc再次进行操作
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.

Command (m for help): p   列出磁盘分区信息

Disk /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xe05d36b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   83  Linux
/dev/sdc2         4196352    10485759     3144704   83  Linux

Command (m for help): m       列出操作指南
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): t    更改分区系统id
Partition number (1,2, default 2): 2       指定第二个分区
Hex code (type L to list all codes): l      列出系统id

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 8e                 指定系统id
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): w                               保存操作
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.     
[root@localhost ~]# fdisk -l                查看磁盘信息

Disk /dev/sda: 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: 0x000021f1

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    41943039    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/sdc: 5368 MB, 5368709120 bytes, 10485760 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: 0xe05d36b5

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048     4196351     2097152   83  Linux
/dev/sdc2         4196352    10485759     3144704   8e  Linux LVM        此时分区2已变为lvm类型

Disk /dev/mapper/centos-root: 19.9 GB, 19872612352 bytes, 38813696 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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 /dev/mapper/centos-home: 10.2 GB, 10200547328 bytes, 19922944 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


[root@localhost ~]# mkfs -t xfs /dev/sdc2        对/dev/sdc2进行格式化
meta-data=/dev/sdc2              isize=512    agcount=4, agsize=196544 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=786176, 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

4、创建物理卷

[root@localhost ~]# pvs           查看物理卷
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <19.51g      0 
  /dev/sdb   centos lvm2 a--  <10.00g 508.00m
[root@localhost ~]# pvcreate /dev/sdc2          创建物理卷
WARNING: xfs signature detected on /dev/sdc2 at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdc2.
  Physical volume "/dev/sdc2" successfully created.
[root@localhost ~]# pvs                       查看物理卷
  PV         VG     Fmt  Attr PSize   PFree  
  /dev/sda2  centos lvm2 a--  <19.51g      0 
  /dev/sdb   centos lvm2 a--  <10.00g 508.00m
  /dev/sdc2         lvm2 ---   <3.00g  <3.00g

5、添加卷组

[root@localhost ~]# lvs   查看逻辑卷,我们的目标是扩充home卷,可以看出home卷所在卷组为centos
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao----   9.50g                                                    
  root centos -wi-ao---- <18.51g                                                    
  swap centos -wi-ao----   1.00g  

[root@localhost ~]# vgs        查看卷组
  VG     #PV #LV #SN Attr   VSize  VFree  
  centos   2   3   0 wz--n- 29.50g 508.00m
[root@localhost ~]# vgextend centos /dev/sdc2           将刚创建好的物理卷添加centos卷组
  Volume group "centos" successfully extended
[root@localhost ~]# vgs  查看卷组
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   3   3   0 wz--n- 32.50g 3.49g

6、扩展逻辑卷

[root@localhost ~]# df -h         查看当前分区空间信息         
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   19G  2.6G   16G  14% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1                497M  126M  372M  26% /boot
/dev/mapper/centos-home  9.5G   33M  9.5G   1% /home       此时home空间9.5G
tmpfs                     98M     0   98M   0% /run/user/0
[root@localhost ~]# lvextend -l +100%FREE /dev/centos/home   扩展逻辑卷home
  Size of logical volume centos/home changed from 9.50 GiB (2432 extents) to 12.99 GiB (3326 extents).
  Logical volume centos/home successfully resized.
[root@localhost ~]# lvs   查看逻辑卷信息
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  home centos -wi-ao----  12.99g        此时home卷容量已增加,但是home分区空间还未增加,需同步文件系统                                      
  root centos -wi-ao---- <18.51g                                                    
  swap centos -wi-ao----   1.00g            

   7、将逻辑卷增加容量同步至文件系统           

                         
[root@localhost ~]# xfs_growfs /dev/centos/home    将逻辑卷增加容量同步至文件系统 
meta-data=/dev/mapper/centos-home isize=512    agcount=4, agsize=622592 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=2490368, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2490368 to 3405824
[root@localhost ~]# df -h   查看分区空间信息
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   19G  2.6G   16G  14% /
devtmpfs                 475M     0  475M   0% /dev
tmpfs                    487M     0  487M   0% /dev/shm
tmpfs                    487M  7.7M  479M   2% /run
tmpfs                    487M     0  487M   0% /sys/fs/cgroup
/dev/sda1                497M  126M  372M  26% /boot
/dev/mapper/centos-home   13G   33M   13G   1% /home    此时home分区已空间增加,从原来的9.5G增加至现在的13G
tmpfs                     98M     0   98M   0% /run/user/0