首页 > 代码库 > 磁盘管理

磁盘管理

设备文件

I/O Ports: I/O 设备地址

一切皆文件:

open(), read(), write(), close()

设备类型:

块设备:block ,存取单位“块”,磁盘

[root@localhost ~]# ll /dev/
brw-rw----. 1 root disk    253,   0 Apr 29 03:53 dm-0    块设备
brw-rw----. 1 root disk    253,   1 Apr 29 03:53 dm-1    块设备

 

字符设备:char ,存取单位“字符”,键盘

[root@localhost ~]# ll /dev/
total 0
crw-rw----. 1 root video    10, 175 Apr 29 03:53 agpgart    字符设备
crw-------. 1 root root     10, 235 Apr 29 03:53 autofs    字符设备

 

区别: 块设备是物理存在的设备,进行访问时基于随机访问;有缓存

          字符设备逻辑上并不存在,进行访问时基于顺序访问;无缓存

 

设备文件:关联至一个设备驱动程序,进而能够跟与之对应硬件设备进行通信

[root@localhost ~]# ll /dev/ 存放在/dev/目录下
total 0
crw-rw----. 1 root video    10, 175 Apr 29 03:53 agpgart
crw-------. 1 root root     10, 235 Apr 29 03:53 autofs
crw-------. 1 root root     10,  61 Apr 29 03:53 cpu_dma_latency
crw-------. 1 root root     10,  62 Apr 29 03:53 crash
brw-rw----. 1 root disk    253,   0 Apr 29 03:53 dm-0
brw-rw----. 1 root disk    253,   1 Apr 29 03:53 dm-1
brw-rw----. 1 root disk    253,   2 Apr 29 03:53 dm-2

  

设备号码:

主设备号:major number,  标识设备类型

次设备号:minor number,  标识同一类型下的不同设备

[root@localhost ~]# ll /dev/sda*
b(设备类型)rw-rw----. 1 root disk 8, 0(主要编码,次要编码) Apr 29 03:53 /dev/sda
b(设备类型)rw-rw----. 1 root disk 8, 1(主要编码,次要编码) Apr 29 03:53 /dev/sda1
b(设备类型)rw-rw----. 1 root disk 8, 2(主要编码,次要编码) Apr 29 03:53 /dev/sda2

 

硬盘接口类型

并行:

IDE :133MB/s

SCSI :640MB/s

串口:

SATA :6Gbps

SAS :6Gbps

USB :480MB/s

rpm: rotations

per minute (每分钟转速)

接口速度为理论值

内存单位:B  K M G T P E Z Y B N D

1B=81bt 1K=1024B 1M=1024K  1G=1024M......

 

设备文件

磁盘设备的设备文件命名:/dev/DEV_FILE

IDE: /dev/hd

SCSI, SATA, SAS, USB: /dev/sd

不同设备:a-z

/dev/sda, /dev/sdb, ...

同一设备上的不同分区:1,2, ...

/dev/sda1, /dev/sda5

主要分区,扩展分区;/dev/sda1-4

逻辑分区;/dev/sda5-......

[root@station29 ~]# 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 / 逻辑分区

  

硬盘存储术语

    head :磁头 最多256个磁头

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

    cylinder:  柱面 柱面数等磁道数

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

[root@station29 ~]# 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
 

[root@localhost ~]# 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

 

机械硬盘和固态硬盘

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

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

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

      相较于SSD ,HDD 在价格、容量、使用寿命上占有绝对优势

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

 

磁盘分区

为什么分区?

    优化I/O 性能

    实现磁盘空间配额限制 (控制用户在磁盘存储多少数据)

    提高修复速度

    隔离系统和程序

    安装多个OS(操作系统)

    采用不同文件系统

两种分区方式:MBR ,GPT

MBR: Master Boot Record ,1982年, 使用32位表示扇区数,分区不超过2T

如何分区:按柱面

0 磁道0 扇区:512bytes

  446bytes: boot loader(启动加载器,引导操作系统)

  64bytes :分区表

16bytes:  标识一个分区

  2bytes: 55AA

4个主分区;3 主分区+1 扩展(N 个逻辑分区)

