linux中磁盘的管理(挂载,分区,配额,加密)

  1. 区分概念

    磁盘:磁盘就是常见的物理磁盘,固态硬盘等。

    文件系统:文件系统是在硬盘上保持跟踪所有文件和目录的一组结构如果没有存储数据的文件系统,信息将会混乱。

    分区:硬盘在使用之前需要进行分区,分区是将磁盘空间分成多个存储空间,每一个子空间成为一个分区,不同的分区可以格式化为不同的文件系统。
    格式化:格式化就是创建文件系统,的mkfs的命令用于将分区格式化为特定的文件系统类型。

    挂载:就是将某一空间(比如目录,格式化后的文件系统)挂载到挂载点中而挂载点就是进入文件系统的入口。

  2. 区别
    文件系统和磁盘

    联系:文件系统和磁盘都可以用来指存储空间不同的是文件系统是指定好了存储文件的存储格式,而磁盘则是空白的存储空间,没有经历过任何处理。

    分区和文件系统
    联系:分区根据不同的功能场景,划分出不同的逻辑单元.Linux系统中,分区主要包括主分区,扩展分区和逻辑分区。文件系统是在分区的基础上的进步操作,分区操作完成后,需要将新的分区通过某种文件系统进行格式化操作,是的分区具备存储文件的功能。
    格式化文件系统的具体内容:

             指定块的大小:磁盘盘被格式化需要指定每一个块的大小,然后格式化命令会把空间分割成一个一个的块块。
    

4.挂载是什么?
挂载是将某个文件系统挂载到根目录下的一个子目录中,通过该子目录可以访问文件系统中的内容。

1、本地存储设备的识别

fdisk -l #查看真实存在的设备
在这里插入图片描述

cat /proc/partitions #查看系统可识别的设备

在这里插入图片描述

blkid # 查看系统可使用的设备
在这里插入图片描述
2】设备的挂载和卸载
设备名称

/dev/xdx   dev/vda  /dev/vdb
/dev/sr0       #光驱
/dev/mapper    #虚拟设备

在插入u盘之前用df查看有无正在正在挂载的设备

插入u盘后再次使用df查看系统挂载的设备,多出来的就是u盘设备。

设备的挂载
格式:mount 设备 挂载点

   mount  /dev/sdc1     /mnt        #挂载sdc1 到/mnt
   umount   /mnt                    #卸载

1

mount   -o  ro  /dev/sdc1  /mnt    #只读挂载 
  mount                             #查看挂载信息

解决设备正忙情况
有程序正在占用该设备,无法卸载。

解决方法1
lsof /mnt # 查看占用该设备的进程信息
kill -9 PID #结束该占用进程
umount /mnt # 再次卸载设备

解决方法2
fuser -kvm /mnt #直接结束占用该设备的进程
umount /mnt # 再次卸载设备

3、MBR 主引导记录

我们将包含磁盘中包含MBR引导代码的扇区(0磁道1扇区)称为主引导扇区。主引导扇区由三个部分组成(共占用512个字节):

主引导程序即主引导记录(MBR),占446个字节。它用于硬盘启动时将系统控制转给用户指定的某个操作系统。
磁盘分区表项,共计64个字节。负责说明磁盘上的分区情况,每一个分区信息占用16个字节。一个磁盘可以最多直接划分成四个分区。如果需要划分更多的分区,可以将一个主分区替换成扩展分区,再在扩展分区中划分逻辑分区。
主分区:分区信息记录在分区表中的分区。
扩展分区:除过主分区占用的磁盘空间之外,为了划分更多的分区,将剩余的空间做成统一的容器,成为扩展分区。扩展分区不能直接使用。
逻辑分区:在扩展分区中再次划分出来的分区,成为逻辑分区。
结束标志(占2个字节)55AA

目前设备只有一块磁盘 添加sdb磁盘,关机菜单栏内找到虚拟机设置,点击添加一块磁盘(大小可拟)

1 分区步骤

添加分区

[root@localhost ~]# fdisk -l

