以前切割硬碟會用 #fdisk 但這個工具漸漸被 #parted 取代,而不是因為 #parted 好用,而是 #fdisk 限制太多.
傳統的 #fdisk 只適用於由 MBR 切割出來的磁區,但 MBR 最大也只支援到 2.2 TB ,所以必須透過 GPT(GUID Partition Table) 的切割硬碟方式.所以只能使用 #parted 這個工具.
一開始全新的硬碟需要選擇 Label type ,大於 2.2T 硬碟要選擇 gpt.
其他支援的 disk label
- bsd
- loop (raw disk access)
- gpt
- mac
- msdos
- pc98
- sun
root@benjr:~# parted /dev/sdb GNU Parted 2.3 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mkpart Error: /dev/sdb: unrecognised disk label New disk label type? gpt (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 2147MB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags
- 新增磁區
要透過 parted 切割磁區時使用 (parted) mkpart ,比較會讓人困惑的是 start 和 end, parted 在做切割時我們可以自己選擇由系統的哪一個區塊的空間開始並由哪一個空間結束,如果你選擇 start ?100 End?10000 就表示系統會保留硬碟前 100MB 的空間之後再切割出 10G (10000MB) 大小的硬碟.但是如果那 10G 的空間已經是被其他 partition tables 占用時也沒關係系統會自動幫我們選擇一個最適合的空間.
Start? 與 End? 預設都是使用 M(Mega) 可以指定為 K(Kilo) M(Mega) G(Giga)(parted) mkpart Partition name? []? Data1 File system type? [ext2]? Start? 0 End? 100M Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? i (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 2147MB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 100MB 100MB Data1
切割完磁區後要選擇檔案格式,如果使用 20TB 容量的硬碟如果要使用 ext2 http://en.wikipedia.org/wiki/Ext2 當成檔案格式時有一點需要注意的,#mkfs.ext2 預設的 Block size 為 2K 其最大檔案系統上限為 8TB ,所以要使用這 20TB 大硬碟時可以在建立 ext2 時加入參數 -b 並指定大小 4KB 可達 16TB 而 8KB 可達 32TB,但如果超過 32 TB就必須選擇 ext4 http://en.wikipedia.org/wiki/Ext4 的格式了.
root@benjr:~# mkfs.ext2 /dev/sdb1 mke2fs 1.42.9 (4-Feb-2014) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 24480 inodes, 97636 blocks 4881 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=67371008 12 block groups 8192 blocks per group, 8192 fragments per group 2040 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Allocating group tables: done Writing inode tables: done Writing superblocks and filesystem accounting information: done
切割第二塊磁區,雖然 start 跟前面磁區重疊 parted 會幫我們修改為合理的範圍.
(parted) mkpart Partition name? []? Data2 File system type? [ext2]? Start? 50M End? 100M Warning: You requested a partition from 50.0MB to 100MB. The closest location we can manage is 100MB to 100MB. Is this still acceptable to you? Yes/No? y Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? i (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 2147MB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 100MB 100MB Data1 2 100MB 100MB 512B Data2
- 刪除磁區
(parted) rm Partition number? 2 (parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 2147MB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 100MB 100MB Data1
- 放大縮小磁區
parted 還可以依據需求放大縮小磁區(parted) print Model: ATA VBOX HARDDISK (scsi) Disk /dev/sdb: 2147MB Sector size (logical/physical): 512B/512B Partition Table: gpt Number Start End Size File system Name Flags 1 17.4kB 100MB 100MB ext2 Data1
不知道為什麼在 Ubuntu 14.04 下面 resize 沒有辦法使用 (RHEL 7 下進入 parted 裡面找不到 resize 的功能).
仔細一看內文說明,resize 將不再支援,建議用戶用 parted (resizepart) + resize2fs 套件做放大縮小磁區,請參考 https://benjr.tw/94843(parted) resize WARNING: you are attempting to use parted to operate on (resize) a file system. parted's file system manipulation code is not as robust as what you'll find in dedicated, file-system-specific packages like e2fsprogs. We recommend you use parted only to manipulate partition tables, whenever possible. Support for performing most operations on most types of file systems will be removed in an upcoming release. Partition number? 1 Start? [17.4kB]? End? [100MB]? 200M 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.
note:
如果是 GUI 安裝 Linux 時,可以透過 [Ctrl]+[Alt]+[F2] 回到 "shell prompt" 使用 parted 將需要的磁碟切割成 GPT 的工具.
圖形介面下可以使用 gparted 來改變硬碟的分割.請參考 GParted 官網站 – http://gparted.org/
2 thoughts on “Linux command – parted”