[root@localhost ~]# echo - - - >/sys/class/scsi_host/host2/scan (扫描磁盘)

 

虚拟机中添加虚拟磁盘后使用,生产中自动识别

例:破坏分区表前512字节后恢复
[root@station29 ~]# hexdump -C /dev/sda -n 512 (查看前512字节)
00000000  eb 48 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0   |.H..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00   |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75   |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 03 02   |.........|...t..|
00000040  80 00 00 80 72 62 00 00  00 08 fa 90 90 f6 c2 80   |....rb..........|
......
[root@station29 ~]# dd if=/dev/sda of=mbr bs=1 count=512
将/dev/sda前512字节读取到mbr中
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.00173963 s, 294 kB/s
[root@station29 ~]# hexdump -C mbr
00000000  eb 48 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.H..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 03 02  |.........|...t..|
00000040  80 00 00 80 72 62 00 00  00 08 fa 90 90 f6 c2 80  |....rb..........|
......
[root@station29 ~]# dd if=/dev/zero of=/dev/sda bs=1 count=512
破坏/dev/sda前512字节,从零文件输出内容到/dev/sda前512字节
512+0 records in
512+0 records out
512 bytes (512 B) copied, 0.00310584 s, 165 kB/s
[root@station29 ~]# hexdump -C /dev/sda -n 512
查看/dev/sda前512字节
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000200
已经归0
[root@station29 ~]# dd if=mbr of=/dev/sda
将mbr中的内容读取到/dev/sda中
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000188347 s, 2.7 MB/s
[root@station29 ~]# hexdump -C /dev/sda -n 512
00000000  eb 48 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.H..............|
00000010  fb be 00 7c bf 00 06 b9 00 02 f3 a4 ea 21 06 00 |...|.........!..|
00000020  00 be be 07 38 04 75 0b 83 c6 10 81 fe fe 07 75 |....8.u......u|
00000030  f3 eb 16 b4 02 b0 01 bb 00 7c b2 80 8a 74 03 02 |.........|...t..|
00000040  80 00 00 80 72 62 00 00 00 08 fa 90 90 f6 c2 80 |....rb.........|
恢复成功

 

MBR分区结构

 技术分享

 

GPT 分区

GPT:GUID (Globals Unique Identifiers) ) partitiontable  支持128个 个 分区,使用64 位,支持8Z( (512Byte/block  )64Z ( (4096Byte/block) )

使用128 位UUID(Universally Unique Identifier)  表示磁盘和分区GPT分区表自动备份在头和尾两份,并有CRC 校验位

UEFI ( 统一扩展固件接口) 硬件支持GPT ,使操作系统启动

GPT 分区结构

 技术分享

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

[root@station29 ~]# blkid (查看UUID)
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2: UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc4: UUID="5f4df3af-f8a8-42cb-a391-79974b5c1aa2" TYPE="ext4"
[root@station29 ~]# uuidgen (生成新的UUID)
afd2aefe-f519-4d78-b04d-7728863c1939
[root@station29 ~]# uuidgen
25aabc53-4031-4272-a54f-7f4a9b0f088e
[root@station29 ~]# uuidgen
36c2795d-7c32-4be2-b241-9b15113da1bc

 

CentOS中查看分区结构是MBR分区还是GPT 分区

[root@localhost ~]# fdisk -l
Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: dos (dos对应MBR  gpt对应GPT)
Disk identifier: 0x0004630b

 

管理分区

列出块设备

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

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

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

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

创建分区使用:

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 [ 选项]... [备 设备 [令 命令 [ 参数]...]...]

parted /dev/sdb mklabel gpt|msdos

parted /dev/sdb print

parted /dev/sdb mkpart primary 1 200  (默认M) ) (创建分区)

parted /dev/sdb rm 1 (删除分区)

parted -l

 

创建MBR 分区

 

[root@localhost ~]# 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  
 
[root@localhost ~]# 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
 
[root@localhost ~]# 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
 
[root@localhost ~]# 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
 
[root@localhost ~]# 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  
 
[root@localhost ~]# parted /dev/sdb mklabel gpt
Information: You may need to update /etc/fstab.
 
[root@localhost ~]# 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
 
