菜鸟教你如何磁盘分区与创建文件系统以及挂载(1)

何为磁盘分区,为什么要分区? 

  计算机中存放信息的主要的存储设备就是硬盘,但是硬盘不能直接使用,必须对硬盘进行分割,分割成的一块一块的硬盘区域就是磁盘分区。在传统的磁盘管理中,将一个硬盘分为两大类分区:主分区和扩展分区。主分区是能够安装操作系统,能够进行计算机启动的分区,这样的分区可以直接格式化,然后安装系统,直接存放文件。

MBR分区结构

  在一个MBR分区表类型的硬盘中最多只能存在4个主分区。如果一个硬盘上需要超过4个以上的磁盘分块的话,那么就需要使用扩展分区了。如果使用扩展分区,那么一个物理硬盘上最多只能3个主分区和1个扩展分区。扩展分区不能直接使用,它必须经过第二次分割成为一个一个的逻辑分区,然后才可以使用。一个扩展分区中的逻辑分区可以任意多个。

 

1、Linux磁盘分区查看

[root@centos6 ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda: 214.7 GB  #该sda硬盘磁盘容量为 214.7GB     214748364800 bytes:总共 214748364800 字节
255 heads:255个磁头     63 sectors/track:一个盘面总共63个扇区     26108 cylinders:26108个柱面
Units = cylinders of 16065 * 512 = 8225280 bytes:每个柱面(cylinders )总共有 16065个扇区,每个扇区总共 512字节,所以每个柱面总共为 16065*512 =8225280 bytes(字节)
Sector size (logical/physical): 512 bytes / 512 bytes:每个扇区的逻辑和物理容量大小都为 512 bytes(字节)
I/O size (minimum/optimal): 512 bytes / 512 bytes:I/O的容量最小和最佳值都为 512 bytes(字节)
Disk identifier: 0x00007b7d:磁盘标识符为 0x00007b7d
Device:设备名称     Boot:是否可引导     Start:起始柱面     End:结束柱面     Blocks:包含的磁盘块数     Id:分区标识     System:应用在那个系统在上常用的分区标识

/dev/sda1:分区名称   *:可引导设备   1:从第1个柱面开始      26:在第26个柱面结束   204800:磁盘块数为204800       83:分区ID标识为83       Linux:应用在linux系统之上
/dev/sda2:分区名称            26:从第26个柱面开始     12774:在第12774个柱面结束   102400000:磁盘块数为102400000   83:分区ID标识为83  Linux:应用在linux系统之上
/dev/sda3:分区名称          12774:从第12774柱面开始     14049:在第14049个柱面结束   10240000:磁盘块数为10240000   83:分区ID标识为83  Linux:应用在linux系统之上
/dev/sda4:分区名称          14049: 从第14049个柱面开始   26109:在第26109个柱面结束   96869376:磁盘块数为96869376    5:分区ID标识为5  Extended:应用在linux系统扩展分区之上
/dev/sda5:分区名称          14049:从第14049个柱面开始   14310:在第14310个柱面结束   2097152:磁盘块数为2097152      82:分区ID标识为82  Linux swap / Solaris:应用在linux系统swap之上

2、Linux磁盘分区

fdisk 命令

简介:
fdisk - Partition table manipulator for Linux
格式:
fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device
fdisk -l [-u] [device...]
fdisk -s partition...
fdisk -v
fdisk -h
Command action
   a   toggle a bootable flag     #设置一个标识,说明这个分区是可启动的    
   b   edit bsd disklabel     #编辑BSD Linux系统用的磁盘标签
   c   toggle the dos compatibility flag     #设置DOS兼容标识
   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     #为Sun Linux系统创建一个新磁盘标签
   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)     #高级功能
实例1:
[root@centos6 ~]# fdisk /dev/sda     #对sda这块硬盘进行分区管理    

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p     #查看当前分区情况

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris

Command (m for help): n     #创建一个新分区
First cylinder (14310-26109, default 14310):     #默认以柱面进行分区,默认柱面开始值为14310,直接回车就是选择默认值
Using default value 14310
Last cylinder, +cylinders or +size{K,M,G} (14310-26109, default 26109): 14500     #填写结束柱面值14500

Command (m for help): p     #查看以分好的区

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       14500     1526194+  83  Linux

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: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos6 ~]# fdisk -l     #查看分区情况(真实情况)

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       14500     1526194+  83  Linux
[root@centos6 ~]# cat /proc/partitions     #查看是否已写入内核中,显示并唯有逻辑分区/dev/sda6
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    2097152 sda5
[root@centos6 ~]# partx -a /dev/sda     #可用此方法让内核扫描分区情况
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@centos6 ~]# partx -a /dev/sda    #如果第一次扫描未有显示,可再次执行该命令
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
[root@centos6 ~]# cat /proc/partitions     #查看内核是否已识别 
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    2097152 sda5
   8        6    1526194 sda6
