欢迎光临
感受代码之美

Linux如何为新硬盘分区并挂载(mount)到指定目录下

随着数据和文件的不断增多,现有硬盘空间逐渐不够用了。现有一个新硬盘,接下来记录自己将这块硬盘进行分区并挂载到目录下的过程。

1.系统环境

  • CentOS Linux release 7.5.1804 (Core)

2.磁盘分区并挂载

2.1 查看磁盘利用情况

[[email protected] ~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda3        96G   80G   13G  87% /
devtmpfs        7.8G     0  7.8G   0% /dev
tmpfs           7.8G     0  7.8G   0% /dev/shm
tmpfs           7.8G  594M  7.2G   8% /run
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1       488M  121M  332M  27% /boot
tmpfs           1.6G     0  1.6G   0% /run/user/0
tmpfs           1.6G     0  1.6G   0% /run/user/1000

2.2 查看所有分区

[[email protected] ~]# fdisk -l

Disk /dev/vda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000d431c

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     1050623      524288   83  Linux
/dev/vda2         1050624     5244927     2097152   82  Linux swap / Solaris
/dev/vda3         5244928   209715166   102235119+  83  Linux

Disk /dev/vdb: 67 MB, 67108864 bytes, 131072 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/vdc: 1073.7 GB, 1073741824000 bytes, 2097152000 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/vdc没有进行分区。

2.3 以树形查看各磁盘列表及分区情况

[[email protected] ~]$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  100G  0 disk 
├─vda1 253:1    0  512M  0 part /boot
├─vda2 253:2    0    2G  0 part [SWAP]
└─vda3 253:3    0 97.5G  0 part /
vdb    253:16   0   64M  0 disk 
vdc    253:32   0 1000G  0 disk 

2.4 对磁盘进行分区(使用root账号)

接下来对vdc这块磁盘进行分区,并挂载到目录下。输入fdisk /dev/vdc开始分区操作:

[[email protected] ~]# fdisk /dev/vdc
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 0x510f2add.

Command (m for help): 

在上面的等待命令输入行,输入p查看分区表,如下:

how-to-mount-new-disk-on-linux-1.jpg

可以看到暂无分区。

然后在等待命令输入行,输入n,进行新建分区:

how-to-mount-new-disk-on-linux-2.jpg

上面的分区类型、分区号、起始扇区、扇区数(空间大小)都回车默认就好。

然后输入w对上述更改进行保存(如果不想保存就输入q退出):

how-to-mount-new-disk-on-linux-3.jpg

查看刚才的新分区,并进行格式化:

how-to-mount-new-disk-on-linux-4.jpg

然后,我这里创建一个挂载点(目录)/xf,将刚才的分区,挂载到此挂载点:

[[email protected] ~]# mkdir /xf
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# mount /dev/vdc1 /xf

查看一下,可以看到分区/dev/vdc1已挂载到/xf

[[email protected] ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda3        96G   80G   13G  87% /
devtmpfs        7.8G     0  7.8G   0% /dev
tmpfs           7.8G     0  7.8G   0% /dev/shm
tmpfs           7.8G  594M  7.2G   8% /run
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1       488M  121M  332M  27% /boot
tmpfs           1.6G     0  1.6G   0% /run/user/0
tmpfs           1.6G     0  1.6G   0% /run/user/1000
/dev/vdc1       985G   77M  935G   1% /xf

查看各分区的UUID:

[[email protected] ~]# blkid
/dev/vda1: UUID="fcbd39e4-ec59-4b56-a03e-75d18ce71142" TYPE="ext4" 
/dev/vda2: UUID="f4f64d75-5cec-4b98-9d0a-3bd978a6fdf8" TYPE="swap" 
/dev/vda3: UUID="3e02962c-b9a8-4bf1-9afe-b08c8eb6b695" TYPE="ext4" 
/dev/vdb: SEC_TYPE="msdos" LABEL="config-2" UUID="771B-A35C" TYPE="vfat" 
/dev/vdc1: UUID="0099e188-afe9-435f-89f0-465cdc48a0d3" TYPE="ext4" 

>>>>>>注意:接下来需谨慎,否则将导致系统不能成功启动!<<<<<<

下面开始配置开机自动挂载。

输入命令vi /etc/fstab,编辑挂载配置文件:

how-to-mount-new-disk-on-linux-5.jpg

如上图,复制/dev/vdc1分区的UUID,加入一行,然后保存文件。

/etc/fstab中定义的所有档案系统挂上:

[email protected] ~]# mount -a

References

  1. Linux添加新硬盘并挂载(mount)到目录下
赞(0)
未经允许禁止转载:四个空格 » Linux如何为新硬盘分区并挂载(mount)到指定目录下

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址