磁盘管理

本文涉及到的磁盘相关命令:查看磁盘管理相关命令

Linux默认会将执行过的命令和文件放到内存中,以便系统下次调用;

一、RAID磁盘阵列

为什么要使用磁盘阵列?
使用磁盘阵列技术,可以获得更大的磁盘空间,更高的磁盘性能以及实现磁盘冗余,保证数据安全。

1、RAID 0(条带)

硬盘数量要求:>=1
磁盘利用率:100%
冗余度:0%

优点:容量大;读写性能好。

2、RAID 1(镜像)

硬盘数量要求:==2
磁盘利用率:50%
冗余度:100%

优点:冗余度高;读取速度较好,写入速度一般。

3、RAID 5

硬盘数量要求:>=3
磁盘利用率:n-1/n
冗余度:1/n

优点:容量较高;冗余度一般;读取速度较好,写入速度一般。

4、RAID 10

硬盘数量要求:>=4
磁盘利用率:50%
冗余度:50%

优点:读写速度较好;冗余度较好。

在RAID 10中,磁盘增加的数量必须是2的倍数

二、添加tmpfs(临时文件系统)分区

数据写入到该文件系统中,相当于将数据直接写入到内存中。

step1:创建挂载点

[root@aspen /]# mkdir -p /Memory
[root@aspen /]# ll -d /Memory/
drwxr-xr-x 2 root root 6 Jun 18 21:43 /Memory/

step2:挂载tmpfs

[root@aspen /]# mount -t tmpfs -o size=500m tmpfs /Memory/
[root@aspen /]# df -h |head -1; df -h |tail -1
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           500M     0  500M   0% /Memory

step3:永久挂载分区

[root@aspen /]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat May 11 15:50:18 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=69d3eed0-c901-4e0e-ae98-4c31b2a23719 /                       xfs     defaults        0 0
UUID=7e8b6491-678b-444f-bbdb-019eb0c2bf8c /boot                   xfs     defaults        0 0
UUID=42eaa7c4-975b-4c3a-8301-1e4543eb730e swap                    swap    defaults        0 0
tmpfs                                               /Memory            tmpfs   size=500m  0 0

step4:验证及测试

[root@aspen ~]# reboot
[root@aspen ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
.....
tmpfs          tmpfs     500M  500M     0 100% /Memory
/dev/sda1      xfs       197M  105M   93M  54% /boot
tmpfs          tmpfs      98M     0   98M   0% /run/user/0
[root@aspen /]# dd if=/dev/zero of=/Memory/500 bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.138386 s, 3.8 GB/s
[root@aspen /]# dd if=/dev/zero of=/tmp/600 bs=1M count=600
600+0 records in
600+0 records out
629145600 bytes (629 MB) copied, 0.705453 s, 892 MB/s
当文件夹被指定为tmpfs分区时,文件夹权限默认变为1777
[root@aspen /]# ll -d /Memory/
drwxrwxrwt 2 root root 40 Jun 18 22:13 /Memory/
[root@aspen /]# stat -c%a /Memory/
1777

三、添加swap分区

step1:创建指定大小的文件

[root@aspen /]# dd if=/dev/zero of=/tmp/Swap_500M bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 13.731 s, 38.2 MB/s
[root@aspen /]# file /tmp/Swap_500M 
/tmp/Swap_500M: data

step2:将指定文件设置为交换分区

[root@aspen /]# mkswap /tmp/Swap_500M 
Setting up swapspace version 1, size = 511996 KiB
no label, UUID=90d43457-f247-442e-9016-dc4adee38798
[root@aspen /]# file /tmp/Swap_500M 
/tmp/Swap_500M: Linux/i386 swap file (new style), version 1 (4K pages), size 127999 pages, no label, UUID=5fc43e39-c039-4600-a321-4471fd44d50e

step3:开启(挂载)swap分区

此处使用swapon命令时,无法通过Tab键自动补全文件路径

[root@aspen /]# cd /tmp/
[root@aspen /tmp]# swapon Swap_500M
swapon: /tmp/Swap_500M: insecure permissions 0644, 0600 suggested.
[root@aspen /tmp]# ll -h Swap_500M 
-rw-r--r-- 1 root root 500M Jun 18 22:21 Swap_500M
[root@aspen /tmp]# stat -c%a Swap_500M 
644

step4:永久挂载分区

[root@aspen ~]# chmod +x /etc/rc.d/rc.local
[root@aspen ~]# vim /etc/rc.local 
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
swapon /tmp/Swap_500M

step5:验证及测试
swapoff 命令用于关闭交换分区

[root@aspen /tmp]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M         83M        291M        7.6M        597M        720M
Swap:          1.5G          0B        1.5G
[root@aspen /tmp]# swapon -s
Filename                Type        Size    Used    Priority
/dev/sda2                               partition   1048572 0   -2
/tmp/Swap_500M                          file    511996  0   -3
[root@aspen /tmp]#
[root@aspen /tmp]# swapoff /tmp/Swap_500M 
[root@aspen /tmp]# free -h
              total        used        free      shared  buff/cache   available
Mem:           972M         83M        291M        7.6M        597M        720M
Swap:          1.0G          0B        1.0G

四、添加数据分区

step1:磁盘分区

  • fdisk 用于MBR分区表磁盘分区命令(仅支持2TB以下的磁盘分区)
  • parted GPT分区表磁盘分区命令(常用于2TB以上的磁盘分区)
[root@aspen ~]# 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): 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: 0x38e5fa46

   Device Boot      Start         End      Blocks   Id  System

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): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +20M
Partition 1 of type Linux and of size 20 MiB 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): 
First sector (43008-41943039, default 43008): 
Using default value 43008
Last sector, +sectors or +size{K,M,G} (43008-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Linux and of size 20 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@aspen ~]# fdisk -l | tail -3
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048       43007       20480   83  Linux
/dev/sdb2           43008    41943039    20950016   83  Linux

