多硬碟分割槽管理fdisk-張丹

leonarding發表於2013-07-08

多硬碟分割槽管理fdisk

ubuntu實用工具系列文章

作業系統實用工具系列文章,將介紹基於Linux ubuntu的各種工具軟體的配置和使用。有些工具大家早已耳熟能詳,有些工具經常用到但確依然陌生。我將記錄我在使用作業系統時,安裝及配置工具上面的一些方法,把使用心得記錄下來也便於自己的以後查詢和回憶。

關於作者

張丹(Conan), 程式設計師Java,R,PHP,Javascript
weibo:@Conan_Z
blog: http://blog.fens.me
email: bsspirit@gmail.com

轉載請註明出處:
http://blog.fens.me/linux-fdisk/

fdisk

前言

硬碟是一臺計算機的必備硬體之一,計算機如何開始使用硬碟資源就要從硬碟分割槽說起。開始時,一臺計算機只有一塊硬碟,在安裝Linux Ubuntu時,第一步就是對硬碟進行分割槽。當我們已經安裝作業系統,硬碟空間不夠的時候,我們需要增加新的硬碟擴大使用空間,特別是對於虛擬化的主機,這種增加新的硬碟就變得有一些不一樣了,要手動進行處理。

我在做測試的走進了一個誤區中,一直對subpartition這個概念不太理解,糾結於sdb2p1,sdb2p1這樣的分割槽命名。經過幾次測試後,才發現,問題出現是因為對作業系統的不理解,以至於在網際網路上都找找不太多相關問題的討論。下面我就介紹一下,我的誤區及如何找到正確的方法。

目錄

  1. 分割槽的基本知識
  2. 新增硬碟分割槽,走入誤區
  3. 完成對新增硬碟分割槽及掛載

1. 分割槽的基本知識

a. 硬碟分類及標識:IDE,SATA,SCSI三個大類

硬碟與標識對應關係如下表所示:

裝置裝置在Linux內的檔名
IDE硬碟機/dev/hd[a-d]
SCSI/SATA/USB硬碟機/dev/sd[a-p]
USB快閃碟/dev/sd[a-p](與SATA不同)
軟盤驅動器/dev/fd[0-1]
印表機25針: /dev/lp[0-2]

USB: /dev/usb/lp[0-15]

滑鼠PS2: /dev/psaux

USB: /dev/usb/mouse[0-15]

當前CDROM/DVDROM/dev/cdrom
當前的滑鼠/dev/mouse

上面表格摘自:http://blog.csdn.net/zollty/article/details/7001950

可以看到我現在硬碟有sda標識,物理上是一個SAS硬碟,300G。

~ sudo fdisk -l

Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders, total 584843264 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000efd7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    97656831    48827392   82  Linux swap / Solaris
/dev/sda2        97656832   136718335    19530752   83  Linux
/dev/sda3       136718336   214843335    39062500   83  Linux
/dev/sda4   *   214843392   215037951       97280   83  Linux

b. 硬碟構成:磁頭(head),磁軌(track),磁柱(cylinder),扇區(setor)
在網際網路上可以很容易找到解釋,我就不細說了。

我們在分割槽的時修改要能看懂硬碟資訊就夠了。

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

硬碟資訊:
/dev/sdb,1999.3 GB,255個磁頭,63個磁軌,243068個磁柱,3904897024個扇區。

c. 硬碟的分割槽
關於linux硬碟的分割槽主要分為 基本分割槽(primary partion)和擴充分割槽(extension partion)兩種,基本分割槽和擴充分割槽的數目之和不能大於4個。基本分割槽可以直接使用但不能再分割槽。擴充分割槽必須再進行二次分割槽才能使用。需要對擴充分割槽建立邏輯分割槽(logical partion),邏輯分割槽沒有數量上限制。

所以,對於新增硬碟來說,如果我們只有4個及以下的分割槽要求,可以直接用基本分割槽完成。如果1T的硬碟想分成6個區,就需要用擴充分割槽+邏輯分割槽來實現。

2. 新增硬碟分割槽,走入誤區

我現在外接了兩塊SATA 7200轉硬碟,分為都是1T的。重新計算機可以看硬碟資訊。

~ sudo fdisk -l

Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders, total 584843264 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000efd7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    97656831    48827392   82  Linux swap / Solaris
/dev/sda2        97656832   136718335    19530752   83  Linux
/dev/sda3       136718336   214843335    39062500   83  Linux
/dev/sda4   *   214843392   215037951       97280   83  Linux

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904892927   976222208    7  HPFS/NTFS/exFAT

兩塊硬碟,分別掛載到了/dev/sdb1,/dev/sdb2。下面我們對/dev/sdb2硬碟進行處理。

/dev/sdb2還沒有進行分割槽。

