1. 程式人生 > >fdisk、parted無損調整普通分區大小

fdisk、parted無損調整普通分區大小

分區擴展

環境:oracle linux 6.4 #fdisk -v fdisk (util-linux-ng 2.17.2) #parted -v parted (GNU parted) 2.1

我們講的調整分區大小,都是要保證不損壞分區中數據為前提。
這裏我們講一下用fdisk、parted調整普通分區的方法。
操作有風險,重要數據還是要備份保存一下。

一、fdisk普通分區調整:

大概步驟:
先執行fdisk查看原分區的cylinder起始值,一定要記住。
再umount分區,進fdisk刪除原有分區, 再重建原有分區,再分區的時候,cylinder起始值一定要跟原來的是一樣的,要不然就會破壞原分區的數據,只要調整cylinder結束值大小即可擴容分區。

分區建好後, 用e2fsck先檢查一下分區, 再用resize2fs擴大就可以了。

切記:此方法適用於擴容分區,如果要縮減分區,特別是根目錄,可能會出問題

實例演示:
我要把swap分區刪掉,劃分一部分空間到根目錄分區/dev/sda2中來,剩余的空間再創建新的swap。
(增加點實驗難度)

[php]
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 3.7G 5.5G 41% /
tmpfs 875M 0 875M 0% /dev/shm
/dev/sda1 194M 50M 135M 27% /boot

#fdisk -l /dev/sda
Disk /dev/sda: 12.9 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 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: 0x0007c9df

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 1301 10240000 83 Linux
/dev/sda3 1301 1556 2048000 82 Linux swap / Solaris
[/php]

具體步驟:
1、刪除swap分區
先查看一下swap分區有沒有在使用:

[php]
#free
total used free shared buffers cached
Mem: 1790856 154188 1636668 0 15472 58480
-/+ buffers/cache: 80236 1710620
Swap: 2047996 0 2047996
[/php]

可以看出未使用狀態,放心刪除

[php]
#swapoff /dev/sda3
#fdisk /dev/sda
Command (m for help): d
Partition number (1-4): 3

Command (m for help): p

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 1301 10240000 83 Linux

Command (m for help): w
[/php]

2、進入急救rescue模式
(如果是其它的分區,不影響系統正常運行的,能umount的,就不用進急救模式就可以操作了,我這裏實驗主要是增加一點難度)
如果沒進急救模式,直接在原系統操作的話,先要umount要擴展的分區

[php]
#fdisk /dev/sda
Command (m for help): d 先刪除要擴展的分區,這裏是根目錄/分區
Partition number (1-4):2

Command (m for help): p

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux

Command (m for help): n 再把刪除的分區重新添加
Command action
e extended
P primary partition (1-4)
P
Partition number (1-4):2
First cylinder (26-1566,default 26):26 這裏一定要跟原先的對應
Last cylinder ,+cylinders or +size{K,M,G} (26-1566,default 1566):1433
原先是到1301,在原先的基礎上加一部分,留點給swap
Command (m for help): w
#reboot
[/php]

3、resize2fs
使用partprobe同步內存中分區信息
使用e2fsck強制檢查分區
#e2fsck -f /dev/sda2
(如果是根目錄擴容,重啟後這兩個命令執行都不會成功所以也不用執行)

使用resize2fs重新定義文件系統大小(這步一定要做,要不然容量不會有變化)

[php]
# resize2fs /dev/sda2
#df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 11G 3.7G 6.4G 37% /
tmpfs 875M 0 875M 0% /dev/shm
/dev/sda1 194M 50M 135M 27% /boot

#fdisk -l /dev/sda
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 1433 11304748+ 83 Linux
[/php]

對比發現,根目錄空間已經擴大了。

恢復swap空間

[php]
#fdisk /dev/sda
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1434-1566, default 1434):
Using default value 1434
Last cylinder, +cylinders or +size{K,M,G} (1434-1566, default 1566):
Using default value 1566

Command (m for help): p

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 1433 11304748+ 83 Linux
/dev/sda3 1434 1566 1068322+ 83 Linux

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 82
Changed system type of partition 3 to 82 (Linux swap / Solaris)

