Linux磁盤分區與格式化

磁盤分區格式說明html

linux分區不一樣於windowsnode

linux下分區標示:linux

例如:hda1windows

hd這兩個字母表示分區所在的設備類型,hd標示IDE類型硬盤,sd表示SCSI類型硬盤ide

第三字母a標示硬盤在哪一個接口工具

 

linux下硬盤設備名爲(IDE硬盤爲hdx(x爲從a—d)由於IDE硬盤最多四個,SCSI,SATA,USB硬盤爲sdx(x爲a—z)),硬盤主分區最多爲4個,不用說你們也知道…..因此主分區從sdb1開始到sdb4,邏輯分區從sdb5開始,(邏輯分區永遠從sdb5開始…)設備名可使用fdisk –l查看測試

 

Linux磁盤分區與格式化 ui

 

格式化原理:this

 

 MBR:主引導扇區
主分區表:64bytes,最多隻能分四個主分區,每一個主分區的記錄(相關信息,好比分區大小,位置)在主分區表裏佔14bytes。
 
若是要建多於四個的分區,就要拿出一個主分區作爲擴展分區,再在擴展分區裏面進行其它的分區操做。在 建擴展分區的時候會創建一張對應的擴展分區表,它記錄了在這個擴展分區裏的分區的相關信息;理論上它沒有分區數量的限制,在擴展分區內部的分區叫作邏輯分區,如上圖中的 /dev/hda5,/dev/hda6/,/dev/hda7
 
在分好區後,分區裏面是空的,沒有任何東西。爲了能讓OS識別,就必需要向分區裏寫入相應格式的數據。好比windows的FAT32,NTFS;Linux的ext2,ext3,ext4(目前ext3格式的用的比較多,ext4還在實驗之中,在新的Fedora上使用的就是ext4的文件系統)。
 
Windows/dos經常使用的分區工具:fdisk/partition magic/diskpart
Linux下經常使用的分區工具:
    fdisk/sfdisk:命令行工具,各類版本和環境都能使用,包含在軟件包util-linux中
    diskdruid:圖形化分區工具,只能在安裝REDHAT系統時使用。
 
下面咱們開始實驗:
環境/工具:Fedora 14/256M內存卡;fdisk
第一步:fdisk
[root@novice ~]# fdisk -l /dev/sdb 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
 
[root@novice ~]# fdisk /dev/sdb 
 
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) 
#help雖然是英文的,可都很簡單,在這裏再也不解釋。
 
#如今,咱們正式開始分區的操做:
Command (m for help): n #新建分區
Command action 
   e   extended 
   p   primary partition (1-4) 
#e/p分別對應擴展分區 /主分區;咱們先分四個主分區,每一個50M;而後再來增長主分區或擴展分區,看會出現怎樣的情況,嘿嘿。
p  #分區類型爲主分區
Partition number (1-4, default 1): 1  #分區號,在這裏咱們依次選擇一、二、三、4
First sector (2048-496127, default 2048):  #指定分區的起始扇區,通常默認,按enter鍵便可。
Last sector, +sectors or +size{K,M,G} (2048-496127, default 496127): +50M  #指定分區的終止扇區,根據前面的提示咱們能夠作出相應的選擇+sectors 或 +size{K,M,G}
Command (m for help): p  #用p打印出已建好的分區列表
 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
 
#剩下的三個分區的創建操做同上
#分好四個主分區後的狀況以下
Command (m for help): p 
 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
/dev/sdb2          104448      206847       51200   83  Linux 
/dev/sdb3          206848      309247       51200   83  Linux 
/dev/sdb4          309248      309298          25+  83  Linux 
#已經建好四個主分區啦,如今咱們來看看若是再建主分區或是擴展分區的話會出現怎樣的狀況:
Command (m for help): 
You must delete some partition and add an extended partition first 
#看到了吧,不能再建分區啦!要再建分區的話必須刪除some分區,再新建一個擴展分區才行。
#如今,咱們刪掉一個主分區,來新建擴展分區
Command (m for help): d  #刪除分區
Partition number (1-4): 4   #選擇要刪除分區的分區號,咱們選第四個
 
