1. 程式人生 > >Centos7擴充套件磁碟空間(LVM管理)

Centos7擴充套件磁碟空間(LVM管理)

1. 檢視磁碟情況

fdisk -l /dev/sda
Disk /dev/sda: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x000ac88a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 266338303 132119552 8e Linux LVM

2. 檢視磁碟佔用情況

df -lh
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   50G   24G   27G  47% /
devtmpfs             5.8G     0  5.8G   0% /dev
tmpfs                
5.8G 84K 5.8G 1% /dev/shm tmpfs 5.8G 8.8M 5.8G 1% /run tmpfs 5.8G 0 5.8G 0% /sys/fs/cgroup /dev/sda1 1014M 173M 842M 18% /boot /dev/mapper/cl-home 69G 517M 68G 1% /home tmpfs 1.2G 16K 1.2G 1% /run/user/42 tmpfs 1
.2G 0 1.2G 0% /run/user/0

3. 建立新分割槽

fdisk /dev/sda

彈出的命令列中輸入對應的命令,命令說明如下:

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)

a) 命令列輸入 n 建立新分割槽

[[email protected] ~]# fdisk /dev/sda

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
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): n

 

b) 命令列輸入 p 設定分割槽型別為主分割槽

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p

c) 設定分割槽數量,這裡輸入為3

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3

d)修改分割槽大小,我們這裡取預設大小,直接按兩次回車即可(開始和結束位置)

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (266338304-1048575999, default 266338304): 
Using default value 266338304
Last sector, +sectors or +size{K,M,G} (266338304-1048575999, default 1048575999): 
Using default value 1048575999
Partition 3 of type Linux and of size 373 GiB is set

e) 輸入t修改分割槽編號

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (266338304-1048575999, default 266338304): 
Using default value 266338304
Last sector, +sectors or +size{K,M,G} (266338304-1048575999, default 1048575999): 
Using default value 1048575999
Partition 3 of type Linux and of size 373 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 

f)修改分割槽編號為3

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (266338304-1048575999, default 266338304): 
Using default value 266338304
Last sector, +sectors or +size{K,M,G} (266338304-1048575999, default 1048575999): 
Using default value 1048575999
Partition 3 of type Linux and of size 373 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 

 g) 選擇分割槽格式,我們選擇lvm,所以輸入8e

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'

h) 輸入w儲存分割槽並退出

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

4. 重啟系統

reboot

5. 檢視已有卷組名

vgdisplay
  --- Volume group ---
  VG Name               cl
  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               <126.00 GiB
  PE Size               4.00 MiB
  Total PE              32255
  Alloc PE / Size       32254 / 125.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               zlg3Lh-rWHl-ozXP-FfIL-xs2w-yT7a-2k3VgA

在本機中卷組為cl.(後面會用到)

6. 為新分配的空間建立一個新的物理卷

pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.

注意:如果出現錯誤說pvcreate命令找不到,則執行:

yum install pv

7. 使用新的物理捲來擴充套件 LVM 的 VolGroup

vgextend cl /dev/sda3

這裡的cl即為第5步中查出來的卷組名. 命令執行輸出如下:

[[email protected] ~]# vgextend cl /dev/sda3
  Volume group "cl" successfully extended

8. 擴充套件 LVM 的邏輯卷 /dev/cl/home

a) 先檢視邏輯卷集合

lvdisplay

輸出如下:

[[email protected] ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/cl/swap
  LV Name                swap
  VG Name                cl
  LV UUID                LM5xZa-J1Af-XPIu-CQYK-2JTw-Cw9N-JOmqdm
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-05-31 15:37:32 +0800
  LV Status              available
  # open                 2
  LV Size                <7.88 GiB
  Current LE             2016
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1
   
  --- Logical volume ---
  LV Path                /dev/cl/home
  LV Name                home
  VG Name                cl
  LV UUID                U2TrdQ-ihpb-SNr3-U1cq-qOKr-4gw0-rYNmOG
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-05-31 15:37:32 +0800
  LV Status              available
  # open                 1
  LV Size                <68.12 GiB
  Current LE             17438
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2
   
  --- Logical volume ---
  LV Path                /dev/cl/root
  LV Name                root
  VG Name                cl
  LV UUID                xXKaHg-WKW0-H3e2-dtjx-MJLS-bUcm-PpPw3z
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2018-05-31 15:37:34 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

這裡我們選擇擴充套件/dev/cl/home邏輯卷

b)擴充套件邏輯卷/dev/cl/home空間

lvextend  /dev/cl/home  /dev/sda3

輸出為:

[[email protected] ~]# lvextend  /dev/cl/home  /dev/sda3
  Size of logical volume cl/home changed from <68.12 GiB (17438 extents) to 441.11 GiB (112925 extents).
  Logical volume cl/home successfully resized.

可以看到原有的磁碟空間從68G擴充套件到了441G, 

lvextend引數-L是指定大小 如果不輸入-L +10G 則預設使用全部

9)調整邏輯卷的大小

xfs_growfs /dev/cl/home

輸出為:

[[email protected] ~]# xfs_growfs /dev/cl/home
meta-data=/dev/mapper/cl-home isize=512 agcount=4, agsize=4464128 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=17856512, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=8719, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 17856512 to 115635200

注意:

有些地方說要使用resize2fs命令更新系統識別的檔案系統大小,但是親測要使用xfs_growfs命令。

10) 檢驗結果

[[email protected] ~]# lvscan
  ACTIVE            '/dev/cl/swap' [<7.88 GiB] inherit
  ACTIVE            '/dev/cl/home' [441.11 GiB] inherit
  ACTIVE            '/dev/cl/root' [50.00 GiB] inherit

也可以使用下面的方式來檢驗

[[email protected] ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   50G   14G   37G  27% /
devtmpfs             5.8G     0  5.8G   0% /dev
tmpfs                5.8G   84K  5.8G   1% /dev/shm
tmpfs                5.8G  8.7M  5.8G   1% /run
tmpfs                5.8G     0  5.8G   0% /sys/fs/cgroup
/dev/sda1           1014M  173M  842M  18% /boot
/dev/mapper/cl-home  442G   23G  419G   6% /home
tmpfs                1.2G   16K  1.2G   1% /run/user/42
tmpfs                1.2G     0  1.2G   0% /run/user/0