Linux新增磁碟到LVM的例子

今天要在一台Linux的機器上安裝一個程式,查詢狀態發現空間不足。
[root@pv_test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       28G   24G  2.4G  91% /
/dev/sda1             101M   13M   82M  14% /boot
tmpfs                  10G  1.4G  8.7G  14% /dev/shm

查詢硬碟的結構,發現還有一個sdb1,因為這台機器是VM上面的虛擬機器,所以可能是之前就已經分配好的空間
[root@pv_test orcl]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        3916    31350847+  8e  Linux LVM

Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+  83  Linux
       ext3     defaults        0 0

       
因為有看到/dev/mapper/VolGroup00-LogVol00這樣的device,所以這台Linux應該有建LVM
實體Device可以建立為PV,多個PV可以建立為VG,VG就像一顆大的虛擬硬碟,就可以規劃分割區與格式化
而分割出來的虛擬區塊就是LV,這是可以實際使用的部份。

尋找是否有PV,發現有一顆PV /dev/sda2,並且正在使用  
[root@pv_test ora01]# pvscan
  PV /dev/sda2   VG VolGroup00   lvm2 [29.88 GB / 0    free]
  Total: 1 [29.88 GB] / in use: 1 [29.88 GB] / in no VG: 0 [0   ]

查詢PV的詳細訊息 
[root@pv_test ora01]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup00
  PV Size               29.90 GB / not usable 24.06 MB
  Allocatable           yes (but full)
  PE Size (KByte)       32768
  Total PE              956
  Free PE               0
  Allocated PE          956
  PV UUID               O9OO6i-UAs5-WsUy-teoh-G8zx-PlcF-ozHzvd       

查詢VG的訊息,的確有一個VG(Found volumn group "VolGroup00")
[root@pv_test ora01]# vgscan
  Reading all physical volumes.  This may take a while...
  Found volume group "VolGroup00" using metadata type lvm2
 
查詢VG的詳細訊息,已經使用了全部的容量(Alloc PE / Size、Free  PE / Size)
[root@pv_test ora01]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup00
  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               29.88 GB
  PE Size               32.00 MB
  Total PE              956
  Alloc PE / Size       956 / 29.88 GB
  Free  PE / Size       0 / 0  
  VG UUID               K4DU8g-qJjI-uJoO-IzIa-Ji7o-vQVo-R4Zi8V  

接著準備把沒有使用到的sdb1加入LVM,讓空間變大,先更改sdb1的檔案格式旗標為Linux LVM
[root@pv_test ora01]# fdisk /dev/sdb

The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): t
Selected partition 1    
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

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

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

將/dev/sdb1建立為PV
[root@pv_test /]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
 
重新尋找PV,已經成功建立/dev/sdb1的PV
[root@pv_test /]# pvscan
  PV /dev/sda2   VG VolGroup00      lvm2 [29.88 GB / 0    free]
  PV /dev/sdb1                      lvm2 [19.99 GB]
  Total: 2 [49.87 GB] / in use: 1 [29.88 GB] / in no VG: 1 [19.99 GB]
 
接著加入到VG
[root@pv_test /]# vgextend VolGroup00 /dev/sdb1
  Volume group "VolGroup00" successfully extended
 
重新查詢VG,發現空間變大了,而且還有19.97GB的空間可以使用(Free  PE / Size)
[root@pv_test /]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup00
  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               49.84 GB
  PE Size               32.00 MB
  Total PE              1595
  Alloc PE / Size       956 / 29.88 GB
  Free  PE / Size       639 / 19.97 GB
  VG UUID               K4DU8g-qJjI-uJoO-IzIa-Ji7o-vQVo-R4Zi8V

查詢LV,目前已分割成LogVol00與LogVol01,而這次我們要擴充空間的是LogVol00
[root@pv_test /]# lvscan
  ACTIVE            '/dev/VolGroup00/LogVol00' [27.91 GB] inherit
  ACTIVE            '/dev/VolGroup00/LogVol01' [1.97 GB] inherit
 
把VG剩餘的空間(Free  PE / Size       639 / 19.97 GB)全部都給LogVol00
[root@pv_test /]# lvresize -l +639 /dev/VolGroup00/LogVol00
  Extending logical volume LogVol00 to 47.88 GB
  Logical volume LogVol00 successfully resized
 
重新查詢LV的詳細訊息,可以發現LogVol00的空間變大了
[root@pv_test /]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol00
  VG Name                VolGroup00
  LV UUID                8bnCPd-9VFM-V2fd-xyIP-MMZ2-1JlX-jhvz2t
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                47.88 GB
  Current LE             1532
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0
  
  --- Logical volume ---
  LV Name                /dev/VolGroup00/LogVol01
  VG Name                VolGroup00
  LV UUID                rJ8EKs-4bev-rfeL-xXBr-8LBj-ndPH-0ejKJJ
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                1.97 GB
  Current LE             63
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1
  
雖然LV(LogVol00)的空間變大了,但是變大的部份還不能被我們使用,我們最後再下resize2fs的指令讓變大的部份可以使用,
這個指令可以on-line直接執行,而且不會影響原本已存在的資料,這樣就直接把空間變大了,真的很方便。
[root@pv_test /]# resize2fs /dev/VolGroup00/LogVol00
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/VolGroup00/LogVol00 is mounted on /; on-line resizing required
Performing an on-line resize of /dev/VolGroup00/LogVol00 to 12550144 (4k) blocks.
The filesystem on /dev/VolGroup00/LogVol00 is now 12550144 blocks long.

查詢之後可以發現,原本不足的空間,實際可用量變多了。
[root@pv_test /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       47G   24G   21G  53% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                  10G  1.4G  8.7G  14% /dev/shm