Command (m for help): p   #打印,以下,四個分區變成了三個!
 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
/dev/sdb2          104448      206847       51200   83  Linux 
/dev/sdb3          206848      309247       51200   83  Linux 
 
 
 
#新建一個擴展分區
#若是在沒有建滿三個主分的區的狀況下創建擴展分區,相關選項會有些不一樣。
Command (m for help): n 
Command action 
   e   extended 
   p   primary partition (1-4) 
e   
Selected partition 4 
First sector (309248-496127, default 309248): #enter,默認
Using default value 309248 
Last sector, +sectors or +size{K,M,G} (309248-496127, default 496127):   #enter,默認,使用剩餘空間
Using default value 496127 
 
Command (m for help): p 
 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
/dev/sdb2          104448      206847       51200   83  Linux 
/dev/sdb3          206848      309247       51200   83  Linux 
/dev/sdb4          309248      496127       93440    5  Extended 
 
#接下來,咱們在新建的擴展分區裏再新建兩個邏輯分區,由於已經有了三個主分區,這裏不會再顯示是創建邏輯分區仍是主分區的提示!
Command (m for help): n 
First sector (311296-496127, default 311296):  #enter
Using default value 311296 
Last sector, +sectors or +size{K,M,G} (311296-496127, default 496127): +50M 
 
Command (m for help): n 
First sector (415744-496127, default 415744):  #enter
Using default value 415744 
Last sector, +sectors or +size{K,M,G} (415744-496127, default 496127):  #enter
Using default value 496127 
 
Command (m for help): p 
 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
/dev/sdb2          104448      206847       51200   83  Linux 
/dev/sdb3          206848      309247       51200   83  Linux 
/dev/sdb4          309248      496127       93440    5  Extended 
/dev/sdb5          311296      413695       51200   83  Linux 
/dev/sdb6          415744      496127       40192   83  Linux 
#上面的列表,就是咱們今天分區的成果啦!接下來保存退出,重啓計算機,就能夠進行下一步的mkfs操做啦!若是忘記了相關的操做命令,記得按m!!!
Command (m for help): w  #保存
The partition table has been altered! 
 
Calling ioctl() to re-read partition table. 
Syncing disks. 
 
另:在建好分區後,咱們還能夠更改相關分區的文件系統類型
#如,咱們要把第二個主分區改爲Linux下的交換分區,操做以下
Command (m for help): t  #更改文件系統類型
Partition number (1-6): 2  #選擇第二個分區
Hex code (type L to list codes): L #選擇要更改的文件系統編碼,能夠按L來查看相關編碼信息
0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT- 
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT- 
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT- 
 ............
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto 
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 
Hex code (type L to list codes): 82    #查找到linux swap的編碼爲82
Changed system type of partition 2 to 82 (Linux swap / Solaris) 
Command (m for help): p 
..............
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
/dev/sdb2          104448      206847       51200   82  Linux swap / Solaris 
/dev/sdb3          206848      309247       51200   83  Linux 
/dev/sdb4          309248      496127       93440    5  Extended 
/dev/sdb5          311296      413695       51200   83  Linux 
/dev/sdb6          415744      496127       40192   83  Linux 
#最後別忘了保存!若是你需要的話!
#擴展分區不能直接使用,邏輯分區只能創建在擴展分區上!
 
第二步:mkfs(mkfs時分區的格式最好與fdisk設定的分區格式一致,否則.......)
mkfs支持ext2 ext3 vfa msdos jfs reiserfs等文件系統。
用法1:mkfs -t <fstype> <partition>
例: mkfs -t ext3 /dev/sdb2
 
用法2:mkfs.<fstype> <partition>
例:mkfs,vfat /dev/sdb3
 
