Linux入门——磁盘管理

Posted angge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux入门——磁盘管理相关的知识,希望对你有一定的参考价值。

无论什么操作系统,归根还是要落实与磁盘上的,对于磁盘的管理也是linux管理必备的一项技能。在linux中“一切皆文件”的思想贯彻整个linux的学习中,包括像是磁盘等的硬件也是在linux的/dev/目录下类似于文件形式的存放。

机械硬盘和固态硬盘

机械硬盘(HDD ):Hard Disk Drive ,即是传统普通硬盘,主要由:盘片,磁头,盘片转轴及控制电机,磁头控制器,数据转换器,接口,缓存等几个部分组成。机械硬盘中所有的盘片都装在一个旋转轴上,每张盘片之间是平行的,在每个盘片的存储面上有一个磁头,磁头与盘片之间的距离比头发丝的直径还小,所有的磁头联在一个磁头控制器上,由磁头控制器负责各个磁头的运动。磁头可沿盘片的半径方向运动,加上盘片每分钟几千转的高速旋转,磁头就可以定位在盘片的指定位置上进行数据的读写操作。数据通过磁头由电磁流来改变极性方式被电磁流写到磁盘上,也可以通过相反方式读取。硬盘为精密设备,进入硬盘的空气必须过滤

固态硬盘(SSD ):Solid State Drive ,用固态电子存储芯片阵列而制成的硬盘,由控制单元和存储单元(FLASH 芯片、DRAM 芯片)组成。固态硬盘在接口的规范和定义、功能及使用方法上与普通硬盘的完全相同,在产品外形和尺寸上也与普通硬盘一致

      相较于HDD ,SSD 在防震抗摔、传输速率、功耗、重量、噪音上有明显优势,SSD 传输速率性能是HDD 的2倍

      相较于SSD ,HDD 在价格、容量、使用寿命上(目前已看不出区别)占有绝对优势

      硬盘有价,数据无价,目前SSD 不能完全取代HH

硬盘存储术语

    head :磁头 最多256个磁头

    track :磁道 1024个磁道,0-1023,;最多存放63个扇区

    cylinder:  柱面 柱面数等磁道数

    secotr:  扇区,一个扇区512bytes(字节)

[[email protected](liang) ~]# 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: 0x000ee556
 

[[email protected](liang) ~]fdisk -l -u=cylinder    #以柱面为单位 (CentOS7中)
 Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *       1         131     1048576   83  Linux
/dev/sda2         131       10444    82836480   8e  Linux LVM

 

磁盘分区

磁盘分区的优点

    优化I/O 性能;实现磁盘空间配额限制 (控制用户在磁盘存储多少数据);提高修复速度

隔离系统和程序;安装多个OS(操作系统);采用不同文件系统

管理分区

列出块设备

  lsblk (查看内存中的分区表)

  ls /dev/sd* (查看内存中有几个分区)

  cat /proc/partitions (查看内存中有几个分区)

  fdisk -l (查看磁盘上的分区表)

[[email protected](liang)~]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0   200G  0 disk
├─sda1   8:1    0   200M  0 part /boot #主要分区
├─sda2   8:2    0    20G  0 part /data #主要分区
├─sda3   8:3    0    10G  0 part /home #主要分区
├─sda4   8:4    0     1K  0 part        #扩展分区不能直接使用,用来包含逻辑分区
├─sda5   8:5    0     2G  0 part [SWAP] #逻辑分区
└─sda6   8:6    0 167.8G  0 part / #逻辑分区

创建分区:

  fdisk  创建MBR 分区

  gdisk  创建GPT 分区

    fdisk /dev/sdb

    gfisk /dev/sdb  类fdisk  的GPT 分区工具

fdisk -l [-u] [device...]

  p 分区列表

  t 更改分区类型

  n 创建新分区

  d 删除分区

  w 保存并退出

  q 不保存并退出

