【linux】lvm擴展根分區


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。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM