以下以 Red Hat AS4 為例。磁碟空間不夠時直覺做法是「擴大分區」,但若沒有採用邏輯卷管理(LVM),動態擴容非常困難。常見的三條路:
一是備份該分區的資料、刪除分區、重建更大的分區再還原。這做法風險最高,刪完可能直接開不了機。
二是建立一個新的邏輯分區,把檔案系統從舊分區複製過去,再改 fstab 用新分區取代舊的。
三是建立新邏輯分區並格式化,直接掛載到空間不足的位置使用。下面採用第三種,風險最低也最快。
sdb 是第二顆 SCSI 硬碟:
/sbin/fdisk /dev/sdb
Disk /dev/sdb: 73.5 GB, 73557090304 bytes
255 heads, 63 sectors/track, 8942 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 * 1 130 1044193+ 83 Linux
/dev/sdb2 131 391 2096482+ 82 Linux swap
/dev/sdb3 392 521 1044225 83 Linux
/dev/sdb4 522 8942 67641682+ 5 Extended
/dev/sdb5 522 586 522081 83 Linux
/dev/sdb6 587 651 522081 83 Linux
/dev/sdb7 652 1173 4192933+ 83 Linux
可見 sdb1、2、3 是主分區,sdb4 是擴展分區,sdb5~7 是邏輯分區。柱面編號連續,sdb7 只用到 1173,而擴展分區最大到 8942,也就是還有 7769 個柱面未使用。一個柱面 8225280 bytes 約 8M,換算下來約 62G 空間可用。
由於四個主分區(含擴展分區)都佔滿了,只能再切邏輯分區:
Command (m for help): n
First cylinder (1174-8942, default 1174):
Using default value 1174
Last cylinder or +size or +sizeM or +sizeK (1174-8942, default 8942): +10240M
看一下結果,多了一個 /dev/sdb8,柱面 1174 到 2419:
/dev/sdb8 1174 2419 10008463+ 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: 裝置或資源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
分完區先用 df -m 看,是看不到新空間的——因為還沒格式化、也還沒掛載。reboot 讓核心讀取新分區表。
/sbin/mkfs.ext3 /dev/sdb8mount /dev/sdb8 /home/develop
再用 df -m 確認:
/dev/sdb8 9621 54 9079 1% /home/develop
新的檔案系統已經掛上,可以正常使用了。若要開機自動掛載,記得把這條 mount 寫進 /etc/fstab。