linux磁盤分區詳解

1.磁盤的分區主要分爲基本分區(primary partion)和擴充分區(extension partion)兩種,基本分區和擴充分區的數目之和不能大於四個。且基本分區能夠立刻被使用但不能再分區。擴充分區必須再進行分區後才能使用,也就是說它必須還要進行二次分區。那麼由擴充分區再分下去的是什麼呢?它就是邏輯分區(logical partion),何況邏輯分區沒有數量上限制。node

在 Linux 中,每個硬件設備都映射到一個系統的文件,對於硬盤、光驅等 IDE 或 SCSI 設備也不例外。Linux把各類 IDE 設備分配了一個由 hd 前綴組成的文件;而對於各類 SCSI 設備,則分配了一個由 sd 前綴組成的文件。linux

對於ide硬盤,驅動器標識符爲「hdx~」,其中「hd」代表分區所在設備的類型,這裏是指ide硬盤了。「x」爲盤號(a爲基本盤,b爲基本從屬盤,c爲輔助主盤,d爲輔助從屬盤),「~」表明分區,前四個分區用數字1到4表示,它們是主分區或擴展分區,從5開始就是邏輯分區。例,hda3表示爲第一個ide硬盤上的第三個主分區或擴展分區,hdb2表示爲第二個ide硬盤上的第二個主分區或擴展分區。對於scsi硬盤則標識爲「sdx~」,scsi硬盤是用「sd」來表示分區所在設備的類型的,其他則和ide硬盤的表示方法同樣,不在多說。app

在 Linux 中規定,每個硬盤設備最多能有 4個主分區(其中包含擴展分區)構成,任何一個擴展分區都要佔用一個主分區號碼,也就是在一個硬盤中,主分區和擴展分區一共最可能是 4 個。ide

Linux 規定了主分區(或者擴展分區)佔用 1 至 16 號碼中的前 4 個號碼。以第一個 IDE 硬盤爲例說明,主分區(或者擴展分區)佔用了 hda一、hda二、hda三、hda4,而邏輯分區佔用了 hda5 到 hda16 等 12 個號碼。
  所以,Linux 下面每個硬盤總共最多有 16 個分區。工具

  IDE硬盤最多有64個分區

ui

1.fdisk -l 查看系統分區詳細信息this

  

1 [root@test4 ~]# fdisk -l
2 Disk /dev/sda: 21.4 GB, 21474836480 bytes
3 255 heads, 63 sectors/track, 2610 cylinders
4 Units = cylinders of 16065 * 512 = 8225280 bytes
5 
6    Device Boot      Start         End      Blocks   Id  System
7 /dev/sda1   *           1          13      104391   83  Linux
8 /dev/sda2              14        2610    20860402+  8e  Linux LVM

註釋:這個硬盤的大小是21.4GB,有255個磁面,63個扇區,2610磁柱(cylinders)spa

每一個cylinder(磁柱)的容量是 8225280 bytes=8225.280 K(約爲)=8.225280M(約爲);日誌

  Device Boot      Start         End      Blocks  Id  Systemcode

/dev/sda1  *           1          13      104391  83  Linux

/dev/sda2              14        1305   10377990   8e  Linux LVM

 

id和System 表示的是一個意思,id看起來不太直觀,咱們要在fdisk 一個分區時,經過指定id來確認分區類型;好比 7表示的就NTFS 分區;這個在fdisk 中要經過t功能來指定。83是linux。下面的部分會提到;

說明:硬盤分區的表示:在Linux 是經過hd*x 或 sd*x 表示的:

其中* 表示的是a、b、c ... ...

另外x 表示的數字 一、二、3 ... ...

hd大可能是IDE硬盤;sd大可能是SCSI或移動存儲;引導(Boot):表示引導分區,在上面的例子中sda1 是引導分區;

Start (開始):表示的一個分區從Xcylinder(磁柱)開始;

End (結束):表示一個分區到 Ycylinder(磁柱)結束;

