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

相关推荐

  • LINUX-echo命令

    echo命令:回显         语法:echo [-neE][字符串]     说明:echo会将输入的字符串送往标准输出。输出的字符串间         以空白字符隔开,并在最后加上换行号。   &nbs…

    Linux干货 2017-05-22
  • openvpn安装配置过程

    前言     之前为了方便远程办公时访问公司的内部系统,如:svn、OA、wiki、禅道等等;通通在防火墙上做了端口映射。然后有个内部系统被黑了,各种弱口令没办法。果断关闭端口映射,看来还是得搭建个VPN服务器,vpn设备感觉大材小用。马上就想到了开源的openvpn,下面就来介绍openvpn的安装配置过程。 ope…

    Linux干货 2015-05-11
  • 建立dns服务

    一 、环境 两台机器,一台LINUX,机器名为LINUX,域名为tongqiang.cnIP地址为192.168.0.5,另一台机器为WIN98,机器名为tongqiang,IP地址从LINUX上DHCP服务器上获得。 二 、建立DNS服务器(假设你的DNS软件包已经安装好了,而且是在安装LINUX是选择安装DNS服务器的) 建立DNS服务器只需要修改以下几…

    2017-10-09
  • date命令总结

    在linux中man手册页,对date -d 参数的的说明比较模糊,一下的举例,进一步说明: #-d –date=STRING display time described by STRING not 'now' 命令: date -d nex-day +%Y%m%d    //明天日期 d…

    Linux干货 2016-08-05
  • 推荐CentOS Linux下的分区及格式化工具

    简介 伴随着科技的飞速发展,越来越多的企业对于服务器的稳定要求越来越高,越来越多的企业开始采用linux系统来部署自己的服务,以求高效的稳定性,当然任何操作系统都需要一个最基本的基础,那就是硬盘,及硬盘分区,今天来给大家推荐几款CentOS Linux下的分区工具及如何查看分区环境,也会给大家来带一些硬盘的基本知识 一、硬盘的接口类型 分区肯定是对硬盘进行分…

    2017-03-19
  • 马哥linux0801课程内容

    0801课堂笔记 硬链接和软链接最根本的区别 硬链接是同一个文件,而软链接是两个文件 软链接查询到文件的软链接个数.比较麻烦. 而硬链接的个数可以通过链接数就可以看到. 硬链接不能跨分区,而软链接可以跨分区 (因为每个分区都有自己的inode表) ln 创建硬链接 ln–s 创建软链接(软链接如果使用相对路径,对象是相对软链接的路径,而硬…

    Linux干货 2016-08-04