在 Linux 下使用 fdisk 擴充套件分割槽容量

嶽永發表於2016-05-26

我們管理的伺服器可能會隨著業務量的不斷增長造成磁碟空間不足的情況,比如:共享檔案伺服器硬碟空間不足,在這個時候我們就需要增加磁碟空間,來滿足線上的業務;又或者我們在使用linux的過程中, 有時會因為安裝系統時分割槽不當導致有的分割槽空間不足,而有的分割槽空間過剩的情況,都可以是使用fdisk分割槽工具來動態調整分割槽的大小。

擴充套件磁碟空間

硬碟空間為20G,使用vSphere Client增加磁碟大小,需要再增加10G空間;

fdisk_l-20G

vshere_add

擴充套件完後,重啟系統,再次使用fdisk -l檢視,會發現硬碟空間變大了;

fdisk_l-30

重新建立分割槽,調整分割槽資訊

本次實驗主要對/dev/sda4這個分割槽擴充套件,如果是生產環境,請提前做好備份儲存到其他分割槽,雖然擴充套件分割槽大小不會導致資料丟失,安全起見,請提前做好備份;
首先模擬出一些資料:

[root@linuxprobe data]# mkdir test
[root@linuxprobe data]# echo "we are Linuxer" > linuxprobe
[root@linuxprobe data]# ll
total 24
-rw-r--r--. 1 root root    15 May 23 21:59 linuxprobe
drwx------. 2 root root 16384 May 23 15:07 lost+found
drwxr-xr-x. 2 root root  4096 May 23 21:51 test
[root@linuxprobe ~]# umount /dev/sda4          ### 解除安裝磁碟分割槽

若提示磁碟忙,使用fuser找出將正在使用磁碟的程式並結束掉;

fuser -m -v /data
fuser -m -v -i -k /data

使用fdisk工具先刪除/dev/sda4分割槽,然後建立新分割槽,注意開始的磁柱號要和原來的一致(是保證資料不丟失的關鍵步驟),結束的磁柱號預設回車使用全部磁碟。

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

WARNING: DOS-compatible mode is deprecated. It is strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p        ### 檢視分割槽表資訊

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        2611     8952832   83  Linux

Command (m for help): d           ### 刪除分割槽
Partition number (1-4): 4         ### 刪除第四個

Command (m for help): p       ### 再次檢視分割槽資訊,/dev/sda4已被刪除

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris

Command (m for help): n      ### 建立新的分割槽
Command action
   e   extended
   p   primary partition (1-4)
p             #建立為主分割槽
Selected partition 4
First cylinder (1497-3916, default 1497):          ### 經對比,正好和上一個磁碟柱一致,預設即可
Using default value 1497
Last cylinder, +cylinders or +size{K,M,G} (1497-3916, default 3916): 
Using default value 3916              ### 直接預設就可以

Command (m for help): p               ### 檢視分割槽表資訊

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        3916    19436582   83  Linux

Command (m for help): wp       ### 儲存並退出,如果建立有誤,直接退出不要儲存即可
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

fdisk-d

fdisk-p
重新建立分割槽後,需要重啟一下;

[root@linuxprobe ~]# init 6
[root@linuxprobe ~]# e2fsck -f /dev/sda4                ### 檢查分割槽資訊
[root@linuxprobe ~]# resize2fs -p /dev/sda4             ### 調整分割槽大小

重新掛載、檢視分割槽大小、資料:

df-hT_19G

可見/dev/sda4分割槽的空間大小已經增加了。

相關文章