Command (m for help): p

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 1433 11304748+ 83 Linux
/dev/sda3 1434 1566 1068322+ 82 Linux swap / Solaris

#mkswap /dev/sda3
#swapon /dev/sda3
#free
total used free shared buffers cached
Mem: 1790856 145724 1645132 0 13020 57600
-/+ buffers/cache: 75104 1715752
Swap: 1068316 0 1068316
[/php]

swap分區回來了,而且也相應的變少了

總結:
通過這個也可以引申出來分區的合理性。
比如可以把第一個分區給/boot 第二個分區給根目錄/,第三個分區給swap,最後要保留一點空間不要分配備用
這樣萬一根目錄/空間不夠,可以先把swap刪除,把根目錄/擴容,然後再掛上swap分區(擴容根目錄要進急救rescue模式)
swap分區自己擴容也很方便。
可以根據情況靈活分配
比如專門放數據的、或者放日誌的分區,隨時都在變大,也可以單獨出來,放在分區的後面,這樣以後擴容也方便。

二、parted分區調整
parted可以對自己格式化的分區進行調整分區。
這裏要註意:一定要是parted中的命令mkfs格式化的分區,用系統自帶的mkfs.ext3之類的命令格式化的分區是沒辦法再用parted命令調整大小的。
parted還不支持格式化ext3分區
實例演示:

[php]
#parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 500MB 200MB ext2 db2
[/php]

對於它自己格式的分區,我們可以隨意調整大小

[php]
(parted) resize
Partition number? 2
Start? [300MB]?
End? [500MB]? 600
OK/Cancel? o
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 600MB 300MB ext2 db2

(parted) resize
Partition number? 2
Start? [300MB]?
End? [600MB]? 400
OK/Cancel? o
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 400MB 100MB ext2 db2
[/php]

如果是系統用自帶的mkfs.ext3之類的命令格式化的分區,用parted調整分區會報錯:
先格式化一下,再回到parted去調整大小

[php]
#mkfs.ext2 /dev/sdb2
#parted /dev/sdb
Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 400MB 100MB ext2 db2

(parted) resize
Partition number? 2
Start? [300MB]?
End? [400MB]? 500
Error: File system has an incompatible feature enabled. Compatible features are has_journal,
dir_index, filetype, sparse_super and large_file. Use tune2fs or debugfs to remove features.
[/php]

上面的例子都是ext2格式的,現在基本上這種格式都不再使用了。
但parted還不支持格式化ext3格式,必須要用系統自帶的mkfs.ext3命令去格式化,但格式化後就不能再用parted調整分區大小了。
這是挺糾結的。

but

還是有解決辦法的
就是先用parted格式化成ext2格式,再用tune2fs命令轉換成ext3格式的,這樣一操作就還能用parted調整這個分區大小

[php]
Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 400MB 100MB ext2 db2
3 400MB 600MB 200MB ext2 db3
[/php]

把/dev/sdb3轉化成ext3格式

[php]
#tune2fs -j /dev/sdb3
tune2fs 1.41.12 (17-May-2010)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[/php]

再回到parted中

[php]
#parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 400MB 100MB ext2 db2
3 400MB 600MB 200MB ext3 db3
[/php]

再調整/dev/sdb3的大小

[php]
(parted) resize
Partition number? 3
Start? [400MB]?
End? [600MB]? 700
error: block relocator should have relocated 8706
這裏有個錯誤,提示塊必須要移動8706
可以調整一下要擴大的大小

(parted) resize
Partition number? 3
Start? [400MB]?
End? [600MB]? 900
Warning: A resize operation on this file system will use EXPERIMENTAL code
that MAY CORRUPT it (although no one has reported any such damage yet).
You should at least backup your data first, and run 'e2fsck -f' afterwards.
OK/Cancel? o
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 12.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB ext2 db1
2 300MB 400MB 100MB ext2 db2
3 400MB 900MB 500MB ext3 db3
[/php]

成功擴容

所以,如果想用parted調整分區大小的功能,分區的時候可以使用parted,也可以使用fdisk,但是格式化分區的時候,只能使用parted自己的mkfs

LVM卷組的就不講了,這個大家都知道也都會。

#更多文章見我個人站點:haibing.org#



fdisk、parted無損調整普通分區大小