使用LVM方式擴容硬盤空間


使用LVM方式擴容硬盤空間

目錄

 [顯示

環境說明

圖片1LVM.png

案例環境說明:該解碼引擎服務器已經采用LVM的方式進行了部署,原始的解碼引擎遇到/data硬盤空間不足的問題,需要通過LVM方式再次對/data擴展空間。如圖所示,在我們的解碼服務器上新增了一塊硬盤,空間大小為5G。

[root@EZSonar ~]# fdisk -l 

顯示內容如圖所示,新增硬盤為/dev/sdb。

圖片2LVM.png

第一步:新建一個分區

[root@EZSonar ~]# fdisk /dev/sdb 

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to

switch off the mode (command 'c') and change display units to

sectors (command 'u').

Command (m for help): n                                            //添加新分區

Command action

e   extended (說明:擴展分區)

p   primary partition (1-4) (說明:主分區)

e                                                            //增加擴展分區

Partition number (1-4): 1

First cylinder (1-652, default 1)::<<ENTER

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): <<ENTER   //也可以指定大小+5G,如果直接輸入回車則使用所有空間

Using default value 652

Command (m for help): p                                  //查看分區詳情,可以看到新建的擴展分區

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xa9b024b3

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         652     5237158+   5  Extended

Command (m for help): n

Command action

l   logical (5 or over)

p   primary partition (1-4)

l

First cylinder (1-652, default 1): <<ENTER

Using default value 1

Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): <<ENTER

Using default value 652

Command (m for help): p

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xa9b024b3

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1           5       40131    5  Extended

/dev/sdb5               1           5       40099+  83  Linux

Command (m for help): t             //修改分區格式,下面選的5是上面的sdb5,默認格式是Linux

Partition number (1-5): 5

Hex code (type L to list codes): 8e

(說明:8e為Linux LVM格式的編碼)

Changed system type of partition 5 to 8e (Linux LVM)

Command (m for help): p                                  //查看分區情況

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xa9b024b3

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1           5       40131    5  Extended

/dev/sdb5               1           5       40099+  8e  Linux LVM

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)

1、 創建物理卷pv。

[root@EZSonar ~]# pvcreate /dev/sdb5         //   創建pv    

Physical volume "/dev/sdb5" successfully created

[root@EZSonar ~]# pvdisplay /dev/sdb5        //   查看pv 

-- "/dev/sdb5" is a new physical volume of "5.00 GiB"

--- NEW Physical volume ---

PV Name               /dev/sdb5

VG Name               

PV Size               5.00 GiB

Allocatable           NO

PE Size               0   

Total PE              0

Free PE               0

Allocated PE          0

PV UUID               Hjoga4-EXmd-G53T-vAvQ-kDtA-60e2-vEHzBY

2、 查看已有的VG配置詳情

[root@EZSonar ~]# vgdisplay 

--- Volume group ---

  VG Name               vg_ezsonar

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               31.80 GiB

PE Size               4.00 MiB

Total PE              8141

Alloc PE / Size       8141 / 31.80 GiB

Free  PE / Size       0 / 0   

VG UUID               JXOpXA-jkl5-Jdfp-IHeS-6MpP-8V3q-Vl3lMh

3、 增減邏輯卷

[root@EZSonar ~]# vgextend vg_ezsonar /dev/sdb5      // 擴展vg  將pv加入到vg_ezsonar 

Volume group "vg_ezsonar" successfully extended

[root@EZSonar ~]# vgdisplay                //查看VG情況

--- Volume group ---

VG Name               vg_ezsonar

System ID             

Format                lvm2

Metadata Areas        2

Metadata Sequence No  5

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                3

Open LV               3

Max PV                0

Cur PV                2

Act PV                2

VG Size               71.80 GiB

PE Size               4.00 MiB

Total PE              18380

Alloc PE / Size       8141 / 31.80 GiB

Free  PE / Size       1023 / 5.00 GiB                        //剩余分配空間

VG UUID               JXOpXA-jkl5-Jdfp-IHeS-6MpP-8V3q-Vl3lMh

4、 確認需要擴容的分區,紅色/data分區需要擴容

[root@EZSonar ~]# df -h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_ezsonar-LogVol02

14G  3.7G  9.2G  29% /

tmpfs                 939M     0  939M   0% /dev/shm

/dev/sda1             194M   33M  152M  18% /boot

/dev/mapper/vg_ezsonar-LogVol01

11G  2.2G  8.0G  22% /data

/dev/mapper/vg_ezsonar-LogVol00

8.0G   82M  8.0G   1% /var

5、 進行擴容

[root@EZSonar ~]# lvextend -L +5G /dev/mapper/vg_ezsonar-LogVol01      //擴展5G到lv:logVol01  

Extending logical volume LogVol01 to 15.18 GiB

Logical volume LogVol01 successfully resized

[root@EZSonar ~]#
[root@EZSonar ~]# resize2fs /dev/mapper/vg_ezsonar-LogVol01   //執行該重設大小 
[root@EZSonar ~]# mount             //查看掛載情況,原有分區采用什么文件格式 