[root@localhost ~]# parted /dev/sdb mkpart primary 1 1000
Information: You may need to update /etc/fstab.
[root@localhost ~]# 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
 
[root@localhost ~]# parted /dev/sdb mkpart primary 1001 3000
Information: You may need to update /etc/fstab.
 
[root@localhost ~]# 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  
 
[root@localhost ~]# 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
 
[root@localhost ~]# parted /dev/sdb rm 1    (删除分区)
Information: You may need to update /etc/fstab.
[root@localhost ~]# 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
 
[root@localhost ~]# parted /dev/sdb rm 2
Information: You may need to update /etc/fstab.
[root@localhost ~]# 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 分区

 

[root@localhost ~]# 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.
[root@localhost ~]# 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 通知内核重新读取 硬盘分区表

新增分区用

partx -a /dev/DEVICE (CentOS6中使用)

kpartx -a /dev/DEVICE -f: force

删除分区用

partx -d --nr M-N /dev/DEVICE (M-N  #-#指定一个范围,单独数字也可)

CentOS 5 与7:  

使用partprobe

partprobe [/dev/DEVICE]

 

文件系统

文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构;即在存储设备上组织文件的方法。操作系统中负责管理和存储文件信息的软件结构称为文件管理系统,简称文件系统。

从系统角度来看,文件系统是对文件存储设备的空间进行组织和分配,负责文件存储并对存入的文件进行保护和检索的系统。具体地说,它负责为用户建立文件,存入、读出、修改、转储文件,控制文件的存取,安全控制,日志,压缩,加密等。

支持的文件系统:/lib/modules/`uname –r`/kernel/fs

[root@localhost ~]# cd /lib/modules/3.10.0-514.el7.x86_64/
[root@localhost 3.10.0-514.el7.x86_64]# cd kernel/fs
[root@localhost fs]# ls
binfmt_misc.ko  ceph    dlm    fat      gfs2   lockd      nfs_common  overlayfs  udf
btrfs           cifs    exofs  fscache  isofs  mbcache.ko  nfsd        pstore     xfs
cachefiles      cramfs  ext4   fuse     jbd2   nfs         nls         squashfs

 

文件系统类型

Linux 文件系统:ext2(Extended file system)无日志功能, ext3,ext4, xfs (SGI ), btrfs (Oracle ), reiserfs, jfs(AIX), swap

光盘:iso9660

Windows :fat32, ntfs

Unix: FFS (fast ), UFS (unix ), JFS2

网络文件系统:NFS, CIFS

集群文件系统:GFS2, OCFS2 (oracle)

分布式文件系统: fastdfs,ceph, moosefs, mogilefs,glusterfs, Lustre

RAW :未经处理或者未经格式化产生的文件系统

 

文件系统分类

根据其是否支持"journal(日志)" 功能:

日志型文件系统: ext3, ext4, xfs, ...

非日志型文件系统: ext2, vfat

文件系统的组成部分:

内核中的模块:ext4, xfs, vfat

用户空间的管理工具:mkfs.ext4, mkfs.xfs,mkfs.vfat

Linux 的虚拟文件系统:VFS

查前支持的文件系统:cat /proc/filesystems

VFS

 技术分享

 

创建文件系统

 mkfs 命令:

(1) # mkfs.FS_TYPE /dev/DEVICE

ext4

xfs

btrfs

vfat

(2) # mkfs -t FS_TYPE /dev/DEVICE

-L ‘LABEL‘: 设定卷标

 

 

例题:
[root@station29 ~]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb      8:16   0    30G  0 disk
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 /
sdc      8:32   0    20G  0 disk
sr0     11:0    1   3.7G  0 rom  
 
[root@station29 ~]# cat <<eof | fdisk /dev/sdc
> n
> p
> 1
>
> +1G
> w
> eof
 
[root@station29 ~]# cat <<eof | fdisk /dev/sdc
n
p
2
 
+2G
w
eof
 
[root@station29 ~]# lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sdb      8:16   0    30G  0 disk
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 /
sdc      8:32   0    20G  0 disk
├─sdc1   8:33   0     1G  0 part
└─sdc2   8:34   0     2G  0 part
sr0     11:0    1   3.7G  0 rom
 
[root@station29 ~]# mkfs.ext4 /dev/sdc1 (在/dev/sdc创建ext4文件系统)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
 
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
[root@station29 ~]# mkfs.vfat /dev/sdc2 (在/dev/sdc创建vfat文件系统)
mkfs.vfat 3.0.9 (31 Jan 2010)
[root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="2c31cbc0-4925-427a-99e2-071e2e0d9b22" TYPE="ext4"
/dev/sdc2: UUID="DF61-B770" TYPE="vfat"

 

mke2fs :ext 系列文件系统专用管理工具

-t {ext2|ext3|ext4}

-b {1024|2048|4096} (指定块大小)

-L ‘LABEL‘

-j: 相当于 -t ext3

mkfs.ext3 = mkfs -t ext3 = mke2fs -j = mke2fs -t ext3

-i #:  为数据空间中每多少个字节创建一个inode ;此大小不应该小于block 的大小

-N # :为数据空间创建个多少个inode

-I  一个inode 记录 占用的磁盘空间 大小,128---4096

-m #:  默认5%, 为管理人员预留空间占总空间的百分比

-O FEATURE[,...] :启用指定特性

-O ^FEATURE:关闭指定特性

 

 

[root@station29 ~]# mke2fs /dev/sdc2
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526128 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
 
Writing inode tables: done                            
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
[root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="2c31cbc0-4925-427a-99e2-071e2e0d9b22" TYPE="ext4"
/dev/sdc2: UUID="56621a76-a419-44f8-8f28-df988b3f196d" TYPE="ext2"
[root@station29 ~]# tune2fs -l /dev/sdc1 (查看ext文件系统特性)
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          868e4945-13c6-41c9-8b4b-62e6180e3838
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features: (文件系统特性)     has_journal ext_attr resize_inode dir_index filetype extent flex_bg
sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              66384
Block count:              265064
Reserved block count:     13253
Free blocks:              252369
Free inodes:              66373
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      64
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         7376
Inode blocks per group:   461
Flex block group size:    16
Filesystem created:       Mon Apr 24 12:34:56 2017
Last mount time:          n/a
Last write time:          Mon Apr 24 12:34:56 2017
Mount count:              0
Maximum mount count:      33
Last checked:             Mon Apr 24 12:34:56 2017
Check interval:           15552000 (6 months)
Next check after:         Sat Oct 21 12:34:56 2017
Lifetime writes:          49 MB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:              256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      071a5b59-49b4-4e2d-9ca5-8524a1382359
Journal backup:           inode blocks
 
[root@station29 ~]# mke2fs -j /dev/sdc2      
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526128 blocks
26306 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
 
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
/dev/sdc2: UUID="56621a76-a419-44f8-8f28-df988b3f196d" TYPE="ext2"
mke2fs -j /dev/sdc2  (将/sdc2的ext2文件系统转为ext3文件系统)
/dev/sdc2: UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"

 

例:指定块大小   (块大小为扇区的整数倍  单位为1024 2048 4096)
[root@station29 ~]# mkfs.ext4 /dev/sdc4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
28224 inodes, 112452 blocks
5622 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2016 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
 
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
[root@station29 ~]# mkfs.ext4  -b 4096 /dev/sdc4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
28128 inodes, 28113 blocks
1405 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=29360128
1 block group
32768 blocks per group, 32768 fragments per group
28128 inodes per group
 
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
 
[root@station29 ~]# mkfs.ext4  -b 4000 /dev/sdc4 (将块大小设置为4000)
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=2048 (log=1)                        “超出块大小”操作失败
 
[root@station29 ~]# mkfs.ext4  -b 5000 /dev/sdc4
Warning: blocksize 5000 not usable on most systems.
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: 5000-byte blocks too big for system (max 4096)
“5000字节块的系统太大(最大4096)”
Proceed anyway? (y,n) y
Warning: 5000-byte blocks too big for system (max 4096), forced to continue
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
块大小最大值为4096,设置完块大小后不能更改,除非格式化后重新指定
 
 
[root@station29 ~]#  tune2fs -l /dev/sdc4
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          5f4df3af-f8a8-42cb-a391-79974b5c1aa2
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              28224 inode数量
Block count:              112452 块数量
 
[root@station29 ~]# mkfs.ext4 -b 4096 -i 10240 /dev/sdc4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
 
root@station29 ~]#  tune2fs -l /dev/sdc4
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          0b95467a-fc7d-49ba-9297-63c455bc8126
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              11264
Block count:              28113

 

 

文件系统标签

指向设备的另一种方法,与设备无关

blkid :块设备属性信息查看

blkid [OPTION]... [DEVICE]

-U UUID:  根据指定的UUID 来查找对应的设备

 

[root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2:UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc4: UUID="0b95467a-fc7d-49ba-9297-63c455bc8126" TYPE="ext4"
[root@station29 ~]# blkid -U 1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6
/dev/sda6
 
[root@station29 ~]# blkid /dev/sda6
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"

 

 

 

-L LABEL :根据指定的LABEL 来查找对应的设备

e2label :管理ext 系列文件系统的LABEL

# e2label DEVICE [LABEL]

findfs  :查找分区

findfs [options] LABEL=<label>

findfs [options] UUID=<uuid>

[root@station29 ~]# findfs UUID="7a8216c5-6781-4494-b2da-fda672f117ad"
/dev/sda5

 

tune2fs

tune2fs:重新设定ext 系列文件系统可调整参数的值

-l :查看指定文件系统超级块信息;super block

-L ‘LABEL‘ :修改卷标   (一般情况下,挂载的文件夹作为卷标,推荐UUID作为卷标)

[root@station29 ~]# mkfs.ext4 /dev/sdc4 -L /mnt/sdc4
mke2fs 1.41.12 (17-May-2010)
Filesystem label=/mnt/sdc4
OS type: Linux
Block size=1024 (log=0)
[root@station29 ~]# blkid
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2:UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
   /dev/sdc4:UUID="acc7aca0-4883-4885-8159-20d533aaa661"TYPE="ext4"LABEL="/mnt/sdc4"

 

 

-m # :修预留给管理员的空间百分比

-j:  将ext2升级为ext3

-O:  文件系统属性启用或禁用, –O ^has_journal

-o:  调整文件系统的默认 挂载选项,–o ^acl

-U UUID:  修改UUID号

 

[root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2:UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc4:UUID="acc7aca0-4883-4885-8159-20d533aaa661"TYPE="ext4"LABEL="/mnt/sdc4"
[root@station29 ~]# uuidgen 生成一个新的UUID
f90b876d-8226-4cb2-9a58-81570578846d
[root@station29 ~]# tune2fs -U f90b876d-8226-4cb2-9a58-81570578846d /dev/sdc4
tune2fs 1.41.12 (17-May-2010)
[root@station29 ~]# blkid
/dev/sda6: UUID="1fd1710f-ff1a-4eba-94f8-c57ecc05d6c6" TYPE="ext4"
/dev/sda1: UUID="34e5ffbe-c2b3-4745-89bc-1811245ae758" TYPE="ext4"
/dev/sda2: UUID="abf30893-850f-4a50-9a0c-e5e153873eb4" TYPE="ext4"
/dev/sda3: UUID="53e082c2-f1fc-4b96-aa31-d9071dc6355c" TYPE="ext4"
/dev/sda5: UUID="7a8216c5-6781-4494-b2da-fda672f117ad" TYPE="swap"
/dev/sdc1: UUID="868e4945-13c6-41c9-8b4b-62e6180e3838" TYPE="ext4"
/dev/sdc2:UUID="79690db4-ae07-4c5d-8a8c-667c60b58e49" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc4:UUID="f90b876d-8226-4cb2-9a58-81570578846d"TYPE="ext4"LABEL="/mnt/sdc4"

 

dumpe2fs:

-h :查看超级块信息,不显示分组信息

 

文件系统检测和修复

常发生于死机或者非正常关机之后

挂载为文件系统标记为“no clean”

注意:一定不要在挂载状态下修复

fsck: File System Check

fsck.FS_TYPE

fsck -t FS_TYPE

-p:  自动修复错误

-r:  交互式修复错误

FS_TYPE 一定要与分区上已经文件类型相同

e2fsck :ext 系列文件专用的检测修复工具

-y :自动回答为yes

-f :强制修复

 

磁盘管理