天天看點

linux下通過lvm對磁盤進行動态擴容

1 檢視磁盤容量資訊

[[email protected] ~]# df -h
Filesystem               Size  Used  Avail  Use%  Mounted on
/dev/mapper/VG-LogVol    1.9T  1.8T    61G   97%  /          # LVM卷組-邏輯卷
tmpfs                     63G     0    63G    0%  /dev/shm
/dev/sda1                485M   40M   421M    9%  /boot      

2 檢視磁盤扇區資訊

[[email protected] ~]# fdisk -l

Disk /dev/sda: 299.0 GB, 298999349248 bytes         # 磁盤/dev/sda
255 heads, 63 sectors/track, 36351 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4d69fe0e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux       # 分為2個區, sda1
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       36352   291785728   8e  Linux LVM   # LVM類型的sda2

# 新添加的磁盤/dev/sdb, 沒有分區
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

# LVM格式的卷組資訊: 
Disk /dev/mapper/VolGroup-LogVol: 4294 MB, 4294967296 bytes
255 heads, 63 sectors/track, 522 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000      

3.3 建立分區

[[email protected] ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x5b3d66ba.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: The size of this disk is 4.0 TB (3999999721472 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n         # 添加分區
Command action
   e   extended
   p   primary partition (1-4)
p                               # 添加主分區
Partition number (1-4): 1       # 1号主分區, 即/dev/sdb1
First cylinder (1-486305, default 1):               
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): 486305
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): 
Using default value 267349

Command (m for help): n         # 繼續添加分區
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2       # 2号主分區, 即/dev/sdc2
First cylinder (267350-486305, default 267350): 
Using default value 267350
Last cylinder, +cylinders or +size{K,M,G} (267350-486305, default 486305): 
Using default value 486305

Command (m for help): p         # 列印分區資訊: 

Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5b3d66ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      267349  2147480811   83  Linux
/dev/sdb2          267350      486305  1758764070   83  Linux

Command (m for help): t         # 轉換類型
Partition number (1-4): 1
Partition number (1-4): 1           # 修改/dev/sdb1為Linux LVM類型: 
Hex code (type L to list codes): L  # 檢視可用類型: 

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

Command (m for help): t
Partition number (1-4): 2               # 修改/dev/sdc2為Linux LVM類型
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

Command (m for help): p                 # 再次檢視相關資訊: 

Disk /dev/sdc: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5b3d66ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      267349  2147480811   8e  Linux LVM   # Id已改變
/dev/sdb2          267350      486305  1758764070   8e  Linux LVM

Command (m for help): w                 # 儲存并退出
The partition table has been altered!   # 修改成功

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

4 建立實體卷

[[email protected] ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
[[email protected] ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created      

5 擴充卷組

# 檢視已有卷組, 發現該卷組就是需要擴容的卷組, 就不必再次建立卷組, 而是直接擴充卷組即可: 
[[email protected] ~]# vgs
  VG       #PV #LV #SN Attr   VSize VFree
  VolGroup   2   2   0 wz--n- 1.91t    0 

# 擴充卷組: 
[[email protected] ~]# vgextend VolGroup /dev/sdb1
  Volume group "VolGroup" successfully extended
[[email protected] ~]# vgextend VolGroup /dev/sdb2
  Volume group "VolGroup" successfully extended      

說明: 如果出現下述無法挂載實體磁盤到卷組中的資訊, 說明這塊實體磁盤已經挂載了, 需要先解除安裝, 然後再執行建立分區+卷組的操作:

[[email protected] /]# vgextend VolGroup /dev/sdb1 
  No physical volume label read from /dev/sdb1
  Physical volume /dev/sdb1 not found
  Can't open /dev/sdb1 exclusively.  Mounted filesystem?
  Unable to add physical volume '/dev/sdb1' to volume group 'VolGroup'.      

6 擴充邏輯卷

# 擴充邏輯卷, 即擴容: 
[[email protected] ~]# lvextend -l +100%FREE /dev/mapper/VolGroup-LogVol 
  Extending logical volume lv_root to 5.54 TiB
  Logical volume lv_root successfully resized

# 上述指令是将所有的空閑空間都擴容到邏輯卷中, 也可指定擴容的大小: 
lvextend -l +100G /dev/mapper/VolGroup-LogVol       

7 檢視磁盤卷組資訊

[[email protected] ~]# lsblk 
NAME                        MAJ:MIN RM   SIZE RO  TYPE  MOUNTPOINT
sda                           8:0    0 278.5G  0  disk  
├─sda1                        8:1    0   500M  0  part  /boot
└─sda2                        8:2    0   278G  0  part  
  ├─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /
sdb                           8:16   0   1.6T  0  disk  
└─sdb1                        8:17   0   1.6T  0  part  
  └─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /
sdc                           8:32   0   3.7T  0  disk  
├─sdc1                        8:33   0     2T  0  part  
│ └─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /
└─sdc2                        8:34   0   1.7T  0  part  
  └─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /      

8 調整檔案系統的大小

# CentOS 7重新讀取磁盤大小: 
[[email protected] ~]# xfs_growfs /dev/mapper/VolGroup-LogVol 
xfs_growfs: /dev/mapper/VolGroup-LogVol is not a mounted XFS filesystem
 
# 趕緊看下擴容成果吧: 
[[email protected] ~]#   df -h
Filesystem                    Size  Used Avail Use%  Mounted on
/dev/mapper/VolGroup-LogVol   5.5T  1.8T  3.5T  34%  /          # 擴容成功
tmpfs                          63G     0   63G   0%  /dev/shm
/dev/sda1                     485M   40M  421M   9%  /boot      

繼續閱讀