天天看點

Centos 7.5利用LVM實作動态擴容大小Centos 7.5 利用LVM實作動态擴容大小

Centos 7.5 利用LVM實作動态擴容大小

作業系統:Centos 7.5 64位

說明:當有在作業系統時發現磁盤空間不足時,可以通過增加磁盤空間大小來滿足,但是以哪種方式來增加可能有不同方法,現在針對作業系統在安裝采用LVM方式來動态調整磁盤空間大小,

知識點解釋:

 LVM是邏輯盤卷管理(Logical VolumeManager)的簡稱,它是Linux環境下對磁盤分區進行管理的一種機制,LVM是建立在硬碟和分區之上的一個邏輯層,來提高磁盤分區管理的靈活性。通過LVM系統管理者可以輕松管理磁盤分區,如:将若幹個磁盤分區連接配接為一個整塊的卷組(volumegroup),形成一個存儲池。管理者可以在卷組上随意建立邏輯卷組(logicalvolumes),并進一步在邏輯卷組上建立檔案系統。管理者通過LVM可以友善的調整存儲卷組的大小,并且可以對磁盤存儲按照組的方式進行命名、管理和配置設定

Linux的LVM非常強大,可以在生産運作系統上面直接線上擴充硬碟分區,可以把分區umount以後收縮分區大小,還可以在系統運作過程中把一個分區從一塊硬碟搬到另一塊硬碟上面去等等,簡直就像變魔術,而且這一切都可以在一個繁忙運作的系統上面直接操作,不會對你的系統運作産生任何影響,很安全。

LVM使用有局限性。雖然能很友善的擴容和縮容磁盤的空間(擴容磁盤大小,檔案不丢失),但是一旦出現問題,資料丢失,想要恢複資料就有點困難!

三步:

①建立一個實體分區-搞成實體卷②制作成一個卷組③劃分成邏輯卷

Centos 7.5利用LVM實作動态擴容大小Centos 7.5 利用LVM實作動态擴容大小

1.如果在安裝系統時選擇是以LVM方式安裝的話,可以通過已經安裝好後系統磁盤查詢

[[email protected] ~]# df -h

檔案系統                 容量  已用  可用 已用% 挂載點

/dev/mapper/centos-root   70G  9.4G   61G   14% /

devtmpfs                 4.8G     0  4.8G    0% /dev

tmpfs                    4.9G     0  4.9G    0% /dev/shm

tmpfs                    4.9G  9.5M  4.8G    1% /run

tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup

/dev/sda1               1014M  171M  844M   17% /boot

/dev/mapper/centos-home   27G   37M   27G    1% /home

tmpfs                    984M   12K  984M    1% /run/user/42

tmpfs                    984M     0  984M    0% /run/user/0

說明:centos-root 和centos-home 是通過LVM方式來配置的

2、使用df  -T -h指令檢視作業系統的檔案類型

[[email protected] ~]#  df  -T -h

檔案系統                類型      容量  已用  可用 已用% 挂載點

/dev/mapper/centos-root xfs        70G  9.4G   61G   14% /

devtmpfs                devtmpfs  4.8G     0  4.8G    0% /dev

tmpfs                   tmpfs     4.9G     0  4.9G    0% /dev/shm

tmpfs                   tmpfs     4.9G  9.5M  4.8G    1% /run

tmpfs                   tmpfs     4.9G     0  4.9G    0% /sys/fs/cgroup

/dev/sda1               xfs      1014M  171M  844M   17% /boot

/dev/mapper/centos-home xfs        27G   37M   27G    1% /home

tmpfs                   tmpfs     984M   12K  984M    1% /run/user/42

tmpfs                   tmpfs     984M     0  984M    0% /run/user/0

說明:通過查詢發現作業系統的檔案格式是:xfs

3.檢視卷組名稱及卷組使用情況  vgdisplay