mke2fs支持ext2/ext3文件系統
用法:mke2fs [-j] <partition>
例:mke2fs -j /dev/sdb5
# 更多更具體的用法請參照相關命令的man手冊
 
下面,接着實驗:
例一
[root@novice ~]# mkfs -t ext3 /dev/sdb1 
mke2fs 1.41.12 (17-May-2010) 
Filesystem label= 
OS type: Linux 
Block size=1024 (log=0) 
Fragment size=1024 (log=0) 
Stride=0 blocks, Stripe width=0 blocks 
12824 inodes, 51200 blocks 
2560 blocks (5.00%) reserved for the super user 
First data block=1 
Maximum filesystem blocks=52428800 
7 block groups 
8192 blocks per group, 8192 fragments per group 
1832 inodes per group 
Superblock backups stored on blocks: 
8193, 24577, 40961 
 
Writing inode tables: done                            
Creating journal (4096 blocks): done 
Writing superblocks and filesystem accounting information: done 
 
This filesystem will be automatically checked every 34 mounts or 
180 days, whichever comes first.  Use tune2fs -c or -i to override. 
 
例二:
[root@novice ~]# fdisk /dev/sdb 
 
Command (m for help): t 
Partition number (1-6): 6 
Hex code (type L to list codes): L 
 
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT- 
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT- 
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT- 
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / . 
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
.........
Hex code (type L to list codes): 7 
Changed system type of partition 6 to 7 (HPFS/NTFS) 
 
Command (m for help): p 
 
Disk /dev/sdb: 254 MB, 254017536 bytes 
8 heads, 61 sectors/track, 1016 cylinders, total 496128 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 
 
   Device Boot      Start         End      Blocks   Id  System 
/dev/sdb1            2048      104447       51200   83  Linux 
/dev/sdb2          104448      206847       51200   82  Linux swap / Solaris 
/dev/sdb3          206848      309247       51200   83  Linux 
/dev/sdb4          309248      496127       93440    5  Extended 
/dev/sdb5          311296      413695       51200   83  Linux 
/dev/sdb6          415744      496127       40192    7  HPFS/NTFS 
 
Command (m for help): w 
The partition table has been altered! 
 
Calling ioctl() to re-read partition table. 
Syncing disks. 
[root@novice ~]# mkfs.ntfs /dev/sdb6 
Cluster size has been automatically set to 4096 bytes. 
Initializing device with zeroes: 100% - Done. 
Creating NTFS volume structures. 
mkntfs completed successfully. Have a nice day. 
 
 
 第三部:掛載
掛載:mount
例:mount /dev/sdb1 /mnt /sdb1
 
卸載:umonut
例:umount /dev/sdb1
 
/etc/fstab:
能夠把要掛載的分區寫入/etc/fstab,這樣在開機的時候就能自動掛載啦!
#關於mount/nmout具體用法請參照man手冊,/etc/fstab內的掛載格式,參數等在該文件內都有相關的說明。這裏只作一個簡單的測試,再也不累述!
 
[root@novice /]# cd /mnt 
[root@novice mnt]# mkdir sdb 
[root@novice mnt]# mount /dev/sdb1 /mnt/sdb 
[root@novice mnt]# cd /mnt/sdb 
[root@novice sdb]# echo "Hello! I am here" > test.txt 
[root@novice sdb]# cat test.txt 
Hello! I am here 
[root@novice sdb]# cd .. 
[root@novice mnt]# umount /dev/sdb1 
[root@novice mnt]# cd /mnt/sdb 
[root@novice sdb]# ls -l 
total 0 
#剛作的文件系統已經能正常使用啦!如今再來看看掛載沒有通過mkfs的分區,會怎樣!
[root@novice sdb]# mount /dev/sdb2 /mnt/sdb 
mount: unknown filesystem type 'LVM2_member' 
#不解釋了喔!
相關文章
相關標籤/搜索