菜鸟教你如何磁盘分区与创建文件系统以及挂载(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

相关推荐

  • 马哥教育网络班第19期+第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; 2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之; 3、写一个脚本  …

    Linux干货 2016-08-08
  • 网络管理

    说起网络,大家都不陌生,因为大家天天上网嘛,连路边老大爷都会聊微信了,但是我们所了解的网络只是茫茫网络大海中的一叶扁舟,网络的范围其实有很大,里面涉及的东西也非常多,现在让小编带你一点一点去了解它吧,相信大家看了之后,会有不一样的体会呢! 1、 什么是网络? 网络是由节点和连线构成,表示诸多对象及其相互联系。在数学上,网络是一种图,一般认为专指加权图。网络除…

    2017-09-02
  • 基于kubernetes构建Docker集群管理详解

    一、前言        Kubernetes 是Google开源的容器集群管理系统,基于Docker构建一个容器的调度服务,提供资源调度、均衡容灾、服务注册、动态扩缩容等功能套件,目前最新版本为0.6.2。本文介绍如何基于Centos7.0构建Kubernetes平台,在正式介绍…

    2015-03-10
  • 集中管理利器-puppet快速入门-中

    集中管理利器-puppet快速入门-中 6.    尝试启动master puppet 启动master puppet,并确认8140端口已被监听:  #puppet返回成功,但不一定能成功启动,puppet这个功能希望能改进… 7.    目录结构 /etc/puppet/  …

    Linux干货 2015-05-04
  • 网络管理(二)之IP地址划分子网、多块网卡共用单一IP

    网络管理(二)IP地址   一、认识学习IP地址的组成: 1、它们可唯一标识IP 网络中的每台设备 2、IP地址由两部分组成: 网络ID:标识网络;每个网段分配一个网络ID 主机ID:标识单个主机;由组织分配给各设备 3、点分十进制计法表示IPv4地址: 4、如下图,将系统中的IP地址用二进制表示,再通过转换合成的十进制数,使用ping命令可得出:…

    Linux干货 2016-09-05
  • Linux用户和组管理类命令以及文本处理工具的各种实例

    列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 取出最后登录到当前系统的用户的相关信息。 取出当前系统上被用户当作其默认shell的最多的那个shell。 将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。 取出当前主机的IP地址,提示:对i…

    2018-03-13