[[email protected] ~]#  vgdisplay

  --- Volume group ---

  VG Name               centos --VG名稱

  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               98.99 GiB --總共的空間大小

  PE Size               4.00 MiB

  Total PE              25342

  Alloc PE / Size       25341 / <98.99 GiB

  Free  PE / Size       1 / 4.00 MiB

  VG UUID               oCJsUm-wF9P-a0Pc-rLvl-aCa1-0Inr-psWPzn

4.檢視目前邏輯卷的空間狀态,如下内容:/ 70G、/home 26G、swap 2G

[[email protected] ~]# lvdisplay

  --- Logical volume ---

  LV Path                 /dev/centos/root

  LV Name                root

  VG Name                centos

  LV UUID                0F9zrD-AfsY-teOk-OLqB-ZKa7-6fcZ-aVQ0VM

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:41 +0800

  LV Status              available

  # open                 1

  LV Size                70.00 GiB

  Current LE             17920

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:0

  --- Logical volume ---

  LV Path                /dev/centos/home

  LV Name                home

  VG Name                centos

  LV UUID                v64Mg2-gDRG-UGoe-InX6-qqsG-GuiD-eRrcs8

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:42 +0800

  LV Status              available

  # open                 1

  LV Size                <26.99 GiB

  Current LE             6909

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:2

  --- Logical volume ---

  LV Path                /dev/centos/swap

  LV Name                swap

  VG Name                centos

  LV UUID                0le8V4-P7XQ-jtC7-7xtY-wKUv-dcNe-mJrB4T

  LV Write Access        read/write

  LV Creation host, time localhost.localdomain, 2018-09-14 17:08:43 +0800

  LV Status              available

  # open                 2

  LV Size                2.00 GiB

  Current LE             512

  Segments               1

  Allocation             inherit

  Read ahead sectors     auto

  - currently set to     8192

  Block device           253:1

5.現在感覺/ 70G不夠用,想擴容增加300G,新插入一塊300G硬碟;用‘fdisk -l’檢視

[[email protected] ~]# fdisk -l

磁盤 /dev/sda:107.4 GB, 107374182400 位元組,209715200 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤标簽類型:dos

磁盤辨別符:0x0004c640

   裝置 Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     2099199     1048576   83  Linux

/dev/sda2         2099200   209707007   103803904   8e  Linux LVM

磁盤 /dev/sdb:322.1 GB, 322122547200 位元組,629145600 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤 /dev/mapper/centos-root:75.2 GB, 75161927680 位元組,146800640 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤 /dev/mapper/centos-swap:2147 MB, 2147483648 位元組,4194304 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤 /dev/mapper/centos-home:29.0 GB, 28978446336 位元組,56598528 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

[[email protected] ~]# df -h

檔案系統                 容量  已用  可用 已用% 挂載點

/dev/mapper/centos-root   70G  9.4G   61G   14% /

devtmpfs                 4.8G     0  4.8G    0% /dev

tmpfs                    4.9G     0  4.9G    0% /dev/shm

tmpfs                    4.9G  9.5M  4.8G    1% /run

tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup

/dev/sda1               1014M  171M  844M   17% /boot

/dev/mapper/centos-home   27G   37M   27G    1% /home

tmpfs                    984M   12K  984M    1% /run/user/42

tmpfs                    984M     0  984M    0% /run/user/0

6、格式化/dev/sdb磁盤

[[email protected] ~]# fdisk /dev/sdb

歡迎使用 fdisk (util-linux 2.23.2)。

更改将停留在記憶體中,直到您決定将更改寫入磁盤。

使用寫入指令前請三思。

Device does not contain a recognized partition table

使用磁盤辨別符 0xbe0b32e5 建立新的 DOS 磁盤标簽。

指令(輸入 m 擷取幫助):p

磁盤 /dev/sdb:322.1 GB, 322122547200 位元組,629145600 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤标簽類型:dos

磁盤辨別符:0xbe0b32e5

   裝置 Boot      Start         End      Blocks   Id  System

指令(輸入 m 擷取幫助):m

指令操作

   a   toggle a bootable flag

   b   edit bsd disklabel

   c   toggle the dos compatibility flag

   d   delete a partition

   g   create a new empty GPT partition table

   G   create an IRIX (SGI) partition table

   l   list known partition types

   m   print this menu

   n   add a new partition

   o   create a new empty DOS partition table

   p   print the partition table

   q   quit without saving changes

   s   create a new empty Sun disklabel

   t   change a partition's system id

   u   change display/entry units

   v   verify the partition table

   w   write table to disk and exit

   x   extra functionality (experts only)

指令(輸入 m 擷取幫助):n

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p): p

分區号 (1-4,預設 1):

起始 扇區 (2048-629145599,預設為 2048):

将使用預設值 2048

Last 扇區, +扇區 or +size{K,M,G} (2048-629145599,預設為 629145599):

将使用預設值 629145599

分區 1 已設定為 Linux 類型,大小設為 300 GiB

指令(輸入 m 擷取幫助):w

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁盤。

7、将 /dev/sdb配置為LVM格式

[[email protected] ~]# fdisk /dev/sdb

歡迎使用 fdisk (util-linux 2.23.2)。

更改将停留在記憶體中,直到您決定将更改寫入磁盤。

使用寫入指令前請三思。

指令(輸入 m 擷取幫助):t

已選擇分區 1

Hex 代碼(輸入 L 列出所有代碼):l

Centos 7.5利用LVM實作動态擴容大小Centos 7.5 利用LVM實作動态擴容大小

Hex 代碼(輸入 L 列出所有代碼):8e

已将分區“Linux”的類型更改為“Linux LVM”

指令(輸入 m 擷取幫助):p

磁盤 /dev/sdb:322.1 GB, 322122547200 位元組,629145600 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤标簽類型:dos

磁盤辨別符:0xbe0b32e5

   裝置 Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048   629145599   314571776   8e  Linux LVM

指令(輸入 m 擷取幫助):w

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁盤。

8、查詢配置為LVM格式後結果如下:

[[email protected] ~]# fdisk -l

磁盤 /dev/sda:107.4 GB, 107374182400 位元組,209715200 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤标簽類型:dos

磁盤辨別符:0x0004c640

   裝置 Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     2099199     1048576   83  Linux

/dev/sda2         2099200   209707007   103803904   8e  Linux LVM

磁盤 /dev/sdb:322.1 GB, 322122547200 位元組,629145600 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤标簽類型:dos

磁盤辨別符:0xbe0b32e5

   裝置 Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048   629145599   314571776   8e  Linux LVM

磁盤 /dev/mapper/centos-root:75.2 GB, 75161927680 位元組,146800640 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤 /dev/mapper/centos-swap:2147 MB, 2147483648 位元組,4194304 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁盤 /dev/mapper/centos-home:29.0 GB, 28978446336 位元組,56598528 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/實體):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

9.将新加的分區 /dev/sdb1 建立為實體卷

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

  Physical volume "/dev/sdb1" successfully created.

10.給卷組‘centos ’擴容,将實體卷 /dev/sdb1 擴充至‘centos ’卷組

[[email protected] ~]# vgextend centos /dev/sdb1

  Volume group "centos" successfully extended

10.此時卷組‘centos ’有300G空餘空間,及 /dev/sdb1,将其全部擴充至 /home

[email protected] ~]# lvextend -l +100%FREE /dev/mapper/centos-home

  Size of logical volume centos/home changed from <26.99 GiB (6909 extents) to <326.99 GiB (83709 extents).

  Logical volume centos/home successfully resized.

11.發現擴充錯分區了,把第10步的增加空間給縮小回去到原來值,26G

[[email protected] ~]# lvreduce -L -300G /dev/mapper/centos-home

  WARNING: Reducing active and open logical volume to <26.99 GiB.

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce centos/home? [y/n]: yes

  Size of logical volume centos/home changed from <326.99 GiB (83709 extents) to <26.99 GiB (6909 extents).

  Logical volume centos/home successfully resized.

12.重新把PV中的300G多餘空間配置設定給LV為root

[[email protected] ~]# lvextend -l +100%FREE /dev/mapper/centos-root

  Size of logical volume centos/root changed from 70.00 GiB (17920 extents) to 370.00 GiB (94720 extents).

  Logical volume centos/root successfully resized.

