LVM-LV的容量增減設定

由 Derek 發表於 十一月 4, 2011 / 尚無評論

 

前言 :

相信在使用LVM上除了設定好能用之外,有時後也會需要增加或是減少的容量大小。因此今天就來介紹一下當如果遇到容量要增減時該如何設定。

 

設定 :

1. 增加LV容量

1.1 增加LV的容量大小其實不難,跟著以下的步驟做就可以了。

 

步驟大概歸納以下幾點 :

1.1.1 fdisk新增一個partition

1.1.2 建立新的PV(使用指令pvcreate)

1.1.3 PV加入到已存在的VG (使用指令vgextend)

1.1.4 增加LV的大小(使用指令lvresize)

1.1.5 確實的將檔案系統容量增加(使用指令resize2fs)

 

1.2 新增一個4Gpartition


[root@localhost ~]# fdisk /dev/sda

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): n

First cylinder (1774-2610, default 1774):

Using default value 1774

Last cylinder or +size or +sizeM or +sizeK (1774-2610, default 2610): +4000M

 

Command (m for help): p

 

Disk /dev/sda: 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/sda9            1774        2260     3911796   83  Linux

 

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.

Syncing disks.

#提示設定需要重開機後才生效,若不想重開幾可使用partprobe(強制生效)

[root@localhost ~]# partprobe

[root@localhost ~]# fdisk -l

Device Boot      Start         End      Blocks   Id  System

/dev/sda9            1774        2260     3911796   83  Linux

 

1.3 建立新的PV


[root@localhost ~]# pvcreate  /dev/sda9        #建立新的PV

  Physical volume "/dev/sda9" successfully created

[root@localhost ~]# pvscan

  PV /dev/sda5   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda6   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda7   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda8   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda9                      lvm2 [3.73 GB]  #會看到多出此項還沒設定的PV

  Total: 5 [11.17 GB] / in use: 4 [7.44 GB] / in no VG: 1 [3.73 GB]

 

1.4PV加入到已存在的VG


[root@localhost ~]# vgextend faqvg /dev/sda9

  /dev/cdrom: open failed: Read-only file system

  /dev/cdrom: open failed: Read-only file system

  Volume group "faqvg" successfully extended

[root@localhost ~]# vgdisplay  

  --- Volume group ---

  VG Name               faqvg

  VG Size               11.16 GB

  PE Size               16.00 MB

  Total PE              714

  Alloc PE / Size       476 / 7.44 GB

  Free  PE / Size       238 / 3.72 GB

  VG UUID               e2lS1t-7Rop-YHU4-qygQ-tDFq-LjVn-jOnv0h

#發現不論是VGPEAlloc PEFree  PE的大小都會增加。

 

1.5 增加LV的大小


[root@localhost ~]# lvresize -l +238 /dev/faqvg/faqlv

  Extending logical volume faqlv to 11.16 GB

  Logical volume faqlv successfully resized

-L  後面接容量大小,單位可為M,G,T。最小單位為PE此數量必須為PE的倍數,要不系統會以

    最相近的容量為主。

-l  面接的為PE的個數,可參考vgdisplay所顯示出的PE數。

 [root@localhost ~]# lvdisplay

  --- Logical volume ---

  LV Name                /dev/faqvg/faqlv

  VG Name                faqvg

  LV Size                11.16 GB          #確實有增加了

[root@localhost ~]# df -h /mnt/lvm

Filesystem           1K-blocks      Used Available Use% Mounted on

/dev/mapper/faqvg-faqlv

                       7.6G    148M   7.1G   3% /mnt/lvm

#這邊發現在LV中確實有增加到11.16GB,但是檔案系統卻沒有增加。因此還需要在設定一下

 

1.6確實的將檔案系統容量增加


[root@localhost ~]# resize2fs /dev/faqvg/faqlv

                  # resize2fs [-f] [device] [size]

resize2fs 1.39 (29-May-2006)

Filesystem at /dev/faqvg/faqlv is mounted on /mnt/lvm; on-line resizing required

Performing an on-line resize of /dev/faqvg/faqlv to 2924544 (4k) blocks.

The filesystem on /dev/faqvg/faqlv is now 2924544 blocks long.

參數說明:

-f      :強制進行 resize

[device]:裝置的檔案名稱;

[size]  :可以加也可以不加。如果加上 size 的話,那麼就必須要給予一個單位,

          譬如 M, G 等等。如果沒有 size 的話,那麼預設使用『整個 partition

          的容量來處理

[root@localhost ~]# df -h /mnt/lvm

Filesystem           1K-blocks      Used Available Use% Mounted on

/dev/mapper/faqvg-faqlv

                      11.5G    1.5G  10G   2% /mnt/lvm

#如此一來檔案系統的容量也有增加了

 

 

2.減少LV容量

2.1 就來假設要減少LV的容量大小,將/dev/sda5移除。

 

步驟大概歸納以下幾點 :

2.1.1 計算檔案系統移除後的總容量和降低檔案系統容量(使用指令resize2fs)

2.1.2 減少LV的大小(使用指令lvresize)

2.1.3 PV移除VG之外 (使用指令vgextend)

2.1.4 PV中移除partition (使用指令pvcreate)

2.1.5 fdisk刪除一個partition

 

2.2計算檔案系統移除後的總容量和降低檔案系統容量


[root@localhost ~]# pvscan

  PV /dev/sda5   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda6   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda7   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda8   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda9   VG faqvg   lvm2 [3.72 GB / 0    free]

  Total: 5 [11.16 GB] / in use: 5 [11.16 GB] / in no VG: 0 [0   ]

#在這邊由於要移除/dev/sda5,因此要算出剩下的總容量: 11.16-1.86=9.3

[root@localhost ~]# umount /mnt/lvm  #在縮小檔案系統時無法直接操作,所以先卸載

[root@localhost ~]# resize2fs /dev/faqvg/faqlv 9300M

resize2fs 1.39 (29-May-2006)

Please run 'e2fsck -f /dev/faqvg/faqlv' first.      #要進行磁碟檢查

[root@localhost ~]# e2fsck -f /dev/faqvg/faqlv      #磁碟檢查

e2fsck 1.39 (29-May-2006)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/dev/faqvg/faqlv: 11/1463040 files (9.1% non-contiguous), 83478/2924544 blocks

[root@localhost ~]# resize2fs /dev/faqvg/faqlv 9300M       #再執行一次resize2fs

resize2fs 1.39 (29-May-2006)

Resizing the filesystem on /dev/faqvg/faqlv to 2380800 (4k) blocks.

The filesystem on /dev/faqvg/faqlv is now 2380800 blocks long.

 

2.2.1檢查是否已降低系統檔案大小


[root@localhost ~]# mount /dev/faqvg/faqlv /mnt/lvm

[root@localhost ~]# df -h /mnt/lvm

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/faqvg-faqlv

                      9.0G  145M  8.4G   2% /mnt/lvm

2.3減少LV的大小

2.3.1 查看/dev/sda5PE


[root@localhost ~]# pvdisplay

  — Physical volume —

  PV Name               /dev/sda5

  VG Name               faqvg

  PV Size               1.87 GB / not usable 9.96 MB

  Allocatable           yes (but full)

  PE Size (KByte)       16384

  Total PE              119

  Free PE               0

  Allocated PE          119

  PV UUID               gXmwpU-0oP0-YSmt-rpSd-qj5H-cEYf-1xBcky

 

2.3.2 降低LV的容量,已知/dev/sda5PE數為119


[root@localhost ~]#  lvresize -l -119 /dev/faqvg/faqlv

  /dev/cdrom: open failed: Read-only file system

  WARNING: Reducing active and open logical volume to 9.30 GB

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce faqlv? [y/n]: y

  Reducing logical volume faqlv to 9.30 GB

  Logical volume faqlv successfully resized

#會提示說實際資料量比9.30GB小,此部份就輸入y繼續吧。

 

2.3.3 查看LV總容量是否有變小


[root@localhost ~]#  lvdisplay

  --- Logical volume ---

  LV Name                /dev/faqvg/faqlv

  VG Name                faqvg

  LV UUID                QMOpsQ-3k87-BHSx-7crB-Wrlh-Zx11-Hn332K

  LV Write Access        read/write

  LV Status              available

  # open                 1

  LV Size                9.30 GB

  Current LE             595

  Segments               5

  Allocation             inherit

  Read ahead sectors     auto

  – currently set to     256

  Block device           253:0

 

 

2.4PV移除VG之外

2.4.1 確認/dev/sda5PE沒被使用才可移除


[root@localhost ~]#  pvdisplay

  --- Physical volume ---

  PV Name               /dev/sda5

  VG Name               faqvg

  PV Size               1.87 GB / not usable 9.96 MB

  Allocatable           yes (but full)

  PE Size (KByte)       16384

  Total PE              119

  Free PE               0

  Allocated PE          119

  PV UUID               gXmwpU-0oP0-YSmt-rpSd-qj5H-cEYf-1xBcky

--- Physical volume ---

  PV Name               /dev/sda9

  VG Name               faqvg

  PV Size               3.73 GB / not usable 12.11 MB

  Allocatable           yes

  PE Size (KByte)       16384

  Total PE              238

  Free PE               119

  Allocated PE          119

  PV UUID               yxprOa-Gcvb-BHe8-LbOu-3Qxi-1sbC-WN3I4l

#現到空閒的PE都在/dev/sda9,而/dev/sda5PE都有在使用。因為要先搬移PE才可移除

 

2.4.2 搬移PE


[root@localhost ~]#  pvmove /dev/sda5 /dev/sda9

  /dev/sda5: Moved: 1.7%

  /dev/sda5: Moved: 100.0%

[root@localhost ~]# pvdisplay     #再次確PE

  --- Physical volume ---

  PV Name               /dev/sda5

  VG Name               faqvg

  PV Size               1.87 GB / not usable 9.96 MB

  Allocatable           yes

  PE Size (KByte)       16384

  Total PE              119

  Free PE               119

  Allocated PE          0

  PV UUID               gXmwpU-0oP0-YSmt-rpSd-qj5H-cEYf-1xBcky

--- Physical volume ---

  PV Name               /dev/sda9

  VG Name               faqvg

  PV Size               3.73 GB / not usable 12.11 MB

  Allocatable           yes (but full)

  PE Size (KByte)       16384

  Total PE              238

  Free PE               0

  Allocated PE          238

  PV UUID               yxprOa-Gcvb-BHe8-LbOu-3Qxi-1sbC-WN3I4l

 

2.4.3 faqvg中移除/dev/sda5


[root@localhost ~]#  vgreduce  faqvg /dev/sda5

  /dev/cdrom: open failed: Read-only file system

  Removed "/dev/sda5" from volume group "faqvg"

[root@localhost ~]# pvscan

  PV /dev/sda6   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda7   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda8   VG faqvg           lvm2 [1.86 GB / 0    free]

  PV /dev/sda9   VG faqvg           lvm2 [3.72 GB / 0    free]

  PV /dev/sda5                      lvm2 [1.87 GB]

  Total: 5 [11.17 GB] / in use: 4 [9.30 GB] / in no VG: 1 [1.87 GB]

 

2.5PV中移除partition


[root@localhost ~]#  pvremove /dev/sda5

  /dev/cdrom: open failed: Read-only file system

  Labels on physical volume "/dev/sda5" successfully wiped

[root@localhost ~]# pvscan  #查看PV會發現/dev/sda5已經被移除了。

  PV /dev/sda6   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda7   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda8   VG faqvg   lvm2 [1.86 GB / 0    free]

  PV /dev/sda9   VG faqvg   lvm2 [3.72 GB / 0    free]

  Total: 4 [9.30 GB] / in use: 4 [9.30 GB] / in no VG: 0 [0   ]

 

2.6刪除一個partition


[root@localhost ~]#  fdisk -l

/dev/sda5             798        1041     1959898+  83  Linux

#先查詢看看此partition是否存在

[root@localhost ~]# fdisk /dev/sda

Command (m for help): d

Partition number (1-9): 5

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.

Syncing disks.

#提示設定需要重開機後才生效,若不想重開幾可使用partprobe(強制生效)

[root@localhost ~]# partprobe

[root@localhost ~]# fdisk -l

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      104391   83  Linux

/dev/sda2              14         666     5245222+  83  Linux

/dev/sda3             667         797     1052257+  82  Linux swap / Solaris

/dev/sda4             798        2610    14562922+   5  Extended

/dev/sda6            1042        1285     1959898+  83  Linux

/dev/sda7            1286        1529     1959898+  83  Linux

/dev/sda8            1530        1773     1959898+  83  Linux

/dev/sda9            1774        2260     3911796   83  Linux

#生效之後在查看一下會發現/dev/sda5已經完全被移除了。

 

到這邊 LVM-LV的容量增減設定,大至上已經完成了。

 

 

3. 最後說明一下如何刪除所有建立的lvm


[root@localhost ~]# umount /mnt/lvm              #卸載LVM檔暗系統

[root@localhost ~]# lvremove /dev/faqvg/faqlv    #移除LV

Do you really want to remove active logical volume "faqlv"? [y/n]: y

  Logical volume "faqlv" successfully removed

[root@localhost ~]# vgchange -a n faqvg   

0 logical volume(s) in volume group "faqvg" now active

#使用 vgchange -a n VGname VGname 這個 VG 不具有 Active 的標誌

[root@localhost ~]# vgremove faqvg                #移除VG

  Volume group "faqvg" successfully removed

[root@localhost ~]# pvremove /dev/sda{5,6,7,8,9}  #移除PV

Labels on physical volume "/dev/hda5" successfully wiped

  Labels on physical volume "/dev/hda6" successfully wiped

  Labels on physical volume "/dev/hda7" successfully wiped

  Labels on physical volume "/dev/hda8" successfully wiped

  Labels on physical volume "/dev/hda9" successfully wiped

#最後請用fdisk修改所分割的磁區

 

如此一來就都刪除了。

 

 

相關文章

LVM 設定說明  

LVM OS安裝設定方式 

LVM系統快照設定教學

 

 

 

關於作者

一個半路殺出來的傻小子,憑著一股傻勁努力的學習、嘗試、分享。希望能用自己微薄之力,替IT界和資訊界盡一點心力。單憑一己之力始終還是有限,歡迎和我有相同理念的夥伴一同加入一同努力。

評論

此文章尚無評論。

發表評論

*