深入理解Linux磁盘的奥秘

来源:互联网 发布:js获取被选中的radio 编辑:程序博客网 时间:2024/05/21 14:49

当我们想在系统里增加一块硬盘的时候,要做以下这四步工作:

  1. 对磁盘进行分区
  2. 对新建的分区进行格式化,目的是为了创建系统可用的文件系统
  3. 对新建的文件系统进行检验
  4. 将新建的文件系统挂载到系统的目录树上

磁盘分区:fdisk

fdisk [-l] 设备名称-l:加上这个参数会输出后面接的这个设备的所有分区的信息;如果后面不写设备名称,那么系统中所有设备的分区信息都会被列出来PS:fdisk这个命令只是一系列磁盘分区功能的入口命令!

例子:给咱电脑的磁盘进行一下分区

//1。找到所有的磁盘设备的名字[root@iZ28st035lsZ ~]# df文件系统               1K-块        已用     可用 已用% 挂载点/dev/hda1             20307028   2666372  16592468  14% ///2。对hda进行分区[root@iZ28st035lsZ ~]# fdisk /dev/hda //这里设备名称不加数字!The number of cylinders for this disk is set to 3343.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): 
//查看帮助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)
//显示分区表信息Command (m for help): pDisk /dev/hda: 21.4 GB, 21474836480 bytes224 heads, 56 sectors/track, 3343 cylindersUnits = cylinders of 12544 * 512 = 6422528 bytes   Device Boot      Start         End      Blocks   Id  System/dev/hda1   *           1        3343    20963801   83  LinuxPartition 1 does not end on cylinder boundary.Device:设备文件名字Boot:是否为开机引导模块,Windows下C盘需要开机引导模块Start,End:表示这个分区的开始柱面号与终止柱面号Blocks:这个分区中block的数量,单位是1KB

注意:fdisk -l可以列出整个系统中所有磁盘的所有分区,如果有多个磁盘(比如插入了一块U盘),那么执行这个命令后,再选择p,结果是这样的:
PS:也就是会显示两块这样的信息,分别代表两块磁盘中的分区情况

Disk /dev/hda: 21.4 GB, 21474836480 bytes224 heads, 56 sectors/track, 3343 cylindersUnits = cylinders of 12544 * 512 = 6422528 bytes   Device Boot      Start         End      Blocks   Id  System/dev/hda1   *           1        3343    20963801   83  LinuxPartition 1 does not end on cylinder boundary.----------------------------------------Disk /dev/hda: 21.4 GB, 21474836480 bytes224 heads, 56 sectors/track, 3343 cylindersUnits = cylinders of 12544 * 512 = 6422528 bytes   Device Boot      Start         End      Blocks   Id  System/dev/hda1   *           1        3343    20963801   83  LinuxPartition 1 does not end on cylinder boundary.

如果出现柱面号是以下情况:

//hda1的起始和结束柱面号是1-3343,而hda5的起始和结束柱面号是1-2000,那么说明hda5是属于hda1的一个逻辑分区,那么hda1就是扩展分区了。  Device Boot      Start         End      Blocks   Id  System/dev/hda1   *           1        3343    20963801   83  Linux/dev/hda5   *           1        2000    20963801   83  Linux
  • 删除磁盘分区
fdisk /dev/hda //进入fdisk界面Command (m for help): d //选择“删除分区“Selected partition 1 //选择要删除的分区号w (or) q //保存或离开

PS:若hda5是由hda4衍生出来的逻辑分区,如果把hda4删除,那么hda5也会随之消失。

  • 新增磁盘分区
//选择n,新建分区Command (m for help): n//选择新建主分区or扩展分区Command action   e   extended   p   primary partition (1-4)
p //选择主分区Partition number (1-4): 4 //选择分区号First cylinder (1-834, default 1): //选择起始柱面号,就用默认值,按下回车即可Using default value 1Last cylinder or +size or +sizeM or +sizeK (1-834, default 834): +512M//输入分区大小,+xxxM,+表示让系统自动找一个最接近xxxM大小的柱面

综上所述,一共有这几种情况:

  • 1-4号记录区域还有剩余,并且没有扩展分区
    此时系统会让你挑选extended或primary,并且指定1-4之间的号码作为分区号。
  • 1-4号记录区域还有剩余,并且已有扩展分区
    此时系统会让你挑选primary或logical,若选p则还需指定1-4之间的号码作为主分区的分区号;若选l则不需要指定号码,系统会自动指定逻辑分区的号码。
  • 1-4号记录区域没有剩余,且系统有扩展分区
    系统不会让你挑选分区类型,直接进入逻辑分区中。

关于新建分区的一些注意点:

  • 按下w保存操作之后,系统还需要重启,如果不想重启可以执行命令partprobe,强制让内核重新找一次分区表。
  • root身份进行磁盘分区的时候,系统不能再有其他的用户正在使用系统!
  • SATA硬盘最多能支持到15号分区(即主分区+逻辑分区),IDE硬盘最多可以支持到63号分区。目前一般都是SATA硬盘。
  • fdisk这个命令无法处理2TB以上的磁盘分区。Ext3文件系统最大可支持16TB,但是fdisk这个命令最大只能支持2TB的磁盘。

磁盘的格式化

分区完毕之后就要进行格式化,格式化非常简单,使用mkfs(make file system)即可。

mkfs [-t 文件系统格式] 磁盘设备的文件名-t后的文件系统格式就是让你指定将文件系统格式化成哪种文件系统。如ext2、ext3、vfat等。

PS:通过mkfs tab tab就可以查看本系统支持的所有文件系统了。

磁盘的检验:fsck

当系统运行出现问题导致文件系统发生错乱,此时就需要磁盘的检验。

fsck [-t 文件系统] [-ACay] 设备名称

注意:通常只有root用户,而且在文件系统有问题的时候才能进行这个操作,因为在正常情况下使用这个命令会对系统伤害很大。
此外,fsck在扫描的时候,有问题的数据会被放在lost+found这个文件夹中。所以正常情况下这个文件夹中是不应该有数据的。

1 0
原创粉丝点击