step2:创建文件系统(格式化分区)

  • xfs(Cent OS 7 默认的文件系统)
  • ext4(Cent OS 6 默认的文件系统)
[root@aspen ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
5136 inodes, 20480 blocks
1024 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20971520
3 block groups
8192 blocks per group, 8192 fragments per group
1712 inodes per group
Superblock backups stored on blocks: 
    8193

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
[root@aspen ~]# mkfs.xfs /dev/sdb2
meta-data=/dev/sdb2              isize=512    agcount=4, agsize=1309376 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=5237504, 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

step3:挂载分区

  • 临时挂载-mount
  • 永久挂载-修改fstab文件
[root@aspen /]# mkdir -p /data_01 /data_02
[root@aspen /]# mount /dev/sdb1 /data_01
[root@aspen /]# mount /dev/sdb2 /data_02
[root@aspen /]# df -ihT | head -1 && df -ihT |tail -2
Filesystem     Type     Inodes IUsed IFree IUse% Mounted on
/dev/sdb1      ext4       5.1K    11  5.1K    1% /data_01
/dev/sdb2      xfs         10M     3   10M    1% /data_02
[root@aspen /]# vim /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Sat May 11 15:50:18 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=69d3eed0-c901-4e0e-ae98-4c31b2a23719 /                       xfs     defaults        0 0
UUID=7e8b6491-678b-444f-bbdb-019eb0c2bf8c /boot                   xfs     defaults        0 0
UUID=42eaa7c4-975b-4c3a-8301-1e4543eb730e swap                    swap    defaults        0 0
tmpfs /Memory tmpfs size=500m 0 0
/dev/sdb1 /data_01 ext4 defaults 0 0
/dev/sdb2 /data_02 xfs defaults 0 0

五、盘空间不足排查流程

1、inode数量不足

step0:故障环境模拟

[root@aspen ~]# dd if=/dev/zero of=/tmp/sdc bs=1k count=100
100+0 records in
100+0 records out
102400 bytes (102 kB) copied, 0.000360444 s, 284 MB/s
[root@aspen ~]# mkfs.ext4 /tmp/sdc 
mke2fs 1.42.9 (28-Dec-2013)
/tmp/sdc is not a block special device.
Proceed anyway? (y,n) y

Filesystem too small for a journal
......
[root@aspen ~]# mount /tmp/sdc /data
[root@aspen ~]# df -ih | head -1 ; df -ih | tail -1
Filesystem     Inodes IUsed IFree IUse% Mounted on
/dev/loop0         16    11     5   69% /data
[root@aspen /data]# touch aspen{01..6}.txt
touch: cannot touch ‘aspen06.txt’: No space left on device
[root@aspen /data]# ls
aspen01.txt  aspen02.txt  aspen03.txt  aspen04.txt  aspen05.txt  lost+found
模拟一个100K的磁盘,但是创建第六个空文件时,提示磁盘空间不足;

step1:使用df -h查看系统磁盘空间使用情况

[root@aspen /data]# df -h| head -1;df -h | grep /data
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0       93K   14K   72K  17% /data
磁盘空间使用14K,仅使用17%;说明磁盘block充足,检查inode使用情况;

step2:使用df -i查看系统inode使用情况

[root@aspen /data]# df -i| head -1;df -i | grep /data
Filesystem       Inodes IUsed    IFree IUse% Mounted on
/dev/loop0           16    16        0  100% /data
发现磁盘inode数量被占满,因此创建文件时报错;

一般inode数量占满,多为系统小容量的碎片文件过多导致;当一个目录下碎片文件过多时,目录本身空间就会被撑大;因为需要借助find命令找出系统中目录空间过大的目录,继续进行排查;

step3:找出系统中目录本身空间占用空间较大(一般搜索>1M)的目录

[root@aspen /data01]# touch a{1..400000}.txt
-bash: /usr/bin/touch: Argument list too long    #系统提示参数过长错误;大量创建/删除文件时常见该错误;
[root@aspen /data01]# echo a{1..400000}.txt |xargs touch 
[root@aspen /data01]#ls | wc -l
400000
[root@aspen /data01]# find / -type d -size +1M |xargs ls -ldh
drwxr-xr-x 2 root root 9.4M Jun 19 21:14 /data01

step4:使用rm命令删除占用inode数量的无用小文件

[root@aspen /]# rm -rf /data01/*.txt
-bash: /usr/bin/rm: Argument list too long
[root@aspen /data01]# ls | xargs rm -f
[root@aspen /data01]# ls | wc -l
0

2、Block空间不足

step1:使用df -h查看系统磁盘空间使用情况

[root@aspen ~]#seq 500000000 >> /var/log/messages
[root@aspen ~]# df -h |head -2
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        99G  6.9G   92G   7% /

step2:使用du -sh递归查看指定分区下的指定目录占用空间情况

[root@aspen /data01]# du -sh /* | sort -h | tail -5
du: cannot access ‘/proc/9853/task/9853/fd/4’: No such file or directory
du: cannot access ‘/proc/9853/task/9853/fdinfo/4’: No such file or directory
du: cannot access ‘/proc/9853/fd/4’: No such file or directory
du: cannot access ‘/proc/9853/fdinfo/4’: No such file or directory
95M /boot
201M    /root
501M    /tmp
1.4G    /usr
4.8G    /var
[root@aspen /data01]# cd /var/
[root@aspen /var]# du -sh /var/* | sort -h | tail -5
8.0K    /var/db
24K /var/spool
78M /var/lib
111M    /var/cache
4.6G    /var/log
[root@aspen /var]# du -sh /var/log/* | sort -h | tail -5
352K    /var/log/sa
576K    /var/log/messages-20190614
1.5M    /var/log/audit
3.0M    /var/log/anaconda
4.6G    /var/log/messages

step3:使用rm命令删除无用大文件(确认后再删除)

[root@aspen /var]# rm -f /var/log/messages
[root@aspen /var]# ls /var/log/messages
ls: cannot access /var/log/messages: No such file or directory
[root@aspen /var]# df -h | head -2  #文件已经被删除了,但是磁盘空间没有被释放;是因为文件不满足被释放条件;
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        99G  6.9G   92G   7% /
文件删除条件
1.文件硬链接数为0
2.没有进程调用该文件

使用lsof | grep deleted查看已删除但未被释放文件,释放文件只需重启占用服务
[root@aspen /var]# lsof | grep deleted
rsyslogd   7230         root    6w      REG                8,3 4889521507        169 /var/log/messages (deleted)
in:imjour  7230 7513    root    6w      REG                8,3 4889521507        169 /var/log/messages (deleted)
rs:main    7230 7515    root    6w      REG                8,3 4889521507        169 /var/log/messages (deleted)
[root@aspen /var]# systemctl restart rsyslog.service 
[root@aspen /var]# df -h | head -2
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        99G  2.3G   97G   3% /

附:思维导图