简言

window下的磁盘管理还是比较容易的,但是linux下的就没那么容易了。这里我们记录一下linux-Centos7下面关于磁盘的查看、分区格式化以及挂载的过程。

查看

查看硬盘挂载情况

可以看到sda 有500G,其下有两个分区sda1, sda2

1
2
3
4
5
6
7
8
9
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 500G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 15G 0 part
├─centos-root 253:0 0 13.4G 0 lvm /
└─centos-swap 253:1 0 1.6G 0 lvm [SWAP]
sr0 11:0 1 1024M 0 rom

查看磁盘使用情况

看到没有很大的磁盘,所以说还有很多未分配的磁盘空间

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 14G 978M 13G 8% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.9M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 783M 0 783M 0% /run/user/0

fdisk -l 可以查看当前系统 所有磁盘分区情况 比如说分区数量 分区大小
df -Th 可以查看当前系统 磁盘分区所挂载的目录 和使用情况

磁盘分区

输入n 开始新增分区,以下都是回车,就是选择的默认将其余空间全分到sda3。

不要忘记partprobe 命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@localhost ~]# fdisk /dev/sda
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): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (33554432-1048575999, default 33554432):
Using default value 33554432
Last sector, +sectors or +size{K,M,G} (33554432-1048575999, default 1048575999):
Using default value 1048575999
Partition 3 of type Linux and of size 484 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@localhost ~]# partprobe
[root@localhost ~]#

磁盘格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost ~]# mkfs -t ext4 /dev/sda3
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
31719424 inodes, 126877696 blocks
6343884 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2275409920
3872 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, 71663616, 78675968,
102400000

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

磁盘挂载

我将此块空间挂在/home/data 目录下。

1
2
3
4
[root@localhost ~]# mkdir /home/data
[root@localhost ~]# mount /dev/sda3 /home/data
[root@localhost ~]#

检查是否正常

可以看到sda3 已经挂载上去了。

1
2
3
4
5
6
7
8
9
10
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 14G 978M 13G 8% /
devtmpfs 3.9G 0 3.9G 0% /dev
tmpfs 3.9G 0 3.9G 0% /dev/shm
tmpfs 3.9G 8.9M 3.9G 1% /run
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
tmpfs 783M 0 783M 0% /run/user/0
/dev/sda3 477G 73M 452G 1% /home/data

设置开机挂载

到 /etc/fstab 下配置挂载信息,添加一条记录。

1
/dev/sda3 /home/data ext4 defaults 0 0

测试一下fstab 文件是否正常运行:

1
[root@localhost ~]# mount -a

下面图形化了解一下

img