Skip to content

Instantly share code, notes, and snippets.

@iguoli
Last active August 20, 2017 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iguoli/590e86c6a6ad0a70de80d45bd463d42b to your computer and use it in GitHub Desktop.
Save iguoli/590e86c6a6ad0a70de80d45bd463d42b to your computer and use it in GitHub Desktop.
使用LVM添加新磁盘并扩展到根目录

使用LVM添加新磁盘

查找新增磁盘文件

sudo fdisk -l

Disk /dev/sdb: 20 GiB, 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

对新磁盘进行分区

sudo fdisk /dev/sdb

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-41943039, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039):

Created a new partition 1 of type 'Linux' and of size 20 GiB.

修改分区类型为LVM

Command (m for help): t                
Selected partition 1                   
Partition type (type L to list all types): 8e                                  
Changed type of partition 'Linux' to 'Linux LVM'.     

格式化分区

sudo mkfs.ext4 /dev/sdb1

创建PV

sudo pvcreate /dev/sdb1

查看新创建的PV

sudo pvdisplay

--- Physical volume ---              
PV Name               /dev/sda5      
VG Name               lvs-ds-vg      
PV Size               9.52 GiB / not usable 2.00 MiB                         
Allocatable           yes            
PE Size               4.00 MiB       
Total PE              2437           
Free PE               6              
Allocated PE          2431           
PV UUID               tgeCdP-SpxQ-Q9YE-5ps1-RKdT-87Ym-33vdis                 

"/dev/sdb1" is a new physical volume of "20.00 GiB"                          
--- NEW Physical volume ---                                                  
PV Name               /dev/sdb1                                              
VG Name                              
PV Size               20.00 GiB      
Allocatable           NO             
PE Size               0              
Total PE              0              
Free PE               0              
Allocated PE          0              
PV UUID               0A4o3n-J1Mt-QVaS-0bgI-0Hku-4zsq-Avlc3I

查看现有卷组

sudo vgdisplay

--- Volume group ---                                                         
VG Name               lvs-ds-vg                                              
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               9.52 GiB       
PE Size               4.00 MiB       
Total PE              2437           
Alloc PE / Size       2431 / 9.50 GiB
Free  PE / Size       6 / 24.00 MiB  
VG UUID               42bLhu-BeVB-8thU-2OJ4-bqzl-LvPC-xtftwJ

将新创建的PV加入现有的卷组

sudo vgextend lvs-ds-vg /dev/sdb1

再次查看卷组

sudo vgdisplay

--- Volume group ---                 
VG Name               lvs-ds-vg      
System ID                            
Format                lvm2           
Metadata Areas        2              
Metadata Sequence No  4              
VG Access             read/write     
VG Status             resizable      
MAX LV                0              
Cur LV                2              
Open LV               2              
Max PV                0              
Cur PV                2              
Act PV                2              
VG Size               29.52 GiB      
PE Size               4.00 MiB       
Total PE              7556           
Alloc PE / Size       2431 / 9.50 GiB
Free  PE / Size       5125 / 20.02 GiB                                       
VG UUID               42bLhu-BeVB-8thU-2OJ4-bqzl-LvPC-xtftwJ

查看逻辑卷

sudo lvdisplay

--- Logical volume ---               
LV Path                /dev/lvs-ds-vg/root                                   
LV Name                root          
VG Name                lvs-ds-vg     
LV UUID                onkHdt-4s7X-5q3v-jGpf-uuFk-hxqj-QxkGOk                
LV Write Access        read/write    
LV Creation host, time lvs-ds, 2017-06-15 22:10:31 +0800                     
LV Status              available     
# open                 1             
LV Size                8.50 GiB      
Current LE             2176          
Segments               1             
Allocation             inherit       
Read ahead sectors     auto          
- currently set to     256           
Block device           252:0         

--- Logical volume ---               
LV Path                /dev/lvs-ds-vg/swap_1                                 
LV Name                swap_1        
VG Name                lvs-ds-vg     
LV UUID                GG6tFZ-fCH0-X1NI-c4Ln-sqba-DvyL-UqOvNO                
LV Write Access        read/write    
LV Creation host, time lvs-ds, 2017-06-15 22:10:31 +0800                     
LV Status              available     
# open                 2             
LV Size                1020.00 MiB   
Current LE             255           
Segments               1             
Allocation             inherit       
Read ahead sectors     auto          
- currently set to     256           
Block device           252:1

扩展根目录所在逻辑卷

sudo lvextend -l +100%FREE /dev/lvs-ds-vg/root

Size of logical volume lvs-ds-vg/root changed from 8.50 GiB (2176 extents) to 28.52 GiB (7301 extents).
Logical volume root successfully resized.

在线调整根文件系统大小

sudo resize2fs /dev/lvs-ds-vg/root

resize2fs 1.42.13 (17-May-2015)        
Filesystem at /dev/lvs-ds-vg/root is mounted on /; on-line resizing required   
old_desc_blocks = 1, new_desc_blocks = 2                                       
The filesystem on /dev/lvs-ds-vg/root is now 7476224 (4k) blocks long. 

检查根文件系统容量已经增加

df -lh

Filesystem                    Size  Used Avail Use% Mounted on                 
udev                          225M     0  225M   0% /dev                       
tmpfs                          49M  2.0M   47M   4% /run                       
/dev/mapper/lvs--ds--vg-root   28G  6.2G   21G  23% /                          
tmpfs                         245M     0  245M   0% /dev/shm                   
tmpfs                         5.0M     0  5.0M   0% /run/lock                  
tmpfs                         245M     0  245M   0% /sys/fs/cgroup             
/dev/sda1                     472M   57M  391M  13% /boot                      
tmpfs                          49M     0   49M   0% /run/user/1001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment