一.查看系統狀態
1.查看Linux系統上的文件系統的磁盤使用情況,發現/目錄快滿了。
[root@Redhat6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_starnet-lv_root
18G 14G 2.7G 84% /
tmpfs 5.2G 224K 5.2G 1% /dev/shm
/dev/sda1 477M 40M 412M 9% /boot
2.查看掛載硬盤及分區情況,發現LINUX磁盤實際53.7GB,實際掛載的才20G左右。
[root@Redhat6 ~]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x00003ec7Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVMDisk /dev/mapper/vg_starnet-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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
Disk /dev/mapper/vg_starnet-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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
3.查看PV,發現只有一個20G的/dev/sda2
[root@Redhat6 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_starnet
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID AYnwsY-Il90-KuTY-Sh2y-IwMb-7CiY-orm4w5
查看VG
[root@Redhat6 ~]# vgdisplay
--- Volume group ---
VG Name vg_starnet
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID oAVlIk-yvc0-sGc9-RAPi-ceYQ-tFS3-PAGEgn
二.創建新的主分區
fdisk /dev/sda依次輸入(n->p ->3->回車->回車-> t->8e -> w)
Command (m for help): n # 新建分區
Command action
e extended
p primary partition (1-4)
p # 創建一個主分區
Partition number (1-4): 3 #已經有/dev/sda1和/dev/sda2,所以從3開始
First cylinder (2611-6527, default 2611): #設置起始柱面,回車選擇默認
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-6527, default 6527): #設置終止柱面,回車選擇默認
Using default value 6527Command (m for help): t #設置分區類型
Partition number (1-4): 3
Hex code (type L to list codes): 8e #設置分區類型爲8e
Changed system type of partition 3 to 8e (Linux LVM)Command (m for help): w #保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
三.讓內核立刻讀取最新的分區表,而不需要重啓系統
[root@Redhat6 ~]# partprobe
查看分區,已經多了一個/dev/sda3
[root@Redhat6 ~]# fdisk -lDisk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x00003ec7Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 6527 31456607+ 8e Linux LVMDisk /dev/mapper/vg_starnet-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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
Disk /dev/mapper/vg_starnet-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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
查看文件系統,/目錄剩餘空間沒變。還需待擴展。
[root@Redhat6 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_starnet-lv_root
18G 14G 2.7G 84% /
tmpfs 5.2G 76K 5.2G 1% /dev/shm
/dev/sda1 477M 40M 412M 9% /boot
四.創建PV
[root@Redhat6 ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
查看PV,可以看到多了個/dev/sda3
[root@Redhat6 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_starnet
PV Size 19.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4994
Free PE 0
Allocated PE 4994
PV UUID AYnwsY-Il90-KuTY-Sh2y-IwMb-7CiY-orm4w5
"/dev/sda3" is a new physical volume of "30.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 30.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 7nBK0C-5fqV-z1tM-IFn2-vFoh-UiQK-s4NIDA
五.擴容VG
[root@Redhat6 ~]# vgdisplay
--- Volume group ---
VG Name vg_starnet
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 19.51 GiB
PE Size 4.00 MiB
Total PE 4994
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 0 / 0
VG UUID oAVlIk-yvc0-sGc9-RAPi-ceYQ-tFS3-PAGEgn
將新建的/dev/sda3增加到vg_starnet中
[root@Redhat6 ~]# vgextend vg_starnet /dev/sda3
Volume group "vg_starnet" successfully extended
再執行vgdisplay,可以看到VG已經增加到50G了。
[root@Redhat6 ~]# vgdisplay
--- Volume group ---
VG Name vg_starnet
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 49.50 GiB
PE Size 4.00 MiB
Total PE 12673
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 7679 / 30.00 GiB
VG UUID oAVlIk-yvc0-sGc9-RAPi-ceYQ-tFS3-PAGEgn
五.擴容LV
每個PE大小4M,30G*1024/4= 7680. 最多有7680個PE。
[root@Redhat6 ~]# lvresize -l +7679 /dev/vg_starnet/lv_root
六.刷新文件系統大小
[root@Redhat6 ~]# resize2fs /dev/vg_starnet/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg_starnet/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/vg_starnet/lv_root to 12452864 (4k) blocks.
The filesystem on /dev/vg_starnet/lv_root is now 12452864 blocks long.
查看文件系統,根目錄的可用空間已經增加。
[root@Redhat6 ~]# df -l
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/vg_starnet-lv_root
48901412 14254964 32170940 31% /
tmpfs 5444328 76 5444252 1% /dev/shm
/dev/sda1 487652 40801 421251 9% /boot
名詞解釋:
LVM 的全名是 Logical Volume Manager,中文可以翻譯作邏輯卷軸管理員。之所以稱爲『卷軸』可能是因爲可以將 filesystem 像卷軸一樣伸長或縮短之故吧!LVM 的作法是將幾個實體的 partitions (或 disk) 透過軟件組合成爲一塊看起來是獨立的大磁碟 (VG) ,然後將這塊大磁碟再經過分割成爲可使用分割槽 (LV), 最終就能夠掛載使用了。但是爲什麼這樣的系統可以進行 filesystem 的擴充或縮小呢?其實與一個稱爲 PE 的項目有關! 底下我們就得要針對這幾個項目來好好聊聊!
- Physical Volume, PV, 實體卷軸
我們實際的 partition 需要調整系統識別碼 (system ID) 成爲 8e (LVM 的識別碼),然後再經過 pvcreate 的命令將他轉成 LVM 最底層的實體卷軸 (PV) ,之後才能夠將這些 PV 加以利用! 調整 system ID 的方是就是透過 fdisk 啦!
- Volume Group, VG, 卷軸羣組
所謂的 LVM 大磁碟就是將許多 PV 整合成這個 VG 的東西就是啦!所以 VG 就是 LVM 組合起來的大磁碟!這麼想就好了。 那麼這個大磁碟最大可以到多少容量呢?這與底下要說明的 PE 有關喔~因爲每個 VG 最多僅能包含 65534 個 PE 而已。 如果使用 LVM 默認的參數,則一個 VG 最大可達 256GB 的容量啊!(參考底下的 PE 說明)
- Physical Extend, PE, 實體延伸區塊
LVM 默認使用 4MB 的 PE 區塊,而 LVM 的 VG 最多僅能含有 65534 個 PE ,因此默認的 LVM VG 會有 4M*65534/(1024M/G)=256G。 這個 PE 很有趣喔!他是整個 LVM 最小的儲存區塊,也就是說,其實我們的文件數據都是藉由寫入 PE 來處理的。 簡單的說,這個 PE 就有點像文件系統裏面的 block 大小啦。 這樣說應該就比較好理解了吧?所以調整 PE 會影響到 VG 的最大容量喔!
- Logical Volume, LV, 邏輯卷軸
最終的 VG 還會被切成 LV,這個 LV 就是最後可以被格式化使用的類似分割槽的咚咚了!那麼 LV 是否可以隨意指定大小呢? 當然不可以!既然 PE 是整個 LVM 的最小儲存單位,那麼 LV 的大小就與在此 LV 內的 PE 總數有關。 爲了方便使用者利用 LVM 來管理其系統,因此 LV 的裝置檔名通常指定爲『 /dev/vgname/lvname 』的樣式!
參考資料:
鳥哥的Linux私房菜: http://cn.linux.vbird.org/linux_basic/0420quota_3.php