[root@centos6 ~]# ls /dev/sda*     #也可以用此类方法查看
/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sda6
[root@centos6 ~]# lsblk     #也可以用这种方法查看
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0  9.8G  0 part /testdir
├─sda4   8:4    0    1K  0 part 
├─sda5   8:5    0    2G  0 part [SWAP]
└─sda6   8:6    0  1.5G  0 part 
[root@centos6 ~]#
实例2:
[root@centos6 ~]# partx -d -n 6 /dev/sda     #不用fdisk命令,直接用partx命令删除分区
[root@centos6 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  209715200 sda
   8        1     204800 sda1
   8        2  102400000 sda2
   8        3   10240000 sda3
   8        4          1 sda4
   8        5    2097152 sda5
实例3:
[root@centos6 ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n     #创建一个新分区
First cylinder (14310-26109, default 14310): 
Using default value 14310     #默认起始柱面
Last cylinder, +cylinders or +size{K,M,G} (14310-26109, default 26109): 15000     #设置结束柱面为15000

Command (m for help): t     #设置分区类型
Partition number (1-6): 6
Hex code (type L to list codes): 82     #设置分区类型为82,也就是swap分区
Changed system type of partition 6 to 82 (Linux swap / Solaris)

Command (m for help): p     #查看分区情况

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       15000     5542444+  82  Linux swap / Solaris

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: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@centos6 ~]# partx -a /dev/sda     #让内核扫描分区
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
[root@centos6 ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
[root@centos6 ~]# lsblk     #查看内核已经识别分好的分区
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0     11:0    1 1024M  0 rom  
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0  200M  0 part /boot
├─sda2   8:2    0 97.7G  0 part /
├─sda3   8:3    0  9.8G  0 part /testdir
├─sda4   8:4    0    1K  0 part 
├─sda5   8:5    0    2G  0 part [SWAP]
└─sda6   8:6    0  5.3G  0 part 
[root@centos6 ~]# fdisk -l /dev/sda      #再次查看分区情况

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00007b7d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       12774   102400000   83  Linux
/dev/sda3           12774       14049    10240000   83  Linux
/dev/sda4           14049       26109    96869376    5  Extended
/dev/sda5           14049       14310     2097152   82  Linux swap / Solaris
/dev/sda6           14310       15000     5542444+  82  Linux swap / Solaris
[root@centos6 ~]#

原创文章,作者:Aleen,如若转载,请注明出处:http://www.178linux.com/40689

(0)
AleenAleen
上一篇 2016-08-27 11:03
下一篇 2016-08-27 16:19

相关推荐

  • 马哥教育网络班N22期+第1周课程练习beta2

    马哥教育网络班N22期+第1周课程练习  1.计算机的组成及其功能 1.1 计算机的组成 根据冯·诺依曼体系结构,将计算机分为五个主要的部件: CPU:运算器 CPU:控制器 RAM:存储器 Input:输入设备 Output: 输出设备 其功能分别为: 运算器(Datapath):运算器的功能是对数据进行各种算术运算和逻辑运算,即对数据进行加工处…

    Linux干货 2016-08-15
  • linux下手工创建新用户

    当我们要创建一个用户时,通常使用useradd命令就可以自动创建了一个用户;实际上,在linux下,一切皆文件,设置好几个文件,也是可以创建出一个用户: 文件如下: /etc/passwd  :用户 /etc/shadow  :密码 /home/username:用户家目录 /etc/group:用户组 /etc/skel/*: &nbs…

    Linux干货 2015-03-30
  • 3、常用命令介绍

    http://note.youdao.com/yws/public/redirect/share?id=4840e26ee1d2b2c928b6c26e2ba14661&type=false

    Linux干货 2016-08-08
  • 0804正则表达式作业

    用正则表达式表示IP地址         首先来分析一下,制IP地址是一个32位的二进制数,通常被分割为4个“8位二进制数”(也就是4个字节)。IP地址通常用“点分十进制”表示成(a.b.c.d)的形式,其中,a,b,c,d都是0~255之间的十进制整数。例:点分十进IP地址(1…

    Linux干货 2016-08-10
  • 计算机构成与Linux基础知识之一

    简述计算机构成,Linux如何诞生及主流发行版;简单命令介绍;如何获取帮助及Linux目录命名及功用简介绍。

    2018-02-26
  • Linux系统vim文本编辑器

                                                      &nbsp…

    Linux干货 2016-08-15