Linux-磁盘挂载

1 说明

避免一头雾水,请先看完说明,再看最下面的挂载教程

1.1 挂载流程

START–>查看要挂载的是哪块硬盘–>将硬盘分区–>格式化分区–>设置开机自启(即永久挂载)–>执行挂载命令–>END

1.2 查看当前挂载的磁盘,磁盘剩余的空间

$ df -h

Linux-磁盘挂载_第1张图片
查看某个文件夹/文件大小可以用这个命令du -sh /usr

1.3 查看分区情况

$ fdisk -l

Linux-磁盘挂载_第2张图片
当前系统一共存在三块磁盘:sda,sdb,sdc;其中sda已经有两个分区sda1和sda2。也可以使用lsblk命令查看具体的磁盘分区情况
Linux-磁盘挂载_第3张图片

2 挂载磁盘正文

我们以挂载未分区的vdb到/data目录为例子

2.1 查看分区情况

$ fdisk -l

Disk /dev/vdb: 200 GiB, 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

2.2 设置分区

$ fdisk /dev/vdb

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

2.3 输入m查看帮助(可跳过)

Command (m for help): m

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

2.4 输入g创建一个空的GPT分区表

Command (m for help): g

Created a new GPT disklabel (GUID: 3B765FDC-74EB-E34E-ADF3-34A6F616C8B6).
The old dos signature will be removed by a write command.

2.5 输入p查看磁盘已被转成GTP分区格式

Command (m for help): p

Disk /dev/sda: 500 GiB, 536870912000 bytes, 1048576000 sectors
Disk model: QEMU HARDDISK   
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: gpt
Disk identifier: 046CA995-F20A-184D-BA13-D7C144240C87

2.6 输入n 创建分区 默认磁盘序号是1-128

Command (m for help): n

2.7 回车下一步,选择磁盘序号为默认

Partition number (1-128, default 1): 1

2.8 回车下一步,默认第一扇区为默认大小

First sector (2048-1048575966, default 2048):

2.9 回车下一步,默认最后一个扇区为默认大小

Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-1048575966, default 1048575966):

Created a new partition 1 of type 'Linux filesystem' and of size 500 GiB.
Partition #1 contains a ext4 signature.

2.10 输入yes,删除签名(可能不会出现这步,未出现可忽略)

Do you want to remove the signature? [Y]es/[N]o: yes

The signature will be removed by a write command.

2.11 输入w 保存设置

Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

2.12 查看分区情况

$ fdisk -l

Disk /dev/vdb: 200 GiB, 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
Disklabel type: gpt
Disk identifier: 1A0C9D01-9A82-402F-B078-73CA4431DFB9

#     Start       End   Sectors  Size Type
1   2048 419430366 419428319  200G Linux filesystem

2.13 格式化 vdb1 为ext4类型

$ mkfs.ext4 /dev/vdb1

mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 52428539 4k blocks and 13107200 inodes
Filesystem UUID: a7cf4158-cc23-49bf-81eb-d19882299d67
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

2.14 设置开机启用

2.14.1 查询分区uuid

$ ls -l /dev/disk/by-uuid/

total 0
lrwxrwxrwx 1 root root 10 Dec 14 14:10 5bb40f03-8000-4453-9218-a3cd77897ca0 -> ../../vda1
lrwxrwxrwx 1 root root 10 Dec 14 14:43 a7cf4158-cc23-49bf-81eb-d19882299d67 -> ../../vdb1

2.14.2 设置

$ vi /etc/fstab

UUID=a7cf4158-cc23-49bf-81eb-d19882299d67 /data                   ext4    defaults        0 0

上面是将vdb1挂载到/data目录,如果无此目录,请新建此目录

2.15 挂载分区

$ mount -a

2.16 检查分区挂载情况

$ df -h

3 挂载磁盘,不报错,却没挂载上

3.1 背景

今天服务器磁盘缩减,卸载磁盘,挂载新磁盘。mount -a不报错,却没有成功挂载上

3.2 查看系统日志

journalctl -S "2023-03-28 15:00:00"

原来是systemd还记录着vdb1挂载到/data,需要重载一些配置

# 重载配置
systemctl daemon-reload
# 挂载
mount -a
# 查看是否挂载好了
df -h

你可能感兴趣的:(linux,bash,运维)