Benjamin Zhang

目前就职于中科院计算机网络信息中心,主要兴趣集中在数据库,操作系统,中间件。

【linux】lvm扩展根分区

23 Oct 2019 » linux

lvm扩展根目录

1.lvm的基本概念

physical volume (PV)

通常是一快硬盘。相当于一个物理设备,磁盘空间物理卷PV。

volume group (VG)

相当于LVM的卷组,属于一个抽象下的组概念,一个VG可以包括多个PV。我的实操是我的VG下面,包含了PV(/dev/sda5)和PV(/dev/sdb1)如下:

root@ubuntudoc:~# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda5
  VG Name               ubuntudoc-vg
  PV Size               49.76 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12738
  Free PE               0
  Allocated PE          12738
  PV UUID               ADh3K6-fjVA-5lLY-9vzu-eIxe-utiL-pOYtZa
   
  --- Physical volume ---
  PV Name               /dev/sdb1
  VG Name               ubuntudoc-vg
  PV Size               16.00 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              4095
  Free PE               0
  Allocated PE          4095
  PV UUID               2RwFCw-lLul-8CIf-gHvb-0rC1-U0aG-SfHZGd
   
root@ubuntudoc:~# pvs       
  PV         VG           Fmt  Attr PSize  PFree
  /dev/sda5  ubuntudoc-vg lvm2 a--  49.76g    0 
  /dev/sdb1  ubuntudoc-vg lvm2 a--  16.00g    0 

logical volume (LV)

LV简单的理解是可以挂载到分区下的磁盘空间了。如下 :根分区/下的空间,代表着root这个lv。

root@ubuntudoc:~# lvs
  LV     VG           Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  root   ubuntudoc-vg -wi-ao--- 53.76g                                           
  swap_1 ubuntudoc-vg -wi-ao--- 12.00g                                           

2.lvm扩展根目录分区的实战

2.1 设置硬盘/dev/sdb为linux lvm

先决条件:在vmware虚拟化平台下,我先添加了一块/dev/sdb的硬盘,并把硬盘设置为linux lvm,操作如下:

root@ubuntudoc:~# 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 0x502d7728.
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)

Command (m for help): p

Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders, total 33554432 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 identifier: 0x502d7728

   Device Boot      Start         End      Blocks   Id  System

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): 
Using default value 1
First sector (2048-33554431, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-33554431, default 33554431): 
Using default value 33554431

Command (m for help): p

Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders, total 33554432 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 identifier: 0x502d7728

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    33554431    16776192   83  Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list 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 codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders, total 33554432 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 identifier: 0x502d7728

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    33554431    16776192   8e  Linux LVM


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

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

2.2 把pv(/dev/sb1)添加至vg中。

命令如下:

root@ubuntudoc:~# vgs       
  VG           #PV #LV #SN Attr   VSize  VFree 
  ubuntudoc-vg   1   2   0 wz--n- 49.76g 32.00m
  
root@ubuntudoc:~# vgextend ubuntudoc-vg /dev/sdb1
  No physical volume label read from /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
  Volume group "ubuntudoc-vg" successfully extended
  
root@ubuntudoc:~# vgs
  VG           #PV #LV #SN Attr   VSize  VFree 
  ubuntudoc-vg   2   2   0 wz--n- 65.75g 16.03g

上述结果,可以看出ubuntudoc-vg添加至65GB了。

2.3 vg中空闲的空间加入lv中

命令如下:

root@ubuntudoc:~# lvs
  LV     VG           Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  root   ubuntudoc-vg -wi-ao--- 37.73g                                           
  swap_1 ubuntudoc-vg -wi-ao--- 12.00g

root@ubuntudoc:~# lvextend --help
  lvextend: Add space to a logical volume

lvextend
        [-A|--autobackup y|n]
        [--alloc AllocationPolicy]
        [-d|--debug]
        [-f|--force]
        [-h|--help]
        [-i|--stripes Stripes [-I|--stripesize StripeSize]]
        {-l|--extents [+]LogicalExtentsNumber[%{VG|LV|PVS|FREE|ORIGIN}] |
         -L|--size [+]LogicalVolumeSize[bBsSkKmMgGtTpPeE]}
        [-m|--mirrors Mirrors]
        [--nosync]
        [--use-policies]
        [-n|--nofsck]
        [--noudevsync]
        [-r|--resizefs]
        [-t|--test]
        [--type VolumeType]
        [-v|--verbose]
        [--version]
        LogicalVolume[Path] [ PhysicalVolumePath... ]

root@ubuntudoc:~# lvextend -l +100%free  /dev/ubuntudoc-vg/root   (100%剩余的空闲空间全部给与root的lv)
  Extending logical volume root to 53.76 GiB
  Logical volume root successfully resized
root@ubuntudoc:~# lvs
  LV     VG           Attr      LSize  Pool Origin Data%  Move Log Copy%  Convert
  root   ubuntudoc-vg -wi-ao--- 53.76g                                           
  swap_1 ubuntudoc-vg -wi-ao--- 12.00g                

2.4 根分区识别空闲的磁盘空间

操作如下:

root@ubuntudoc:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            989M  4.0K  989M   1% /dev
tmpfs           200M  712K  200M   1% /run
/dev/dm-0        38G  2.6G   33G   8% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none           1000M     0 1000M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/sda1       236M   40M  184M  18% /boot

root@ubuntudoc:~# resize2fs /dev/ubuntudoc-vg/root
resize2fs 1.42.9 (4-Feb-2014)
Filesystem at /dev/ubuntudoc-vg/root is mounted on /; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/ubuntudoc-vg/root is now 14092288 blocks long.

root@ubuntudoc:~# df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            989M  4.0K  989M   1% /dev
tmpfs           200M  712K  200M   1% /run
/dev/dm-0        53G  2.6G   48G   6% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none           1000M     0 1000M   0% /run/shm
none            100M     0  100M   0% /run/user
/dev/sda1       236M   40M  184M  18% /boot
root@ubuntudoc:~# 

至此,/分区扩展至53G。

Related Posts