首页>软件资讯>常见问题

常见问题

红帽RedHat-Linux磁盘管理不要怕,看这篇文章铁定懂

发布时间:2022-12-08 14:26:01人气:1558

一、磁盘管理

1.磁盘接口类型

IDE/ATA:并行接口,有ATA-1到ATA-7版本,理论数据传输率33MB/s到133MB/s。已经淘汰了。


SATA接口:串行接口,分别有SATA 1.5Gbit/s、SATA 3Gbit/s和SATA 6Gbit/s三种规格。


SCSI:并行接口,小型计算机系统接口。


SAS接口:串行连接的SCSI。


FC:光纤通道SSD:固态存储单元


USB:U盘


2.磁盘设备文件命名

(1)以设备名命名

在 Linux 系统中,磁盘设备对应于系统中的特殊文件,这些特殊文件放在“ /dev”目录中,不同的设备对应的设备名称如下:

系统的第一块 IDE 接口的硬盘称为 /dev/hda。

系统的第二块 IDE 接口的硬盘称为 /dev/hdb。

系统的第一块 SCSI 接口的硬盘称为 /dev/sda。

系统的第二块 SCSI 接口的硬盘称为 /dev/sdb。


(2)使用数字编号为了表示不同的分区,通常会用数字进行编号如下:

系统的第一块 IDE 接口硬盘的第 1 个分区称为 /dev/hda1。

系统的第一块 IDE 接口硬盘的第 5 个分区称为 /dev/hda5。

系统的第二块 SCSI 接口硬盘的第 1 个分区称为 /dev/sdb1。

系统的第二块 SCSI 接口硬盘的第 5 个分区称为 /dev/sdb5。

等等命名。


需要注意的是,在对分区编号时,数字 1 ~ 4 只能留给主分区或扩展分区使用,逻辑分区(在扩展分区基础上建立)编号只能从 5 开始。在对 Linux 系统设置了分区之后,还要在分区上创建文件系统才能安装系统,这个在安装时由系统自行完成创建。


一块磁盘最多有四个主分区(或者三个主分区加上一个扩展分区)

所以分区命名法中第1-第4分区一定是主分区或者扩展分区

而第5分区之后则是逻辑分区


所以/dev/hdb5 代表的是第二块IDE设备的第一块逻辑分区

ATA,IDE: /dev/hda,/dev/hdb…

SCSI,SATA,USB,

SAS:/dev/sda,/dev/sdb…

不同磁盘设备文件命名: a-z

同一磁盘不同分区:/dev/sda1,/dev/sda5


3.磁盘查看:

查看磁盘fdisk \-l lsblk


[root@server ~]# fdisk -l

Disk /dev/nvme0n1: 20 GiB, 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

Disklabel type: dos

Disk identifier: 0x4066130f


Device         Boot   Start      End  Sectors Size Id Type

/dev/nvme0n1p1 *       2048  2099199  2097152   1G 83 Linux

/dev/nvme0n1p2      2099200 41943039 39843840  19G 8e Linux LVM



Disk /dev/mapper/rhel-root: 17 GiB, 18249416704 bytes, 35643392 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 /dev/mapper/rhel-swap: 2 GiB, 2147483648 bytes, 4194304 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

RHEL7.8


[root@server ~]# lsblk 

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda             8:0    0   50G  0 disk 

├─sda1          8:1    0    1G  0 part /boot

└─sda2          8:2    0   49G  0 part 

  ├─rhel-root 253:0    0   47G  0 lvm  /

  └─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

sr0            11:0    1  4.2G  0 rom  /run/media/root/RHEL-7.8 Server.x86_64

RHEL8.3


[root@server ~]# lsblk 

NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                   8:0    0   50G  0 disk 

├─sda1                8:1    0  600M  0 part /boot/efi

├─sda2                8:2    0    1G  0 part /boot

└─sda3                8:3    0 48.4G  0 part 

  ├─rhel_rhel8-root 253:0    0 46.4G  0 lvm  /

  └─rhel_rhel8-swap 253:1    0    2G  0 lvm  [SWAP]

