一、说明
本文章转载自知乎:一篇看懂!Linux磁盘的管理(分区、格式化、挂载),LVM逻辑卷;如有错误,欢迎留言指正!
二、基础过程
这里我们使用一台虚拟机来演示过程
1、分配磁盘
2、磁盘相关命令
2.1 查看磁盘信息
可以看到,我们这台虚拟机现在有三块磁盘: /dev/sdb /dev/sdc /dev/sdd 未作任何操作的
fdisk -l
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0004a0b9
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/sdd: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
2.2 查看磁盘挂载信息
df -h
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 4.3G 13G 25% /
/dev/sda1 1014M 157M 858M 16% /boot
tmpfs 378M 0 378M 0% /run/user/0
[root@localhost ~]#
2.3 查看磁盘容量
du -sh *
[root@localhost /]# du -sh *
0 bin
124M boot
0 dev
39M etc
132K home
0 lib
0 lib64
0 media
0 mnt
107M mount
0 nohup.out
169M opt
du: cannot access ‘proc/1/task/1/fdinfo/13’: No such file or directory
du: cannot access ‘proc/2536/task/2536/fd/3’: No such file or directory
du: cannot access ‘proc/2536/task/2536/fdinfo/3’: No such file or directory
du: cannot access ‘proc/2536/fd/3’: No such file or directory
du: cannot access ‘proc/2536/fdinfo/3’: No such file or directory
0 proc
60K root
12M run
0 sbin
0 srv
0 sys
0 tmp
3.4G usr
456M var
[root@localhost /]#
3、磁盘分区
这里我们先将前两块盘分区
3.1 分区
[root@localhost /]# fdisk /dev/sdb
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 0x187080d4.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):
Using default value 20971519
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost /]#
3.2 再次查看磁盘信息
可以看到 sdb–》sdb1分区 sdc–>sdc1分区
[root@localhost /]# clear
[root@localhost /]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x0004a0b9
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x187080d4
Device Boot Start End Blocks Id System
/dev/sdb1 2048 20971519 10484736 83 Linux
Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 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: 0x8d82881d
Device Boot Start End Blocks Id System
/dev/sdc1 2048 20971519 10484736 83 Linux
Disk /dev/sdd: 10.7 GB, 10737418240 bytes, 20971520 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 /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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
4、创建物理卷(PV)
命令
pvcreate /dev/sdb1 /dev/sdc1
[root@localhost /]# pvcreate /dev/sdb1 /dev/sdc1
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdc1" successfully created.
[root@localhost /]#
查看pv
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 lvm2 --- <10.00g <10.00g
/dev/sdc1 lvm2 --- <10.00g <10.00g
[root@localhost /]#
5、创建卷组(VG)
创建名为miniovg的卷组 ,并将 /dev/sdb1 /dev/sdc1 两个物理卷(pv)加入到卷组中
vgcreate miniovg /dev/sdb1 /dev/sdc1
[root@localhost /]# vgcreate miniovg /dev/sdb1 /dev/sdc1
Volume group "miniovg" successfully created
查看卷组,可以看到刚才创建的miniovg 卷组有19.99G的空间
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
miniovg 2 0 0 wz--n- 19.99g 19.99g
6、基于卷组创建逻辑卷(LV)
以miniovg 卷组,创建一个名为miniolv的逻辑卷,分配19G(不能超过卷组最大)的空间
lvcreate -n miniolv -L 19G miniovg
[root@localhost /]# lvcreate -n miniolv -L 19G miniovg
Logical volume "miniolv" created.
查看逻辑卷
[root@localhost /]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
miniolv miniovg -wi-a----- 19.00g
7、格式化逻辑卷
mkfs.ext4 /dev/miniovg/miniolv
[root@localhost /]# mkfs.ext4 /dev/miniovg/miniolv
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
1245184 inodes, 4980736 blocks
249036 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
152 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
8、挂载使用
我们新建一个挂载目录
mkdir /miniodata
挂载
mount /dev/miniovg/miniolv /miniodata/
查看挂载。可以看到挂载目录 /miniodata
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 4.3G 13G 25% /
/dev/sda1 1014M 157M 858M 16% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/miniovg-miniolv 19G 45M 18G 1% /miniodata
[root@localhost /]#
9、设置永久挂载
查看磁盘uuid,可以看到我们的物理卷miniovg-miniolv的uuid为fd23bbbf-a673-4c80-a5f2-0f3fd083a719
[root@localhost /]# blkid
/dev/sda1: UUID="2d803c33-0704-43ec-b3be-aed6e9ce8c04" TYPE="xfs"
/dev/sda2: UUID="S3N1ts-UI0b-rWtZ-etha-NBcb-ISTC-heTXrF" TYPE="LVM2_member"
/dev/sdb1: UUID="zlGCxN-rY4W-llz8-ajZO-KuEV-tkGr-ZiZ0sg" TYPE="LVM2_member"
/dev/sdc1: UUID="wBfzdb-3Mco-FIAl-smrz-l8rq-B3xT-OJnTU6" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="5f9f5388-0f0c-4b8e-8252-66d60989f572" TYPE="xfs"
/dev/mapper/centos-swap: UUID="cf9f709f-e5b7-48b9-844d-41798efb676e" TYPE="swap"
/dev/mapper/miniovg-miniolv: UUID="fd23bbbf-a673-4c80-a5f2-0f3fd083a719" TYPE="ext4"
[root@localhost /]#
编辑 /etc/fstab文件,写入挂载信息
#
# /etc/fstab
# Created by anaconda on Mon Oct 25 14:36:42 2021
#
# 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/centos-root / xfs defaults 0 0
UUID=2d803c33-0704-43ec-b3be-aed6e9ce8c04 /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID=fd23bbbf-a673-4c80-a5f2-0f3fd083a719 /miniodata ext4 defaults 1 1
检查是否设置成功,我们可以先卸载刚才挂载的目录
umount /miniodata
在使用命令,重新读取挂载信息
mount -a
这样重启后就可以自动挂载了
10、磁盘扩容
现在我还剩下 /dev/sdd 这块磁盘没有使用,也相当于我们新分配的一块盘,我们把他加入到 /miniolv逻辑卷中。
10.1 扩容卷组
先看看我们现在的磁盘大小,可以看到我们现在挂载了一块19G的miniodata盘
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 4.3G 13G 25% /
/dev/sda1 1014M 157M 858M 16% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/miniovg-miniolv 19G 45M 18G 1% /miniodata
[root@localhost /]#
当前lvm信息
[root@localhost /]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root centos -wi-ao---- <17.00g
swap centos -wi-ao---- 2.00g
miniolv miniovg -wi-ao---- 19.00g
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
miniovg 2 1 0 wz--n- 19.99g 1016.00m
[root@localhost /]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 centos lvm2 a-- <19.00g 0
/dev/sdb1 miniovg lvm2 a-- <10.00g 0
/dev/sdc1 miniovg lvm2 a-- <10.00g 1016.00m
[root@localhost /]#
我们先把剩下的sdd盘分区 /sdd1
[root@localhost /]#
[root@localhost /]# fdisk -l /dev/sdd
Disk /dev/sdd: 10.7 GB, 10737418240 bytes, 20971520 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: 0xda833282
Device Boot Start End Blocks Id System
/dev/sdd1 2048 20971519 10484736 83 Linux
[root@localhost /]#
将 /dev/sdd1 分区加入到卷组(miniovg)中,可看到vg中多出来了10G的剩余空间
[root@localhost /]# vgextend miniovg /dev/sdd1
Physical volume "/dev/sdd1" successfully created.
Volume group "miniovg" successfully extended
[root@localhost /]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.00g 0
miniovg 3 1 0 wz--n- <29.99g <10.99g
10.2 lv扩容
第一种方法:直接增大5G,写“+5G”
1.lv扩容5G
lvresize -L +5G /dev/mapper/miniovg-miniolv
[root@localhost /]# lvresize -L +10G /dev/mapper/miniovg-miniolv
Size of logical volume miniovg/miniolv changed from 19.00 GiB (4864 extents) to 29.00 GiB (7424 extents).
Logical volume miniovg/miniolv successfully resized.
[root@localhost /]#
2.文件系统大小变更
resize2fs /dev/mapper/miniovg-miniolv
[root@localhost /]# resize2fs /dev/mapper/miniovg-miniolv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/miniovg-miniolv is mounted on /miniodata; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/mapper/miniovg-miniolv is now 7602176 blocks long.
[root@localhost /]#
第二种方法:原来是10G,增大5G就是“15G”
1.lv扩容到15G
lvresize -L 15G /dev/mapper/miniovg-miniolv
2.文件系统大小变更
resize2fs /dev/mapper/miniovg-miniolv
至此,我们在此查看挂载信息,可以看到已经从原来的19G扩容到29G了
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 12M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 4.3G 13G 25% /
/dev/sda1 1014M 157M 858M 16% /boot
tmpfs 378M 0 378M 0% /run/user/0
/dev/mapper/miniovg-miniolv 29G 44M 28G 1% /miniodata
[root@localhost /]#