Disk /dev/vda: 8589 MB, 8589934592 bytes, 16777216 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: 0x00044471

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048      411647      204800   83  Linux
/dev/vda2          411648     1435647      512000   82  Linux swap / Solaris
/dev/vda3         1435648    16777215     7670784   83  Linux

Disk /dev/vdb: 10.7 GB, 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

对vdb磁盘进行分区

键入 fdisk /dev/vdb
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          ##修改分区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): n   新建
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p    选择主分区
Partition number (1-4, default 1): 1      #选择id 一般情况用默认
First sector (2048-20971519, default 2048):       #分区起始默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M   #分区大小
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x77690cb1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux

Command (m for help): wq    #保存退出

partprobe 同步分区信息

当主分区已经存在3个但是还需要对剩余的空间进行划分时,会提示将一个主分区替换成扩展分区,然后在扩展分区中再次进行划分逻辑分区:

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x77690cb1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
显示已经有3个主分区
Command (m for help): n   新建
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e     在次新见分区提示默认分区
Selected partition 4
First sector (616448-20971519, default 616448):     #默认
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519):     #分区大小,默认剩余空间
Command (m for help): p

Disk /dev/vdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x77690cb1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended

显示已经有3个主分区一个扩展分区
Command (m for help): n
All primary partitions are in use
Adding logical partition 5      #系统不再询问建立分区类型 默认在扩展分区中建立逻辑分区
First sector (618496-20971519, default 618496): 
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +100M
Partition 5 of type Linux and of size 100 MiB is set

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x77690cb1
   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux

Command (m for help): wq    保存 推出
The partition table has been altered!

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

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux

partprobe 同步分区信息

系统中一个硬盘最多可以被识别16个设备 ,当划分的分区过多时,虽然存在但是系统不能识别超过十六个之外的设备:

cat /proc/partitions 查看系统识别的设备
在这里插入图片描述

5、给设备安装文件系统

文件系统是操作系统用于明确存储设备或分区上的文件的方法和数据结构;操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统。刚划分出来的设备不能直接使用,只有在安装了文件系统之后才能使用。

文件系统的类型
在这里插入图片描述
安装文件系统

mkfs.xfs     /dev/vdb1      #格式化设备并安装文件类型xfs
mount  /dev/vdb1    /mnt    #挂载安装文件类型后的设备

在这里插入图片描述
设置开机自动安装文件系统
编辑文件:vim /etc/fstab
mount -a

在这里插入图片描述

挂载参数      是否备份    是否检测
defauls        0           0

在这里插入图片描述

6、swap分区管理

Swap分区在系统的物理内存不够用的时候,把物理内存中的一部分空间释放出来,以供当前运行的程序使用。这些被释放的空间被临时保存到Swap分区中,等到要运行时再从Swap分区中恢复保存的数据到内存中。是保证系统正常运行的必要分区

mkswap /dev/vdb2           #将设备格式化成swap类型
swapon -a /dev/vdb 2       #添加swap分区
swapoff /dev/vdb2          # 移除swap分区
[root@localhost ~]# cat /proc/partitions    查看系统可以识别的设备
major minor  #blocks  name

 253        0    8388608 vda
 253        1     204800 vda1
 253        2     512000 vda2
 253        3    7670784 vda3
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
  11        0    3704832 sr0
[root@localhost ~]# blkid       #显示系统能够使用的设备
/dev/vda1: UUID="e5baefde-f9e9-44b7-a230-51ff3d984876" TYPE="xfs" 
/dev/vda2: UUID="35f9b7c8-68c3-4486-be92-e88e142e1aad" TYPE="swap" 
/dev/vda3: UUID="45db2d31-9e5f-4f7e-a6aa-360f73d8518f" TYPE="xfs" 
/dev/vdb1: UUID="5520bf0e-a3ea-4e35-8c1c-56bf6851c633" TYPE="xfs" 
/dev/sr0: UUID="2016-10-19-18-32-06-00" LABEL="RHEL-7.3 Server.x86_64" TYPE="iso9660" PTTYPE="dos" 
[root@localhost ~]# 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): t    修改分区id
Partition number (1,2, default 2): 2    #选择第二个设备
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): p   显示信息