~ fdisk -l /dev/sdb2

Disk /dev/sdb2: 999.7 GB, 999651540992 bytes
255 heads, 63 sectors/track, 121534 cylinders, total 1952444416 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb2 doesn't contain a valid partition table

注意下面是進入誤區的思考:

對sdb2進行磁碟分割槽規劃:

/         50G
/extends  750G
  /ext1   150G
  /ext2   150G
  /ext3   150G
  /ext4   150G
  /ext5   150G

開始進行分割槽

~ sudo fdisk  /dev/sdb2
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x6e28e6e6.
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)

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)

建立第一個基本分割槽/,大小為50G, 50*1024*1024*1024/512+2048=104859648

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):
Using default value 1
First sector (2048-1952444415, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-1952444415, default 1952444415): 104859648

Command (m for help): p

Disk /dev/sdb2: 999.7 GB, 999651540992 bytes
255 heads, 63 sectors/track, 121534 cylinders, total 1952444416 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x44198dad

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb2p1            2048   104859648    52428800+  83  Linux

建立第二個擴充分割槽750G,750*1024*1024*1024/512+104859648=1677723648

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (104859649-1952444415, default 104859649):
Using default value 104859649
Last sector, +sectors or +size{K,M,G} (104859649-1952444415, default 1952444415): 1677723648

Command (m for help): p

Disk /dev/sdb2: 999.7 GB, 999651540992 bytes
255 heads, 63 sectors/track, 121534 cylinders, total 1952444416 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x44198dad

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb2p1            2048   104859648    52428800+  83  Linux
/dev/sdb2p2       104859649  1677723648   786432000    5  Extended

建立邏輯分割槽1, 150G, 150*1024*1024*1024/512+104861697=419434497

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (104861697-1677723648, default 104861697):
Using default value 104861697
Last sector, +sectors or +size{K,M,G} (104861697-1677723648, default 1677723648): 419434497

建立邏輯分割槽2, 150G, 150*1024*1024*1024/512+419436546=734009346
建立邏輯分割槽3, 150G, 150*1024*1024*1024/512+734009346=1048582146
建立邏輯分割槽4, 150G, 150*1024*1024*1024/512+1048582146=1363154946
建立邏輯分割槽5, 150G, 150*1024*1024*1024/512+1363154946=1677723648

檢視分割槽結果

Command (m for help): p

Disk /dev/sdb2: 999.7 GB, 999651540992 bytes
255 heads, 63 sectors/track, 121534 cylinders, total 1952444416 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x44198dad

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb2p1            2048   104859648    52428800+  83  Linux
/dev/sdb2p2       104859649  1677723648   786432000    5  Extended
/dev/sdb2p5       104861697   419434497   157286400+  83  Linux
/dev/sdb2p6       419436546   734009346   157286400+  83  Linux
/dev/sdb2p7       734011395  1048582146   157285376   83  Linux
/dev/sdb2p8      1048584195  1363154946   157285376   83  Linux
/dev/sdb2p9      1363156995  1677723648   157283327   83  Linux

退出儲存

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 22: Invalid argument.
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.

更新核心分割槽表
不用重啟系統,更新核心分割槽表

sudo partprobe

用fdisk檢視新建的分割槽

~ sudo fdisk -l

Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders, total 584843264 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000efd7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    97656831    48827392   82  Linux swap / Solaris
/dev/sda2        97656832   136718335    19530752   83  Linux
/dev/sda3       136718336   214843335    39062500   83  Linux
/dev/sda4   *   214843392   215037951       97280   83  Linux

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904892927   976222208    7  HPFS/NTFS/exFAT

這個時候,相信大家已經發現問題了,新建的分割槽完全沒有生效。

對硬碟sdb2使用fdisk檢視分割槽

~ fdisk -l /dev/sdb2

Disk /dev/sdb2: 999.7 GB, 999651540992 bytes
255 heads, 63 sectors/track, 121534 cylinders, total 1952444416 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x44198dad

     Device Boot      Start         End      Blocks   Id  System
/dev/sdb2p1            2048   104859648    52428800+  83  Linux
/dev/sdb2p2       104859649  1677723648   786432000    5  Extended
/dev/sdb2p5       104861697   419434497   157286400+  83  Linux
/dev/sdb2p6       419436546   734009346   157286400+  83  Linux
/dev/sdb2p7       734011395  1048582146   157285376   83  Linux
/dev/sdb2p8      1048584195  1363154946   157285376   83  Linux
/dev/sdb2p9      1363156995  1677723648   157283327   83  Linux

為什麼會這樣的?網上查了一下,一些朋友也遇到了這樣的情況。他們把sdb2p1的這種分割槽叫做子分割槽subpartition。

