一、fdisk命令概述
fdisk命令是Linux下的一個磁碟分區工具,它可以創建、刪除、查看硬碟的分區。在使用fdisk分區之前,需要注意以下幾點:
1、fdisk只能對未掛載的硬碟進行分區,如果需要對已掛載到系統中的硬碟進行分區,需要先將硬碟卸載。
2、在分區之前需要備份數據,因為分區操作會清除硬碟上的數據。
3、分區時需要根據實際需要來分配分區的大小。
二、使用fdisk命令進行磁碟分區
1、使用fdisk查看磁碟信息
首先,我們需要使用fdisk來查看磁碟的分區情況:
fdisk -l
這個命令會列出系統上所有的硬碟分區,並給出它們的分區表信息。例如:
Disk /dev/sda: 512 GB, 512110190592 bytes 255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0x0009b5d8 Device Boot Start End Blocks Id System /dev/sda1 * 2048 100021452 50009652+ 83 Linux Disk /dev/sdb: 512 GB, 512110190592 bytes 255 heads, 63 sectors/track, 62260 cylinders, total 1000215216 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 4096 bytes I/O size (minimum/optimal): 4096 bytes / 4096 bytes Disk identifier: 0x0009b5d6 Device Boot Start End Blocks Id System /dev/sdb1 2048 100021452 50009652+ 83 Linux
2、創建新的分區
要創建一個新的分區,請使用以下命令:
fdisk /dev/sda
接下來進入fdisk界面,輸入n創建新分區,分配大小,默認單位是MB。創建多個分區需要重複執行n命令。
Welcome to fdisk (util-linux 2.21.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Device Boot Start End Blocks Id System /dev/sda1 * 2048 100021452 50009652+ 83 Linux Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First sector (100021452+1): Last sector, +sectors or +size{K,M,G} (100021452+1): +100000M Created a new partition 2 of type 'Linux' and of size 95.4 GiB.
在這個例子中,我們創建了一個95.4GB(100000MB)的新分區。
3、選擇分區類型
在分區之後,我們需要指定這個分區的類型,例如,我們可以將這個分區設為Linux交換分區類型,使用以下命令:
Command (m for help): t Partition number (1-4): 2 Hex code (type L to list all codes): 82
在這個例子中,我們將這個分區的類型改為Linux交換分區類型。
4、保存分區表
分區操作完成後,需要使用w命令保存分區表:
Command (m for help): w 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.
在保存之後,將會出現警告信息,需要使用partprobe命令或者重啟系統,讓系統重新讀取分區表信息。
三、刪除分區
如果需要刪除一個分區,需要使用以下命令:
fdisk /dev/sda Command (m for help): d Partition number (1-4): 2 Partition 2 is deleted Command (m for help): w
在這個例子中,我們使用d命令刪除了/dev/sda的第二個分區。
四、總結
fdisk是Linux下一個常用的磁碟分區工具,可以方便地對硬碟進行分區操作。使用fdisk進行分區時需要先備份數據,並且注意分配分區的大小。使用fdisk時需要小心,避免誤操作導致數據丟失。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/236489.html