2.fdisk  對磁盤進行分區

[root@test4 ~]# fdisk /dev/sda  //對sda磁盤進行分區
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.


The number of cylinders for this disk is set to 2597.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
    (e.g., DOS FDISK, OS/2 FDISK)
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 //建立一個新的空白DOS分區表
    p   print the partition table    //打印分區表
    q   quit without saving changes  //退出不保存設置
    s   create a new empty Sun disklabel
    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     
Command action
   e   extended                 //e是擴展分區
   p   primary partition (1-4)  //p是主分區
p
Partition number (1-4): 1       //定義分區數量   --主分區最多隻能有四個
First cylinder (1-2597, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-2597, default 2597): +100M

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: 無效的參數.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

 

[root@test6 ~]# fdisk /dev/sda

The number of cylinders for this disk is set to 2610.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): n
First cylinder (1710-2610, default 1710): 
Using default value 1710
Last cylinder or +size or +sizeM or +sizeK (1710-2610, default 2610): +100M     

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: 設備或資源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
[root@test6 ~]# partprobe /dev/sda //對硬盤進行更新
[root@test6 ~]# fdisk -l

Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14         274     2096482+  82  Linux swap / Solaris
/dev/sda3             275         404     1044225   8e  Linux LVM
/dev/sda4             405        2610    17719695    5  Extended
/dev/sda5             405        1709    10482381   83  Linux
/dev/sda6            1710        1722      104391   83  Linux
[root@test6 ~]# mkfs.ext3 /dev/sda6  //須要先進行格式化,才能使用 。mkfs -t ext3 /dev/sda5 (mkfs.ext3 同樣效果 -t 指定類型 -b 指定block大小·)
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
26104 inodes, 104388 blocks
5219 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729

Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@test6 ~]# mount /dev/sda6 /mnt/sda7/  //進行掛載使用
anaconda-ks.cfg  install.log  install.log.syslog
[root@test6 ~]# df -h
文件系統              容量  已用 可用 已用% 掛載點
/dev/sda5             9.7G  1.1G  8.1G  12% /
/dev/sda1              99M   12M   83M  13% /boot
tmpfs                 252M     0  252M   0% /dev/shm
/dev/hdc              3.9G  3.9G     0 100% /mnt/cdrom
/dev/sda6              99M  5.6M   89M   6% /mnt/sda7
1.磁盤管理經常使用命令
  1.1 ls -i 文件名 查看文件存儲在哪一個innode中
1 [root@test6 ~]# ls -i lstest 
2 1179659 lstest

  1.2 ls -id 文件夾名 查看文件夾存儲在哪一個innode中

1 [root@test6 ~]# ls -id lstest1
2 1179661 lstest1

  1.3 filefrag -v 文件名 查看文件存儲block具體位置

1 [root@test6 ~]# filefrag -v lstest
2 Checking lstest
3 Filesystem type is: ef53
4 Filesystem cylinder groups is approximately 78
5 Blocksize of file lstest is 4096
6 File size of lstest is 5 (1 blocks)
7 First block: 1183744
8 Last block: 1183744
9 lstest: 1 extent found

2.硬盤工具:

  2.1 dumpe2fs /dev/sda1 查看分區superblock 和blockgroup等詳細信息
[root@test6 ~]# dumpe2fs /dev/sda1
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:   /boot
Last mounted on:          <not available>
Filesystem UUID:          0c7f5306-fd13-4b13-819f-4b6cab126938
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              26104
Block count:              104388
Reserved block count:     5219
Free blocks:              89552
Free inodes:              26069
First block:              1
Block size:               1024
Fragment size:            1024
Reserved GDT blocks:      256
Blocks per group:         8192
Fragments per group:      8192
Inodes per group:         2008
Inode blocks per group:   251
Filesystem created:       Tue Mar 28 07:57:05 2017
Last mount time:          Tue Mar 28 08:29:57 2017
Last write time:          Tue Mar 28 08:29:57 2017
Mount count:              3
Maximum mount count:      -1
Last checked:             Tue Mar 28 07:57:05 2017
Check interval:           0 (<none>)
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:               128
Journal inode:            8  //ext3 日誌inode位置
Default directory hash:   tea
Directory Hash Seed:      5e624e4a-049c-4cbf-a65f-8b8a7d08e398
Journal backup:           inode blocks
Journal size:             4114k  //日誌文件大小


