Linux system 存储设备的管理

来源:互联网 发布:nc.windows.app.23787 编辑:程序博客网 时间:2024/06/05 00:27

                                存储设备的管理



1.设备的查看

1)发现系统中的设备

fdisk -l


cat /proc/partitions #能够发现设备,但设备不一定能使用


2)系统发现的,但没有投入使用,随时可以使用的设备

blkid #查看设备的id信息


3)发现并且在使用的设备


df -h



df -H



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/* #系统中的虚拟设备



3.设备的使用

设备必须要用目录来对设备中的内容进行读取
所以设备在使用时需要作挂载动作

#设备挂载

blkid          #识别可用设备
mount            #设备|挂在点
u盘 ====>/dev/sdb1

mount /dev/sdb1 /mnt                       #把系统中第二块硬盘的第一个分区挂载道mnt下


mount -o ro /dev/sdb1 /mnt/ #只读挂载


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


lsof /dev/sdb1



#进程终止的方式
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 #查看设备是否被系统识别


[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!




mkfs.xfs /dev/vdb1    #格式化设备,在设备上安装文件系统xfs



blkid      #查看可用设备,可以看到被格式化好的/dev/vdb1


mount /dev/vdb1 /mnt       #挂载设备,使设备投入使用




#设备永久挂载


vim /etc/fstab    #设备挂载策略文件

设备 挂载点  文件系统类型  挂载参数  是否备份设备  是否检测设备

/dev/vdb1 /westos xfs defaults 0 0



mount -a #重置



#删除添加的分区

#umount /dev/vdb1

删除vim /etc/fstab 里面的文件


[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.


#[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





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


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!


mkswap /dev/vdb1             #格式化设备为swap系统文件


swapon -a /dev/vdb1             #激活swap设备,是系统利用此设备

swapon -s             #查看已激活设备


[root@server ~]# vim /etc/fstab            #修改配置文件,开机自动激活

/dev/vdb1 swap swap defaults 0 0



#当磁盘全部被占用,不能创建新分区时,可以用文件来代替分区

[root@server ~]# dd if=/dev/zero of=/swapfile bs=1M count=1000


[root@server ~]# vim /etc/fstab     #修改配置文件

/swapfile swap swap defaults 0 0

#此文件创建后的步骤和设备的步骤一致



#swap分区的删除

[root@server ~]# vim /etc/fstab #删除配置文件


swapoff /dev/vdb1

swapoff /swapfile


fdisk /dev/vdb


rm -fr /swapfile




6.磁盘配额

#为磁盘用户分配额度
#分区配额时是针对于设备的

1.
mount -o usrquota /dev/vdb1 /redhat/

chmod 777 /redhat/


edquota -u student /dev/vdb1


切换用户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



#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


partprobe #同步并检错

说明:mbr格式存储一般为2TB,而gpt存储为18EB,由于一般企业所需存储u较大,故需要分区类型转换





7.分区加密

1)建立分区

2)cryptsetup luksFormat /dev/vdb1 #加密

注意:输入yes为大写的YES

3)cryptsetup open /dev/vdb1 redhat #打开加密


4)mkfs.xfs /dev/mapper/redhat #格式化


5)mount /dev/mapper/redhat /mnt/ #挂载

touch /mnt/file #建立文件;现在可查看文件内容


6)umount /mnt #卸载

7)cryptsetup close redhat #加密;文件不仅不显示,而且无法查看

想要再次查看文件,进行挂载后打开加密即可



#加密设备开机自动挂载

1.vim /etc/fstab

/dev/mapper/redhat /mnt xfs defaults 0 0


2.vim /etc/crypttab

redhat /dev/vdb1 /root/passfile


3.vim /root/passfile      #添加所设置的加密密码

redhat2017


chmod 600 /root/passfile       #修改权限,提高安全性


4.cryptsetup luksAddKey /dev/vdb1 /root/passfile

开启加密性并要求输入密码

#加密的清除

[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


原创粉丝点击