13.此時300G磁盤雖已擴充至 /root,但并沒寫入檔案系統;進入 /etc/fstab 确認 /root 檔案系統--->xfs檔案系統

[[email protected] ~]# more /etc/fstab

#

# /etc/fstab

# Created by anaconda on Fri Sep 14 17:08:44 2018

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/centos-root /                              xfs     defaults        0 0

UUID=28137713-1148-49bc-b74d-7ac9bdf0bc1b /boot                   xfs     defaults        0 0

/dev/mapper/centos-home /home                   xfs     defaults        0 0

/dev/mapper/centos-swap swap                    swap    defaults        0 0

14、用xfs_growfs 指令把檔案寫入系統中,使擴容生效

[[email protected] ~]# xfs_growfs /dev/mapper/centos-root

meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=4587520 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=0 spinodes=0

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

         =                       sunit=0      swidth=0 blks

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

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

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

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

data blocks changed from 18350080 to 96993280

說明:若使用ext4檔案格式的,是使用resize2fs指令來生效

15.檢視擴容後的硬碟空間小,如下内容則說明,已實作擴容。大功告成!

[[email protected] ~]# df -h

檔案系統                 容量  已用  可用 已用% 挂載點

/dev/mapper/centos-root  370G  9.4G  361G    3% /

devtmpfs                4.8G     0  4.8G    0% /dev

tmpfs                    4.9G     0  4.9G    0% /dev/shm

tmpfs                    4.9G  9.5M  4.8G    1% /run

tmpfs                    4.9G     0  4.9G    0% /sys/fs/cgroup

/dev/sda1               1014M  171M  844M   17% /boot

/dev/mapper/centos-home   27G   37M   27G    1% /home

tmpfs                    984M   12K  984M    1% /run/user/42

tmpfs                    984M     0  984M    0% /run/user/0

本次擴容指令彙總:

1.建立分區                                                #fdisk /dev/sdb

2.建立實體卷                                             #pvcreat /dev/sdb1

3.檢視卷組名稱及使用情況                         #vgdisplay

4.将實體卷擴充到卷組                               #vgextend cl /dev/sdb1   (此處‘cl’是卷組名稱)

5.将卷組中空閑空間擴充到 /home           #lvextend -l +100%FREE /dev/mapper/cl-home

6.重新整理檔案系統是擴容生效 xfs_growfs /dev/mapper/centos-root

7.若是ext4檔案格式使用擴容生效                     #resize2fs /dev/mapper/centos-home

相關LVM知識點如下:

這裡總結下擴充與縮小LV的實施順序:

擴充LV

1、解除安裝LV

指令:umount  “挂載目錄”

2、擴充LV

指令:lvextend -L +500m  /dev/lv00/lv01 或者 lvresize -L 5G  /dev/lv00/lv01

lvextend 表示增加500M,lvresize表示增加到5G,是調整後的容量。

3、檢查檔案系統

指令:fsck -f  /dev/vg00/lv01

4、重建檔案系統

  指令:resize2fs  /dev/lv00/lv01

這一步很關鍵,重建檔案系統不用擔心LV的資料會丢失,當然,以防萬一,也可以先備份下LV裡的資料。

5、挂載LV

指令:mount  /dev/lv001/lv01  /opt

縮小LV

  縮小LV需要事先知道LV中的資料容量大小,縮小容量值不應超過剩餘空間容量,而且要知道減少LV容量後的新大小。可以通過lvscan來檢視LV容量,通過vgdisplay來檢視剩餘容量。

1、解除安裝LV

指令:umount  “挂載目錄”

2、檢查檔案系統

指令:fsck -f  /dev/lv00/lv01

3、調整LV大小并重建檔案系統

指令:resize2fs  /dev/vg00/lv01  2G

4、減小LV容量

指令:lvreduce -L -3G  /dev/vg00/lv01

5、挂載LV

  指令:mount  /dev/lv001/lv01  /opt

繼續閱讀