sr0                  11:0    1  8.8G  0 rom  /run/media/root/RHEL-8-3-0-BaseOS-x86_64

系统识别的设备


[root@localhost ~]# cat /proc/partitions 

major minor  #blocks  name


 259        0   20971520 nvme0n1

 259        1    1048576 nvme0n1p1

 259        2   19921920 nvme0n1p2

  11        0    1048575 sr0

 253        0   17821696 dm-0

 253        1    2097152 dm-1

虚拟机不重启识别新添加磁盘:

echo "- \- \-" >/sys/class/scsi_host/host0/scan


二、分区

1.分区方式

MBR:Master Boot Record主引导记录分区表。

使用32表示扇区数,分区不超过2T 主分区+扩展分区 < = 4。

按柱面分区。


GPT:GUID patition table全局唯一标识分区表。

最大支持128个主分区。


主分区:也称为主磁盘分区,和扩展分区、逻辑分区一样,是一 种分区类型。主分区中不能再划分其它类型的分区。


扩展分区:主分区以外分区,不能直接使用,必须在扩展分区中划分若干逻辑分区,每块磁盘最多只可以划分一个扩展分区。

1.png


逻辑分区:从扩展分区中划分,可以直接使用。


分区编号:主分区和扩展分区的分区编号为1-4,逻辑分区的分区编号从5开始。


2.虚拟机添加硬盘

1.点击RHEL8.3 -> 虚拟机 -> 设置。

2.点击硬盘 -> 添加 -> 确定。

3.点击硬盘 -> 下一步。

4.点击SCSI -> 下一步。

5.点击创建新虚拟磁盘 -> 下一步。

6.磁盘大小改成50GB -> 下一步。

7.完成。


3.分区操作

使用fdisk parted分区操作。

fdisk默认是MBR分区方式,可以使用parted进行GPT分区。


4.fdisk分区操作

交互模式输入错了按alt+Backspace


[root@server ~]# fdisk /dev/sdb  //进入分区交互模式


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.


Device does not contain a recognized partition table.

Created a new DOS disklabel with disk identifier 0x9a66aab5.


Command (m for help): m   //RHEL8的获取帮助是m。RHEL7的获得帮助m或h。


Help:


  DOS (MBR)

   a   toggle a bootable flag   /切换可启动标志

   b   edit nested BSD disklabel  /

   c   toggle the dos compatibility flag


  Generic

   d   delete a partition    //删除分区

   F   list free unpartitioned space

   l   list known partition types  //列出分区类型

   n   add a new partition    //添加一个新分区

   p   print the partition table  //打印分区表

   t   change a partition type   //修改分区类型

   v   verify the partition table

   i   print information about a partition


  Misc

   m   print this menu

   u   change display/entry units

   x   extra functionality (experts only)


  Script

   I   load disk layout from sfdisk script file 

   O   dump disk layout to sfdisk script file


  Save & Exit

   w   write table to disk and exit  //保存分区信息并退出

   q   quit without saving changes  //不保存退出


  Create a new label

   g   create a new empty GPT partition table

   G   create a new empty SGI (IRIX) partition table

   o   create a new empty DOS partition table

   s   create a new empty Sun partition table



Command (m for help): 

fdisk /dev/sdb 进入分区交互模式

p 显示当前磁盘的分区

n 添加一个新分区

e 扩展分区

p 主分区

l 逻辑分区

d 删除一个分区

t 修改分区类型

l 显示所⽀支持的所有类型

w 保存退出

q 不保存退出


5.主分区操作

[root@server ~]# fdisk /dev/sdb  //进入MBR分区交互模式


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.


Device does not contain a recognized partition table.

Created a new DOS disklabel with disk identifier 0xc6feff16.


Command (m for help): n  //添加一个新分区

Partition type

   p   primary (0 primary, 0 extended, 4 free)

   e   extended (container for logical partitions)

Select (default p): p  //创建主分区

