硬盘管理


一:硬盘基础概念

硬盘最基本的组成部分是由坚硬金属材料制成的涂以磁性介质的盘片,不同容量硬盘的盘片数不等。每个盘片有两面,都可记录信息。盘片被分成许多扇形的区 域,每个区域叫一个扇区,每个扇区可存储128×2的N次方(N=0.1.2.3)字节信息

盘片表 面上以盘片中心为圆心,不同半径的同心圆称为磁道。硬盘中,不同盘片相同半径的磁道所组成的圆柱称为柱面。磁道与柱面都是表示不同半径的圆,在许多场合, 磁道和柱面可以互换使用,我们知道,每个磁盘有两个面,每个面都有一个磁头,习惯用磁头号来区分。扇区,磁道(或柱面)和磁头数构成了硬盘结构的基本参 数,帮这些参数可以得到硬盘的容量,基计算公式为:
存储容量=磁头数×磁道(柱面)数×每道扇区数×每扇区字节数
要点:(1)硬盘有数个盘片,每盘片两个面,每个面一个磁头
(2)盘片被划分为多个扇形区域即扇区
(3)同一盘片不同半径的同心圆为磁道
(4)不同盘片相同半径构成的圆柱面即柱面
(5)公式: 存储容量=磁头数×磁道(柱面)数×每道扇区数×每扇区字节数
(6)信息记录可表示为:××磁道(柱面),××磁头,××扇区

磁道:

当磁盘旋转时,磁头若保持在一个位置上,则每个磁头都 会在磁盘表面划出一个圆形轨迹,这些圆形轨迹就叫做磁道。这些磁道用肉眼是根本看不到的,因为它们仅是盘面上以特殊方式磁化了的一些磁化区,磁盘上的信息 便是沿着这样的轨道存放的。相邻磁道之间并不是紧挨着的,这是因为磁化单元相隔太近时磁性会相互产生影响,同时也为磁头的读写带来困难。

扇区:

磁盘上的每个磁道被等分为若干个弧段,这些弧段便是磁盘的扇区,每个扇区可以存放512个字节的信息,磁盘驱动器在向磁盘读取和写入数据时,要以扇区为单位。1.44MB3.5英寸的软盘,每个磁道分为18个扇区。
柱面:硬盘通常由重叠的一组盘片构成,每个盘面都被划分为数目相等的磁道,并从外缘的“0”开始编号,具有相同编号的磁道形成一个圆柱,称之为磁盘的柱面。磁盘的柱面数与一个盘面上的磁道数是相等的。由于每个盘面都有自己的磁头,因此,盘面数等于总的磁头数。所谓硬盘的CHS,即Cylinder(柱面)、Head(磁头)、Sector(扇区),只要知道了硬盘的CHS的数目,即可确定硬盘的容量,硬盘的容量=柱面数*磁头数*扇区数*512B


二:MBR

主引导记录(MBR,Main Boot Record)是位于磁盘最前边的一段引导(Loader)代码。它负责磁盘操作系统(DOS)对磁盘进行读写时分区合法性的判别、分区引导信息的定位,它由磁盘操作系统(DOS)在对硬盘进行初始化时产生的

通常,我们将包含MBR引导代码的扇区称为主引导扇区。因这一扇区中,引导代码占有绝大部分的空间,故而将习惯将该扇区称为MBR扇区(简称MBR)。由于这一扇区承担有不同于磁盘上其他普通存储空间的特殊管理职能,作为管理整个磁盘空间的一个特殊空间,它不属于磁盘上的任何分区,因而分区空间内的格式化命令不能清除主引导记录的任何信息。

优点:MBR支持32bit和64bit系统;

缺点:1) MBR支持的分区数量有限制;2)MBR只支持不超过2TB的硬盘,超过2TB的硬盘只能使用2TB的空间(但是有第三方解决方法),应为MBR的寻址空间只有32bit长;


MBR结构:


主引导扇区由三个部分组成(共占用512个字节):

1.主引导程序即主引导记录(MBR)(占446个字节)

可在FDISK程序中找到,它用于硬盘启动时将系统控制转给用户指定的并在分区表中登记了的某个操作系统。

2.磁盘分区表项(DPT,Disk Partition Table)

由四个分区表项构成(每个16个字节)。

负责说明磁盘上的分区情况,其内容由磁盘介质及用户在使用FDISK定义分区时决定。(具体内容略)

3.结束标志(占2个字节)

其值为AA55,存储时低位在前,高位在后,即看上去是55AA(十六进制)。

