Linux磁盘分区及其常用命令


一、磁盘分区命名方式

              在Linux中,每一个硬件设备都映射到一个系统的文件,包括硬盘、光驱等IDE或SCSI设备。Linux把各种IDE设备分配了一个由hd前缀组成的文件。而各种SCSI设备,则被分配了一个由sd前缀组成的文件,编号方法为拉丁字母表顺序。例如,第一个IDE设备(如IDE硬盘或IDE光驱),Linux定义为hda;第二个IDE设备定义为hdb;下面依次类推。SCSI设备就是sda、sdb、sdc等。(USB磁盘通常会被识别诶SCSI设备,因此其设备名可能是sda)。

            在Linux中规定,每一个磁盘设备最多能有4个主分区(其中包括扩展分区)。任何一个扩展分区都要占用一个主分区号码。在一个硬盘中,主分区和扩展分区一共最多是4个。编号顺序为阿拉伯数字顺序。

            需要注意的是,主分区按1234编号,扩展分区中的逻辑分区,编号直接从5开始,无论是否有2号或3号主分区。对于第一个IDE硬盘的第一主分区,则编号为hda1,而第二个IDE硬盘的第一个逻辑分区编号应为hdb5。

             常见的Linux磁盘命名的规则维hdXY(或sdXY),其中,X为小写拉丁字母,Y为阿拉伯数字。个别系统可能命名有差异。

二、常用磁盘管理命令。

        1、挂载磁盘分区——mount

                  要使用磁盘分区,就需要挂载该分区。挂载时需要指定需要挂载的设备和挂载目录(该目录也成为挂载d点)

                  常用的命令格式如下

                        mount -t type device dir

                            选项 -t 的参数type为文件系统格式(ext4,vfat,ntfs等; )

                                     device为设备名称(如:"/dev/hda1" "/dev/sdb1")

                                     dir为挂载目录,成功挂载后,就可以通过访问该目录以访问该分区内的文件(如:"/mnt/windows_c" "/mnt/cdrom")只要是未被使用的空目录都可用于挂载分区                   

             -V:显示程序版本;
             -l:显示已加载的文件系统列表;
             -h:显示帮助信息并退出;
             -v:冗长模式,输出指令执行的详细信息;
             -n:加载没有写入文件“/etc/mtab”中的文件系统;
             -r:将文件系统加载为只读模式;
             -a:加载文件“/etc/fstab”中描述的所有文件系统。

 

2、卸载磁盘分区——umount

          要移除磁盘,首先需要卸载该分区。常用命令格式如下

                 umount [device|dir]

              卸载时只需要一个参数,可以是设备名称,也可以是挂载点

               

          -a:卸除/etc/mtab中记录的所有文件系统;
          -h:显示帮助;
          -n:卸除时不要将信息存入/etc/mtab文件中;
          -r:若无法成功卸除,则尝试以只读的方式重新挂入文件系统;
          -t<文件系统类型>:仅卸除选项中所指定的文件系统;
          -v:执行时显示详细的信息;
          -V:显示版本信息。

3、查看磁盘分区信息

          1)、查看磁盘的挂载情况——mount                查看磁盘的挂载情况方法:还接输入不带参数的mount命令;

           2)、查看磁盘的分区情况——fdisk                 查看磁盘的分区情况使用命令“fdisk -l”;

          3)、查看磁盘的使用情况——df                        查看磁盘的使用情况,直接使用df;

三,磁盘分区

       1, linux磁盘分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数目之和不能大于四个。且基本分区可以马上被使用但不能再分区。扩充分区必须再进行分区后才能使用,也就是说它必须还要进行二次分区。在 Linux 中,每一个硬件设备都映射到一个系统的文件,对于硬盘、光驱等 IDE 或 SCSI 设备也不例外。Linux把各种 IDE 设备分配了一个由 hd 前缀组成的文件;而对于各种 SCSI 设备,则分配了一个由 sd 前缀组成的文件。

    2,磁盘分区工具fdisk命令

首先选择要进行操作的磁盘:

[root@localhost ~]# fdisk /dev/sdb

输入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)

输入p列出磁盘目前的分区情况:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           1        8001   8e  Linux LVM
/dev/sdb2               2          26      200812+  83  Linux

输入d然后选择分区,删除现有分区:

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

查看分区情况,确认分区已经删除:

Command (m for help): print

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Command (m for help):

输入n建立新的磁盘分区,首先建立两个主磁盘分区:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p    //建立主分区
Partition number (1-4): 1  //分区号
First cylinder (1-391, default 1):  //分区起始位置
Using default value 1
last cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100  //分区结束位置,单位为扇区

Command (m for help): n  //再建立一个分区
Command action
   e   extended
   p   primary partition (1-4)
p 
Partition number (1-4): 2  //分区号为2
First cylinder (101-391, default 101):
Using default value 101
Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M  //分区结束位置,单位为M

确认分区建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux

再建立一个逻辑分区:

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e  //选择扩展分区
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391

确认扩展分区建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended

在扩展分区上建立两个逻辑分区:

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l //选择逻辑分区
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M    

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391

确认逻辑分区建立成功:

Command (m for help): p

Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         100      803218+  83  Linux
/dev/sdb2             101         125      200812+  83  Linux
/dev/sdb3             126         391     2136645    5  Extended
/dev/sdb5             126         175      401593+  83  Linux
/dev/sdb6             176         391     1734988+  83  Linux

Command (m for help):

从上面的结果我们可以看到,在硬盘sdb我们建立了2个主分区(sdb1,sdb2),1个扩展分区(sdb3),2个逻辑分区(sdb5,sdb6)

注意:主分区和扩展分区的磁盘号位1-4,也就是说最多有4个主分区或者扩展分区,逻辑分区开始的磁盘号为5,因此在这个实验中试没有sdb4的。

最后对分区操作进行保存:

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

建立好分区之后我们还需要对分区进行格式化才能在系统中使用磁盘。

四,磁盘分区的格式化

1,格式化:指将分区格式化成不同的文件系统。 那什么是文件系统呢? 文件系统:指操作系统用于明确存储设备或分区上的文件的方法和数据结构:即在存储设备上组织文件的方法。

2,格式化磁盘的主要命令是mkfs,其常用的命令格式为:mkfs -t  type device [block_size]

      选项 t 的参数type为文件系统格式(如ext4,vfat,ntfs等),参数device为设备名称(如“/dev/hda1”),参数block_size为block大小,为可选项;

      格式化交换分区的命令略有不同,不是mkfs,而是mkswap。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM