菜鳥教你如何磁盤分區與創建文件系統以及掛載(1)

何為磁盤分區,為什么要分區? 

  計算機中存放信息的主要的存儲設備就是硬盤,但是硬盤不能直接使用,必須對硬盤進行分割,分割成的一塊一塊的硬盤區域就是磁盤分區。在傳統的磁盤管理中,將一個硬盤分為兩大類分區:主分區和擴展分區。主分區是能夠安裝操作系統,能夠進行計算機啟動的分區,這樣的分區可以直接格式化,然后安裝系統,直接存放文件。

MBR分區結構

  在一個MBR分區表類型的硬盤中最多只能存在4個主分區。如果一個硬盤上需要超過4個以上的磁盤分塊的話,那么就需要使用擴展分區了。如果使用擴展分區,那么一個物理硬盤上最多只能3個主分區和1個擴展分區。擴展分區不能直接使用,它必須經過第二次分割成為一個一個的邏輯分區,然后才可以使用。一個擴展分區中的邏輯分區可以任意多個。

 

1、Linux磁盤分區查看

[root@centos6 ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda: 214.7 GB  #該sda硬盤磁盤容量為 214.7GB     214748364800 bytes:總共 214748364800 字節
255 heads:255個磁頭     63 sectors/track:一個盤面總共63個扇區     26108 cylinders:26108個柱面
Units = cylinders of 16065 * 512 = 8225280 bytes:每個柱面(cylinders )總共有 16065個扇區,每個扇區總共 512字節,所以每個柱面總共為 16065*512 =8225280 bytes(字節)
Sector size (logical/physical): 512 bytes / 512 bytes:每個扇區的邏輯和物理容量大小都為 512 bytes(字節)
I/O size (minimum/optimal): 512 bytes / 512 bytes:I/O的容量最小和最佳值都為 512 bytes(字節)
Disk identifier: 0x00007b7d:磁盤標識符為 0x00007b7d
Device:設備名稱     Boot:是否可引導     Start:起始柱面     End:結束柱面     Blocks:包含的磁盤塊數     Id:分區標識     System:應用在那個系統在上常用的分區標識

/dev/sda1:分區名稱   *:可引導設備   1:從第1個柱面開始      26:在第26個柱面結束   204800:磁盤塊數為204800       83:分區ID標識為83       Linux:應用在linux系統之上
/dev/sda2:分區名稱            26:從第26個柱面開始     12774:在第12774個柱面結束   102400000:磁盤塊數為102400000   83:分區ID標識為83  Linux:應用在linux系統之上
/dev/sda3:分區名稱          12774:從第12774柱面開始     14049:在第14049個柱面結束   10240000:磁盤塊數為10240000   83:分區ID標識為83  Linux:應用在linux系統之上
/dev/sda4:分區名稱          14049: 從第14049個柱面開始   26109:在第26109個柱面結束   96869376:磁盤塊數為96869376    5:分區ID標識為5  Extended:應用在linux系統擴展分區之上
/dev/sda5:分區名稱          14049:從第14049個柱面開始   14310:在第14310個柱面結束   2097152:磁盤塊數為2097152      82:分區ID標識為82  Linux swap / Solaris:應用在linux系統swap之上

2、Linux磁盤分區

fdisk 命令

簡介:
fdisk - Partition table manipulator for Linux
格式:
fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
fdisk -l [-u] [device...]
fdisk -s partition...
fdisk -v
fdisk -h
Command action
   a   toggle a bootable flag     #設置一個標識,說明這個分區是可啟動的    
   b   edit bsd disklabel     #編輯BSD Linux系統用的磁盤標簽
   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 Linux系統創建一個新磁盤標簽
   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)     #高級功能
實例1:
[root@centos6 ~]# fdisk /dev/sda     #對sda這塊硬盤進行分區管理    

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): p     #查看當前分區情況

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris

Command (m for help): n     #創建一個新分區
First cylinder (14310-26109, default 14310):     #默認以柱面進行分區,默認柱面開始值為14310,直接回車就是選擇默認值
Using default value 14310
Last cylinder, +cylinders or +size{K,M,G} (14310-26109, default 26109): 14500     #填寫結束柱面值14500

Command (m for help): p     #查看以分好的區

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       14500     1526194+  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.
[root@centos6 ~]# fdisk -l     #查看分區情況(真實情況)

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       14500     1526194+  83  Linux
[root@centos6 ~]# cat /proc/partitions     #查看是否已寫入內核中,顯示并唯有邏輯分區/dev/sda6
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    2097152 sda5
[root@centos6 ~]# partx -a /dev/sda     #可用此方法讓內核掃描分區情況
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@centos6 ~]# partx -a /dev/sda    #如果第一次掃描未有顯示,可再次執行該命令
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
[root@centos6 ~]# cat /proc/partitions     #查看內核是否已識別 
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    2097152 sda5
   8        6    1526194 sda6
[root@centos6 ~]# ls /dev/sda*     #也可以用此類方法查看
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6
[root@centos6 ~]# lsblk     #也可以用這種方法查看
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0  9.8G  0 part /testdir
├─sda4   8:4    0    1K  0 part 
├─sda5   8:5    0    2G  0 part [SWAP]
└─sda6   8:6    0  1.5G  0 part 
[root@centos6 ~]#
實例2:
[root@centos6 ~]# partx -d -n 6 /dev/sda     #不用fdisk命令,直接用partx命令刪除分區
[root@centos6 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    2097152 sda5
實例3:
[root@centos6 ~]# fdisk /dev/sda

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): n     #創建一個新分區
First cylinder (14310-26109, default 14310): 
Using default value 14310     #默認起始柱面
Last cylinder, +cylinders or +size{K,M,G} (14310-26109, default 26109): 15000     #設置結束柱面為15000

Command (m for help): t     #設置分區類型
Partition number (1-6): 6
Hex code (type L to list codes): 82     #設置分區類型為82,也就是swap分區
Changed system type of partition 6 to 82 (Linux swap / Solaris)

Command (m for help): p     #查看分區情況

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       15000     5542444+  82  Linux swap / Solaris

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.
[root@centos6 ~]# partx -a /dev/sda     #讓內核掃描分區
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@centos6 ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
[root@centos6 ~]# lsblk     #查看內核已經識別分好的分區
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0  9.8G  0 part /testdir
├─sda4   8:4    0    1K  0 part 
├─sda5   8:5    0    2G  0 part [SWAP]
└─sda6   8:6    0  5.3G  0 part 
[root@centos6 ~]# fdisk -l /dev/sda      #再次查看分區情況

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       15000     5542444+  82  Linux swap / Solaris
[root@centos6 ~]#

原創文章,作者:Aleen,如若轉載,請注明出處:http://www.www58058.com/40689

(0)
AleenAleen
上一篇 2016-08-27
下一篇 2016-08-27

相關推薦

欧美性久久久久