1.命令簡介
fdisk 命令用于創建和維護磁盤分區表。它采用傳統的問答式界面,而不是類似于 fdisk 的 cfdisk 的互動式操作界面,因此在使用上較為不便,但功能卻絲毫不打折扣。它兼容 DOS 類型的分區表、BSD 或者 SUN 類型的磁盤列表。
2.命令格式
fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
fdisk -l [-u] [device.。.]
fdisk -s partition.。.
fdisk -v
fdisk -h
3.選項說明
-b [sectorsize]:指定硬盤扇區大小,可用數值為512, 1024, 2048 or 4096;
-c:關閉DOS兼容模式;
-C [cyls]:指定硬盤的柱面數(number of cylinders);
-H [heads]:指定硬盤的磁頭數(number of heads),當然不是物理數值,而是作用于分區表。合理取值是255和16;
-S [sects]:指定每個磁道的扇區數,當然不是物理數值,而是用于分區表。一個合理的數值是63;
-l:列出指定設備的分區表,然后退出。如果沒有給定設備,則使用在/proc/partitions(如果存在)中提到的那些設備;
-u:在列出分區表時,給出扇區大小而不是柱面大小;
-s [partition]:以塊(block)為單位,顯示指定分區的大小;
-v:顯示版本信息;
-h:顯示幫助信息。
閱讀以上選項說明,需要注意以下幾個問題:
(1)塊(block)與扇區(sector)的區別。
扇區是對硬盤而言,扇區是硬盤的最小存儲單位,塊是對文件系統而言,塊是文件系統最小存取單位。一般而言,一個扇區大小為512B,一個塊大小為4KB,一個block是由連續的8個sector組成。
(2)理解上面選項的含義,需了解磁盤的物理組成結構與相關概念,例如sector、cylinder、head等組成部件的具體含義,可參見硬盤的存儲原理和內部架構 。
4.常用示例
(1)列出指定設備的分區情況。
[root@add_friend_protect /home/oicq]# fdisk -l /dev/sda
Disk /dev/sda: 300.0 GB, 299966445568 bytes
255 heads, 63 sectors/track, 36468 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: 0x0009808c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1306 10485760 83 Linux
/dev/sda2 1306 1566 2088960 82 Linux swap / Solaris
/dev/sda3 1566 4177 20971520 83 Linux
/dev/sda4 4177 36469 259387694 83 Linux
對fdisk -l相關數值的解釋如下:
第一行中說明硬盤/dev/sda總大小為300.0 GB;
第二行中heads表示硬盤磁頭數,也是盤面數,因為磁頭數等于盤面數。隨后63 sectors/track說明每條磁道分63個扇區。共36468個柱面,柱面是分區的最小單位;
第三行說明每個柱面單位是8225280 byets,柱面單位大小=磁頭數*每條磁道的扇區數*扇區大小=255*63*512B=8225280B;
第四行說明扇區的大小是512B;
第五行說明硬盤最小與最佳的存儲單位是512 bytes,等于扇區大小,因為扇區是硬盤的最小存儲單位;
第六行說明硬盤標識符是0x0009808c。
第七行表示每個分區相關參數的含義。
Device:分區名稱;
Boot:是否是活動分區。活動分區只能是主分區,一個硬盤只能有一個活動的主分區;一個硬盤的主分區與擴展分區總和不能超過4個。硬盤分區遵循著“主分區→擴展分區→邏輯分區”的次序原則,而刪除分區則與之相反。
主分區:一個硬盤可以劃分多個主分區,但沒必要劃分那么多,一個足矣。
擴展分區:主分區之外的硬盤空間就是擴展分區,
邏輯分區:是對擴展分區再行劃分得到的。
Start:分區柱面的開始下標;
End:分區柱面的結束下標;
Blocks:該分區的塊數量。當前文件系統block=2*sector,所以塊數量=(End-Start)*柱面的扇區數/2=1305*255*63/2=10482412.5;
Id:各種分區的文件系統不同,如有ntfs分區,fat32分區,ext3分區,swap分區等。每一種文件系統都有一個代號,對應這里的Id。常見的文件系統ID有:
f:FAT32 Extend,只限于擴展分區。
86:NTFS。
7:HPFS/NTFS
b:FAT32。
83:Linux Ext2。
82:Linux 交換區。
System:文件系統名稱。
總結:
一個磁盤的大小=一個柱面大小*柱面的總數=磁頭數量*每個磁道上的扇區數*一個扇區大小*柱面總數
即:
磁盤大小=8225280*36468=299959511040 bytes=299GB=255*63*512*36468
上例中顯示出的硬盤大小與實際計算出來的有少許出入,這個不必太在意,Linux顯示的這些數據不會十分精確。
(2)對指定設備進行分區并掛載。
第一步,選擇要進行操作的磁盤,進入問答式界面。
fdisk /dev/sdb
第二步,輸入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)
第三步,輸入p列出磁盤目前的分區情況。
Disk /dev/sda: 300.0 GB, 299966445568 bytes
255 heads, 63 sectors/track, 36468 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: 0x0009808c
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1306 10485760 83 Linux
/dev/sda2 1306 1566 2088960 82 Linux swap / Solaris
/dev/sda3 1566 4177 20971520 83 Linux
/dev/sda4 4177 36469 259387694 83 Linux
第四步,輸入d然后選擇分區,刪除現有分區。
Command (m for help): d
Partition number (1-4): 1
Command (m for help): d
Selected partition 2
Command (m for help): d
Selected partition 3
Command (m for help): d
Selected partition 4
第五步,輸入n建立新的磁盤分區,首先建立兩個主磁盤分區。
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p //建立主分區
Partition number (1-4): 1 //分區號
First cylinder (1-391, default 1): //分區起始位置
Using default value 1
last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100 //分區結束位置,單位為柱面
Command (m for help): n //再建立一個主分區
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2 //分區號為2
First cylinder (101-391, default 101):
Using default value 101
Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M //分區結束位置,單位為M
第七步,確認分區建立成功。
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 125 200812+ 83 Linux
第八步,再建立一個邏輯分區。
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e //選擇擴展分區
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391
第九步,確認擴展分區建立成功。
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 125 200812+ 83 Linux
/dev/sdb3 126 391 2136645 5 Extended
第十步,在擴展分區上建立兩個邏輯分區。
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l //選擇邏輯分區
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391
第十一步,確認邏輯分區建立成功。
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 100 803218+ 83 Linux
/dev/sdb2 101 125 200812+ 83 Linux
/dev/sdb3 126 391 2136645 5 Extended
/dev/sdb5 126 175 401593+ 83 Linux
/dev/sdb6 176 391 1734988+ 83 Linux
Command (m for help):
從上面的結果我們可以看到,在硬盤 sdb 我們建立了 2 個主分區(sdb1,sdb2),1個擴展分區(sdb3),2 個邏輯分區(sdb5,sdb6)。
注意:主分區和擴展分區的磁盤號位1-4,也就是說最多有 4 個主分區或者擴展分區,邏輯分區開始的磁盤號為 5,因此在這個實驗中是沒有 sdb4 的。
最后對分區操作進行保存:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
建立好分區之后我們還需要對分區進行格式化才能在系統中使用磁盤。
在 sdb1 上建立 ext2 文件系統。
[root@localhost ~]# mkfs.ext2 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
100576 inodes, 200804 blocks
10040 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=209715200
7 block groups
32768 blocks per group, 32768 fragments per group
14368 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
在 sdb6 上建立 ext3 文件系統。
[root@localhost ~]# mkfs.ext3 /dev/sdb6
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
217280 inodes, 433747 blocks
21687 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=444596224
14 block groups
32768 blocks per group, 32768 fragments per group
15520 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]#
建立兩個目錄/oracle和/web,將新建好的兩個分區掛載到系統:
[root@localhost ~]# mkdir /oracle
[root@localhost ~]# mkdir /web
[root@localhost ~]# mount /dev/sdb1 /oracle
[root@localhost ~]# mount /dev/sdb6 /web
查看分區掛載情況:
[root@localhost ~]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/VolGroup00-LogVol00
6.7G 2.8G 3.6G 44% /
/dev/sda1 99M 12M 82M 13% /boot
tmpfs 125M 0 125M 0% /dev/shm
/dev/sdb1 773M 808K 733M 1% /oracle
/dev/sdb6 1.7G 35M 1.6G 3% /web
如果需要每次開機自動掛載則需要修改/etc/fstab文件,加入兩行配置:
[root@localhost ~]# vim /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
/dev/sdb1 /oracle ext2 defaults 0 0
/dev/sdb6 /web ext3 defaults 0 0
參考文獻
[1] fdisk(8) manual
[2] Linux命令大全.fdisk命令
[3] 磁盤的塊大小(Block Size)和扇區大小(Sector Size)
[4] fdisk -l 命令詳解
給我們公號發送 命令 二字,獲取“每天一個Linux命令”系列的完整目錄。
- EOF -
推薦閱讀 點擊標題可跳轉
1、每天一個 Linux 命令(120):tee 命令
2、每天一個 Linux 命令(119):echo 命令
3、每天一個 Linux 命令(118):rename 命令
原文標題:每天一個 Linux 命令(124):fdisk 命令
文章出處:【微信公眾號:Linux愛好者】歡迎添加關注!文章轉載請注明出處。
責任編輯:haq
-
Linux
+關注
關注
87文章
11342瀏覽量
210336 -
操作系統
+關注
關注
37文章
6889瀏覽量
123719 -
命令
+關注
關注
5文章
696瀏覽量
22105
原文標題:每天一個 Linux 命令(124):fdisk 命令
文章出處:【微信號:LinuxHub,微信公眾號:Linux愛好者】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論