分类

(1) 主分区:一块硬盘最多只能创建4个主分区(因为在MBR分区表结构中中最多可以创建4个主分区表信息,也就是4个16字节的空间,如上图)

(2)扩展分区:一个扩展分区会占用一个主分区的位置

(3) 逻辑分区:

1)是基于扩展分区创建出来的,先有扩展分区,然后在扩展分区的基础上再创建逻辑分区;也就是说我们要使用逻辑分区,必须先要创建扩展分区;

2)扩展分区的空间是不能被直接使用的,我们必须在扩展分区的基础上去建立逻辑分区,才能够被使用;

3)Linux最多支持63个IDE分区和15个SCSI分区

三:GPT

GPT(GUID Partition Table):是一个较新的分区机制,解决了MBR的很多缺点

1)支持超过2TB的磁盘:GPT有64bit寻址空间;而MBR对硬盘空间地址寻址最多只支持32bit,2的32次方,硬盘空间是以地址方式来被识别的,所以MBR只支持容量为2TB以内的磁盘

2)向后兼容MBR

3)但是底层硬件必须支持UEFI(Intel提出的取代BIOS的新一代的引导系统)才能使用,也就是底层硬件必须使用UEFI

4)必须使用64位操作系统

5)Mac、LInux系统都能支持GPT分区格式

6)Windows 7 64bit、Windows Server2008 64bit支持GPT


四:磁盘分区

*注:分区并不是硬盘的物理功能,而是软件功能


1:查看磁盘信息

fdisk -l   ##系统中的所有磁盘设备
df  -TH       ##系统正在挂载的磁盘设备
blkid        ##系统可以挂载的磁盘设备id

2:磁盘分区

(1)mbr硬盘分区

[root@localhost mnt]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (1435648-20971519, default 1435648): 
Using default value 1435648
Last sector, +sectors or +size{K,M,G} (1435648-20971519, default 20971519): +200M
Partition 3 of type Linux and of size 200 MiB is set

Command (m for help): wq
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.

(2)新建gpt分区

[root@ftp-server ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): 
Command (m for help): n
Partition number (1-128, default 1): 
First sector (34-20971486, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +2M
Created partition 1


Command (m for help):

3:分区操作
partprobe         ##同步分区表
mkfs.xfs /dev/vdbx    ##格式化
mount /dev/vdb1  /mnt     ##临时挂载
vim /etc/fstab         ##永久挂载
编写格式:
device    mountpoint    ftype   defaults(mountpoint)   0   0
/dev/vdb1    /mnt        xfs    defaults       0     0
mount -a         ##使/etc/fastab中记录的挂载生效

[root@localhost mnt]# partprobe 
[root@localhost mnt]# cat /proc/partitions 
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     512000 vdb1
 253       18     204800 vdb2
 253       19     204800 vdb3
 252        0     507904 dm-0

4:删除分区

先使用umount命令解除挂载
  注:当挂载点被使用时,无法解除挂载,需要用fuser -kvm结束使用的进程,然后进行解除挂载
解除挂载之后,使用fdisk /dev/vdb命令 删除分区

[root@localhost mnt]# fdisk /dev/vdb   ##划分/dev/vdb    
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): d
Partition number (1-3, default 3): 1
Partition 1 is deleted

Command (m for help): wq
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.


5:设置分区方式mbr转换为gpt

在linux我们可以先把大容量的磁盘进行转换,转换为GPT格式。由于GPT格式的磁盘相当于原来MBR磁盘中原来保留4个 partition table的4*16个字节只留第一个16个字节,其它的类似于扩展分区,真正的partition table在512字节之后,所以对GPT磁盘表来讲没有四个主分区的限制。
对GPT磁盘分区表进行操作,我们要使用功能强大的parted命令。

mbr 转为gpt

[root@ftp-server ~]# parted /dev/vdb                                      
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands. 
(parted) mklabel                                              
New disk label type?                                                      
aix    amiga  bsd    dvh    gpt    loop   mac    msdos  pc98   sun    
New disk label type? gpt 
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk
will be lost. Do you want to continue?
Yes/No? Yes                                                               
(parted) quit                                                             
Information: You may need to update /etc/fstab.



6:判断当前硬盘是gpt还是mbr。

[root@ftp-server ~]# fdisk -l /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt


7:gpt转mbr

[root@ftp-server ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.                                                      
mklabel  mkpart   mktable  
(parted) mklabel msdos
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk
will be lost. Do you want to continue?
Yes/No? Yes                                                              
(parted) quit