這篇文章基本是之前老東家公司上班時候一直用的linux磁碟擴容,採用fdisk,後邊改成了parted進行擴容。 這裡也順便記錄一下用fdisk擴容方法。
注意:未防止意外發生,請提前備份要操作的磁碟,避免操作失敗導致資料丟失。
[root@localhost ~]# df -lh //查看擴容分區所在的硬碟名稱,這裡以home對應的/dev/sdb1 為例。
檔案系統 容量 已用 可用 已用% 掛載點
/dev/sda1 9.7G 2.6G 6.7G 28% /
tmpfs 252M 0 252M 0% /dev/shm
/dev/sdb1 9.9G 172M 9.2G 2% /home
[root@localhost ~]# fdisk /dev/sdb //輸入fdisk /dev/sdb 請確認擴容分區對應的硬碟名稱,以免誤刪其他分區!
The number of cylinders for this disk is set to 41610.
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): d //輸入d刪除分區
Selected partition 1 //顯示要刪除的分區號,因為只有一個分區,所以顯示1
Command (m for help): n //輸入n建立分區
Command action
e extended
p primary partition (1-4)
p //輸入p,選擇要操作的項目
Partition number (1-4): 1 //輸入要建立的分區號,這裡輸入1
First cylinder (1-41610, default 1): //選擇要截止的磁碟位置,這裡直接按斷行符號,全部增加。
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-41610, default 41610): //直接按斷行符號。
Using default value 41610
Command (m for help): w //輸入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.
輸入reboot重啟伺服器:
[root@localhost ~]# reboot //輸入reboot重啟伺服器
重啟伺服器後執行以下命令:
[root@localhost ~]# resize2fs -f /dev/sdb1 //輸入resize2fs -f /dev/sdb1,sdb1這裡指要擴容分區對應的硬碟分區名稱
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/sdb1 is mounted on /home; on-line resizing required
Performing an on-line resize of /dev/sdb1 to 5242852 (4k) blocks.
The filesystem on /dev/sdb1 is now 5242852 blocks long.
[root@localhost ~]# df –lh //再用df –lh 查詢下home分區大小,可以看到已經增加了。
檔案系統 容量 已用 可用 已用% 掛載點
/dev/sda1 9.7G 2.6G 6.7G 28% /
tmpfs 252M 0 252M 0% /dev/shm
/dev/sdb1 20G 177M 19G 1% /home
至此,擴容已經完成。另外如果個別有顯示不正確的朋友,可以再進一步進行重啟,避免df 顯示不正確。
fdisk 的說明
當我們通過 fdisk 裝置,進入相應裝置的操作時,會發現有如下的提示;以 fdisk /dev/sda 裝置為例,以下同;
[root@localhost beinan]# fdisk /dev/sda
Command (m for help): 在這裡按m ,就會輸出協助;
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition 註:這是刪除一個分區的動作;
l list known partition types 註:l是列出分區類型,以供我們設定相應分區的類型;
m print this menu 註:m 是列出協助資訊;
n add a new partition 註:添加一個分區;
o create a new empty DOS partition table
p print the partition table 註:p列出分區表;
q quit without saving changes 註:不儲存退出;
s create a new empty Sun disklabel
t change a partition's system id 註:t 改變分區類型;
u change display/entry units
v verify the partition table
w write table to disk and exit 註:把分區表寫入硬碟並退出;
x extra functionality (experts only) 註:擴充應用,專家功能;
其實我們常用的只有注有中文的,其它的功能我們不常用(呵,主要是我不會用,否則早會賣弄一下了);x擴充功能,也不是常用的;一般的情況下只要懂得 d l m p q t w 就行了;