linux centos7 文件挂载学习记录

概念

我理解挂载就是将磁盘和目录关联起来,当然也可以目录和目录关联

使用

通过fdisk可以查看磁盘分区情况

fsdisk -l  

查询结果

[root@lazy ~]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes, 976773168 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: 0xdced7faf

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   100677631    50337792    7  HPFS/NTFS/exFAT
/dev/sda2       100677632   101701631      512000   83  Linux
/dev/sda3       101701632   654843903   276571136   8e  Linux LVM

Disk /dev/mapper/centos-root: 53.7 GB, 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


Disk /dev/mapper/centos-swap: 4026 MB, 4026531840 bytes, 7864320 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/centos-home: 214.7 GB, 214748364800 bytes, 419430400 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/centos-var: 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

可以看出我的机器上有一块硬盘/dev/sda ,500G大小,被分成了三个区,其中sda3被创建为LVM 物理卷,有4个逻辑卷。三一个区大小总和不到500G,说明还有可被分配的空间,使用fdisk进行分区

[root@lazy home]# fdisk /dev/sda
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): n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): p
Selected partition 4
First sector (654843904-976773167, default 654843904):
Using default value 654843904
Last sector, +sectors or +size{K,M,G} (654843904-976773167, default 976773167):
Using default value 976773167
Partition 4 of type Linux and of size 153.5 GiB is set

Command (m for help): p

Disk /dev/sda: 500.1 GB, 500107862016 bytes, 976773168 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: 0xdced7faf

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 100677631 50337792 7 HPFS/NTFS/exFAT
/dev/sda2 100677632 101701631 512000 83 Linux
/dev/sda3 101701632 654843903 276571136 8e Linux LVM
/dev/sda4 654843904 976773167 160964632 83 Linux

 命令说明:

  n                         # 新建分区
    -e                      # 创建扩展分区)
    -p                      # 创建主分区)
    -t                       # 改变分区类型)
  p                         # 查看分区
  w                         # 将信息写入磁盘

使用df 命令查看磁盘挂载情况

[root@lazy home]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  1.7G   49G   4% /
devtmpfs                 847M     0  847M   0% /dev
tmpfs                    859M     0  859M   0% /dev/shm
tmpfs                    859M   33M  826M   4% /run
tmpfs                    859M     0  859M   0% /sys/fs/cgroup
/dev/mapper/centos-home  200G   16G  184G   8% /home
/dev/mapper/centos-var    10G  8.1G  2.0G  81% /var
/dev/sda2                497M  130M  367M  27% /boot
tmpfs                    172M     0  172M   0% /run/user/0

创建目录/mnt/haha,将刚刚创建的分区sda4 挂载到该目录

[root@lazy /]# mkdir /mnt/haha
[root@lazy /]# mount /dev/sda4 /mnt/haha

发现报错mount: special device /dev/sda4 does not exist,找不到分区,查看/pro/partitions

[root@lazy /]# cat /proc/partitions
major minor  #blocks  name

  11        0    1048575 sr0
   8        0  488386584 sda
   8        1   50337792 sda1
   8        2     512000 sda2
   8        3  276571136 sda3
 253        0   52428800 dm-0
 253        1    3932160 dm-1
 253        2  209715200 dm-2
 253        3   10485760 dm-3

确实没有分区sda4,这时需要让内核重读磁盘分区表,可以通过partx工具让内核重读磁盘分区表

partx -d /dev/sda  //所有信息清零
partx -a /dev/sda  //添加分区信息
partx -s /dev/sda  //显示分区信息 

再进行挂载,报错

mount: unknown filesystem type '(null)'

格式化分区

mkfs -t ext2 /dev/sda4

 挂载不再报错,df -h 查看

[root@lazy haha]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  1.7G   49G   4% /
devtmpfs                 847M     0  847M   0% /dev
tmpfs                    859M     0  859M   0% /dev/shm
tmpfs                    859M   33M  826M   4% /run
tmpfs                    859M     0  859M   0% /sys/fs/cgroup
/dev/mapper/centos-home  200G   16G  184G   8% /home
/dev/mapper/centos-var    10G  8.1G  2.0G  81% /var
/dev/sda2                497M  130M  367M  27% /boot
tmpfs                    172M     0  172M   0% /run/user/0
/dev/sda4                152G   60M  144G   1% /mnt/haha
[root@lazy haha]#

可以利用umount取消挂载

[root@lazy haha]# umount /mnt/haha
[root@lazy haha]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  1.7G   49G   4% /
devtmpfs                 847M     0  847M   0% /dev
tmpfs                    859M     0  859M   0% /dev/shm
tmpfs                    859M   33M  826M   4% /run
tmpfs                    859M     0  859M   0% /sys/fs/cgroup
/dev/mapper/centos-home  200G   16G  184G   8% /home
/dev/mapper/centos-var    10G  8.1G  2.0G  81% /var
/dev/sda2                497M  130M  367M  27% /boot
tmpfs                    172M     0  172M   0% /run/user/0
[root@lazy haha]#

/etc/fstab和/etc/mtab

 /etc/fstab 记录了计算机上硬盘分区的相关信息,启动 Linux 的时候,检查分区的 fsck 命令,和挂载分区的 mount 命令,都需要 fstab 中的信息,来正确的检查和挂载硬盘。 所以可以在这个文件中加入挂载盘的信息,实现启动时挂载;

[root@lazy haha]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Sun Mar  6 22:36:43 2016
#
# 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
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=846e966f-11ed-4b3f-b7d4-ddfff975cb6a /boot                   xfs     defaults        0 0
/dev/mapper/centos-home /home                   xfs     defaults        0 0
/dev/mapper/centos-var  /var                    xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
[root@lazy haha]#

用第一行来说明各列代表的含义,

  • /dev/mapper/centeros-home 可以是设备名称、LABEL UUID 伪文件系统名称
  • /                                             挂载点,即指定的文件目录
  • xfs                                         文件系统类型
  • defaults                                 挂载选项
  • 0                                            存储频率  0 不做备份,1 每天转储,2  每隔一天转储
  • 0                                            自检次序  0 不自检,1 首先自检,一般只有rootfs 才用到

/etc/mtab 记载的是现在系统已经装载的文件系统,包括操作系统建立的虚拟文件等;而/etc/fstab是系统准备装载的。 每当 mount 挂载分区、umount 卸载分区,都会动态更新 mtab,mtab 总是保持着当前系统中已挂载的分区信息,fdisk、df 这类程序,必须要读取 mtab 文件,才能获得当前系统中的分区挂载情况。当然我们自己还可以通过读取/proc/mount也可以来获取当前挂载信息 

 

posted @ 2018-08-19 11:10  懒人三四天  阅读(3166)  评论(0编辑  收藏  举报