1. 硬件基础:磁盘
磁盘的性能
数据的读取或写入速度取决于磁头移动到磁盘相应位置的速度。
寻道时间即磁头从开始移动到数据所在磁道所需要的时间,寻道时间越短,I/O操作越快。
机械硬盘的连续读写性能很好,但随机读写性能很差,这是因为磁头移动到正确的磁道上需要时间。随机读写时,磁头需要不停地移动,时间都浪费在了磁头寻址上,所以性能不高。
衡量磁盘性能的指标有两个:
- 磁盘每秒能够完成的随机读写次数IOPS(Input/Output per Second)
- 吞吐量,即单位时间内可以成功传输的数据量
磁盘分类
根据磁盘制作工艺,可分为机械硬盘和固态硬盘
- 机械硬盘:是机械设备
- 固态硬盘:是电子或说是电气设备(性能更好)
磁盘分区
磁盘分区信息存储在磁盘第一个扇区内(即第0磁道、第0扇区),共512字节,其中前446字节存放操作系统引导程序(即MBR分区);中64字节称为硬盘分区表DPT,每个分区表项占16字节(所以最多只支持四个主分区);最后的两字节存放扇区有效性表示(即MBR签名)。
MBR还支持扩展分区,即拿出一个分区表的空间,不再用于存放分区内容,而是指向磁盘的一段空间(这个空间理论上可以跟磁盘一样大),来存放新的分区表,那么这个分区表则不再受16字节的限制,可以创建任意多个需要的分区。
即Linux系统最多只支持4个主分区,但可以支持任意多个扩展分区,扩展分区编号只能从5开始。
分区的好处:
- 数据安全:若所有的数据只存储在一个分区上,那么一旦这个分区损坏,则所有数据全部损坏。但若提前分区,就可只影响当本分区内的数据
- 读写效率:数据的读写需要调用磁头去寻找磁道。那么将所有数据存放在一个分区上可使磁头跨越的磁道非常之多,增加了寻道时间
2. 创建分区并安装文件系统步骤
文件系统:指操作系统用于明确存储设备或分区上的文件的方法和数据结构,即在存储设备上组织文件的方法。
给存储空间去装文件系统整体步骤:
- 添加设备,即加上一个块设备,可能是一个硬盘或U盘等块设备
- 对设备进行分区(不分区也可,但正常情况下,像现在的存储都会比较大,一般都会进行分区),即将一个设备分成若干个小块设备,既可以提高数据安全,还可以提高数据存取效率
- 然后在分区上装文件系统(
mkfs -t
可指定文件系统类型) - 装完文件系统后要把它挂在到目录结构上,这样才能从目录中访问这个设备,完成对该设备的读或写(建议挂载至靠近根的一级目录下,可提高文件的访问效率)
1. 添加硬盘
笔者使用VMware虚拟机,在编辑虚拟机设置中的硬盘选项进行设置
2. 对硬盘进行分区
首先查看所有磁盘信息
[root@rebekk ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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
Disk identifier: 0x000b6ea2
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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 /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
其中,/dev/sdb即为新添加的硬盘。此处对该新添加的硬盘进行分区操作。
[root@rebekk ~]# fdisk /dev/sdb
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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x60d5fc65.
# 添加第一个分区
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +4G
Partition 1 of type Linux and of size 4 GiB is set
# 添加第二个分区
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (8390656-41943039, default 8390656):
Using default value 8390656
Last sector, +sectors or +size{K,M,G} (8390656-41943039, default 41943039): +2G
Partition 2 of type Linux and of size 2 GiB is set
# 将第二个分区转换成swap类型
Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): L
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
# 查看当前分区情况
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk identifier: 0x60d5fc65
Device Boot Start End Blocks Id System
/dev/sdb1 2048 8390655 4194304 83 Linux
/dev/sdb2 8390656 12584959 2097152 82 Linux swap / Solaris
# 添加第三个分区
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): 3
First sector (12584960-41943039, default 12584960):
Using default value 12584960
Last sector, +sectors or +size{K,M,G} (12584960-41943039, default 41943039): +1G
Partition 3 of type Linux and of size 1 GiB is set
# 添加扩展分区
Command (m for help): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e):
Using default response e
Selected partition 4
First sector (14682112-41943039, default 14682112):
Using default value 14682112
Last sector, +sectors or +size{K,M,G} (14682112-41943039, default 41943039):
Using default value 41943039
Partition 4 of type Extended and of size 13 GiB is set
# 添加逻辑分区
Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (14684160-41943039, default 14684160):
Using default value 14684160
Last sector, +sectors or +size{K,M,G} (14684160-41943039, default 41943039): +512M
Partition 5 of type Linux and of size 512 MiB is set
# 查看当前分区情况
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk identifier: 0x60d5fc65
Device Boot Start End Blocks Id System
/dev/sdb1 2048 8390655 4194304 83 Linux
/dev/sdb2 8390656 12584959 2097152 82 Linux swap / Solaris
/dev/sdb3 12584960 14682111 1048576 83 Linux
/dev/sdb4 14682112 41943039 13630464 5 Extended
/dev/sdb5 14684160 15732735 524288 83 Linux
# 这里第四个扩展分区,就是用来指明这个磁盘剩下还有多大的空间可以用来创建为逻辑分区的
# 保存
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# 查看磁盘是否同步成功
[root@rebekk ~]# cat /proc/partitions
major minor #blocks name
11 0 940032 sr0
8 0 20971520 sda
8 1 1048576 sda1
8 2 19921920 sda2
8 16 20971520 sdb
8 17 4194304 sdb1
8 18 2097152 sdb2
8 19 1048576 sdb3
8 20 1 sdb4
8 21 524288 sdb5
253 0 17821696 dm-0
253 1 2097152 dm-1
# 可知有sdb1~sdb5,同步成功
3. 安装文件系统
当内核可以识别到分区信息后,就可以在分区上安装文件系统,之后挂载至某个目录中作为存储使用了。将分区格式化成不同的文件系统成为格式化。
# 查看/dev/sdb的分区表信息
[root@rebekk ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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
Disk identifier: 0x60d5fc65
Device Boot Start End Blocks Id System
/dev/sdb1 2048 8390655 4194304 83 Linux
/dev/sdb2 8390656 12584959 2097152 82 Linux swap / Solaris
/dev/sdb3 12584960 14682111 1048576 83 Linux
/dev/sdb4 14682112 41943039 13630464 5 Extended
/dev/sdb5 14684160 15732735 524288 83 Linux
# 为sdb1创建一个ext4类型的文件系统
[root@rebekk ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
262144 inodes, 1048576 blocks
52428 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1073741824
32 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
# 查看/dev/sdb1的属性
[root@rebekk ~]# blkid /dev/sdb1
/dev/sdb1: UUID="2e786801-3800-4860-99e7-225811e42c96" TYPE="ext4"
# 为/dev/sdb2创建一个xfs类型的文件系统,并指定卷标为NEWDATA
[root@rebekk ~]# mkfs.xfs -L NEWDATA /dev/sdb2
meta-data=/dev/sdb2 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# 查看/dev/sdb2的属性
[root@rebekk ~]# blkid /dev/sdb2
/dev/sdb2: LABEL="NEWDATA" UUID="adc86f37-bc14-4806-a44f-95ec0d465a1c" TYPE="xfs"
# 修改/dev/sdb1文件系统的参数
# 先查看当前该文件系统的属性
[root@rebekk ~]# blkid /dev/sdb1
/dev/sdb1: UUID="2e786801-3800-4860-99e7-225811e42c96" TYPE="ext4"
# 添加卷标NEWDATA1
[root@rebekk ~]# tune2fs -L NEWDATA1 /dev/sdb1
tune2fs 1.42.9 (28-Dec-2013)
# 再次查看修改后的文件系统属性
[root@rebekk ~]# blkid /dev/sdb1
/dev/sdb1: LABEL="NEWDATA1" UUID="2e786801-3800-4860-99e7-225811e42c96" TYPE="ext4"
# 修改xfs类型文件系统的属性
[root@rebekk ~]# xfs_admin -L NEWDATA2 /dev/sdb2
writing all SBs
new label = "NEWDATA2"
[root@rebekk ~]# blkid /dev/sdb2
/dev/sdb2: LABEL="NEWDATA2" UUID="adc86f37-bc14-4806-a44f-95ec0d465a1c" TYPE="xfs"
# 通过卷标和UUID来查看是哪个块设备
[root@rebekk ~]# blkid -L NEWDATA2
/dev/sdb2
[root@rebekk ~]# blkid -U "2e786801-3800-4860-99e7-225811e42c96"
/dev/sdb1
4. 挂载文件系统
将设备分区,并在分区内完成文件系统的安装后,若要使用该存储,则需要将文件系统挂载到操作系统的某个目录下。
# 首先创建挂载文件系统的目录
[root@rebekk ~]# mkdir -pv /DATA/{DATA1,DATA2}
mkdir: created directory ‘/DATA’
mkdir: created directory ‘/DATA/DATA1’
mkdir: created directory ‘/DATA/DATA2’
# 将/dev/sdb1挂载到/DATA/DATA1目录下
[root@rebekk ~]# mount /dev/sdb1 /DATA/DATA1
# 对于ext系列文件系统,挂载完成后,该目录下会默认生成一个lost+found目录(xfs文件系统没有)
[root@rebekk ~]# ls /DATA/DATA1
lost+found
# 查看Linux系统中各文件系统的硬盘使用情况
[root@rebekk ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 17G 1.2G 16G 7% /
devtmpfs devtmpfs 898M 0 898M 0% /dev
tmpfs tmpfs 910M 0 910M 0% /dev/shm
tmpfs tmpfs 910M 9.6M 901M 2% /run
tmpfs tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 146M 869M 15% /boot
tmpfs tmpfs 182M 0 182M 0% /run/user/0
/dev/sdb1 ext4 3.9G 16M 3.6G 1% /DATA/DATA1
3. 文件系统相关有三个表:
- /etc/fstab表:文件系统分区表的静态生成文件,即
mount -a
命令会读取这个表中的所有条目,系统启动时也会读取这个表,会由操作系统直接完成里面的条目挂载 - /etc/mtab:使用 mount 或 umount 进行文件挂载或卸载时,默认都会写入到 /etc/mtab 和 /proc/mounts 文件中,当 /proc 文件系统被挂载时,这两者几乎是一致的
- /proc/mounts:同2