1. 什么是挂载?
笔者刚开始听到"挂载"这个概念时,很懵。
这里讲一下自己的理解:
一般情况下,我们对一块新硬盘的操作分为两步。第一步:分区
;第二步: windows上叫分配盘符,与之相对,Linux就是"挂载"
。通常情况下,windows上将分区和分配盘符操作合成一步,在分区的过程中,盘符也已经分配了。
2. 服务器硬盘挂载(以 /dev/vdb1 挂载到 /data目录为例)
- 查看硬盘信息(lsblk)
[root@Server]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 470K 0 rom
sr1 11:1 1 1024M 0 rom
sr2 11:2 1 1024M 0 rom
vda 253:0 0 100G 0 disk
├─vda1 253:1 0 1G 0 part /boot
└─vda2 253:2 0 99G 0 part /
vdb 253:16 0 500G 0 disk`vdb 没有挂载`
- 给磁盘分区
这一步相当去 windows 系统,将硬盘分成 C盘,D盘 等操作
[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 0x27c214c9. 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 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): First sector (2048-1048575999, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-1048575999, default 1048575999): Using default value 1048575999 Partition 1 of type Linux and of size 500 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
- 再次查看硬盘信息(lsblk)并且格式化硬盘(mkfs.ext4 /dev/vdb1)
注意:我这里硬盘是新的,没有数据,所以格式化,如果硬盘有数据,建议先备份
[root@Server]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 470K 0 rom sr1 11:1 1 1024M 0 rom sr2 11:2 1 1024M 0 rom vda 253:0 0 100G 0 disk ├─vda1 253:1 0 1G 0 part /boot └─vda2 253:2 0 99G 0 part / vdb 253:16 0 500G 0 disk └─vdb1 253:17 0 500G 0 part [root@Server]# mkfs.ext4 /dev/vdb1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 32768000 inodes, 131071744 blocks 6553587 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2279604224 4000 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 102400000 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done
- 创建文件夹并且挂载
[root@Server]# mkdir /data && mount /dev/vdb1 /data [root@Server]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 470K 0 rom sr1 11:1 1 1024M 0 rom sr2 11:2 1 1024M 0 rom vda 253:0 0 100G 0 disk ├─vda1 253:1 0 1G 0 part /boot └─vda2 253:2 0 99G 0 part / vdb 253:16 0 500G 0 disk └─vdb1 253:17 0 500G 0 part /data
可以看到 vdb1 已经挂载到 /data上了
- 设置服务器开机自动挂载(重要)
修改配置文件(vim /etc/fstab), 添加 /dev/vdb1 /data ext4 defaults 0 0
另外一种,以uuid形势挂载(blkid)[root@Server]# vim /etc/fstab # /etc/fstab # Created by anaconda on Fri Dec 9 13:51:35 2022 # # 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 # UUID=cd7d23e1-9116-449e-9c66-73cd4bf9cb3f /xfs defaults0 0 UUID=275988b5-470f-431e-952c-dc3ea838f690 /bootxfs defaults0 0 /dev/vdb1 /data ext4 defaults 0 0
修改配置文件(vim /etc/fstab),添加UUID=13fa0360-cb58-4656-9676-06f86bcd5b6d /data ext4 defaults 0 0
最后,重启(reboot),再使用 lsblk 查看是否正常挂载.[root@Server]# blkid /dev/sr0: UUID="2023-05-23-12-48-50-00" LABEL="config-2" TYPE="iso9660" /dev/vda1: UUID="275988b5-470f-431e-952c-dc3ea838f690" TYPE="xfs" /dev/vda2: UUID="cd7d23e1-9116-449e-9c66-73cd4bf9cb3f" TYPE="xfs" /dev/vdb1: UUID="13fa0360-cb58-4656-9676-06f86bcd5b6d" TYPE="ext4"
Linux命令
以 /dev/vdb1 挂载到 /data目录为例
# 查看
lsblk
# 分区 && 格式化 && 创建挂载点(就是创建一个空目录) && 挂载
fdisk /dev/vdb && mkfs.ext4 /dev/vdb1 && mkdir /data && mount /dev/vdb1 /data
# 修改配置文件vim /etc/fstab, 添加 /dev/vdb1 /data ext4 defaults 0 0
vim /etc/fstab (/dev/vdb1 /data ext4 defaults 0 0)
# 重启
reboot
#验证
lsblk