8.1 fdisk:磁盤分區工具


fdisk

  是Linux下常用的磁盤分區工具。受mbr分區表的限制,fdisk工具只能給小於2TB的磁盤划分分區。如果使用fdisk對大於2TB的磁盤進行分區,雖然可以分區,但其僅識別2TB的空間,所以磁盤容量若超過2TB,就要使用parted分區工具(后面會講)進行分區。
fdisk [選項] [設備名] 
-l    顯示所有磁盤分區的信息
 
[root@cs6 ~]# fdisk -l #<==查看當前系統所有磁盤的分區信息。
 
Disk /dev/sda: 32.2 GB, 32212254720 bytes  #<==磁盤/dev/sda的大小。
255 heads, 63 sectors/track, 3916 cylinders #<==255個虛擬磁頭,63個扇區/磁道,3916 個柱面。
Units = cylinders of 16065 * 512 = 8225280 bytes #<==一個柱面大小8225280 bytes。
Sector size (logical/physical): 512 bytes / 512 bytes #<==每個扇區的字節數。
I/O size (minimum/optimal): 512 bytes / 512 bytes  #<==每次讀寫的字節數。
Disk identifier: 0x0001038a
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        3917    30944256   8e  Linux LVM
 
Disk /dev/mapper/vg_cs6-lv_root: 28.5 GB, 28462546944 bytes
255 heads, 63 sectors/track, 3460 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
 
 
Disk /dev/mapper/vg_cs6-lv_swap: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 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
 
 
Device:分區,這里有二個分區;
Boot:啟動分區,用*表示的是啟動分區;
Start;表示開始的柱面:
End:表示結束的在面;
Blocks:block 塊數量;
Id:分區類型Id;
System:分區類型。
 

在虛擬機(VMware Workstation Pro)模擬磁盤分區實戰

步驟1:先在虛擬機關機狀態下添加一塊1GB硬盤,若是在開機狀態下添加,則需要重啟系統。
步驟2:重啟系統后查看添加的磁盤。 
[root@cs6 ~]# fdisk -l
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes #<==剛剛新添加的硬盤名為sdb,是第二塊盤。
255 heads, 63 sectors/track, 130 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
還可以直接指定特定分區查看信息。
[root@cs6 ~]# fdisk -l /dev/sdb
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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 

步驟3:交互式分區實踐。

 
[root@cs6 ~]# ls /dev/sd* #<=需查看分區前設備的狀態。
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
 
[root@cs6 ~]# 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 0x34d8eef3.
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  #<==提示使用-c關閉dos兼容模式。
         sectors (command 'u').       #<==提示-u參數使用扇區為單位分區。
                            
Command (m for help): m                #<= m是幫助,需要人工輸入m后回車,
Command action
   a   toggle a bootable flag                #<==設置引導扇區。
   b   edit bsd disklabel                    #<==編輯bsd卷標。
   c   toggle the dos compatibility flag     #<==設置dos兼容扇區。
   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     #<==創建一個新的空DOS分區表,
   p   print the partition table             #<==打印分區表。
   q   quit without saving changes           #<==退出不保存更改
   s   create a new empty Sun disklabel      #<==創建新的空 sun卷標。
   t   change a partition's system id        #<==更改分區系統id。
   u   change display/entry units            #<==改變顯示/條目的單位。
   v   verify the partition table            #<==驗證分區表。        
   w   write table to disk and exit          #<==將操作寫入分區表並退出程序。        
   x   extra functionality (experts only)    #<==額外的功能。
 
 
Command (m for help): n     #<==新建一個分區,需要人工輸入n后回車。
Command action
   e   extended                    #<==創建擴展分區。
   p   primary partition (1-4)     #<==創建主分區(編號1-4)。
