我們管理的伺服器可能會随着業務量的不斷增長造成磁盤空間不足的情況,比如:共享檔案伺服器硬碟空間不足,在這個時候我們就需要增加磁盤空間,來滿足線上的業務;又或者我們在使用linux的過程中, 有時會因為安裝系統時分區不當導緻有的分區空間不足,而有的分區空間過剩的情況,都可以是使用fdisk分區工具來動态調整分區的大小。
<a target="_blank"></a>
硬碟空間為20g,使用vsphere client增加磁盤大小,需要再增加10g空間;
fdisk_l-20g
vshere_add
擴充完後,重新開機系統,再次使用fdisk -l檢視,會發現硬碟空間變大了;
fdisk_l-30
本次實驗主要對/dev/sda4這個分區擴充,如果是生産環境,請提前做好備份儲存到其他分區,雖然擴充分區大小不會導緻資料丢失,安全起見,請提前做好備份;
首先模拟出一些資料:
<code>[root@linuxprobe data]# mkdir test</code>
<code>[root@linuxprobe data]# echo "we are linuxer" > linuxprobe</code>
<code>[root@linuxprobe data]# ll</code>
<code>total 24</code>
<code>-rw-r--r--. 1 root root 15 may 23 21:59 linuxprobe</code>
<code>drwx------. 2 root root 16384 may 23 15:07 lost+found</code>
<code>drwxr-xr-x. 2 root root 4096 may 23 21:51 test</code>
<code>[root@linuxprobe ~]# umount /dev/sda4 ### 解除安裝磁盤分區</code>
若提示磁盤忙,使用fuser找出将正在使用磁盤的程式并結束掉;
<code>fuser -m -v /data</code>
<code>fuser -m -v -i -k /data</code>
使用fdisk工具先删除/dev/sda4分區,然後建立新分區,注意開始的磁柱号要和原來的一緻(是保證資料不丢失的關鍵步驟),結束的磁柱号預設回車使用全部磁盤。
<code>[root@linuxprobe ~]# fdisk /dev/sda</code>
<code></code>
<code>warning: dos-compatible mode is deprecated. it is strongly recommended to</code>
<code>switch off the mode (command 'c') and change display units to</code>
<code>sectors (command 'u').</code>
<code>command (m for help): p ### 檢視分區表資訊</code>
<code>disk /dev/sda: 32.2 gb, 32212254720 bytes</code>
<code>255 heads, 63 sectors/track, 3916 cylinders</code>
<code>units = cylinders of 16065 * 512 = 8225280 bytes</code>
<code>sector size (logical/physical): 512 bytes / 512 bytes</code>
<code>i/o size (minimum/optimal): 512 bytes / 512 bytes</code>
<code>disk identifier: 0x0005210c</code>
<code>device boot start end blocks id system</code>
<code>/dev/sda1 * 1 26 204800 83 linux</code>
<code>partition 1 does not end on cylinder boundary.</code>
<code>/dev/sda2 26 1301 10240000 83 linux</code>
<code>/dev/sda3 1301 1497 1572864 82 linux swap / solaris</code>
<code>/dev/sda4 1497 2611 8952832 83 linux</code>
<code>command (m for help): d ### 删除分區</code>
<code>partition number (1-4): 4 ### 删除第四個</code>
<code>command (m for help): p ### 再次檢視分區資訊,/dev/sda4已被删除</code>
<code>command (m for help): n ### 建立新的分區</code>
<code>command action</code>
<code>e extended</code>
<code>p primary partition (1-4)</code>
<code>p #建立為主分區</code>
<code>selected partition 4</code>
<code>first cylinder (1497-3916, default 1497): ### 經對比,正好和上一個磁盤柱一緻,預設即可</code>
<code>using default value 1497</code>
<code>last cylinder, +cylinders or +size{k,m,g} (1497-3916, default 3916):</code>
<code>using default value 3916 ### 直接預設就可以</code>
<code>command (m for help): p ### 檢視分區表資訊</code>
<code>/dev/sda4 1497 3916 19436582 83 linux</code>
<code>command (m for help): wp ### 儲存并退出,如果建立有誤,直接退出不要儲存即可</code>
<code>the partition table has been altered!</code>
<code>calling ioctl() to re-read partition table.</code>
<code>warning: re-reading the partition table failed with error 16: device or resource busy.</code>
<code>the kernel still uses the old table. the new table will be used at</code>
<code>the next reboot or after you run partprobe(8) or kpartx(8)</code>
<code>syncing disks.</code>
fdisk-d
重新建立分區後,需要重新開機一下;
fdisk-p
<code>[root@linuxprobe ~]# init 6</code>
<code>[root@linuxprobe ~]# e2fsck -f /dev/sda4 ### 檢查分區資訊</code>
<code>[root@linuxprobe ~]# resize2fs -p /dev/sda4 ### 調整分區大小</code>
重新挂載、檢視分區大小、資料:
df-ht_19g
可見/dev/sda4分區的空間大小已經增加了。
原文釋出時間為:2016-05-26
本文來自雲栖社群合作夥伴“linux中國”