文章目录

1. 磁盘基础介绍

#淘汰了


SATA III SAS #企业级


SSD( SATA III PCIE )


M2 超极本的接口标准 MSATA


机械硬盘 HDD 固态硬盘 SSD

容量大价格低 容量小价格贵

抗击打能力弱 抗击打能力强

读写速度慢 读写速度快

数据损坏恢复易 数据恢复难

寿命一直使用 使用期限有一定限制



磁盘的命名方式:

/dev/sda1 #第一块物理硬盘的第一个分区

/dev/sdb3 #第二块物理硬盘的第三个分区

/dev/vdc5 #第三块虚拟硬盘的第5个分区


sd #物理硬盘


vd #虚拟硬盘


磁盘的相关单位和术语


尺寸: 2.5英寸 3.5英寸

数据大小: bit B KB MB GB TB PB EB

单位换算: 1024 1B=8b

转速: RPM 转/每分钟 5400 7200 10k 15k

IOPS 衡量磁盘读写 IO的单位 每秒的输入输出


两个分区表:


MBR 适用于2TB 只能有4个主分区 扩展分区(逻辑分区 )


3个主分区 + 1个扩展分区( 逻辑分区...... )


MBR的位置: 0磁头0磁道1扇区 512字节


446字节 MBR引导记录

64字节4个主分区

2字节分区结束标识符



GPT 适用于2TB 128个主分区



分区工具:


fdisk #适用于2TB的分区


gdisk #适用于2TB以上

2. Fdisk分区