/dev/mapper/vg_ezsonar-LogVol02 on / type ext4 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs (rw)

/dev/sda1 on /boot type ext4 (rw)

/dev/mapper/vg_ezsonar-LogVol01 on /data type xfs (rw)

/dev/mapper/vg_ezsonar-LogVol00 on /var type xfs (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

[root@EZSonar ~]# xfs_growfs /dev/mapper/vg_ezsonar-LogVol01      //xfs文件格式采用xfs_growfs 

meta-data=/dev/mapper/vg_ezsonar-LogVol01 isize=256    agcount=4, agsize=666880 blks

=                       sectsz=512   attr=2, projid32bit=0

data     =                       bsize=4096   blocks=2667520, imaxpct=25

=                       sunit=0      swidth=0 blks

naming   =version 2              bsize=4096   ascii-ci=0

log      =internal               bsize=4096   blocks=2560, version=2

=                       sectsz=512   sunit=0 blks, lazy-count=1

realtime =none                   extsz=4096   blocks=0, rtextents=0

data blocks changed from 2667520 to 3978240

5、查看擴容結果

[root@EZSonar ~]# df –h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_ezsonar-LogVol02

14G  3.7G  9.2G  29% /

tmpfs                 939M     0  939M   0% /dev/shm

/dev/sda1             194M   33M  152M  18% /boot

/dev/mapper/vg_ezsonar-LogVol01

                       16G  2.2G   13G  15% /data

/dev/mapper/vg_ezsonar-LogVol00

8.0G   82M  8.0G   1% /var

附錄,對根分區進行擴容

首先確認VG有剩余空間可用

[root@EZSonar ~]# vgdisplay  

--- Volume group ---

VG Name               vg_ezsonar

System ID             

Format                lvm2

Metadata Areas        2

Metadata Sequence No  6

VG Access             read/write

VG Status             resizable

MAX LV                0

Cur LV                3

Open LV               3

Max PV                0

Cur PV                2

Act PV                2

VG Size               41.79 GiB

PE Size               4.00 MiB

Total PE              10699

Alloc PE / Size       9421 / 36.80 GiB

 Free  PE / Size       1278 / 4.99 GiB //有5G剩余空間可用

VG UUID               JXOpXA-jkl5-Jdfp-IHeS-6MpP-8V3q-Vl3lMh

[root@EZSonar ~]# mount             //查看掛載情況,原有分區采用什么文件格式 

/dev/mapper/vg_ezsonar-LogVol02 on / type ext4 (rw)

根分區采用ext4格式

[root@EZSonar ~]# lvextend -L +4.5G /dev/mapper/vg_ezsonar-LogVol02 /擴展4.5G到lv:logVol02  

Extending logical volume LogVol02 to 18.12 GiB

Logical volume LogVol02 successfully resized

[root@EZSonar ~]# resize2fs /dev/mapper/vg_ezsonar-LogVol02              

//ext4 文件格式采用resize2f

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/mapper/vg_ezsonar-LogVol02 is mounted on /; on-line resizing required

old desc_blocks = 1, new_desc_blocks = 3

Performing an on-line resize of /dev/mapper/vg_ezsonar-LogVol02 to 8814592 (4k) blocks.

The filesystem on /dev/mapper/vg_ezsonar-LogVol02 is now 8814592 blocks long.

[root@EZSonar ~]# df -h 

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/vg_ezsonar-LogVol02

                       18G  3.7G   14G  22% /

tmpfs                 939M     0  939M   0% /dev/shm

/dev/sda1             194M   33M  152M  18% /boot

/dev/mapper/vg_ezsonar-LogVol01

16G  2.2G   13G  15% /data

/dev/mapper/vg_ezsonar-LogVol00

8.0G   82M  8.0G   1% /var

附錄:LVM技術優勢說明

優點

比起正常的硬盤分區管理,LVM更富於彈性:

§ 使用卷組(VG),使眾多硬盤空間看起來像一個大硬盤。

§ 使用邏輯卷(LV),可以創建跨越眾多硬盤空間的分區。

§ 可以創建小的邏輯卷(LV),在空間不足時再動態調整它的大小。

§ 在調整邏輯卷(LV)大小時可以不用考慮邏輯卷在硬盤上的位置,不用擔心沒有可用的連續空間。It does not depend on the position of the LV within VG, there is no need to ensure surrounding available space.

§ 可以在線(online)對邏輯卷(LV)和卷組(VG)進行創建、刪除、調整大小等操作。LVM上的文件系統也需要重新調整大小,某些文件系統也支持這樣的在線操作。

§ 無需重新啟動服務,就可以將服務中用到的邏輯卷(LV)在線(online)/動態(live)遷移至別的硬盤上。

§ 允許創建快照,可以保存文件系統的備份,同時使服務的下線時間(downtime)降低到最小。

這些優點使得LVM對服務器的管理非常有用,對於桌面系統管理的幫助則沒有那么顯著,你需要根據實際情況進行取舍。


免責聲明!

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



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