Linux中的存储设备管理
1.设备识别
设备接入系统后都是以文件的形式存在
设备名称 | ## |
---|---|
SATA/SAS/USB | /dev/sda,/dev/sdb s=STAT ,d=DISK ,a=第几块 |
IDE | /dev/hd0,/dev/hsd1 ##h=hard |
VIRTIO-BLOCK | /dev/vda, /dev/vdb #v=virtio |
M2 (SSD) | /dev/nvme0,.dev/nvme1 #nvme=m2 |
SD/MMC/EMMC(卡) | /dev/mmcblk0,/dev/mmcblk1 # mmcblk=mmc卡 |
光驱 | /dev/cdrom,/dev/sr0,/dev/sr1 |
设备查看
fdisk -l :查看磁盘分区情况 (真实存在的设备)
lsblk:设备使用情况
blkid:设备管理方式及设备id
df:查看正在被系统挂载的设备
cat /proc/partitions:查看系统识别设备 (系统识别的设备与系统识别的设备存在差异,没有uuid。无驱动,需要被提醒,)
2、设备挂载
在系统中有设备id的设备是可以被系统使用的
2.1挂载命令
mount -o 挂载参数 device 挂载点
umount 设备|挂载点
mount:查看挂载信息
mount -o rw /dev/nvme0n1p1 /westos
mount -o remount,ro /westos:转换挂载参数
2.2在卸载时当出现设备正忙:
fuser -kvm 设备|挂载点 :-k结束进程,-v现实详细信息,-m现实进程
[root@localhost westos]# umount /dev/vdb1
umount: /westos: target is busy.
[root@localhost westos]# fuser -kvm /dev/vdb1
USER PID ACCESS COMMAND
/dev/vdb1: root kernel mount /westos
lee 4332 ..c.. bash
root 4387 ..c.. bash
lee 4407 ..c.. bash
root 4510 ..c.. bash
Killed
Killed
Killed
Killed
以上设备挂载是临时挂载
2.3.设备永久挂载及出现错误时修改
vim/etc/fstab 设备挂载策略文件
(1)设备 (2)挂载点 (3)文件系统类型 (4)挂载参数 (5)是否备份 (6)是否检测
注意:
1.此文件在编写完成后不会立即生效mount -a 重新读取/etc/fstab文件
2.此文件内容编写错误会导致系统启动失败
按照提示在操作界面输入超级用户密码
注释错误行
重启系统即可
就可以重启系统成功。
3.设备中文件的查找
3.1find 命令参数注解
find 参数 | |
---|---|
-name | 名字 |
-user | 用户 |
– | – |
-group | 组 |
-type | 类型 f:文件 l:符号连接 d:目录 c:字符设备 b:块设备 s:套接字 |
exec | 执行 |
-maxdepth | 最大深度 |
-mindepth | 最小深度 |
-cmin | 最近更改的文件 1 :1分钟 +1:大于1分钟 -1:1分钟之内 |
-o | 或者 |
-a | 而且 |
not | 相反 |
size | 大小 |
find / -size +100M
查找/下的大于100M的
3.2.find命令用法示例
4.
444
精确查找
/444
u位或g或o位上有r权限
-444
u位,g位,o位必须有r权限
find /mnt -perm -001 -exac chmod o-x {} \;
-001:找到o位上有x权限的
4、分区方法
fdisk /dev/XXX
Command (m for help): m
命令 | 作用 |
---|---|
d | 删除 |
l | 列出所有分区类型 |
n | 新建 |
p | 显示分区表 |
t | 更改分区类型 |
w | 保存更改 |
q | 退出 |
g | 设定分区方式为GPT |
o | 设定分区方式为mbr2 |
partx -d /devdb:清理分区表
partx -a /devdb:重新加载分区表
mkfs.xfs -k /devdb1:格式化设备为xfs文件系统(相当于在/devdb1上安装设备管理软件,-K不丢弃空数据块)
mount /devdb1 /mnt/westos:mount命令临时挂载/devdb1,如果需要永久挂载需要编写/etc/fstab
设备删除命令
dd if=/dev/zero of=/dev/westosdir/westosfile bs=1M count=1
[root@localhost Desktop]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.32.1).
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 (0 primary, 0 extended, 4 free) ##主分区
e extended (container for logical partitions) ##扩展分区
Select (default p):
Using default response p.
Partition number (1-4, default 1): ####分区表位置
First sector (2048-20971519, default 2048): ###分区起始位置推荐使用默认
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +100M #####分区结束位置
Created a new partition 1 of type 'Linux' and of size 100 MiB.
Command (m for help): p
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xd88ec85a
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 83 Linux
Command (m for help): wq ###退出保存
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost Desktop]# udevadm settle 同步分区表
partprobe
同步分区表
主分区;扩展分区
在建立分区的时候,将第三块分区建成扩展分区,才能继续分(前三块分区选主分区,在last处+100G(所想要的大小),第四块的时候直接回车作为扩展分区)
mkfs.xfs -k /devdb1:格式化设备为xfs文件系统(相当于在/devdb1上安装设备管理软件,-K不丢弃空数据块)
-f
会格式化更快
非交互式分区(可以写入脚本执行)parted
parted /dev/vdb help
查看命令的帮助
parted -l
[root@localhost mnt]# parted -l
Error: /dev/vdb: unrecognised disk label
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: unknown
Disk Flags:
[root@localhost mnt]# parted /dev/vdb mklabel msdos
#######指定分区模式
Information: You may need to update /etc/fstab.
[root@localhost mnt]# parted -l
Model: Virtio Block Device (virtblk)
Disk /dev/vdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos ############
Disk Flags:
[root@localhost mnt]#parted /dev/vdb mkpart primary 1 100
####建立分区表,需要手动计算位置
Information: You may need to update /etc/fstab.
[root@localhost mnt]#parted /dev/vdb mkpart primary 101 1101
Information: You may need to update /etc/fstab.
[root@localhost mnt]# fdisk -l
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 194559 192512 94M 83 Linux
/dev/vdb2 196608 2150399 1953792 954M 83 Linux
[root@localhost mnt]#parted /dev/vdb rm 2
Information: You may need to update /etc/fstab.
[root@localhost mnt]# parted /dev/vdb rm 1
Information: You may need to update /etc/fstab.
[root@localhost mnt]#parted /dev/vdb mkpart
Partition type? primary/extended? primary
File system type? [ext2]? xfs
Start? 1
End? 101
Information: You may need to update /etc/fstab.
Disklabel type
分区表类型
[root@localhost mnt]# fdisk /dev/vdb
Command (m for help): p
Disklabel type: dos
Command (m for help): g -----> gpt
Command (m for help): p
Disklabel type: gpt
Command (m for help): o ------dos
Disklabel type: dos
5、swap分区
5.1作用:
程序在运行时所有数据在RAM
当RAM使用量超过了限额
为了让系统变得更加稳定
我们在硬盘上划分一部分空间来作为内存缓冲区swap
当内存使用超过限额,内核会把内存中间闲置的数据交还给内存进程处理
5.2swap分区大小建议:
内存大小 | swap分区建议大小 | 当允许HIBERNATE |
---|---|---|
2GiB以下 | 内存两倍 | 内存三倍 |
2-8GiB | 等于物理内存 | 物理内存两倍 |
8-64GiB | 4GiB | 1.5倍物理内存 |
64GiB以上 | 4GiB | HIBERNATE不开 |
5.3swap管理
swapon -s:查看swap分区信息
5.4创建swap分区
创建分区并设定分区的类型为Linuxswap
mkswap /devdb1:格式化设备为swap格式
swapon /devdb -p 0-32767:-p表示指定swap的优先级
上述方式都为临时操作,如果想永久添加swap分区需要vim /etcfstab进行修改
**/devdb1 swap swap pri=4 0 0**:如果想取消永久添加swap分区可删除此行
swapoff /devdb1
建立swap分区
root@localhost westos]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.32.1).
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 (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p):
Using default response p.
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +100M
Created a new partition 1 of type 'Linux' and of size 100 MiB.
Command (m for help): t ###更改分区类型
Selected partition 1
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 or 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 ea Rufus alignment
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC b
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fd Linux raid auto
1c Hidden W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e Hidden W95 FAT1 80 Old Minix be Solaris boot ff BBT
Hex code (type L to list all codes): 82 ##选择swap分区
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p ##查看分区列表
Disk /dev/vdb: 10 GiB, 10737418240 bytes, 20971520 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
Disklabel type: dos
Disk identifier: 0xd88ec85a
Device Boot Start End Sectors Size Id Type
/dev/vdb1 2048 206847 204800 100M 82 Linux swap / Solaris
Command (m for help): wq ##退出保存
The partition table has been altered.
Failed to add partition 1 to system: Device or resource busy
The kernel still uses the old partitions. The new table will be used at the next reboot.
Syncing disks.
[root@localhost westos]# mkswap /dev/vdb1 ##格式化设备为swap形式
mkswap: /dev/vdb1: warning: wiping old xfs signature.
Setting up swapspace version 1, size = 100 MiB (104853504 bytes)
no label, UUID=3505fce6-7b5d-40cf-adab-359cc4d9fa3a
设定优先级
[root@localhost Desktop]# swapon -p 2 -a /dev/vdb1
swapon: /dev/vdb1: swapon failed: Device or resource busy
[root@localhost Desktop]# swapoff /dev/vdb1
[root@localhost Desktop]# swapon -p 2 -a /dev/vdb1
[root@localhost Desktop]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 1048572 561428 -2
/dev/vdb1 partition 102396 0 2
永久添加swap分区
[root@localhost Desktop]# vim /etc/fstab
[root@localhost Desktop]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 6 09:26:07 2020
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=708f6d3a-fd46-4a50-bade-f2d6630435a6 /boot xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
/dev/vdb1 swap swap pri=2 0 0
[root@localhost Desktop]# swap
swaplabel swapoff swapon
[root@localhost Desktop]# swapon -a
删除swap分区
[root@localhost Desktop]# swapon -s
Filename Type Size Used Priority
/dev/dm-1 partition 1048572 561428 -2
/dev/vdb1 partition 102396 0 2
[root@localhost Desktop]# vim /etc/fstab ##删除配置文件之前的设定
[root@localhost Desktop]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 6 09:26:07 2020
#
# 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.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
/dev/mapper/rhel-root / xfs defaults 0 0
UUID=708f6d3a-fd46-4a50-bade-f2d6630435a6 /boot xfs defaults 0 0
/dev/mapper/rhel-swap swap swap defaults 0 0
[root@localhost Desktop]# swapoff /dev/vdb1
[root@localhost Desktop]# swapon -s ##查看结果
Filename Type Size Used Priority
/dev/dm-1 partition 1048572 563776 -2
6、磁盘配额
作用:设定用户能写入指定设备的最大额度
6.1设定方法:
mount /devdb1 /目标目录/ -o usrquota:挂载设备并激活配置参数
quotaon -uv /devdb1:激活配额
edquota -u lee:设定用户lee配额
6.2永久开启配额
vim /etc/fstab
/devdb1 /westosdir xfs default,usrquota 0 0
6.3关闭配额
quotaoff -uv /devdb1
vim /etc/fstab:去掉配置参数usrquota