[root@qls ~]# ll  /dev/sd*
brw-rw---- 1 root disk 8, 0 Aug 3 09:34 /dev/sda
brw-rw---- 1 root disk 8, 1 Aug 3 09:34 /dev/sda1
brw-rw---- 1 root disk 8, 2 Aug 3 09:34 /dev/sda2
brw-rw---- 1 root disk 8, 3 Aug 3 09:34 /dev/sda3
[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sr0 11:0 1 4.3G 0 rom /mnt


[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sdb 8:16 0 100G 0 disk
sdc 8:32 0 3T 0 disk
sr0 11:0 1 4.3G 0 rom




[root@qls ~]# ll /dev/sd*
brw-rw---- 1 root disk 8, 0 Aug 3 17:38 /dev/sda
brw-rw---- 1 root disk 8, 1 Aug 3 17:38 /dev/sda1
brw-rw---- 1 root disk 8, 2 Aug 3 17:38 /dev/sda2
brw-rw---- 1 root disk 8, 3 Aug 3 17:38 /dev/sda3
brw-rw---- 1 root disk 8, 16 Aug 3 17:38 /dev/sdb
brw-rw---- 1 root disk 8, 32 Aug 3 17:38 /dev/sdc



#显示某块磁盘的分区信息

[root@qls ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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@qls ~]# fdisk -l /dev/sda

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ae9a7

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 5220351 2097152 82 Linux swap / Solaris
/dev/sda3 5220352 209715199 102247424 83 Linux
[root@qls ~]#



#1. 进行分区 创建分区

[root@qls ~]# 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 0x5b8d3e75.

Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition #删除分区
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types #显示分区文件系统类型
m print this menu #菜单
n add a new partition #创建一个新分区
o create a new empty DOS partition table
p print the partition table #打印分区表信息
q quit without saving changes #不保存退出
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit #保存退出
x extra functionality (experts only)

Command (m for help):

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



Command (m for help): n #创建分区 #主分区的编号是1-4 逻辑分区从5开始
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-209715199, default 2048): #扇区的起始位置
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +10G #扇区的结束位置
Partition 1 of type Linux and of size 10 GiB is set


Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux



Command (m for help): n #再次创建一个分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p):
Using default response p
Partition number (2-4, default 2):
First sector (20973568-209715199, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-209715199, default 209715199): +10G
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 41945087 10485760 83 Linux


Command (m for help): d #删除分区
Partition number (1,2, default 2): 2 #指定分区编号
Partition 2 is deleted

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux


#创建扩展分区

Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
Partition number (2-4, default 2):
First sector (20973568-209715199, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-209715199, default 209715199): +50G
Partition 2 of type Extended and of size 50 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 125831167 52428800 5 Extended



#创建一个逻辑分区

Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (20975616-125831167, default 20975616):
Using default value 20975616
Last sector, +sectors or +size{K,M,G} (20975616-125831167, default 125831167): +20G
Partition 5 of type Linux and of size 20 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 125831167 52428800 5 Extended
/dev/sdb5 20975616 62918655 20971520 83 Linux



Command (m for help): w #保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.


#检查

[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 10G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 20G 0 part
sdc 8:32 0 3T 0 disk
sr0 11:0 1 4.3G 0 rom


[root@qls ~]# ll /dev/sd*
brw-rw---- 1 root disk 8, 0 Aug 3 2020 /dev/sda
brw-rw---- 1 root disk 8, 1 Aug 3 2020 /dev/sda1
brw-rw---- 1 root disk 8, 2 Aug 3 2020 /dev/sda2
brw-rw---- 1 root disk 8, 3 Aug 3 2020 /dev/sda3
brw-rw---- 1 root disk 8, 16 Aug 3 09:53 /dev/sdb
brw-rw---- 1 root disk 8, 17 Aug 3 09:53 /dev/sdb1
brw-rw---- 1 root disk 8, 18 Aug 3 09:53 /dev/sdb2
brw-rw---- 1 root disk 8, 21 Aug 3 09:53 /dev/sdb5
brw-rw---- 1 root disk 8, 32 Aug 3 2020 /dev/sdc


#2. 创建文件系统 格式化


[root@qls ~]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=655360 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=2621440, 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


#3. 创建挂载点

[root@qls ~]# mkdir /fdisk01


#4. 挂载使用

[root@qls ~]# mount /dev/sdb1 /fdisk01
[root@qls ~]# df -h | grep '/fdisk01'
/dev/sdb1 10G 33M 10G 1% /fdisk01

#测试是否可用
[root@qls ~]# dd if=/dev/zero of=/fdisk01/test.log bs=10M count=100
100+0 records in
100+0 records out
1048576000 bytes (1.0 GB) copied, 10.8088 s, 97.0 MB/s
[root@qls ~]# df -h | grep '/fdisk01'
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01




#5. 实现永久挂载


[root@qls ~]# tail -1 /etc/fstab
/dev/sdb1 /fdisk01 xfs defaults 0 0
[root@qls ~]#


[root@qls ~]# mount -a #重新挂载/etc/fstab文件中的挂载列表
[root@qls ~]#



#数据是跟着设备走的 目录只是一个挂载点 就是入口

[root@qls ~]# ll /fdisk01/
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug 3 09:59 test.log
[root@qls ~]# umount /dev/sdb1
[root@qls ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 98G 5.5G 92G 6% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
[root@qls ~]# ll /fdisk01/
total 0
[root@qls ~]# mount -a
[root@qls ~]# ll /fdisk01/

3. Gdisk分区

[root@qls ~]# yum  install  -y  gdisk


[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 10G 0 part /fdisk01
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 20G 0 part
sdc 8:32 0 3T 0 disk
sr0 11:0 1 4.3G 0 rom


[root@qls ~]# gdisk -l /dev/sdc
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present

Creating new GPT entries.
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2BF39D49-315B-42C3-9C9C-C5A98D1EBB71
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 6442450877 sectors (3.0 TiB)

Number Start (sector) End (sector) Size Code Name



#2. 创建分区

[root@qls ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present

Creating new GPT entries.

Command (? for help): ?
b back up GPT data to a file
c change a partition's 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 partition's type code
v verify disk
w write table to disk and exit #保存退出
x extra functionality (experts only)
? print this menu #帮助

Command (? for help):



Command (? for help): n #创建分区
Partition number (1-128, default 1): #分区编号
First sector (34-6442450910, default = 2048) or {+-}size{KMGTP}: #扇区起始位置
Last sector (2048-6442450910, default = 6442450910) or {+-}size{KMGTP}: #扇区结束位置
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/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2E58E6D9-4971-4B2F-A455-16D3A5115002
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number Start (sector) End (sector) Size Code Name
1 2048 6442450910 3.0 TiB 8300 Linux filesystem

Command (? for help):


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/sdc.
The operation has completed successfully.
[root@qls ~]#


#3. 格式化,创建文件系统


[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 10G 0 part /fdisk01
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 20G 0 part
sdc 8:32 0 3T 0 disk
└─sdc1 8:33 0 3T 0 part
sr0 11:0 1 4.3G 0 rom


[root@qls ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=201326527 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=805306107, imaxpct=5
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=393215, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0




#4. 创建挂载点,进行挂载


[root@qls ~]# mkdir /gdisk01
[root@qls ~]# mount /dev/sdc1 /gdisk01
[root@qls ~]# df -h | grep '/gdisk01'
/dev/sdc1 3.0T 33M 3.0T 1% /gdisk01
[root@qls ~]# cp /fdisk01/test.log /gdisk01/
[root@qls ~]# df -h | grep '/gdisk01'
/dev/sdc1 3.0T 1.1G 3.0T 1% /gdisk01
[root@qls ~]#


#5. 永久挂载

[root@qls ~]# tail -1 /etc/fstab
/dev/sdc1 /gdisk01 xfs defaults 0 0
[root@qls ~]# mount -a
[root@qls ~]#

4. 磁盘的挂载方式

mount    #挂载的命令

选项:

-t #指定文件系统 网络协议 nfs

-o #指定挂载的参数

-a #重新挂载/etc/fstab文件中的挂载列表

[root@qls ~]# mount -t xfs /dev/sdb1 /fdisk01/
[root@qls ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 98G 5.5G 92G 6% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.5M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01
[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 10G 0 part /fdisk01
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 20G 0 part
sdc 8:32 0 3T 0 disk
└─sdc1 8:33 0 3T 0 part
sr0 11:0 1 4.3G 0 rom
[root@qls ~]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="70df4a5b-091c-4aaa-9cc7-d745201ec394" TYPE="xfs"
/dev/sda2: UUID="6aefd444-30dc-4c0e-9a8d-75a8b1d9856b" TYPE="swap"
/dev/sda3: UUID="4aa02e85-ffe1-4783-9886-a328f923bee8" TYPE="xfs"
/dev/sdb1: UUID="0668e9f1-7d37-42eb-9e79-980f6352c342" TYPE="xfs"
/dev/sdc1: UUID="60d9a87f-fa86-4fdc-a1c9-2a88df756a24" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="71869797-d467-4293-807e-7c6f917c7422"
[root@qls ~]#


[root@qls ~]# mount -o ro /dev/sdb1 /fdisk01/
[root@qls ~]# cp /etc/hosts /fdisk01/
cp: cannot create regular file ‘/fdisk01/hosts’: Read-only file system


umount #卸载

选项:

-l #强制卸载

[root@qls ~]# umount /dev/sdb1
[root@qls ~]# df -h | grep '/dev/sdb1'
[root@qls ~]# mount /dev/sdb1 /fdisk01/
[root@qls ~]# cd /fdisk01/
[root@qls fdisk01]# umount /fdisk01 #不能在挂载的目录中进行卸载
umount: /fdisk01: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@qls fdisk01]# umount /dev/sdb1
umount: /fdisk01: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@qls fdisk01]# umount -l /dev/sdb1
[root@qls fdisk01]# cd
[root@qls ~]# mount /dev/sdb1 /fdisk01/
[root@qls ~]# umount /fdisk01/
[root@qls ~]# df -h | grep '/dev/sdb1'
[root@qls ~]#



#通过UUID进行挂载

[root@qls ~]# blkid
/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"
/dev/sda1: UUID="70df4a5b-091c-4aaa-9cc7-d745201ec394" TYPE="xfs"
/dev/sda2: UUID="6aefd444-30dc-4c0e-9a8d-75a8b1d9856b" TYPE="swap"
/dev/sda3: UUID="4aa02e85-ffe1-4783-9886-a328f923bee8" TYPE="xfs"
/dev/sdb1: UUID="0668e9f1-7d37-42eb-9e79-980f6352c342" TYPE="xfs"
/dev/sdc1: UUID="60d9a87f-fa86-4fdc-a1c9-2a88df756a24" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="71869797-d467-4293-807e-7c6f917c7422"
[root@qls ~]# mount UUID="0668e9f1-7d37-42eb-9e79-980f6352c342" /fdisk01/
[root@qls ~]# df -h | grep 'fdisk'
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01
[root@qls ~]#


[root@qls ~]# tail -1 /etc/fstab
UUID=0668e9f1-7d37-42eb-9e79-980f6352c342 /fdisk01/ xfs defaults 0 0
[root@qls ~]# mount -a


[root@qls ~]# tail -4 /etc/fstab
UUID=4aa02e85-ffe1-4783-9886-a328f923bee8 / xfs defaults 0 0
UUID=70df4a5b-091c-4aaa-9cc7-d745201ec394 /boot xfs defaults 0 0
UUID=6aefd444-30dc-4c0e-9a8d-75a8b1d9856b swap swap defaults 0 0
UUID=0668e9f1-7d37-42eb-9e79-980f6352c342 /fdisk01/ xfs defaults 0 0


#以空白字符为分隔符,分为6列:


第五列:  是否允许dump进行备份  

0 #不备份

1 #每天备份

2 #不定期备份

第六列: 是否通过fsck命令检查磁盘

0 #不检查

1 #检查 检查顺序 先检查 /

2 #检查顺序

5. Swap虚拟内存

Out of memory

OOM #内存溢出 内存不足


[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 133 1625 9 221 1672
Swap: 2047 0 2047
[root@qls ~]# swapoff -a
[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 132 1627 9 220 1673
Swap: 0 0 0



虚拟内存


#添加一个swap分区


#创建一个1G的分区

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


Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): p
Partition number (3,4, default 3):
First sector (125831168-209715199, default 125831168):
Using default value 125831168
Last sector, +sectors or +size{K,M,G} (125831168-209715199, default 209715199): +1G
Partition 3 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 125831167 52428800 5 Extended
/dev/sdb3 125831168 127928319 1048576 83 Linux
/dev/sdb5 20975616 62918655 20971520 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@qls ~]# partprobe #通知内核,系统分区表发生变化


#将这个分区变为swap分区

[root@qls ~]# mkswap /dev/sdb3
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=8a9c926b-de0d-4b49-bcb8-39a9c0b834a8


#把这个分区加入到swap

[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 77 1819 0 84 1774
Swap: 2047 69 1978
[root@qls ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2097148 71168 -2
[root@qls ~]# swapon /dev/sdb3
[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 78 1818 0 84 1773
Swap: 3071 69 3002
[root@qls ~]# swapon -s
Filename Type Size Used Priority
/dev/sda2 partition 2097148 70912 -2
/dev/sdb3 partition 1048572 0 -3


#移除swap

[root@qls ~]# swapoff /dev/sdb3
[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 78 1818 0 84 1773
Swap: 2047 69 1978

#禁用swap
[root@qls ~]# swapoff -a
[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 128 1759 9 92 1715
Swap: 0 0 0

#启用swap

[root@qls ~]# swapon -a
[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 129 1758 9 92 1713
Swap: 2047 0 2047



#生成一个大文件 作为swap


[root@qls ~]# dd if=/dev/zero of=/root/swap.txt bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 8.33166 s, 126 MB/s
[root@qls ~]# ll
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug 3 11:21 swap.txt
[root@qls ~]# mkswap swap.txt
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=51e07b8a-971a-47ec-972e-36f8273ec3cd
[root@qls ~]# ll
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug 3 11:22 swap.txt
[root@qls ~]# file swap.txt
swap.txt: Linux/i386 swap file (new style), version 1 (4K pages), size 255999 pages, no label, UUID=51e07b8a-971a-47ec-972e-36f8273ec3cd


[root@qls ~]# chmod 600 swap.txt


[root@qls ~]# swapon /root/swap.txt

[root@qls ~]# free -m
total used free shared buff/cache available
Mem: 1980 132 727 9 1120 1663
Swap: 3047 0 3047


[root@qls ~]# swapoff swap.txt
[root@qls ~]# free -m
total used free

6. RAID基本介绍

磁盘阵列技术 

做磁盘阵列的好处:

1. 获得更高的容量

2. 获得更高的性能

3. 让数据更安全 冗余


如何做RAID:


软件实现 软RAID 不推荐 mdadm


硬件RAID RAID卡实现的 推荐


做RAID什么时候做:

在装系统之前做


RAID的级别:

级别 别名 硬盘数量 容量 性能 容错性

RAID0 条带卷 至少两块 N*单块 读写最快 0

RAID1 镜像卷 只能两块 损失一半 写慢 读一般 50%

RAID5 校验卷 至少3块 浪费一块 读写一般 N分之一 只能坏一块

RAID10 镜像阵列条带卷 至少4块 浪费一半 读写很快 50%

7. LVM概述

逻辑卷管理   


PV #物理卷 硬盘 分区

VG #卷组 多个硬盘 多个分区组成

LV #逻辑卷 分区 逻辑分区

PE #基本单元 4MB 一个LV只能分配65534个PE

8. 硬盘故障

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


Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p):
Using default response p
Selected partition 4
First sector (127928320-209715199, default 127928320):
Using default value 127928320
Last sector, +sectors or +size{K,M,G} (127928320-209715199, default 209715199): +2G
Partition 4 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 107.4 GB, 107374182400 bytes, 209715200 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: 0x5b8d3e75

Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 125831167 52428800 5 Extended
/dev/sdb3 125831168 127928319 1048576 83 Linux
/dev/sdb4 127928320 132122623 2097152 83 Linux
/dev/sdb5 20975616 62918655 20971520 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@qls ~]#


#准备2G的分区


[root@qls ~]# dd if=/dev/zero of=/opt/test.log bs=100M count=21
dd: error writing ‘/opt/test.log’: No space left on device
21+0 records in
20+0 records out
2099183616 bytes (2.1 GB) copied, 3.34918 s, 627 MB/s




[root@qls ~]# echo "123" >> /opt/file.txt
-bash: echo: write error: No space left on device #磁盘空间不足


#解决:

检查磁盘空间

[root@qls ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 98G 6.4G 92G 7% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01
/dev/sdb4 2.0G 2.0G 20K 100% /opt


#找到这个大文件 确认好之后在删

[root@qls ~]# du -sh /opt/* | grep 'G'
2.0G /opt/test.log


一级一级往下找




[root@qls ~]# echo "123" >>/opt/test.txt
-bash: /opt/test.txt: No space left on device
[root@qls ~]#


[root@qls ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 98G 6.4G 92G 7% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01
/dev/sdb4 2.0G 582M 1.5G 29% /opt


[root@qls ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 51123712 46093 51077619 1% /
devtmpfs 250786 417 250369 1% /dev
tmpfs 253511 1 253510 1% /dev/shm
tmpfs 253511 774 252737 1% /run
tmpfs 253511 16 253495 1% /sys/fs/cgroup
/dev/sda1 256000 326 255674 1% /boot
tmpfs 253511 1 253510 1% /run/user/0
/dev/sdb1 5242880 4 5242876 1% /fdisk01
/dev/sdb4 1048576 1048576 0 100% /opt



[root@qls ~]# find /opt/ -type d -size +2M | xargs ls -lhd
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test01
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test02
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test03
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test04
drwxr-xr-x 2 root root 3.7M Aug 3 12:27 /opt/test/test05


[root@qls ~]# find /opt/ -type d -size +1M | xargs ls -lhd
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data01
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data02
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data03
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data04
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data05
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test01
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test02
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test03
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test04
drwxr-xr-x 2 root root 3.7M Aug 3 12:27 /opt/test/test05



[root@qls ~]# ll /opt/test/data01 | wc -l
50001


#找到之后,确认在删除



[root@qls ~]# find /opt/test/data01 -type f -delete


[root@qls ~]# rm -f /opt/test/test01/*
-bash: /usr/bin/rm: Argument list too long


[root@qls ~]# ll /opt/test/test01/ | wc -l