Partition number (1-4, default 1): 回车键  //分区编号,默认为1

First sector (2048-104857599, default 2048): 回车键 //分区的起始扇区

Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): +15G  //结束扇区,可直接写+size{K,M,G,T,P}


Created a new partition 1 of type 'Linux' and of size 15 GiB.


Command (m for help): p  //打印分区表

Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x17417341


Device     Boot Start      End  Sectors Size Id Type

/dev/sdb1        2048 31459327 31457280  15G 83 Linux


Command (m for help):

6.扩展分区

Command (m for help): n  //添加一个新分区

Partition type

   p   primary (1 primary, 0 extended, 3 free)

   e   extended (container for logical partitions)

Select (default p): e  //创建扩张分区

Partition number (2-4, default 2): 回车键  //分区编号,默认为2

First sector (31459328-104857599, default 31459328): 回车键 //分区的起始扇区

Last sector, +sectors or +size{K,M,G,T,P} (31459328-104857599, default 104857599): 回车键 //结束扇区不写,直接回车键表示将剩余所有空间都划分给这个分区


Created a new partition 2 of type 'Extended' and of size 35 GiB.


Command (m for help): p  //打印分区表

Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x17417341


Device     Boot    Start       End  Sectors Size Id Type

/dev/sdb1           2048  31459327 31457280  15G 83 Linux

/dev/sdb2       31459328 104857599 73398272  35G  5 Extended


Command (m for help):  

7.逻辑分区操作

Command (m for help): n  //添加一个新分区

All space for primary partitions is in use.

Adding logical partition 5

First sector (31461376-104857599, default 31461376): 回车键

Last sector, +sectors or +size{K,M,G,T,P} (31461376-104857599, default 104857599): +20G //结束扇区,可直接写+size{K,M,G,T,P}


Created a new partition 5 of type 'Linux' and of size 20 GiB.


Command (m for help): p  //打印分区表

Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x17417341


Device     Boot    Start       End  Sectors Size Id Type

/dev/sdb1           2048  31459327 31457280  15G 83 Linux

/dev/sdb2       31459328 104857599 73398272  35G  5 Extended

/dev/sdb5       31461376  73404415 41943040  20G 83 Linux


Command (m for help): w  //保存退出

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.


8.刷新分区

[root@server ~]# partprobe  //刷新分区

Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

[root@server data]# ls /dev/sda* //查看分区文件

/dev/sda  /dev/sda1  /dev/sda2  /dev/sda3


9.删除分区

[root@server ~]# fdisk /dev/sdb  //进入分区交互模式


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): d  //删除分区

Partition number (1,2,5, default 5): 1  //删除的分区号


Partition 1 has been deleted.


Command (m for help): p  //打印分区表

Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x84cfbc05


Device     Boot    Start       End  Sectors Size Id Type

/dev/sdb2       31459328 104857599 73398272  35G  5 Extended

/dev/sdb5       31461376  73404415 41943040  20G 83 Linux


Command (m for help): w  //保存退出

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.


[root@server ~]# 


10.parted分区操作

在添加一块硬盘步骤省略。


[root@server ~]# parted /dev/sdc  //进入GPT分区交互模式

GNU Parted 3.2

Using /dev/sdc

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) mklabel gpt                                                      

(parted) print                                                            

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 53.7GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags: 


Number  Start  End  Size  File system  Name  Flags


(parted) mkpart print 0k 20g  //划分一个20G的主分区 

Warning: You requested a partition from 0.00B to 20.0GB (sectors 0..39062500).

The closest location we can manage is 17.4kB to 20.0GB (sectors 34..39062500).

Is this still acceptable to you?

Yes/No? y  //是

Warning: The resulting partition is not properly aligned for best performance: 34s % 2048s !=

0s

Ignore/Cancel? i      //忽略                                                    

(parted) mkpart print 20g -1  //将剩下的空间分一个主分区

(parted) p        //打印分区查看信息                                   

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 53.7GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags: 