Disk /dev/vdb: 10.7 GB, 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
Disk label type: dos
Disk identifier: 0x77690cb1

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   82  Linux swap / Solaris

Command (m for help): wq


partprobe 同步分区信息

mkswap /dev/vdb2 #格式化设备为swap类型

在这里插入图片描述

swapon -a /dev/vdb2  #安装设备扩展swap分区
swapon -s       # 查看安装到swap分区的设备

在这里插入图片描述
设置开机自动设置添加swap分区
编辑文件:vim /etc/fstab
在这里插入图片描述
在这里插入图片描述

7、磁盘的配额

mount -o usrquota /dev/vdb2 /mnt # 带有对用户的配额对分区进行挂载
在这里插入图片描述
mount 查看挂载
在这里插入图片描述

chmod 777 /mnt    给权限  (先挂载在给)

edquota -u westos /dev/vdb1      #给westos用户 配额

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
westos 用户配额为20M , 写入文件大雨20M时禁止写入 文件个数不受限制。

设置开机自动携带配额参数挂载分区
vim /etc/fstab #编辑配置文件
在这里插入图片描述
关机重启系统
测试配额效果

[root@localhost ~]# su - westos     切换用户 
[westos@localhost ~]$ cd /mnt
[westos@localhost mnt]$ dd if=/dev/zero of=/mnt/westosfile bs=1M count=15
                                                                              #每个包1M      填充15个即为15M
15+0 records in
15+0 records out
15728640 bytes (16 MB) copied, 0.0234536 s, 671 MB/s       #正常性完成
[westos@localhost mnt]$ ls
westosfile
[westos@localhost mnt]$ rm -fr sestosfile
[westos@localhost mnt]$ dd if=/dev/zero of=/mnt/westosfile bs=1M count=25
dd: error writing ‘/mnt/westosfile’: Disk quota exceeded    #借取25M出现报错
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0604319 s, 347 MB/s
[westos@localhost mnt]$ ls
westosfile
[westos@localhost mnt]$ du westos
du: cannot access ‘westos’: No such file or directory
[westos@localhost mnt]$ du westosfile
20480	westosfile
[westos@localhost mnt]$ 

8、磁盘加密

使用文件系统底层加密, 就是先给磁盘加密,在进行安装文件系统。
加密步骤

cryptsetup luksFormat /dev/vdb3 # 设置该分区为加密磁盘类型  ( 密码大于8个字符)

在这里插入图片描述

mount /dev/mapper/haha /mnt # 将打开的虚拟设备挂载
quotaon -uv /dev/vdb3
umount /mnt # 卸载
cryptsetup close haha # 关闭加密磁盘
在这里插入图片描述

[root@localhost ~]# cd /mnt  
[root@localhost mnt]# ls
file1  file2  file3  file4  file5  file6  file7  file8  file9
[root@localhost mnt]# cd 
[root@localhost ~]# umount /mnt           #卸载
[root@localhost ~]# cryptsetup close haha     #关闭加密盖
[root@localhost ~]# ls /dev/mapper/            #生成的虚拟设备消失
control

加密磁盘开机自动挂载
vim /etc/fstab
在这里插入图片描述
vim /etc/crypttab
在这里插入图片描述

vim /root/lalakey 密码
在这里插入图片描述
cryptsetup luksAddKey /dev/vdb3 /root/hahakey # 将放置密码的文件与加密设备进行绑定
开机重启查看加密磁盘自动挂载情况

删除加密设备
/etc/fstab #删除该配置文件中写入的内容

rm -fr /root/hahakey #删除编写的密码文件
mkfs.xfs /dev/vdb3#给磁盘添加xfs文件系统。

猜你喜欢

转载自blog.csdn.net/weixin_44818720/article/details/89435431