CentOS7下挂载新磁盘

Author Avatar
山小杰 2月 02, 2018
  • 在其它设备中阅读本文章

虚拟机也好,物理机也罢,在CentOS的使用过程中都有可能会出现需要增加磁盘的情况,这篇文章就是介绍如何为新磁盘进行分区以及挂载操作。

所有操作都在root用户下进行。

查看当前磁盘使用状况

# df -h

Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   18G   15G  3.3G  82% /
devtmpfs             902M     0  902M   0% /dev
tmpfs                912M     0  912M   0% /dev/shm
tmpfs                912M  8.6M  904M   1% /run
tmpfs                912M     0  912M   0% /sys/fs/cgroup
/dev/sda1            197M  119M   79M  61% /boot
tmpfs                183M     0  183M   0% /run/user/0

列出现有的磁盘及分区信息

# fdisk -l

# /dev/sda 是原有的磁盘,可以看到这块磁盘下面的分区信息
Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b4958

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41943039    20765696   8e  Linux LVM
/dev/sda3        41943040    83886079    20971520   83  Linux

# /dev/sdb 就是我们刚添加的磁盘(如果原来只有一块磁盘的话),可以看到这块磁盘下面没有任何分区
Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 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/cl-root: 19.1 GB, 19113443328 bytes, 37330944 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/cl-swap: 2147 MB, 2147483648 bytes, 4194304 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/sdb 进行分区操作

# fdisk /dev/sdb

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 0xfd5ee39c.

# 输入 m 可以查看命令帮助(我们只需要用到 n 和 w 两个命令)
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)

# 输入 n 添加分区(一块磁盘最多可以有4个主分区-primary,没有特殊需求的话,全部按默认分一个区就行)
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-83886079, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): 
Using default value 83886079
Partition 1 of type Linux and of size 40 GiB is set

# 输入 w 保存分区信息
Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

再次查看磁盘分区信息

# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000b4958

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      411647      204800   83  Linux
/dev/sda2          411648    41943039    20765696   8e  Linux LVM
/dev/sda3        41943040    83886079    20971520   83  Linux

Disk /dev/sdb: 42.9 GB, 42949672960 bytes, 83886080 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: 0xfd5ee39c

# 可以看到刚刚分的一个区 /dev/sdb1
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    83886079    41942016   83  Linux

Disk /dev/mapper/cl-root: 19.1 GB, 19113443328 bytes, 37330944 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/cl-swap: 2147 MB, 2147483648 bytes, 4194304 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/sdb1

这里使用ext4文件系统
# mkfs.ext4 /dev/sdb1

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
2621440 inodes, 10485504 blocks
524275 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2157969408
320 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

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

创建分区挂载目录

# mkdir /mnt/disk01

挂载新分区

# mount /dev/sdb1 /mnt/disk01/

查看磁盘使用情况

# df -h

Filesystem           Size  Used Avail Use% Mounted on
/dev/mapper/cl-root   18G   15G  3.3G  82% /
devtmpfs             902M     0  902M   0% /dev
tmpfs                912M     0  912M   0% /dev/shm
tmpfs                912M  8.6M  904M   1% /run
tmpfs                912M     0  912M   0% /sys/fs/cgroup
/dev/sda1            197M  119M   79M  61% /boot
tmpfs                183M     0  183M   0% /run/user/0
# 多出来了这个
/dev/sdb1             40G   49M   38G   1% /mnt/disk01

设置开机自动挂载磁盘

# vim /etc/fstab

/dev/sdb1    /mnt/disk01    ext4    defaults    0 0

说明:

第一列是设备名
第二列是挂载点(挂载目录)
第三列是要使用的文件系统
第四列是挂载参数,使用默认即可(defaults)
第五列是dump备份参数,1:每天备份,2:不定期备份,0:不备份
第六列是扇区检验参数,1:最早检验,2:标识为1的设备检验完之后检验,0:不检验

完工!