linux磁盘分区、格式化和挂载
这篇文档主要是记录新加磁盘常用的操作。时间久了就会变得陌生。还是需要记录起来。
1. 查看磁盘空间基本使用情况
[root@htl-test01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.7G 0 3.7G 0% /dev
tmpfs 3.7G 0 3.7G 0% /dev/shm
tmpfs 3.7G 504K 3.7G 1% /run
tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
/dev/vda1 40G 2.3G 36G 7% /
tmpfs 747M 0 747M 0% /run/user/0
2. 查看磁盘挂载配置情况
[root@htl-test01 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Sep 14 07:14:22 2020
#
# 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=9f2d3e15-a78a-4f3d-8385-0165b4b67864 / ext4 defaults 1 1
[root@htl-test01 ~]#
3. 查看磁盘分区(空间)情况
[root@htl-test01 ~]# fdisk -l
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: 0x000bb9c1
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 83886046 41941999+ 83 Linux
Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 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
[root@htl-test01 ~]#
4. 查看磁盘分区(文件系统)的情况
[root@htl-test01 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
└─vda1 ext4 9f2d3e15-a78a-4f3d-8385-0165b4b67864 /
vdb
[root@htl-test01 ~]#
从以上可以看到vdb的物理磁盘,没有挂载,没有分区,没有被使用。需要进行这些操作。
5. 对磁盘(vdb)进行分区操作
[root@htl-test01 ~]# 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 0x66ce43b2.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@htl-test01 ~]#
6. 查看上一步分区操作的结果
[root@htl-test01 ~]#lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
└─vda1 ext4 9f2d3e15-a78a-4f3d-8385-0165b4b67864 /
vdb
└─vdb1
7. 对新的分区进行格式化,格式化也是创建文件系统的过程
[root@htl-test01 ~]# mkfs -t ext4 /dev/vdb1
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
3276800 inodes, 13106944 blocks
655347 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712
400 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@htl-test01 ~]#
8. 将新的分区进行挂载
[root@htl-test01 ~]# mkdir -p /disk
[root@htl-test01 ~]# mount /dev/vdb1 /disk/
[root@htl-test01 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
└─vda1 ext4 9f2d3e15-a78a-4f3d-8385-0165b4b67864 /
vdb
└─vdb1 ext4 998fcad6-967f-4042-b11d-c4f526de52b5 /disk
[root@htl-test01 ~]#
9. 将新分区信息写入配置文件
[root@htl-test01 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Sep 14 07:14:22 2020
#
# 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=9f2d3e15-a78a-4f3d-8385-0165b4b67864 / ext4 defaults 1 1
[root@htl-test01 ~]# vi /etc/fstab
[root@htl-test01 ~]#
10. 查看分区信息,并使之生效
[root@htl-test01 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Mon Sep 14 07:14:22 2020
#
# 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=9f2d3e15-a78a-4f3d-8385-0165b4b67864 / ext4 defaults 1 1
UUID=998fcad6-967f-4042-b11d-c4f526de52b5 /disk ext4 defaults 0 0
[root@htl-test01 ~]# mount -a
至此,新加磁盘的操作已完成,可开始正常使用。即使操作系统重启也会有效。