磁盤分區工具fdisk 詳解


一、什麼是磁盤分區工具

磁盤分區工具一種可以幫助你配置和管理分區的工具。它可以刪除或者格式化分區,也可以重新設置分區大小以及移動你的分區。

二、爲什麼要使用磁盤分區工具

如果不對磁盤進行分區,所有的文件都保存在一個分區內,這樣如果其中一個文件受到病毒感染,其他文件的安全性就會受到很大的威脅。分區後也便於文件的查找與管理,對磁盤進行合理的分區更有提高磁盤的有效利用率。


三、如何使用磁盤分區工具

Linux系統中的磁盤分區工具主要是fdisk。本文也將着重對其進行介紹


fdisk ---Partition table manipulator for Linux (對Linux系統進行磁盤分區的工具)

語法

       fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
       fdisk -l [-u] [device...]


常用參數

-l 列出指定設備的磁盤分區信息,默認顯示 /proc/partitions的分區信息


管理分區

# fdisk DEVICE

fdisk 提供了一個交互式接口來管理分區,分別用於不同的管理功能,所有操作均在內存中完成;沒有直接同步到磁盤:


p: 顯示現有分區表;
n: 創建新分區
d: 刪除現有分區
t: 修改分區文件系統類型
l: 顯示fdisk所支持的文件系統代碼

q: 不保存退出
m: 顯示幫助信息
b: 設置卷標
v: 檢驗分區


查看/dev/sdb磁盤信息

[root@1inux ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0x00000000


給/dev/sdb磁盤進行分區,

[root@1inux ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x6a09b062.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
Command (m for help):


然後輸入 “ m ” 可以查看支持的選項:

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition    //刪除分區
   l   list known partition types 
   m   print this menu //查看幫助
   n   add a new partition    //創建新分區
   o   create a new empty DOS partition table
   p   print the partition table //顯示現有分區表;
   q   quit without saving changes    //不保存退出
   s   create a new empty Sun disklabel
   t   change a partition's system id    //修改分區文件系統類型
   u   change display/entry units
   v   verify the partition table    //檢驗分區
   w   write table to disk and exit    //保存更改並退出
   x   extra functionality (experts only)


輸入“n” 然後回車新建分區

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)

選擇要創建的分區類型:e-創建擴展分區;p-創建主分區,在此我們選擇創建主分區輸入“p”

Partition number (1-4):

提示要輸入要創建主分區的ID號,在1-4之間,在此我們輸入1

Partition number (1-4): 1
First cylinder (1-10443, default 1):


設置分區起始的柱面,直接回車就行:然後如下:

First cylinder (1-10443, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443):


此處是設置分區結束柱面,也可指定要分區的大小,可支持k、M、G單位,在此我們設置一個大小爲5G的分區,+5G ,然後回車

Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +5G


通過輸入‘p’可以查看已經建立的分區,

Command (m for help): p
Disk /dev/sdb: 85.9 GB, 85899345920 bytes
255 heads, 63 sectors/track, 10443 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: 0x6a09b062
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux

查看分區設置無誤後可以通過鍵入“w” 來保存對磁盤的更改設置

Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@1inux ~]#



到此 已經使用fdisk命令在/dev/sdb磁盤上創建了一個/dev/sdb1大小爲5G的主分區,


然後我們通過內核查看系統是否已經識別了我們剛纔創建的分區

[root@1inux ~]# cat /proc/partitions
major minor  #blocks  name
   8        0  125829120 sda
   8        1     122880 sda1
   8        2   31426560 sda2
   8        3   10485760 sda3
   8        4          1 sda4
   8        5   10485760 sda5
   8        6    6291456 sda6
   8        7    2097152 sda7
   8       16   83886080 sdb
   8       17    5253223 sdb1    //發現已經被識別



如果發現分區未被識別,我們可以通知內核強制重讀分區


CentOS 5: partprobe /dev/DEVICE
CentOS 6: partx,kpartx
 partx -a /dev/DEVICE
 kpartx -af /dev/DEVICE

注意:如果執行一次命令,未能成功讀取分區表,可重複執行2次或以上


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章