挂载硬盘

本文仅适用于使用 fdisk 命令对一个不大于 2 TB 的数据盘执行分区操作。如果需要分区的数据盘大于 2 TB,请参考 阿里云-32TB 块存储分区

查看新磁盘

查看硬盘

df -h

1
2
3
4
5
6
7
8
[root@sfdsfsdf ]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 99G 18G 77G 19% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 644K 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
tmpfs 783M 0 783M 0% /run/user/0

1. 查找未挂载的硬盘

[root@cgsl ]# fdisk –l

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Disk /dev/vda: 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: 0x0008d73a

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83884031 41940992 83 Linux

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

# 注意看这里:这个就是新的硬盘
# 记下/dev/vdb 这个名称
# 注意阿里云不同的硬盘可能名称不同,如,高效盘:vdb开头 SSD:xvdb

找到新添加的磁盘的编号为/dev/vdb

注:

  • 如果您的数据盘显示的是 dev/xvd?,表示您使用的是非 I/O 优化实例。
  • 其中 ? 是 a−z 的任一个字母。

2. 创建硬盘分区

使用 fdisk /dev/vdb

1
2
3
4
5
6
7
8
9
$ 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 0x774b38c4.

输入n
Command (m for help):n

1
2
3
4
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):

输入p

1
Partition number (1-4): 1

回车

1
First sector (2048-209715199, default 2048):

回车

1
2
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):

回车

1
2
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set

输入w
Command (m for help): w

1
2
3
4
The partition table has been altered!

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

3. 格式化分区

# mkfs.ext4 /dev/vdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
6553600 inodes, 26214400 blocks
1310720 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2174746624
800 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

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

4. 建立挂载目录

# mkdir /data

5. 挂载分区

# mount /dev/vdb /data

6. 设置开机自动挂载

运行命令

echo /dev/vdb1 /data ext4 defaults 0 0 >> /etc/fstab

或手动编辑 fstab:

vi /etc/fstab

1
2
3
4
5
6
7
8
9
# 
# /etc/fstab
# Created by anaconda on Tue May 3 13:48:10 2016
#
# 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=80b9b662-0a1d-4e84-b07b-c1bf19e72d97 / ext4 defaults 1 1
/dev/vdb1 /data ext4 defaults 0 0

/dev/vdb /data ext4 defaults 0 0

7. 确认是否挂载成功

重启服务器

# reboot

查看硬盘分区

1
2
# df
/dev/vdb 20635700 176196 19411268 1% /data

参考