parted  高级分区操作(parted 的操作都是实时生效的,小心使用)

  parted –l 列

  parted /dev/sda print 列出指定硬盘的分区信息

  parted /dev/sdb mklabel gpt|msdos 给指定硬盘重建指定类型的分区表gpt|mbr

                 parted /dev/sdb mklabel gpt -s 非交互

两种分区方式:MBR GPT

MBR:1982年,使用32位表示扇区数,分区不超过2T,按柱面分区,3三个主分区,一个扩展分区(N个逻辑分区)

 技术分享图片

硬盘主引导记录MBR由4个部分组成

主引导程序(偏移地址0000H--0088H),它负责从活动分区中装载,并运行系统引导程序。

出错信息数据区,偏移地址0089H--00E1H为出错信息,00E2H--01BDH全为0字节。

分区表(DPT,DiskPartition Table)含4个分区项,偏移地址01BEH--01FDH,每个分区表项长16个字节,共64字节为分区项1、分区项2、分区项3、分区项4

结束标志字,偏移地址01FE--01FF的2个字节值为结束标志55AA

echo ‘- - -‘ >/sys/class/scsi_host/host2/scan (扫描磁盘,vim支持在线添加硬盘,通过此命令可扫描磁盘添加到虚拟机中)

例:创建MBR分区

