存储设备的管理
1.设备的查看
1)发现系统中的设备
fdisk -l
![Linux system 存储设备的管理_第1张图片](http://img.e-com-net.com/image/info8/7392fd8a8c5a44b1a79a94ada51cb70d.jpg)
cat /proc/partitions #能够发现设备,但设备不一定能使用
![Linux system 存储设备的管理_第2张图片](http://img.e-com-net.com/image/info8/aa76f6e4e82941a494cd0b1ef617cff3.jpg)
2)系统发现的,但没有投入使用,随时可以使用的设备
blkid #查看设备的id信息
![](http://img.e-com-net.com/image/info8/487c930d5b274cacbe35ee4498d54ac4.jpg)
3)发现并且在使用的设备
![Linux system 存储设备的管理_第3张图片](http://img.e-com-net.com/image/info8/f4afdd90831846ad8cccc7a0a32ea16e.jpg)
df -h
![Linux system 存储设备的管理_第4张图片](http://img.e-com-net.com/image/info8/aca41fc3f3a24c91af654b95d0d92b4e.jpg)
df -H
![Linux system 存储设备的管理_第5张图片](http://img.e-com-net.com/image/info8/ce9c604cbef5476a9e509e157d295c0f.jpg)
2.设备名称的读取
/dev/xd*
x=s /dev/sd* #sata硬盘,或者iscsi网络存储
x=v /dev/vd* #虚拟硬盘,一般出现在虚拟机里
x=h /dev/hd* #ide硬盘,一般出现在老式电脑
/vde/sda1 #系统中第一个sata硬盘的第一个分区
*=a。。。。 # /dev/vda,系统中第一块虚拟硬盘
/dev/cdrom /dev/sr[0-...] #系统中的光驱
/dev/mapper/* #系统中的虚拟设备
![](http://img.e-com-net.com/image/info8/85e3b304de7c4937a2be74977cc5d5f3.jpg)
3.设备的使用
设备必须要用目录来对设备中的内容进行读取
所以设备在使用时需要作挂载动作
#设备挂载
blkid #识别可用设备
mount #设备|挂在点
u盘 ====>/dev/sdb1
mount /dev/sdb1 /mnt #把系统中第二块硬盘的第一个分区挂载道mnt下
![](http://img.e-com-net.com/image/info8/ed900515b6ca4146999102a76fc69da6.jpg)
![](http://img.e-com-net.com/image/info8/344005d3798848149a07e62460e8060b.jpg)
mount -o ro /dev/sdb1 /mnt/ #只读挂载
![](http://img.e-com-net.com/image/info8/569f52383d984edaa39d3829972446e8.jpg)
![Linux system 存储设备的管理_第6张图片](http://img.e-com-net.com/image/info8/81cb3b780be6429b858ae2b974824c46.jpg)
mount -o remount,rw /mnt/ #rw:读写 对设备的挂载进行在线更改
#设备卸载
umount #设备|挂载点
注意:当卸载设备时出现下列情况
[root@foundation15 mnt]# umount /dev/sdb1
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
表示设备正在被某个进程使用,处理方式:
#进程发现的方式
fuser -vm /dev/sdb1
![](http://img.e-com-net.com/image/info8/5c691a34516d41848666bb14f5135768.jpg)
lsof /dev/sdb1
![](http://img.e-com-net.com/image/info8/fac4fb55b1d64a5dbebe8550280da63a.jpg)
#进程终止的方式
kill -9 pid
fuser -kvm /dev/sdb1
4.分区管理
设备分区信息
1)mbr主引导记录446个字节 #代码最短写446个字节;让磁头去找并读取启动分区
2)mpt主分区表64个字节
3)硬盘的有效性标识“55aa”2个字节 #446+64+2=512
4)一个主分区占用16个字节记录分区信息
5)一块硬盘上如果用mbr的分区方式最多可以存在4个主分区
6)主分区 #一个磁盘存在3个主分区后,需要将剩下的磁盘容量放到一个制定容器中
7)扩展分区 这个容器叫做逻辑分区(LPAR),它所在的分区叫做扩展分区
8)逻辑分区 扩展分区可分为若干个逻辑分区
#分区的划分
fdisk -l
cat /proc/partitions #查看设备是否被系统识别
![Linux system 存储设备的管理_第7张图片](http://img.e-com-net.com/image/info8/a34da4b103614e45aa4e2e0a60d9dfdc.jpg)
[root@server ~]# fdisk /dev/vdb
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 0x1c189d1b.
Command (m for help): n #创建分区
Partition type: #创建分区类型
p primary (0 primary, 0 extended, 4 free) 主分区
e extended 扩展分区
Select (default p):
Using default response p
Partition number (1-4, default 1): #确定主分区id
First sector (2048-20971519, default 2048): #分区起始块的位置,用默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1000M
Partition 1 of type Linux and of size 1000 MiB is set
Command (m for help): wq #保存分区策略并退出fdisk界面
The partition table has been altered!
![Linux system 存储设备的管理_第8张图片](http://img.e-com-net.com/image/info8/dd5ff0c983174854b93e06b58d8ea5c7.jpg)
mkfs.xfs /dev/vdb1 #格式化设备,在设备上安装文件系统xfs
![](http://img.e-com-net.com/image/info8/7a5746535e9d4095af918e3bae8de8de.jpg)
![Linux system 存储设备的管理_第9张图片](http://img.e-com-net.com/image/info8/6a14d21548e440f1adb4ae99d32391b6.jpg)
blkid #查看可用设备,可以看到被格式化好的/dev/vdb1
![](http://img.e-com-net.com/image/info8/5a45fa7167bf431a9ff93d9b03bf77db.jpg)
mount /dev/vdb1 /mnt #挂载设备,使设备投入使用
![](http://img.e-com-net.com/image/info8/9446a57571554f81b33f05ea41d7a9d9.png)
#设备永久挂载
vim /etc/fstab #设备挂载策略文件
设备 挂载点 文件系统类型 挂载参数 是否备份设备 是否检测设备
/dev/vdb1 /westos xfs defaults 0 0
![](http://img.e-com-net.com/image/info8/d6c3e837a53e4e10a0edc73ec97abcf0.jpg)
mount -a #重置
![](http://img.e-com-net.com/image/info8/c6310a7852814255b96db7b3f7ec0a3f.jpg)
![Linux system 存储设备的管理_第10张图片](http://img.e-com-net.com/image/info8/27813fec467e42b9a6303fce9e553fb2.jpg)
#删除添加的分区
#umount /dev/vdb1
删除vim /etc/fstab 里面的文件
![](http://img.e-com-net.com/image/info8/e89a03c10da14731bf1dc8014e187385.jpg)
![](http://img.e-com-net.com/image/info8/58372357dd9e4b9e9e82223190512c23.jpg)
[root@server ~]# fdisk /dev/vdb
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): d #删除分区
Selected partition 1
Partition 1 is deleted
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
![Linux system 存储设备的管理_第11张图片](http://img.e-com-net.com/image/info8/3c491fb9c60c4fde8d81468c8828c01a.jpg)
#[root@server ~]# df #查看设备是否成功卸载
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3286664 7187236 32% /
devtmpfs 481120 0 481120 0% /dev
tmpfs 496708 84 496624 1% /dev/shm
tmpfs 496708 13060 483648 3% /run
tmpfs 496708 0 496708 0% /sys/fs/cgroup
#[root@server ~]# cat /proc/partitions
major minor #blocks name
253 0 10485760 vda
253 1 10484142 vda1
253 16 10485760 vdb
![Linux system 存储设备的管理_第12张图片](http://img.e-com-net.com/image/info8/da20a159e9b44ad6aed14921be9aba06.jpg)
5.swap分区的管理
swap交换分区,系统自己使用,不需要挂载
1)swap分区的查看
swapon -s
2)swap分区的建立
fdisk /dev/vdb #建立一个分区
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +10000M
Partition 1 of type Linux and of size 9.8 GiB is set
Command (m for help): wq
The partition table has been altered!
fdisk /dev/vdb1 #修改分区为swap分区
Command (m for help): t
Selected partition 1
![](http://img.e-com-net.com/image/info8/fc1642c96d7843deadbd853b12609a82.jpg)
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): wq
The partition table has been altered!
![Linux system 存储设备的管理_第13张图片](http://img.e-com-net.com/image/info8/399a218a1ab64351b92480e811ac867a.jpg)
mkswap /dev/vdb1 #格式化设备为swap系统文件
![](http://img.e-com-net.com/image/info8/c2adebcb267b46308df2a9701d73992d.jpg)
swapon -a /dev/vdb1 #激活swap设备,是系统利用此设备
swapon -s #查看已激活设备
![](http://img.e-com-net.com/image/info8/abe4e98d743c4b55966619b9d8845007.jpg)
[root@server ~]# vim /etc/fstab #修改配置文件,开机自动激活
/dev/vdb1 swap swap defaults 0 0
![](http://img.e-com-net.com/image/info8/aefb6cf0de4648b9a13d4164113d06f1.jpg)
#当磁盘全部被占用,不能创建新分区时,可以用文件来代替分区
[root@server ~]# dd if=/dev/zero of=/swapfile bs=1M count=1000
![Linux system 存储设备的管理_第14张图片](http://img.e-com-net.com/image/info8/644e197202d14fc2af0050ffc2e2de97.jpg)
[root@server ~]# vim /etc/fstab #修改配置文件
/swapfile swap swap defaults 0 0
![](http://img.e-com-net.com/image/info8/91f007992ac64a1bb720417c20098be2.jpg)
#此文件创建后的步骤和设备的步骤一致
![](http://img.e-com-net.com/image/info8/896459fd8ca342288129da01cb27ae7f.jpg)
#swap分区的删除
[root@server ~]# vim /etc/fstab #删除配置文件
![](http://img.e-com-net.com/image/info8/4cabbbef8fd3430db718d945268152e3.jpg)
swapoff /dev/vdb1
swapoff /swapfile
![](http://img.e-com-net.com/image/info8/58bb94880b0540a3bf9de45c9d1722de.jpg)
fdisk /dev/vdb
![](http://img.e-com-net.com/image/info8/889a6eaec89d4651be5039048d27af63.png)
rm -fr /swapfile
6.磁盘配额
#为磁盘用户分配额度
#分区配额时是针对于设备的
1.
mount -o usrquota /dev/vdb1 /redhat/
chmod 777 /redhat/
![](http://img.e-com-net.com/image/info8/7e654ff8702b418bbd2922855d8bb548.jpg)
edquota -u student /dev/vdb1
![](http://img.e-com-net.com/image/info8/6928ab5dee8f474997e658985c3c0eb5.jpg)
切换用户su - student
dd if=/dev/zero of=/redhat/file bs=1M count=100
vim /etc/fstab #开机自动激活配额
#分区方式修改
#1.mbr-------->gpt
[root@server ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel #修改设备分区方式标签
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? gpt #把原来的dos格式改为gpt格式
Warning: The existing disk label on /dev/vdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted) quit
Information: You may need to update /etc/fstab.
修改成功后的建立的磁盘的格式为gpt
![Linux system 存储设备的管理_第15张图片](http://img.e-com-net.com/image/info8/1d7f6e90e17e4d01bc1548c9efefef7f.jpg)
#2.gpt-------->mbr
[root@server ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type?
aix amiga bsd dvh gpt loop mac msdos pc98 sun
New disk label type? msdos #把gpt各格式改为dos格式
Warning: The existing disk label on /dev/vdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? Yes
(parted) quit
Information: You may need to update /etc/fstab.
修改成功后的建立的磁盘的格式为dos
![](http://img.e-com-net.com/image/info8/7a5746535e9d4095af918e3bae8de8de.jpg)
partprobe #同步并检错
说明:mbr格式存储一般为2TB,而gpt存储为18EB,由于一般企业所需存储u较大,故需要分区类型转换
7.分区加密
1)建立分区
2)cryptsetup luksFormat /dev/vdb1 #加密
![Linux system 存储设备的管理_第16张图片](http://img.e-com-net.com/image/info8/c5cf6535e72048428da50f4614318c74.png)
注意:输入yes为大写的YES
3)cryptsetup open /dev/vdb1 redhat #打开加密
![](http://img.e-com-net.com/image/info8/5d38d1a162fd4f70aecf747be0669ecf.png)
4)mkfs.xfs /dev/mapper/redhat #格式化
![Linux system 存储设备的管理_第17张图片](http://img.e-com-net.com/image/info8/e425a95af94045b7ad001145eb85704a.jpg)
5)mount /dev/mapper/redhat /mnt/ #挂载
touch /mnt/file #建立文件;现在可查看文件内容
![](http://img.e-com-net.com/image/info8/fc1a52fa1f5a4061ae225589c715c2e5.png)
6)umount /mnt #卸载
7)cryptsetup close redhat #加密;文件不仅不显示,而且无法查看
![](http://img.e-com-net.com/image/info8/21bde036bf914f70bcd657436e5ab2b4.jpg)
想要再次查看文件,进行挂载后打开加密即可
#加密设备开机自动挂载
1.vim /etc/fstab
/dev/mapper/redhat /mnt xfs defaults 0 0
![](http://img.e-com-net.com/image/info8/ab700ba7faf24d3db8ed075bbd286a17.jpg)
2.vim /etc/crypttab
redhat /dev/vdb1 /root/passfile
![](http://img.e-com-net.com/image/info8/5d398d2e11b14dfc816123b40251c5fd.png)
3.vim /root/passfile #添加所设置的加密密码
redhat2017
![](http://img.e-com-net.com/image/info8/3dabf79ff8df428fba84efe3be58dc5e.jpg)
chmod 600 /root/passfile #修改权限,提高安全性
![](http://img.e-com-net.com/image/info8/f0e659491c2a43749071674b39ed40cb.png)
4.cryptsetup luksAddKey /dev/vdb1 /root/passfile
![](http://img.e-com-net.com/image/info8/1c6e838116804fc4994237b3b2d02a80.jpg)
开启加密性并要求输入密码
#加密的清除
[root@server ~]# umount /dev/mapper/redhat
[root@server ~]# cryptsetup close redhat
[root@server ~]# mkfs.xfs /dev/vdb1 -f #格式化后文件内信息被清除
#清除开机自动挂载
[root@server ~]# vim /etc/fstab
[root@server ~]# vim /etc/crypttab #删除上列两个配置文件内信息
[root@server ~]# rm -rf /root/passfile