菜鸟教你如何磁盘分区与创建文件系统以及挂载(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)
上一篇 2016-08-27 11:03
下一篇 2016-08-27 16:19

相关推荐

  • 第三周作业

    列出当前系统上所有已经登录的用户名,同一个用户登录多次,则只显示一次 [root@app02 ~]# who |grep -o -E '^\<[a-zA-Z]+\>'|sort -u root XIAOXIN 2. 取出最后登录到当前系统的用户相关信息 [r…

    Linux干货 2016-08-29
  • 逻辑卷管理

    逻辑卷管理 一 创建逻辑卷 1 准备分区或硬盘 这里使用/dev/sdb、/dev/sdc两块硬盘和/dev/sda9、/dev/sda10两个分区,大小都为1G,磁盘有限,我也不想这么抠的。 添加分区/dev/sda9、 /dev/sda10 [root@centos7 ~]# fdisk /dev/sda Welcome to fdisk (u…

    Linux干货 2017-05-02
  • 博客作业1

    一、linux 常用文件管理命令有哪些?其常用使用方法及相关实例演示 1、文件操作命令 (1).pwd       功能:显示当前目录       实例:     #pwd (2).cd           功能:改变当前目录 …

    Linux干货 2016-06-23
  • MBR

    关于磁盘的使用,必须要弄明白MBR 分区划分方式     centos 6:扇区划分     centos 7:柱面划分 MBR:Master Boot Record:512bytes 0磁道0扇区 512bytes=446bytes(boot loader) + 4*16byt…

    Linux干货 2016-09-05
  • LVS

        Linux虚拟服务器(LVS)是基于Linux内核的操作系统的负载平衡软件。     LVS提供了良好的可扩展性,可靠性和可服务性。 LVS项目的主要工作是开发先进的IP 负载平衡软件(IPVS),应用级负载平衡软件(KTCPVS)和集群管理组件。 IPVS:在…

    Linux干货 2017-08-21