天天看點

linux lvm擴容硬碟,Linux LVM空間擴容示範

虛拟化逐漸成為我們日常運維工作的主要部署政策。在虛拟化帶來的各種好處中,硬體資源動态調整是傳統實體機難以企及的優勢方面。針對不同類型的應用和性能需求,我們可以為各個應用系統提供更加個性化的部署方案和資源解決方案。而且,當存儲、CPU和記憶體等資源不足的時候,我們是可以快速的提供資源補充給應用。

LVM是一種源自Unix環境,在Linux上廣泛應用的邏輯虛拟盤存儲方案。借助LVM,可以在保證各個Linux目錄分區穩定,又可以實作各目錄存儲資源靈活配置設定。

本文主要系統介紹Linux環境下,LVM實作資源擴容步驟方法。留待需要朋友待查。

1、環境介紹

筆者使用Linux 6.5進行測試,背景是一台用于測試搭建的資料庫伺服器,需要轉換為正式投産,但是原有可用根節點空間隻有50G左右。

[[email protected] vg_li6]# df -h

Filesystem                          Size  Used Avail Use% Mounted on

/dev/mapper/vg_li6-lv_root  50G  20G  27G  43% /

tmpfs                                3.9G  232K  3.9G  1% /dev/shm

/dev/sda1                            485M  39M  421M  9% /boot

/dev/mapper/vg_li6-lv_home  26G  173M  24G  1% /home

目标是通過外圍虛拟化環境,增加存儲空間,并且将增加的空間轉移到/目錄下。也就是增加在vg_li6虛拟磁盤組的lv_root邏輯卷下面。

2、增加磁盤空間和分區

聯系負責虛拟化的同僚,為虛拟機增加虛拟磁盤sdb。通過fdisk指令,可以比較清晰的檢視到。

[[email protected] ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes

255 heads, 63 sectors/track, 10443 cylinders

(篇幅原因,有省略……)

Disk /dev/sdb: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x00000000

使用fdisk指令進行格式化。

[[email protected] ~]# 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 0x44e9daf0.

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)

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)

p

Partition number (1-4): 1

First cylinder (1-13054, default 1):

Using default value 1

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

Using default value 13054

Command (m for help): w –寫入磁盤分區資訊

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

檢視fdisk結果。

[[email protected] ~]# fdisk -l

Disk /dev/sda: 85.9 GB, 85899345920 bytes

255 heads, 63 sectors/track, 10443 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: 0x00064fe0

Device Boot      Start        End      Blocks  Id  System

/dev/sda1  *          1          64      512000  83  Linux

Partition 1 does not end on cylinder boundary.

/dev/sda2              64      10444    83373056  8e  Linux LVM

(篇幅原因,有省略……)

Disk /dev/sdb: 107.4 GB, 107374182400 bytes

255 heads, 63 sectors/track, 13054 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: 0x44e9daf0

Device Boot      Start        End      Blocks  Id  System

/dev/sdb1              1      13054  104856223+  83  Linux

3、LVM管理設定

總的步驟應該是這樣:首先需要讓LVM認可/dev/sdb1是一個可用的PV(Physical Volume),之後将其加入到VG裡面去,補充VG資源。最後拓展lv_root磁盤空間。

目前LVM情況如下:

lvm> vgs

VG              #PV #LV #SN Attr  VSize  VFree

vg_li6  1  3  0 wz--n- 79.51g    0

lvm> lvs

LV      VG              Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert

lv_home vg_li6 -wi-ao---- 25.63g

lv_root vg_li6 -wi-ao---- 50.00g

lv_swap vg_li6 -wi-ao----  3.88g

lvm> pvs

PV        VG              Fmt  Attr PSize  PFree

/dev/sda2  vg_li6 lvm2 a--  79.51g    0

此時的pv隻有一個/dev/sda2,全部用滿。首先建立/dev/sdb1作為PV。

[[email protected] ~]# pvcreate /dev/sdb1

dev_is_mpath: failed to get device for 8:17

Physical volume "/dev/sdb1" successfully created

[[email protected] ~]# pvs

PV        VG              Fmt  Attr PSize  PFree

/dev/sda2  vg_li6 lvm2 a--  79.51g      0

/dev/sdb1                  lvm2 a--  100.00g 100.00g

往VG裡面增加PV,作為空間拓容。

lvm> vgs

VG              #PV #LV #SN Attr  VSize  VFree

vg_li6  1  3  0 wz--n- 79.51g    0

lvm> vgextend vg_li6 /dev/sdb1

Volume group "vg_li6" successfully extended

lvm> vgs

VG              #PV #LV #SN Attr  VSize  VFree

vg_li6  2  3  0 wz--n- 179.50g 100.00g –剩餘空間增加

lvm> pvs

PV        VG              Fmt  Attr PSize  PFree

/dev/sda2  vg_li6 lvm2 a--  79.51g      0

/dev/sdb1  vg_li6 lvm2 a--  100.00g 100.00g

拓展lv_root空間。

[[email protected] vg_li6]# lvextend -L +70G /dev/vg_li6/lv_root

Extending logical volume lv_root to 120.00 GiB

Logical volume lv_root successfully resized

4、檔案系統識别

注意:此時檔案系統并沒有認可本次增加。

[[email protected] vg_li6]# df -h

Filesystem                          Size  Used Avail Use% Mounted on

/dev/mapper/vg_li6-lv_root  50G  20G  27G  43% /

tmpfs                                3.9G  232K  3.9G  1% /dev/shm

/dev/sda1                            485M  39M  421M  9% /boot

/dev/mapper/vg_li6-lv_home  26G  173M  24G  1% /home

需要使用resize2fs指令進行重新整理。

[[email protected] vg_li6]# resize2fs /dev/vg_li6/lv_root

resize2fs 1.41.12 (17-May-2010)

Filesystem at /dev/vg_li6/lv_root is mounted on /; on-line resizing required

old desc_blocks = 4, new_desc_blocks = 8

Performing an on-line resize of /dev/vg_li6/lv_root to 31457280 (4k) blocks.

The filesystem on /dev/vg_li6/lv_root is now 31457280 blocks long.

[[email protected] vg_li6]# df -h

Filesystem                          Size  Used Avail Use% Mounted on

/dev/mapper/vg_li6-lv_root  119G  20G  93G  18% /

tmpfs                                3.9G  232K  3.9G  1% /dev/shm

/dev/sda1                            485M  39M  421M  9% /boot

/dev/mapper/vg_li6-lv_home  26G  173M  24G  1% /home

5、結論

在之前筆者的文章中,比較詳細的介紹過LVM的相關内容。本文從操作步驟的角度,逐漸介紹增加LVM空間的方法。

linux lvm擴容硬碟,Linux LVM空間擴容示範