[[email protected] ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   80G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   79G  0 part
  ├─cl-root 253:0    0   50G  0 lvm  /
  ├─cl-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cl-home 253:2    0   27G  0 lvm  /home
sdb           8:16   0   30G  0 disk
├─sdb1        8:17   0    1G  0 part
└─sdb2        8:18   0    2G  0 part
sdc           8:32   0   20G  0 disk
sr0          11:0    1  7.7G  0 rom  
 
[[email protected] ~]# dd if=/dev/zero of=/dev/sdb bs=1 count=66 skip=446 seek=446
66+0 records in
66+0 records out
66 bytes (66 B) copied, 0.0407596 s, 1.6 kB/s
 
[[email protected] ~]# hexdump -C /dev/sdb -n 512
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  af e8 a9 f3 00 00 00 00  |................|
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200
 
[[email protected] ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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
 
[[email protected] ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   80G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   79G  0 part
  ├─cl-root 253:0    0   50G  0 lvm  /
  ├─cl-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cl-home 253:2    0   27G  0 lvm  /home
sdb           8:16   0   30G  0 disk
sdc           8:32   0   20G  0 disk
sr0          11:0    1  7.7G  0 rom  
 
[[email protected] ~]# parted /dev/sdb mklabel gpt
Information: You may need to update /etc/fstab.
 
[[email protected] ~]# fdisk -l /dev/sdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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
#         Start          End    Size  Type            Name
 
[[email protected] ~]# parted /dev/sdb mkpart primary 1 1000
Information: You may need to update /etc/fstab.
[[email protected] ~]# fdisk -l /dev/sdb                                     
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdb: 32.2 GB, 32212254720 bytes, 62914560 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
#         Start          End    Size  Type            Name
1         2048      1953791    953M  Microsoft basic primary
 
[[email protected] ~]# parted /dev/sdb mkpart primary 1001 3000
Information: You may need to update /etc/fstab.
 
[[email protected] ~]# lsblk                                                 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   80G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   79G  0 part
  ├─cl-root 253:0    0   50G  0 lvm  /
  ├─cl-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cl-home 253:2    0   27G  0 lvm  /home
sdb           8:16   0   30G  0 disk
├─sdb1        8:17   0  953M  0 part
└─sdb2        8:18   0  1.9G  0 part
sdc           8:32   0   20G  0 disk
sr0          11:0    1  7.7G  0 rom  
 
[[email protected] ~]# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name     Flags
 1      1049kB  1000MB  999MB   ext4         primary
 2      1001MB  3000MB  1999MB               primary
 
[[email protected] ~]# parted /dev/sdb rm 1    (删除分区)
Information: You may need to update /etc/fstab.
[[email protected] ~]# parted /dev/sdb print                                 
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number  Start   End     Size    File system  Name     Flags
 2      1001MB  3000MB  1999MB               primary
 
[[email protected] ~]# parted /dev/sdb rm 2
Information: You may need to update /etc/fstab.
[[email protected] ~]# parted /dev/sdb print                                 
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number  Start  End  Size  File system  Name  Flags
删除分区后,分区表还在

 

GPT:  支持128个分区,使用64位,支持8Z(512Byte/block )64Z (4096Byte/block);使用128位UUID表示磁盘和分区GPT分区表自动备份在头和尾两份,并有CRC校验位;UEFI (统一扩展固件接口)硬件支持GPT,使操作系统启动

 技术分享图片

EFI部分又可以分为4个区域:EFI信息区(GPT头)、分区表、GPT分区、备份区域

例:创建GPT分区

[[email protected] ~]# gdisk /dev/sdb
GPT fdisk (gdisk) version 0.8.6
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present
Found valid GPT with protective MBR; using GPT.
Command (? for help): ?
b    back up GPT data to a file
c    change a partitions name
d    delete a partition
i    show detailed information on a partition
l    list known partition types
n    add a new partition
o    create a new empty GUID partition table (GPT)
p    print the partition table
q    quit without saving changes
r    recovery and transformation options (experts only)
s    sort partitions
t    change a partitions type code
v    verify disk
w    write table to disk and exit
x    extra functionality (experts only)
?    print this menu
Command (? for help): p
Disk /dev/sdb: 62914560 sectors, 30.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0C46C34F-DB4A-4E3A-8D8B-EE2D49F2027A
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 62914526
Partitions will be aligned on 2048-sector boundaries
Total free space is 62914493 sectors (30.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
Command (? for help): n
Partition number (1-128, default 1): 100
First sector (34-62914526, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-62914526, default = 62914526) or {+-}size{KMGTP}: +1G          
Current type is Linux filesystem
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to Linux filesystem
Command (? for help): p
Disk /dev/sdb: 62914560 sectors, 30.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 0C46C34F-DB4A-4E3A-8D8B-EE2D49F2027A
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 62914526
Partitions will be aligned on 2048-sector boundaries
Total free space is 60817341 sectors (29.0 GiB)
Number  Start (sector)    End (sector)  Size       Code  Name
 100            2048         2099199   1024.0 MiB  8300  Linux filesystem
Command (? for help): w
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdb.
The operation has completed successfully.
[[email protected] ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   80G  0 disk
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   79G  0 part
  ├─cl-root 253:0    0   50G  0 lvm  /
  ├─cl-swap 253:1    0    2G  0 lvm  [SWAP]
  └─cl-home 253:2    0   27G  0 lvm  /home
sdb           8:16   0   30G  0 disk
└─sdb100    259:0    0    1G  0 part
sdc           8:32   0   20G  0 disk
sr0          11:0    1  7.7G  0 rom 

GPT分区和MBR分区不能转换

扩展分区如果被删除,所有逻辑分区都被删除

partprobe-重新设置内存中的内核分区表版本

同步分区表

查看内核是否已经识别新的分区:

cat /proc/partations

centos6通知内核重新读取硬盘分区表(6的内核bug)

新增分区用:partx-a /dev/DEVICE;partx-a /dev/DEVICE -f: force

删除分区用:artx-d --nrM-N /dev/DEVICE

CentOS 5,7: partprobe[/dev/DEVICE]

 

以上是关于Linux入门——磁盘管理的主要内容,如果未能解决你的问题,请参考以下文章

Linux入门之磁盘管理分区管理

linux入门--磁盘管理之分区格式化与挂载

Linux入门之磁盘管理RAID的实现

Linux入门之磁盘管理文件系统挂载

Linux入门之磁盘管理LVM逻辑卷的使用

Linux入门之磁盘管理文件系统