linux-LVM磁盘扩容

查看磁盘

[ops@stock_kline_database ~]$ sudo fdisk -l

磁盘 /dev/sda:42.9 GB, 42949672960 字节,83886080 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x0006ba9b

   设备 Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    83886079    40893440   8e  Linux LVM

磁盘 /dev/sdb:493.9 GB, 493921239040 字节,964689920 个扇区               【已添加到LVM的磁盘,已经格式化】
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x8e329555

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   964689919   482343936   8e  Linux LVM

磁盘 /dev/mapper/rhel-root:37.6 GB, 37576769536 字节,73392128 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/rhel-swap:4294 MB, 4294967296 字节,8388608 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/mapper/vgdata-lvdata:493.5 GB, 493497614336 字节,963862528 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节


磁盘 /dev/sdc:214.7 GB, 214748364800 字节,419430400 个扇区            【未格式化的磁盘】
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

[ops@stock_kline_database ~]$ 

注意上面已经格式化与未格式化的磁盘。【未格式化的磁盘就是要添加到LVM的磁盘】

还可以通过vgdisplay和pvdisplay两个命令查看已经添加到lvm的磁盘!

[ops@stock_kline_database ~]$ df -h
文件系统                   容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root       35G  3.2G   30G   10% /
devtmpfs                    16G     0   16G    0% /dev
tmpfs                       16G     0   16G    0% /dev/shm
tmpfs                       16G  212M   16G    2% /run
tmpfs                       16G     0   16G    0% /sys/fs/cgroup
/dev/sda1                  976M  112M  798M   13% /boot
/dev/mapper/vgdata-lvdata  453G  312G  119G   73% /data
tmpfs                      3.2G     0  3.2G    0% /run/user/0
tmpfs                      3.2G     0  3.2G    0% /run/user/1001

格式化新添加的磁盘为lvm格式

小于2T的磁盘使用fdisk格式化:

1:sudo fdisk /dev/sdc     格式化磁盘
进入交互界面
2: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-1677721599, default 2048):    #默认即可
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1677721599, default 1677721599): 
Using default value 1677721599
Partition 1 of type Linux and of size 800 GiB is set
Command (m for help): t        # 选择格式化分区类型
Selected partition 1
Hex code (type L to list all codes): L  # 会打印所有的类型的代码,选择8e
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

详细过程如下:

sudo fdisk /dev/sdf
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 0xa8cd607c.

Command (m for help): p

Disk /dev/sdf: 859.0 GB, 858993459200 bytes, 1677721600 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: 0xa8cd607c

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 1
1: unknown command
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): 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-1677721599, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1677721599, default 1677721599): 
Using default value 1677721599
Partition 1 of type Linux and of size 800 GiB is set

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 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
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.
格式化磁盘为LVM格式

格式化后的磁盘如下:

磁盘 /dev/sdc:214.7 GB, 214748364800 字节,419430400 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x6861bee8

   设备 Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048   419430399   209714176   8e  Linux LVM

创建物理卷

[ops@stock_kline_database ~]$ sudo pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
[ops@stock_kline_database ~]$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               rhel
  PV Size               <39.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              9983
  Free PE               0
  Allocated PE          9983
  PV UUID               WLW0XL-n3le-1W7N-S6Vd-fYmE-Z1kf-dtkUXW
   
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               vgdata
  PV Size               <460.00 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              117759
  Free PE               100
  Allocated PE          117659
  PV UUID               wF3cCs-ecG8-OYTT-kWHk-Vjgm-5WqE-zfZpc9
   
  "/dev/sdc1" is a new physical volume of "<200.00 GiB" #新创建的物理卷
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name               
  PV Size               <200.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               oq2NDg-GLrY-YMPO-id6F-B6mQ-dpl7-T0C7OT

把新创建的物理卷添加到逻辑卷组中,注意服务器上可能不止一个lvm卷,选择对应的名称!【这里有两个物理卷,新加入的是vgdata这个卷名】

[ops@stock_kline_database ~]$ sudo vgdisplay 
  --- Volume group ---
  VG Name               rhel
  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               <39.00 GiB
  PE Size               4.00 MiB
  Total PE              9983
  Alloc PE / Size       9983 / <39.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               fDS5Qs-jmUa-WTAy-Pojt-zGvt-VtJt-UftWNG
   
  --- Volume group ---
  VG Name               vgdata
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <460.00 GiB
  PE Size               4.00 MiB
  Total PE              117759
  Alloc PE / Size       117659 / <459.61 GiB
  Free  PE / Size       100 / 400.00 MiB
  VG UUID               XIqWiX-jTEm-8xCa-rp2j-TmW3-kmHW-YT8Biw
[ops@stock_kline_database ~]$ sudo vgextend vgdata /dev/sdc1       # 扩展逻辑卷
  Volume group "vgdata" successfully extended
[ops@stock_kline_database ~]$ sudo lvextend /dev/mapper/vgdata-lvdata /dev/sdc1   # 扩展lvm卷
  Size of logical volume vgdata/lvdata changed from <459.61 GiB (117659 extents) to 659.60 GiB (168858 extents).
  Logical volume vgdata/lvdata successfully resized.

查看磁盘空间以及文件系统格式:

[ops@stock_kline_database ~]$ df -Th
文件系统                  类型      容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root     ext4       35G  3.2G   30G   10% /
devtmpfs                  devtmpfs   16G     0   16G    0% /dev
tmpfs                     tmpfs      16G     0   16G    0% /dev/shm
tmpfs                     tmpfs      16G  212M   16G    2% /run
tmpfs                     tmpfs      16G     0   16G    0% /sys/fs/cgroup
/dev/sda1                 ext4      976M  112M  798M   13% /boot
/dev/mapper/vgdata-lvdata ext4      453G  311G  119G   73% /data
tmpfs                     tmpfs     3.2G     0  3.2G    0% /run/user/0
tmpfs                     tmpfs     3.2G     0  3.2G    0% /run/user/1001

然后执行最后一步,扩展磁盘:ext4系统和xfs使用不同的命令:

[ops@stock_kline_database ~]$ sudo resize2fs /dev/mapper/vgdata-lvdata
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/vgdata-lvdata is mounted on /data; on-line resizing required
old_desc_blocks = 58, new_desc_blocks = 83
The filesystem on /dev/mapper/vgdata-lvdata is now 172910592 blocks long.

[ops@stock_kline_database ~]$ df -h
文件系统                   容量  已用  可用 已用% 挂载点
/dev/mapper/rhel-root       35G  3.2G   30G   10% /
devtmpfs                    16G     0   16G    0% /dev
tmpfs                       16G     0   16G    0% /dev/shm
tmpfs                       16G  212M   16G    2% /run
tmpfs                       16G     0   16G    0% /sys/fs/cgroup
/dev/sda1                  976M  112M  798M   13% /boot
/dev/mapper/vgdata-lvdata  650G  311G  308G   51% /data
tmpfs                      3.2G     0  3.2G    0% /run/user/0
tmpfs                      3.2G     0  3.2G    0% /run/user/1001

可以看到磁盘空间已经增加!

 xfs格式系统扩展如下:

[ops@VM100 ~]$ sudo xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2956800 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=11827200, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=5775, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 11827200 to 38040576
原文地址:https://www.cnblogs.com/wxzhe/p/11984527.html