详解Linux 下LVM扩展磁盘空间

将现有系统磁盘的空间进行扩展,需要磁盘管理方式为LVM,具体操作如下:
一、查看系统的信息
查看系统版本:

[root@test ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 

查看当前系统的磁盘情况:

[root@test ~]# fdisk -l

Disk /dev/sda: 322.1 GB, 322122547200 bytes, 629145600 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: 0x000c666c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048        6143        2048   83  Linux
/dev/sda2   *        6144     4200447     2097152   83  Linux
/dev/sda3         4200448   629145599   312472576   8e  Linux LVM

Disk /dev/mapper/centos-root: 32.2 GB, 32212254720 bytes, 62914560 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: 8589 MB, 8589934592 bytes, 16777216 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-u01: 279.2 GB, 279164485632 bytes, 545243136 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@test ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   30G  1.1G   29G   4% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  8.5M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda2                2.0G  125M  1.9G   7% /boot
/dev/mapper/centos-u01   260G   12G  249G   5% /u01
tmpfs                    799M     0  799M   0% /run/user/0
/dev/sr0                 4.3G  4.3G     0 100% /mnt

查看结果,该服务器只有一块硬盘,为sda,接下来计划对/u01的空间增加100G
二、添加硬盘
添加磁盘需要关机加硬盘,或者在虚拟环境下划出一块磁盘。
添加完后通过fdisk -l命令进行查看:

[root@test ~]# fdisk -l

Disk /dev/sda: 322.1 GB, 322122547200 bytes, 629145600 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: 0x000c666c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048        6143        2048   83  Linux
/dev/sda2   *        6144     4200447     2097152   83  Linux
/dev/sda3         4200448   629145599   312472576   8e  Linux LVM

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 32.2 GB, 32212254720 bytes, 62914560 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: 8589 MB, 8589934592 bytes, 16777216 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-u01: 279.2 GB, 279164485632 bytes, 545243136 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

可以看到/dev/sdb硬盘。
三、创建新分区

[root@test ~]# 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 0x4463c103.
弹出的命令行中输入m,这是对磁盘操作的相关命令的说明:
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 system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
   
a) 命令行输入 n 创建新分区:
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
b) 命令行输入 p 设置分区类型为主分区(一路回车,因为咱们将这块盘都给了/u01目录):
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-209715199, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): 
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set
c)选择分区格式,我们选择lvm,所以输入8e
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): L

 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 Extd (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
Changed type of partition 'Linux' to 'Linux LVM'
d)按w键保存:
Command (m for help): w
The partition table has been altered!

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

磁盘分区完成。
四、重启系统
#reboot
五、查看已有卷组名

[root@test ~]# vgdisplay
  --- Volume group ---
  VG Name              
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <298.00 GiB
  PE Size               4.00 MiB
  Total PE              76287
  Alloc PE / Size       76286 / 297.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               cxg6dS-1DIF-Z7PI-bUF5-sK0q-Tbq4-03Pf2V

卷组名称为: centos,要记住,后面要用到。
六、为新分配的空间创建一个新的物理卷

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

注意:如果出现错误说pvcreate命令找不到,则执行:

#yum install pv

七、扩展卷组centos
使用新的物理卷来扩展 LVM 的 VolGroup

#vgextend centos /dev/sdb1

这里的centos即为第5步中查出来的卷组名. 命令执行输出如下:

[root@test ~]# vgextend centos /dev/sdb1
  Volume group "centos" successfully extended

八、查看当前系统的逻辑卷

[root@test ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                57a0fe-KC20-IGXD-jBeD-KbkI-B8l2-HYpqIQ
  LV Write Access        read/write
  LV Creation host, time test, 2021-11-28 07:51:13 +0800
  LV Status              available
  # open                 2
  LV Size                8.00 GiB
  Current LE             2048
  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                2pR7LF-kZe7-8rqG-BdWr-wx6O-pVvV-1zrcYk
  LV Write Access        read/write
  LV Creation host, time test, 2021-11-28 07:51:13 +0800
  LV Status              available
  # open                 1
  LV Size                30.00 GiB
  Current LE             7680
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
  --- Logical volume ---
  LV Path                /dev/centos/u01
  LV Name                u01
  VG Name                centos
  LV UUID                7UuVs3-MbVB-hVjJ-FJ6n-Xge9-1aWG-C6OLiu
  LV Write Access        read/write
  LV Creation host, time test, 2021-11-28 07:51:13 +0800
  LV Status              available
  # open                 1
  LV Size                259.99 GiB
  Current LE             66558
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

这里我们选择扩展/dev/centos/u01逻辑卷
九、通过lvextend命令扩展逻辑卷/dev/centos/u01空间

[root@test ~]# lvextend /dev/centos/u01 /dev/sdb1 
  Size of logical volume centos/u01 changed from 259.99 GiB (66558 extents) to <359.99 GiB (92157 extents).
  Logical volume centos/u01 successfully resized.

可以看到原有的磁盘空间从259.99扩展到了359.99 GiB。
十、调整逻辑卷的大小

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

注意:
有些地方说要使用resize2fs命令更新系统识别的文件系统大小,但是亲测要使用xfs_growfs命令。
十一、 检验结果

[root@test ~]# lvscan
  ACTIVE            '/dev/centos/swap' [8.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [30.00 GiB] inherit
  ACTIVE            '/dev/centos/u01' [<359.99 GiB] inherit
[root@test ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   30G  1.1G   29G   4% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G     0  3.9G   0% /dev/shm
tmpfs                    3.9G  8.5M  3.9G   1% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda2                2.0G  125M  1.9G   7% /boot
/dev/mapper/centos-u01   360G   12G  349G   4% /u01
tmpfs                    799M     0  799M   0% /run/user/0

至此,扩展磁盘空间完毕。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值