本文介绍 CentOS 7.x 系统磁盘分区和挂载的命令与方法。
目录
- 分区
- 挂载
分区
1 首先使用命令 cat /etc/redhat-release
确定 CentOS 版本。
[root@01 ~]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
2 使用命令 fdisk -l
查看当前磁盘分区信息。
[root@01 ~]# fdisk -l
磁盘 /dev/xvda:451.0 GB, 450971566080 字节,880803840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c6ea8
设备 Boot Start End Blocks Id System
/dev/xvda1 * 2048 4196351 2097152 83 Linux
/dev/xvda2 4196352 266348543 131076096 8e Linux LVM
/dev/xvda3 266348544 333457407 33554432 83 Linux
磁盘 /dev/mapper/cl-root:128.8 GB, 128849018880 字节,251658240 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/cl-home:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
从上可看出只挂载了一个磁盘 xvda
,包含 3 个主分区 xvda1
、xvda2
、 xvda3
。
3 创建扩展分区
首先普及一下主分区和扩展分区的区别:一块新硬盘需要经过分区后才能正常使用,分区又分为主分区、扩展分区和逻辑分区三类。一块硬盘最多可以创建 4 个主分区,当 4 个主分区创建完后就无法创建扩展分区了。
3.1 使用 fdisk /dev/xvda
创建扩展分区。
[root@01 ~]# fdisk /dev/xvda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):m
命令操作
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)
3.2 输入命令 n
添加一个新的分区,选择扩展分区类型 e
,在【起始 扇区】位置直接输入回车,在【Last 扇区】位置也直接输入回车,将剩下的所有磁盘空间都分配给新的扩展分区,最后输入 p
查看创建结果。
命令(输入 m 获取帮助):n
Partition type:
p primary (3 primary, 0 extended, 1 free)
e extended
Select (default e): e
已选择分区 4
起始 扇区 (333457408-880803839,默认为 333457408):
将使用默认值 333457408
Last 扇区, +扇区 or +size{K,M,G} (333457408-880803839,默认为 880803839):
将使用默认值 880803839
分区 4 已设置为 Extended 类型,大小设为 261 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/xvda:451.0 GB, 450971566080 字节,880803840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c6ea8
设备 Boot Start End Blocks Id System
/dev/xvda1 * 2048 4196351 2097152 83 Linux
/dev/xvda2 4196352 266348543 131076096 8e Linux LVM
/dev/xvda3 266348544 333457407 33554432 83 Linux
/dev/xvda4 333457408 880803839 273673216 5 Extended
3.3 确认无误后输入命令 w
写入。
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
3.4 写入完成后输入命令 partprobe
(或重启操作系统)使分区生效。
[root@01 ~]# partprobe
[root@01 ~]# fdisk -l
磁盘 /dev/xvda:451.0 GB, 450971566080 字节,880803840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c6ea8
设备 Boot Start End Blocks Id System
/dev/xvda1 * 2048 4196351 2097152 83 Linux
/dev/xvda2 4196352 266348543 131076096 8e Linux LVM
/dev/xvda3 266348544 333457407 33554432 83 Linux
/dev/xvda4 333457408 880803839 273673216 5 Extended
磁盘 /dev/mapper/cl-root:128.8 GB, 128849018880 字节,251658240 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/cl-home:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
3.5 使用命令 lsblk
确认分区创建成功。
[root@01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
xvda 202:0 0 420G 0 disk
├─xvda1 202:1 0 2G 0 part /boot
├─xvda2 202:2 0 125G 0 part
│ ├─cl-root 253:0 0 120G 0 lvm /
│ └─cl-home 253:1 0 5G 0 lvm /home
├─xvda3 202:3 0 32G 0 part /swap
└─xvda4 202:4 0 1K 0 part
4 在扩展分区上创建逻辑分区
实际上扩展分区只是一个概念,在硬盘中是看不到的,也无法直接使用,需要在扩展分区上继续创建多个逻辑分区。
4.1 同样使用 fdisk /dev/xvda
创建逻辑分区,输入命令 n
添加一个新的分区,提示所有主分区都已被使用,自动选择分区类型为逻辑分区,在【起始 扇区】位置直接输入回车,在【Last 扇区】位置设置新分区大小(本示例设置新分区大小为 64G),最后输入 p
查看创建结果。
[root@01 ~]# fdisk /dev/xvda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
All primary partitions are in use
添加逻辑分区 5
起始 扇区 (333459456-880803839,默认为 333459456):
将使用默认值 333459456
Last 扇区, +扇区 or +size{K,M,G} (333459456-880803839,默认为 880803839):+64G
分区 5 已设置为 Linux 类型,大小设为 64 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/xvda:451.0 GB, 450971566080 字节,880803840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c6ea8
设备 Boot Start End Blocks Id System
/dev/xvda1 * 2048 4196351 2097152 83 Linux
/dev/xvda2 4196352 266348543 131076096 8e Linux LVM
/dev/xvda3 266348544 333457407 33554432 83 Linux
/dev/xvda4 333457408 880803839 273673216 5 Extended
/dev/xvda5 333459456 467677183 67108864 83 Linux
4.2 确认无误后输入命令 w
写入。
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
4.3 写入完成后输入命令 partprobe
(或重启操作系统)使分区生效,输入命令 fdisk -l
、 lsblk
确认分区创建结果。
[root@01 ~]# partprobe
[root@01 ~]# fdisk -l
磁盘 /dev/xvda:451.0 GB, 450971566080 字节,880803840 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x000c6ea8
设备 Boot Start End Blocks Id System
/dev/xvda1 * 2048 4196351 2097152 83 Linux
/dev/xvda2 4196352 266348543 131076096 8e Linux LVM
/dev/xvda3 266348544 333457407 33554432 83 Linux
/dev/xvda4 333457408 880803839 273673216 5 Extended
/dev/xvda5 333459456 467677183 67108864 83 Linux
磁盘 /dev/mapper/cl-root:128.8 GB, 128849018880 字节,251658240 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/cl-home:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
[root@01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
xvda 202:0 0 420G 0 disk
├─xvda1 202:1 0 2G 0 part /boot
├─xvda2 202:2 0 125G 0 part
│ ├─cl-root 253:0 0 120G 0 lvm /
│ └─cl-home 253:1 0 5G 0 lvm /home
├─xvda3 202:3 0 32G 0 part /swap
├─xvda4 202:4 0 512B 0 part
└─xvda5 202:5 0 64G 0 part
5 分区在使用前需要先进行格式化,格式化时需要选定文件系统类型(本示例选择 ext3
)
[root@01 ~]# mkfs.ext3 /dev/xvda5
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
4194304 inodes, 16777216 blocks
838860 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
512 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: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
挂载
1 使用命令 mkdir -p
创建需要挂载的文件目录
[root@01 ~]# mkdir -p /demo
2 使用 mount
命令挂载
[root@01 ~]# mount /dev/xvda5 /demo
3 使用 lsblk
命令查看挂载结果
[root@01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
xvda 202:0 0 420G 0 disk
├─xvda1 202:1 0 2G 0 part /boot
├─xvda2 202:2 0 125G 0 part
│ ├─cl-root 253:0 0 120G 0 lvm /
│ └─cl-home 253:1 0 5G 0 lvm /home
├─xvda3 202:3 0 32G 0 part /swap
├─xvda4 202:4 0 512B 0 part
└─xvda5 202:5 0 64G 0 part /demo
注意:使用 mount
命令挂载在系统重启后会失效。
4 修改 /etc/fstab
文件使挂载永久有效
4.1 先查看一下 /etc/fstab
文件内容。
[root@01 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed Jan 9 15:15:30 2019
#
# 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
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=4725d47c-b7de-434b-8cd5-80896c922160 /boot xfs defaults 0 0
/dev/mapper/cl-home /home xfs defaults 0 0
UUID=17f69266-5180-4fbe-a565-c568ec8df3aa /swap xfs defaults 0 0
最好使用 UUID 挂载。
4.2 使用命令 blkid
查看所有分区的 UUID 信息。
[root@01 ~]# blkid
/dev/xvda1: UUID="4725d47c-b7de-434b-8cd5-80896c922160" TYPE="xfs"
/dev/xvda2: UUID="MihdFc-ZNfO-EITU-qTFq-bKYC-ep81-wtGhce" TYPE="LVM2_member"
/dev/xvda3: UUID="17f69266-5180-4fbe-a565-c568ec8df3aa" TYPE="xfs"
/dev/xvda5: UUID="d5abd132-1204-4d35-8b89-3703ff5a28b2" SEC_TYPE="ext2" TYPE="ext3"
/dev/mapper/cl-root: UUID="e4445215-94c9-49c4-8d56-7b190579b68e" TYPE="xfs"
/dev/mapper/cl-home: UUID="fd5d5ec8-bc36-466a-b511-18659b272cf1" TYPE="xfs"
记录挂载分区的 UUID。
4.3 修改 /etc/fstab
文件,添加以下最后一行内容
#
# /etc/fstab
# Created by anaconda on Wed Jan 9 15:15:30 2019
#
# 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
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=4725d47c-b7de-434b-8cd5-80896c922160 /boot xfs defaults 0 0
/dev/mapper/cl-home /home xfs defaults 0 0
UUID=17f69266-5180-4fbe-a565-c568ec8df3aa /swap xfs defaults 0 0
UUID=d5abd132-1204-4d35-8b89-3703ff5a28b2 /demo ext3 defaults 0 0
保存后重启系统,使用 lsblk
命令检查挂载是否生效(略)。