作業系統不明白,為什麼要在sdb2的分割槽上再建立sdb2p1子分割槽。但當時我也不明白,如果我想把一塊新的硬碟分割槽後再使用應該如何進行。其實,這種糾結的想法,原於對作業系統的不瞭解,導致了明白的人不會出現這種錯誤,出現這種錯誤的人找不到明白的解釋。

跳出子分割槽的概念,我們應該如何操作呢!

3. 完成對新增硬碟分割槽及掛載

上面的錯誤在於,我們不應該對/dev/sdb2硬碟分割槽,而是對/dev/sdb進行分割槽。

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904892927   976222208    7  HPFS/NTFS/exFAT

作業系統已經把兩塊新增加的硬碟,識別為了/dev/sdb,這個時候我們只需要刪除原有的/dev/sdb2基本分割槽,重建為擴充分割槽,然後再按要求做邏輯分割槽就可以了。

刪除sdb2分割槽,重建為擴充分割槽/dev/sdb2

~ sudo fdisk /dev/sdb
Command (m for help): d
Partition number (1-4): 2

Command (m for help): p

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2):
Using default value 2
First sector (1952448512-3904897023, default 1952448512):
Using default value 1952448512
Last sector, +sectors or +size{K,M,G} (1952448512-3904897023, default 3904897023):
Using default value 3904897023

Command (m for help): p

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904897023   976224256    5  Extended

重新規劃分割槽:

建立邏輯分割槽1, 150G, 150*1024*1024*1024/512+1952450560=2267023360
建立邏輯分割槽2, 150G, 150*1024*1024*1024/512+2267023360=2581596160
建立邏輯分割槽3, 150G, 150*1024*1024*1024/512+2581596160=2896168960
建立邏輯分割槽4, 150G, 150*1024*1024*1024/512+2896168960=3210741760
建立邏輯分割槽5, 150G, 150*1024*1024*1024/512+3210741760=3525314560
建立邏輯分割槽6, 189G, 剩餘空間

Command (m for help): p

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904897023   976224256    5  Extended
/dev/sdb5      1952450560  2267023360   157286400+  83  Linux
/dev/sdb6      2267025409  2581596160   157285376   83  Linux
/dev/sdb7      2581598209  2896168960   157285376   83  Linux
/dev/sdb8      2896171009  3210741760   157285376   83  Linux
/dev/sdb9      3210743809  3525314560   157285376   83  Linux
/dev/sdb10     3525316609  3904897023   189790207+  83  Linux

儲存退出

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.

更新核心分割槽表
sudo partprobe

檢視新建的分割槽

ls /dev/sdb
sdb    sdb1   sdb10  sdb2   sdb5   sdb6   sdb7   sdb8   sdb9

格式化分割槽

sudo mkfs -t ext4 /dev/sdb5
sudo mkfs -t ext4 /dev/sdb6
sudo mkfs -t ext4 /dev/sdb7
sudo mkfs -t ext4 /dev/sdb8
sudo mkfs -t ext4 /dev/sdb9
sudo mkfs -t ext4 /dev/sdb10

掛載分割槽

~ sudo mount -t ext4 /dev/sdb5 /disk/sdb5
~ ls /disk/sdb5
lost+found

檢視最終的分割槽效果

cos@delta:~$ sudo fdisk -l
[sudo] password for cos:

Disk /dev/sda: 299.4 GB, 299439751168 bytes
255 heads, 63 sectors/track, 36404 cylinders, total 584843264 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000efd7c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048    97656831    48827392   82  Linux swap / Solaris
/dev/sda2        97656832   136718335    19530752   83  Linux
/dev/sda3       136718336   214843335    39062500   83  Linux
/dev/sda4   *   214843392   215037951       97280   83  Linux

Disk /dev/sdb: 1999.3 GB, 1999307276288 bytes
255 heads, 63 sectors/track, 243068 cylinders, total 3904897024 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xf919a976

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048  1952448511   976223232    7  HPFS/NTFS/exFAT
/dev/sdb2      1952448512  3904897023   976224256    5  Extended
/dev/sdb5      1952450560  2267023360   157286400+  83  Linux
/dev/sdb6      2267025409  2581596160   157285376   83  Linux
/dev/sdb7      2581598209  2896168960   157285376   83  Linux
/dev/sdb8      2896171009  3210741760   157285376   83  Linux
/dev/sdb9      3210743809  3525314560   157285376   83  Linux
/dev/sdb10     3525316609  3904897023   189790207+  83  Linux

完成對/dev/sdb2硬碟的分割槽操作。

希望幫助和我一樣走入到誤區的朋友吧。


2013.7.8 

張丹部落格 http://blog.fens.me/linux-fdisk/

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26686207/viewspace-765782/,如需轉載,請註明出處,否則將追究法律責任。

相關文章