linux中有时会出现磁盘容量不够需要新增磁盘,这里简单的介绍下以centos7为例新增磁盘并完成分区、格式化和自动挂载的流程。
1)添加磁盘之前,先用fdisk -l查看磁盘的基本信息
[root@localhost ~]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004cfc7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary. /dev/sda2 39 549 4096000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 549 5222 37538816 83 Linux
2)系统增加磁盘后,通过fdisk可以查看到新添加的磁盘信息。示例是在虚拟机运行的,关闭系统,在虚拟机的设置里,增加了一块20G的磁盘。
[root@localhost ]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004cfc7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 549 4096000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 549 5222 37538816 83 Linux
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x782437c8
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 83 Linux
其中/dev/sdb为新添加的磁盘信息
3)这个时候磁盘还不能挂载到系统,需要对新添加的磁盘进行分区
[root@localhost ]# fdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n(新建)
Command action
e extended
p primary partition (1-4)
p(主分区)
Partition number (1-4): 2(编号为2,在MBR里最多只能建立四个主分区,所以通常至少建一个扩展分区,编号1-4为主分区的编号,逻辑分区的编号从5开始。如果只有一个主分区1,则2,3,4号空缺,直接从5号开始及1,5)
First cylinder (655-2610, default 655):
Using default value 655
Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G(用+表示分5G大小的磁盘空间)
Command (m for help): w(w表示保存)
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
4)分区结束后,再次通过fdisk -l查询,结果如下:
[root@localhost ]# fdisk -l
Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0004cfc7
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 549 4096000 82 Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3 549 5222 37538816 83 Linux
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x782437c8
Device Boot Start End Blocks Id System
/dev/sdb1 1 654 5253223+ 83 Linux
/dev/sdb2 655 1308 5253255 83 Linux(磁盘划分成功)
5)挂载之前需要对分区进行格式化,这里格式成ext4
[root@localhost ]# mkfs -t ext4 /dev/sdb2
6)创建挂载点
[root@localhost ]# mkdir /data
7)通过mount挂载
[root@localhost ]# mount /dev/sdb2 data
注意:挂载点必须是一个目录,如果挂载之前,目录内有文件,则挂载后文件不可见,但是没有消失,unmount之后才能看见。
unmount 用法,
unmount /dev/sdb2 data
unmount /dev/sdb2
8) 开机自动挂载
命令行的挂载是临时的,重启会失效。这时候就需要修改配置文件进行自动挂载。
[root@localhost ]# vim /etc/fstab
/etc/vdb1 /data ext4 defaulte 0 0
它分别代表的是:设备路径、挂载点、磁盘文件类型、参数、备份标记、检测顺序
[root@localhost ]# mount -a
Mount –a是验证fstab文件是否有错误,如报错则证明fstab挂载存在问题。
9)df –h进行验证。
[root@localhost ]# df -h
/dev/sdb2 5120M 156M 4964M 3% /data
如上述显示则代表挂载完成。