Number  Start   End     Size    File system  Name   Flags

 1      17.4kB  20.0GB  20.0GB               print

 2      20.0GB  53.7GB  33.7GB               print


(parted) rm 1      //删除分区1                                             

(parted) p         //打印分区查看信息                                                       

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 53.7GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags: 


Number  Start   End     Size    File system  Name   Flags

 2      20.0GB  53.7GB  33.7GB               print


(parted) quit //退出

Information: You may need to update /etc/fstab.


[root@server ~]# lsblk  //查看分区信息                    

NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                   8:0    0   50G  0 disk 

├─sda1                8:1    0  600M  0 part /boot/efi

├─sda2                8:2    0    1G  0 part /boot

└─sda3                8:3    0 48.4G  0 part 

  ├─rhel_rhel8-root 253:0    0 46.4G  0 lvm  /

  └─rhel_rhel8-swap 253:1    0    2G  0 lvm  [SWAP]

sdb                   8:16   0   50G  0 disk 

├─sdb2                8:18   0    1K  0 part 

└─sdb5                8:21   0   20G  0 part 

sdc                   8:32   0   50G  0 disk 

└─sdc2                8:34   0 31.4G  0 part 

sr0                  11:0    1  8.8G  0 rom  


三、创建文件系统并格式化

1.文件系统

文件系统是一种存储和组织计算机数据的方法,文件系统种类有 很多,每一种文件系统的结构、逻辑、大小、安全性、存取效率 都不一样。


Windows Linux

FAT16,FAT32,NTFS,exFAT EXT2,EXT3,EXT4,XFS,BTRFS

mkfs \-t ext4 \-m 3 \-b size=1024 /dev/sdb1

-t 指定文件系统类型

-m 指定管理块所占百分比,默认5%

-b 指定块大小,默认4096byte


2.格式分区

[root@server ~]# mkfs.ext4 /dev/sdb5  //把sdb5格式成xfs格式

mke2fs 1.45.6 (20-Mar-2020)

Creating filesystem with 5242880 4k blocks and 1310720 inodes

Filesystem UUID: 0cec4dfd-5654-4bf6-9ce5-2fe23664bbe0

Superblock backups stored on blocks: 

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 

        4096000


Allocating group tables: done                            

Writing inode tables: done                            

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done   

 

3.查看分区类型信息

[root@server ~]# blkid /dev/sdb5  //查看分区类型信息

/dev/sdb5: UUID="fd80396a-db08-42ac-a6c1-2a2b001f33d3" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="93c2580b-05"

必须加上-f参数来覆盖它。


[root@server ~]# mkfs.xfs -f /dev/sdb5  //覆盖ext4,格式化成xfs

meta-data=/dev/sdb5              isize=512    agcount=4, agsize=1310720 blks

         =                       sectsz=512   attr=2, projid32bit=1

         =                       crc=1        finobt=1, sparse=1, rmapbt=0

         =                       reflink=1

data     =                       bsize=4096   blocks=5242880, 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

[root@server ~]# blkid /dev/sdb5

/dev/sdb5: UUID="4681b058-6c00-435c-a704-ed88f398178f" BLOCK_SIZE="512" TYPE="xfs" PARTUUID="93c2580b-05"


4.创建交换分区

swap分区一般是内存的两倍。

创建sdb6,步骤省略。


修改分区类型标记


[root@server ~]# fdisk /dev/sdb


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): t

Partition number (1,2,5,6, default 6):回车键 

Hex code (type L to list all codes): l //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  //找到Linux swap / So分区类型,输入82


Changed type of partition 'Linux' to 'Linux swap / Solaris'.


Command (m for help): p  

Disk /dev/sdb: 50 GiB, 53687091200 bytes, 104857600 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: 0x93c2580b


Device     Boot    Start       End  Sectors Size Id Type

/dev/sdb2       41945088 104857599 62912512  30G  5 Extended

/dev/sdb5       41947136  83890175 41943040  20G 83 Linux

/dev/sdb6       83892224  92280831  8388608   4G 82 Linux swap / Solaris