p
Partition number (1-4): 1            #<==設置分區編號為1,需要人工輸入1后回車。
First cylinder (1-130, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M
#<==設置結束柱面(130)或分區大小(+100M),因為要划分出指定大小的分區,所以常用+100M這種方法,如果分區時使用fdisk -cu /dev/sdb,則這里就會使用扇區為單位來進行分區。
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
2
Invalid partition number for type `2'
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (15-130, default 15):
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130):
Using default value 130    #<==按回車鍵,默認設置結束柱而號130。
 
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
 
Command (m for help): n
Command action
   l   logical (5 or over)        #<=分了擴展分區,這里自動變為邏輯分區。
   p   primary partition (1-4)
p
Partition number (1-4): 3
No free sectors available     #<==不能再創建主分區,沒有磁盤空間了。
                              #<==再新建一個分區。
Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (15-130, default 15): #<=按回車鍵,開始柱面號為15。
Using default value 15
Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130): +400M
                        #<==設置分區大小為400MB
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
/dev/sdb5              15          66      417658+  83  Linux
 
Command (m for help): n #<==新建一個分區。
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l    #<==再新建一個邏輯分區。
First cylinder (67-130, default 67):
Using default value 67
Last cylinder, +cylinders or +size{K,M,G} (67-130, default 130):
Using default value 130
 
Command (m for help): p    #<==打印分區表。
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
/dev/sdb5              15          66      417658+  83  Linux
/dev/sdb6              67         130      514048+  83  Linux
 
Command (m for help): w    #<==將操作寫入分區表生效並退出程序。
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@cs6 ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1  /dev/sdb2  /dev/sdb5  /dev/sdb6
[root@cs6 ~]#
[root@cs6 ~]# yum -y install parted
[root@cs6 ~]# partprobe /dev/sdb    #<==執行該命令通知內核分區表已更改,此步是不重啟讓分區表生效的命令。
步驟4:格式化磁盤。
 
[root@cs6 ~]# mkfs.ext4 /dev/sdb1    #<==只有格式化后的磁盤才能掛載到系統中使用,后面將會詳細講解mkfs.ext4命令。
mke2fs 1.41.12 (17-May-2010)
文件系統標簽=
操作系統:Linux
塊大小=1024 (log=0)
分塊大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
28112 inodes, 112420 blocks
5621 blocks (5.00%) reserved for the super user
第一個數據塊=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729
 
正在寫入inode表: 完成                            
Creating journal (4096 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
 
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
 
[root@cs6 ~]# tune2fs -c -1 /dev/sdb1    #<=執行這個命令可以避免磁盤掛載自動檢查磁盤,后面將會講解tune2fs命令。
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1

 步驟5:掛載磁盤分區。

[root@cs6 ~]# vi /etc/fstab    #<==最后一行加入,要開機自動掛載磁盤就要加入/etc/fstab或將上面的mount命令放入/etc/rc.local中。
 
/dev/sdb1              /mnt                     ext4       defaults     0  0
 
[root@cs6 ~]# vi /etc/rc.local    #<==或者編輯/etc/rc.local,最后一行加入,兩種方法二選一。
mount  /dev/sdb1  /mnt

 步驟6:其他事項。

    用交互指令d刪除分區時要小心,看好分區的序號,如果刪除了擴展分區,那么擴展分區之下的邏輯分區都會刪除,所以操作時一定要小心。如果不小心操作錯了,直接使用交互指令q不保存退出,這樣先前的操作就會無效。如果輸入w(保存指令)則會保存所有修改。
[root@cs6 ~]# fdisk /dev/sdb
 
Command (m for help): d
Partition number (1-6): 2
 
Command (m for help): p
 
Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x34d8eef3
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux

fdsik 非交互式分區(批量分區案例)

 
 
以下是實現非交互式分區的代碼。
fdisk /dev/sdb <<EOF    #<==也可以將下面的內容寫入文本文件,然后讀文本執行。
n
P
1

+100M n e 2

n l +400 n l
p
w EOF
 
 
 
 
-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM