前言 :
相信在使用LVM上除了設定好能用之外,有時後也會需要增加或是減少的容量大小。因此今天就來介紹一下當如果遇到容量要增減時該如何設定。
設定 :
步驟大概歸納以下幾點 :
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 新增一個4G的partition
[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.4將PV加入到已存在的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
#會發現不論是VG、PE、Alloc PE或Free 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.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.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.
LVM-LV的容量增減設定(下)
全文同步於FAQ-BOOK
IT鐵人文章分享