Command (m for help): w

The partition table has been altered.

Calling ioctl() to re-read partition table.

Syncing disks.


5.查看交换分区

[root@server ~]# swapon -s  //查看交换分区

Filename                                Type            Size    Used    Priority

/dev/dm-1                               partition       2129916 0       -2


6.创建swap分区

[root@server ~]# mkswap /dev/sdb6 //格式化swap分区

Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)

no label, UUID=d0d84a34-9a6a-457d-a3b2-6cd7be4960f5


7.启用交换分区

[root@server ~]# swapon /dev/sdb6   //启用交换分区

[root@server ~]# swapon -s

Filename                                Type            Size    Used    Priority

/dev/dm-1                               partition       2129916 0       -2

/dev/sdb6                               partition       4194300 0       -3


8.关闭交换分区

[root@server ~]# swapoff /dev/sdb6


四、磁盘挂载临时挂载

挂载设备其实就是给设备提供一个访问入口。

mount 设备名 挂载点设备名


设备名字可以是UUID或/dev/sda5。


挂载点:


目录必须存在,且未被其他进程使用。

目录中原有文件会暂时隐藏。

mount显示当前系统已经挂载的设备和挂载点。

挂载配置文件/etc/fstab

mount options -o options DEVICE MOUNT_POINT

-a 挂载/etc/fstab 中定义的所有文件系统(重新读下/etc/fstab文件)

-r 只读挂载

-w 读写挂载

-o 额外指定挂载选项

remount 重新挂载

ro

rw


永久挂载

开机自动挂载,需要把挂载信息写入到挂载表中/etc/fstab

参数格式

/dev/mapper/rhel_rhel8-root / xfs defaults 0 0

设备 挂载点 挂载类型 挂载参数 是否备份 检查顺序


挂载类型:

ext2 ext3 ext4 xfs iso9660 nfs smbfs swap auto


挂载参数:

defaults


是否备份:

0 不备份

1 使用dump工具制作备份


检查顺序:0|1|2

1 优先检查

0 不检查


1.挂载普通分区

[root@server ~]# mkdir /data

[root@server ~]# vim /etc/fstab

在最后添加上

/dev/sdb5 /data                       xfs     defaults        0 0

[root@server ~]# mount -a  //挂载成功是没有任何输出信息

[root@server ~]# lsblk

NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                   8:0    0   50G  0 disk 

├─sda1                8:1    0  600M  0 part /boot/efi

├─sda2                8:2    0    1G  0 part /boot

└─sda3                8:3    0 48.4G  0 part 

  ├─rhel_rhel8-root 253:0    0 46.4G  0 lvm  /

  └─rhel_rhel8-swap 253:1    0    2G  0 lvm  [SWAP]

sdb                   8:16   0   50G  0 disk 

├─sdb2                8:18   0    1K  0 part 

├─sdb5                8:21   0   20G  0 part /data

└─sdb6                8:22   0    4G  0 part [SWAP]

sdc                   8:32   0   50G  0 disk 

└─sdc2                8:34   0 31.4G  0 part 

sr0                  11:0    1  8.8G  0 rom 

挂载文件错误输出的信息可以查看


[root@server ~]# mount -a

mount: wrong fs type, bad option, bad superblock on /dev/sdb5,

missing codepage or helper program, or other error


In some cases useful info is found in syslog - try

dmesg | tail or so.

2.挂载swap分区:

[root@server ~]# mkdir /dev/mapper/rhel8-swap

[root@server ~]# vim /etc/fstab

在最后添加上

/dev/mapper/rhel8-swap none                     swap    defaults        0 0 

[root@server ~]# mount -a 

3.卸载挂载设备

umount DEVICE /


[root@server ~]# lsblk

├─sdb5                8:21   0   20G  0 part /data  //挂载点

[root@server ~]# umount /dev/sdb5  //卸载sdb5挂载data目录

├─sdb5                8:21   0   20G  0 part 

4.卸载设备时,挂载点下文件不能被其他进程占用