Group 0: (Blocks 1-8192)
  Primary superblock at 1, Group descriptors at 2-2
  Reserved GDT blocks at 3-258
  Block bitmap at 259 (+258), Inode bitmap at 260 (+259)
  Inode table at 261-511 (+260)
  1023 free blocks, 1990 free inodes, 2 directories
  Free blocks: 4642-5664
  Free inodes: 19-2008
Group 1: (Blocks 8193-16384)
  Backup superblock at 8193, Group descriptors at 8194-8194
  Reserved GDT blocks at 8195-8450
  Block bitmap at 8451 (+258), Inode bitmap at 8452 (+259)
  Inode table at 8453-8703 (+260)
  4583 free blocks, 2008 free inodes, 0 directories
  Free blocks: 11802-16384
  Free inodes: 2009-4016
Group 2: (Blocks 16385-24576)
  Block bitmap at 16385 (+0), Inode bitmap at 16386 (+1)
  Inode table at 16387-16637 (+2)
  7939 free blocks, 2008 free inodes, 0 directories
  Free blocks: 16638-24576
  Free inodes: 4017-6024
Group 3: (Blocks 24577-32768)
  Backup superblock at 24577, Group descriptors at 24578-24578
  Reserved GDT blocks at 24579-24834
  Block bitmap at 24835 (+258), Inode bitmap at 24836 (+259)
  Inode table at 24837-25087 (+260)
  7439 free blocks, 1991 free inodes, 1 directories
  Free blocks: 25193-25600, 25609-26112, 26121-26624, 26632-27136, 27144-27648, 27657-28160, 28168-28672, 28683-29184, 29192-29696, 29704-30208, 30218-31744, 31804-32768
  Free inodes: 6042-8032
Group 4: (Blocks 32769-40960)
  Block bitmap at 32769 (+0), Inode bitmap at 32770 (+1)
  Inode table at 32771-33021 (+2)
  7939 free blocks, 2008 free inodes, 0 directories
  Free blocks: 33022-40960
  Free inodes: 8033-10040
Group 5: (Blocks 40961-49152)
  Backup superblock at 40961, Group descriptors at 40962-40962
  Reserved GDT blocks at 40963-41218
  Block bitmap at 41219 (+258), Inode bitmap at 41220 (+259)
  Inode table at 41221-41471 (+260)
  7681 free blocks, 2008 free inodes, 0 directories
  Free blocks: 41472-49152
  Free inodes: 10041-12048
Group 6: (Blocks 49153-57344)
  Block bitmap at 49153 (+0), Inode bitmap at 49154 (+1)
  Inode table at 49155-49405 (+2)
  7939 free blocks, 2008 free inodes, 0 directories
  Free blocks: 49406-57344
  Free inodes: 12049-14056
Group 7: (Blocks 57345-65536)
  Backup superblock at 57345, Group descriptors at 57346-57346
  Reserved GDT blocks at 57347-57602
  Block bitmap at 57603 (+258), Inode bitmap at 57604 (+259)
  Inode table at 57605-57855 (+260)
  7681 free blocks, 2008 free inodes, 0 directories
  Free blocks: 57856-65536
  Free inodes: 14057-16064
Group 8: (Blocks 65537-73728)
  Block bitmap at 65537 (+0), Inode bitmap at 65538 (+1)
  Inode table at 65539-65789 (+2)
  7939 free blocks, 2008 free inodes, 0 directories
  Free blocks: 65790-73728
  Free inodes: 16065-18072
