Linux 挂载磁盘

第一种方式(不分区)

磁盘不分区,直接挂载到某个目录。
查看磁盘列表

fdisk -l

bash 复制代码
[root]# fdisk -l

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

创建目录

mkdir /data

格式化磁盘

有好几种文件格式,需要自己选择下,一般选xfs

mkfs.xfs /dev/vdb

bash 复制代码
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# mkfs.
mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.fat     mkfs.minix   mkfs.msdos   mkfs.vfat    mkfs.xfs     

挂载到目录(临时)

mount /dev/vdb /data

挂载到目录(永久)

找到磁盘 UUID

bash 复制代码
[root@openstack-140-170 tmp]# blkid 
/dev/vda1: UUID="6cd50e51-cfc6-40b9-9ec5-f32fa2e4ff02" TYPE="xfs" 

写到/etc/fstab文件中

bash 复制代码
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Fri Nov  8 10:19:15 2019
#
# 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_host--172--25--9--73-root /                       xfs     defaults        0 0
UUID=8477d0c9-a162-4653-825e-7d10436bbfb3 /boot                   xfs     defaults        0 0
/dev/mapper/centos_host--172--25--9--73-swap swap                    swap    defaults        0 0
/dev/mapper/centos_host--172--25--9--73-swap	none	swap	sw,comment=cloudconfig	0	0

字段解释

bash 复制代码
<device>:描述要挂载的特定块设备或远程文件系统,可以是 UUID。
<dir>:描述挂载目录。
<type>:文件系统类型。
<options>:相关的挂载选项; 请见 mount(8) § FILESYSTEM-INDEPENDENT_MOUNT_OPTIONS 和 ext4(5) § MOUNT_OPTIONS。
<dump>:会被 dump(8) 工具检查。该字段通常设置为 0, 以禁用检查。
<fsck>:设置引导时文件系统检查的顺序; 请见 fsck(8)。对于 root 设备该字段应该设置为 1。对于其它分区该字段应该设置为 2,或设置为 0 以禁用检查。

执行 mount -a 立即挂载

第二种方式(分区)

查看磁盘信息

fdisk -l

bash 复制代码
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# fdisk -l

Disk /dev/vda: 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 label type: dos
Disk identifier: 0x000d041c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    41943039    19921920   8e  Linux LVM

可以看到/dev/vda又被分成两个分区,占用了20g空间,磁盘中其他的空间还没被挂载。

交互式方式创建分区

fdisk /dev/vda

bash 复制代码
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# fdisk /dev/vda
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): 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): 

输入p打印分区信息

bash 复制代码
Command (m for help): p

Disk /dev/vda: 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 label type: dos
Disk identifier: 0x000d041c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    41943039    19921920   8e  Linux LVM
/dev/vda3        41943040   419430399   188743680   83  Linux

Command (m for help): 

输入n创建分区

bash 复制代码
Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): 

可以看到还能创建1个分区。

每个磁盘最多能创建4个分区。

创建分区并分配空间

一路空格把剩余空间都分给最后一个分区。

最后输入w保存

完整命令执行过程

bash 复制代码
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# fdisk -l

Disk /dev/vda: 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 label type: dos
Disk identifier: 0x000d041c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/vdb: 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_host--172--25--9--73-root: 18.5 GB, 18513657856 bytes, 36159488 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_host--172--25--9--73-swap: 1879 MB, 1879048192 bytes, 3670016 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

[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# lsblk
NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                                   11:0    1 1024M  0 rom  
vda                                  252:0    0  200G  0 disk 
├─vda1                               252:1    0    1G  0 part /boot
└─vda2                               252:2    0   19G  0 part 
  ├─centos_host--172--25--9--73-root 253:0    0 17.2G  0 lvm  /
  └─centos_host--172--25--9--73-swap 253:1    0  1.8G  0 lvm  [SWAP]
vdb                                  252:16   0  200G  0 disk /data
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# fdisk /dev/vda
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): 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): p

Disk /dev/vda: 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 label type: dos
Disk identifier: 0x000d041c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200    41943039    19921920   8e  Linux LVM

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (41943040-419430399, default 41943040): 
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-419430399, default 419430399): 
Using default value 419430399
Partition 3 of type Linux and of size 180 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# lsblk /dev/vda
NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda                                  252:0    0  200G  0 disk 
├─vda1                               252:1    0    1G  0 part /boot
└─vda2                               252:2    0   19G  0 part 
  ├─centos_host--172--25--9--73-root 253:0    0 17.2G  0 lvm  /
  └─centos_host--172--25--9--73-swap 253:1    0  1.8G  0 lvm  [SWAP]
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# partprobe /dev/vda
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# lsblk 
NAME                                 MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sr0                                   11:0    1 1024M  0 rom  
vda                                  252:0    0  200G  0 disk 
├─vda1                               252:1    0    1G  0 part /boot
├─vda2                               252:2    0   19G  0 part 
│ ├─centos_host--172--25--9--73-root 253:0    0 17.2G  0 lvm  /
│ └─centos_host--172--25--9--73-swap 253:1    0  1.8G  0 lvm  [SWAP]
└─vda3                               252:3    0  180G  0 part 
vdb                                  252:16   0  200G  0 disk /data
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# mkfs.xfs /dev/vda3
meta-data=/dev/vda3              isize=512    agcount=4, agsize=11796480 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=47185920, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=23040, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# 
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# mkdir /data02
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# mount /dev/vda3 /data02
[root@ecs-jindie-distributedmessage-0002 admq-V2.2.1]# df -h
Filesystem                                    Size  Used Avail Use% Mounted on
/dev/mapper/centos_host--172--25--9--73-root   18G  4.5G   13G  27% /
devtmpfs                                      7.8G     0  7.8G   0% /dev
tmpfs                                         7.8G     0  7.8G   0% /dev/shm
tmpfs                                         7.8G   12M  7.8G   1% /run
tmpfs                                         7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1                                    1014M  209M  806M  21% /boot
tmpfs                                         1.6G   12K  1.6G   1% /run/user/42
/dev/vdb                                      197G  1.4G  186G   1% /data
tmpfs                                         1.6G     0  1.6G   0% /run/user/0
/dev/vda3                                     180G   33M  180G   1% /data02
相关推荐
Ven%5 分钟前
centos查看硬盘资源使用情况命令大全
linux·运维·centos
JaneJiazhao8 分钟前
HTTPSOK:SSL/TLS证书自动续期工具
服务器·网络协议·ssl
萨格拉斯救世主1 小时前
戴尔R930服务器增加 Intel X710-DA2双万兆光口含模块
运维·服务器
无所谓จุ๊บ1 小时前
树莓派开发相关知识十 -小试服务器
服务器·网络·树莓派
Jtti1 小时前
Windows系统服务器怎么设置远程连接?详细步骤
运维·服务器·windows
TeYiToKu1 小时前
笔记整理—linux驱动开发部分(9)framebuffer驱动框架
linux·c语言·arm开发·驱动开发·笔记·嵌入式硬件·arm
dsywws1 小时前
Linux学习笔记之时间日期和查找和解压缩指令
linux·笔记·学习
yeyuningzi1 小时前
Debian 12环境里部署nginx步骤记录
linux·运维·服务器
上辈子杀猪这辈子学IT1 小时前
【Zookeeper集群搭建】安装zookeeper、zookeeper集群配置、zookeeper启动与关闭、zookeeper的shell命令操作
linux·hadoop·zookeeper·centos·debian
minihuabei2 小时前
linux centos 安装redis
linux·redis·centos