fuser /PATH 查看目录/文件当前被那些进程使用

-v 详细显示

-km 结束使用目录/文件的进程


[root@server ~]# fuser -kvm /data  //结束目录占有进程

5.查看磁盘分区使用情况

df

-h 以标准单位显示

-l 不换行显示

-T 显示文件系统类型


[root@server ~]# df -Th  //查看磁盘分区使用情况

Filesystem                  Type      Size  Used Avail Use% Mounted on

devtmpfs                    devtmpfs  865M     0  865M   0% /dev

tmpfs                       tmpfs     895M     0  895M   0% /dev/shm

tmpfs                       tmpfs     895M  9.5M  886M   2% /run

tmpfs                       tmpfs     895M     0  895M   0% /sys/fs/cgroup

/dev/mapper/rhel_rhel8-root xfs        47G  4.7G   42G  11% /

/dev/sda2                   xfs      1014M  235M  780M  24% /boot

/dev/sda1                   vfat      599M  6.9M  592M   2% /boot/efi

tmpfs                       tmpfs     179M  1.2M  178M   1% /run/user/42

tmpfs                       tmpfs     179M     0  179M   0% /run/user/0

6.查看目录文件大小

du

-s 查看目录大小

-h 标准单位显示


[root@server ~]# du -sh /etc/  //查看目录大小显示标准单位

30M     /etc/


五、自动挂载:autofs

autofs服务程序是一种Linux系统守护进程,当检测到用户试 图访问一个尚未挂载的文件系统时,将自动挂载该文件系统 将挂载信息填入/etc/fstab文件后,每次开机的时候,无论挂载需求是否需要,设备将会被自动挂载,而autofs服务程序

则是在用户需要使用该文件系统时才去动态挂载,从而节约了网 络资源和服务器的硬件资源。

autofs多用于NFS和samba这种基于网络的挂载。


1.安装服务程序

[root@server ~]# dnf install -y autofs

2.编辑主配置文件

[root@server ~]# vim /etc/auto.master

#

# Sample auto.master file

# This is a 'master' automounter map and it has the following format:

# mount-point [map-type[,format]:]map [options]

# For details of the format look at auto.master(5).

#

/misc   /etc/auto.misc


/data   /etc/auto.sdb // /data自动挂载点的目标位置,/etc/auto.sdb定义子配置文件。

省略

3.编辑子配置文件:

[root@server ~]# vim /etc/auto.sdb

*       -fstype=xfs     :/dev/sdb5

* 挂载点目录(这里表示可以任意常见文件名作为挂载点目录)

-fstype=xfs 挂载的类型

:/dev/sdb5 需要挂载的设备


4.开启并重启autofs服务

[root@server ~]# systemctl enable autofs.service //开机自启动autofs

Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service.

[root@server ~]# systemctl restart autofs.service //重新启动autofs

5.创建标签,激活本地挂载

[root@server ~]# cd /data/  //切换目录到data

[root@server data]# touch 1  //创建1文件

6.查看设备挂载状态:

[root@server data]# lsblk  //列出块设备信息

NAME                MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda                   8:0    0   50G  0 disk 

├─sda1                8:1    0  600M  0 part /boot/efi

├─sda2                8:2    0    1G  0 part /boot

└─sda3                8:3    0 48.4G  0 part 

  ├─rhel_rhel8-root 253:0    0 46.4G  0 lvm  /

  └─rhel_rhel8-swap 253:1    0    2G  0 lvm  [SWAP]

sdb                   8:16   0   50G  0 disk 

├─sdb2                8:18   0    1K  0 part 

├─sdb5                8:21   0   20G  0 part /data/1

└─sdb6                8:22   0    4G  0 part [SWAP]

sdc                   8:32   0   50G  0 disk 

└─sdc2                8:34   0 31.4G  0 part 

sr0                  11:0    1  8.8G  0 rom  /media



上一条:红帽 RHEL 9.1 发布,下一代伟大的企业级操作系统

下一条:红帽企业Linux 9.1版本