Group 9: (Blocks 73729-81920)
  Backup superblock at 73729, Group descriptors at 73730-73730
  Reserved GDT blocks at 73731-73986
  Block bitmap at 73987 (+258), Inode bitmap at 73988 (+259)
  Inode table at 73989-74239 (+260)
  7681 free blocks, 2008 free inodes, 0 directories
  Free blocks: 74240-81920
  Free inodes: 18073-20080
Group 10: (Blocks 81921-90112)
  Block bitmap at 81921 (+0), Inode bitmap at 81922 (+1)
  Inode table at 81923-82173 (+2)
  7939 free blocks, 2008 free inodes, 0 directories
  Free blocks: 82174-90112
  Free inodes: 20081-22088
Group 11: (Blocks 90113-98304)
  Block bitmap at 90113 (+0), Inode bitmap at 90114 (+1)
  Inode table at 90115-90365 (+2)
  7939 free blocks, 2008 free inodes, 0 directories
  Free blocks: 90366-98304
  Free inodes: 22089-24096
Group 12: (Blocks 98305-104387)
  Block bitmap at 98305 (+0), Inode bitmap at 98306 (+1)
  Inode table at 98307-98557 (+2)
  5830 free blocks, 2008 free inodes, 0 directories
  Free blocks: 98558-104387
  Free inodes: 24097-26104
View Code
  2.2 fsck /dev/sda 檢查硬盤狀況 -C 顯示檢查過程 -f 強制檢查
  2.3 badblocks /dev/sda5 檢查硬盤壞道 -sv 顯示進度和結果 
df 查看文件系統(-h 以合適的單位示系統大小 -T 顯示系統類型)
du 查看文件夾大小,包含文件夾裏面文件
du -s 僅僅查看文件夾大小
du -h 以M方式顯示大小,方便查看
 
 
fuser -mv /mnt 當顯示 設備正在忙的時候,能夠使用這個命令查看使用進程
tune2fs -l /dev/sda 查看磁盤參數(-h 查看可選選項,進行選項更改)
 1 [root@test6 ~]# tune2fs -l /dev/sda5
 2 tune2fs 1.39 (29-May-2006)
 3 Filesystem volume name:   /     //卷標
 4 Last mounted on:          <not available>
 5 Filesystem UUID:          4b64bcce-0acc-40f2-85e6-07e198152c20
 6 Filesystem magic number:  0xEF53
 7 Filesystem revision #:    1 (dynamic)
 8 Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
 9 Default mount options:    user_xattr acl
10 Filesystem state:         clean
11 Errors behavior:          Continue
12 Filesystem OS type:       Linux
13 Inode count:              2621440
14 Block count:              2620595
15 Reserved block count:     131029
16 Free blocks:              2257390
17 Free inodes:              2582833
18 First block:              0
19 Block size:               4096
20 Fragment size:            4096
21 Reserved GDT blocks:      639
22 Blocks per group:         32768
23 Fragments per group:      32768
24 Inodes per group:         32768
25 Inode blocks per group:   1024
26 Filesystem created:       Tue Mar 28 07:57:02 2017
27 Last mount time:          Tue Mar 28 08:29:56 2017
28 Last write time:          Tue Mar 28 08:29:56 2017
29 Mount count:              3
30 Maximum mount count:      -1    //超過最大次數,則須要對分區進行自檢
31 Last checked:             Tue Mar 28 07:57:02 2017
32 Check interval:           0 (<none>)
33 Reserved blocks uid:      0 (user root)
34 Reserved blocks gid:      0 (group root)
35 First inode:              11
36 Inode size:               128
37 Journal inode:            8
38 First orphan inode:       163883
39 Default directory hash:   tea
40 Directory Hash Seed:      8633f5c3-e870-428e-8ce3-7d636f0ef80a
41 Journal backup:           inode blocks

 

e21able /dev/sda7 aa 卷標的另外一種改法
相關文章
相關標籤/搜索