linux磁盘分区详解

1.磁盘的分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数目之和不能大于四个。且基本分区可以马上被使用但不能再分区。扩充分区必须再进行分区后才能使用,也就是说它必须还要进行二次分区。那么由扩充分区再分下去的是什么呢?它就是逻辑分区(logical partion),况且逻辑分区没有数量上限制。

在 Linux 中,每一个硬件设备都映射到一个系统的文件,对于硬盘、光驱等 IDE 或 SCSI 设备也不例外。Linux把各种 IDE 设备分配了一个由 hd 前缀组成的文件;而对于各种 SCSI 设备,则分配了一个由 sd 前缀组成的文件。

对于ide硬盘,驱动器标识符为“hdx”,其中“hd”表明分区所在设备的类型,这里是指ide硬盘了。“x”为盘号(a为基本盘,b为基本从属盘,c为辅助主盘,d为辅助从属盘),“”代表分区,前四个分区用数字1到4表示,它们是主分区或扩展分区,从5开始就是逻辑分区。例,hda3表示为第一个ide硬盘上的第三个主分区或扩展分区,hdb2表示为第二个ide硬盘上的第二个主分区或扩展分区。对于scsi硬盘则标识为“sdx~”,scsi硬盘是用“sd”来表示分区所在设备的类型的,其余则和ide硬盘的表示方法一样,不在多说。

在 Linux 中规定,每一个硬盘设备最多能有 4个主分区(其中包含扩展分区)构成,任何一个扩展分区都要占用一个主分区号码,也就是在一个硬盘中,主分区和扩展分区一共最多是 4 个。

Linux 规定了主分区(或者扩展分区)占用 1 至 16 号码中的前 4 个号码。以第一个 IDE 硬盘为例说明,主分区(或者扩展分区)占用了 hda1、hda2、hda3、hda4,而逻辑分区占用了 hda5 到 hda16 等 12 个号码。
  因此,Linux 下面每一个硬盘总共最多有 16 个分区。

IDE硬盘最多有64个分区

1.fdisk -l 查看系统分区详细信息

[root@test4 ~]# 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        2610    20860402+  8e  Linux LVM

注释:这个硬盘的大小是21.4GB,有255个磁面,63个扇区,2610磁柱(cylinders)

每个cylinder(磁柱)的容量是 8225280 bytes=8225.280 K(约为)=8.225280M(约为);

Device Boot Start End Blocks Id System

/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 是通过hdx 或 sdx 表示的:

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

另外x 表示的数字 1、2、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
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)
inodes, 104388 blocks
blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
block groups
blocks per group, 8192 fragments per group
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
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中

[root@test6 ~]# ls -i lstest 
 1179659 lstest

1.2 ls -id 文件夹名 查看文件夹存储在哪个innode中

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

1.3 filefrag -v 文件名 查看文件存储block具体位置

[root@test6 ~]# filefrag -v lstest
Checking lstest
Filesystem type is: ef53
Filesystem cylinder groups is approximately 78
Blocksize of file lstest is 4096
File size of lstest is 5 (1 blocks)
First block: 1183744
Last block: 1183744
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
free blocks, 2008 free inodes, 0 directories
  Free blocks: 98558-104387
  Free inodes: 24097-26104

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 查看可选选项,进行选项更改)

[root@test6 ~]# tune2fs -l /dev/sda5
tune2fs 1.39 (29-May-2006)
Filesystem volume name:   /     //卷标
Last mounted on:          <not available>
Filesystem UUID:          4b64bcce-0acc-40f2-85e6-07e198152c20
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery sparse_super large_file
Default mount options:    user_xattr acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              2621440
Block count:              2620595
Reserved block count:     131029
Free blocks:              2257390
Free inodes:              2582833
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      639
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         32768
Inode blocks per group:   1024
Filesystem created:       Tue Mar 28 07:57:02 2017
Last mount time:          Tue Mar 28 08:29:56 2017
Last write time:          Tue Mar 28 08:29:56 2017
Mount count:              3
Maximum mount count:      -1    //超过最大次数,则需要对分区进行自检
Last checked:             Tue Mar 28 07:57:02 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
First orphan inode:       163883
Default directory hash:   tea
Directory Hash Seed:      8633f5c3-e870-428e-8ce3-7d636f0ef80a
Journal backup:           inode blocks

e21able /dev/sda7 aa 卷标的另一种改法

    原文作者:cooperSun
    原文地址: https://blog.csdn.net/sunpeng1117/article/details/89307613
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