一、 概述
理解Linux文件系统的管理,需要了解以下的一张图:
一般使用LVM (Logical Volume Manager) 管理磁盘存储,该工具允许用户更灵活地分配和管理存储空间。主要有以下几个概念:
-
PV(Physical Volume,物理卷):
物理卷是LVM中的基本存储单元,通常是一个物理硬盘或硬盘分区。通过将物理卷添加到LVM中,用户可以将其纳入逻辑卷管理的范围。PV可以是整个磁盘,也可以是磁盘的一部分。 -
VG(Volume Group,卷组):
卷组是由一个或多个物理卷组成的逻辑集合。VG将多个PV的存储空间聚合在一起,形成一个统一的存储池。用户可以在VG中创建多个逻辑卷,从而实现更灵活的存储管理。在LVM模式下,VG中以PE为存储单元 -
LV(Logical Volume,逻辑卷):
逻辑卷是从卷组中分配的存储空间,类似于传统文件系统中的分区。用户可以根据需要创建、调整大小和删除逻辑卷。LV可以被格式化为文件系统并挂载到系统中,供操作系统和应用程序使用。
在进行管理时,我们可能会用到以下几种命令:
fdisk -l
用于列出系统中所有磁盘的分区表信息
Disk /dev/sdb: 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: 0x42c194df
Device Boot Start End Blocks Id System
/dev/sdb1 2048 41943039 20970496 83 Linux
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: 0x0008e0a3
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
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: 0x2b132231
Device Boot Start End Blocks Id System
/dev/sdc1 2048 20971519 10484736 83 Linux
Disk /dev/mapper/cl-root: 39.7 GB, 39720058880 bytes, 77578240 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/cl-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
Disk /dev/mapper/cl--test-mnt: 10.6 GB, 10632560640 bytes, 20766720 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
1. Disk /dev/sda: 表示系统中的第一个磁盘设备,通常是主硬盘。
2. 21.5 GB: 磁盘的总容量。
3. 21474836480 bytes: 磁盘的总字节数。
4. 41943040 sectors: 磁盘的总扇区数。
5. Units: 显示扇区的单位,通常为512字节。
7. Sector size: 显示逻辑和物理扇区的大小。
8. Disk label type: 磁盘分区表的类型,这里是dos。
9. Disk identifier: 磁盘的唯一标识符。
# 分区信息:
- Device: 分区的设备名称。
- Boot: 该分区是否可引导, * 表示可以引导
- Start/End: 分区在磁盘上的起始和结束扇区。
- Blocks: 分区的大小,以块为单位
- Id: 分区类型的标识符。
83 表示该分区是Linux文件系统,
8e 表示该分区是Linux LVM(逻辑卷管理)。
- System: 描述分区的文件系统类型或用途。
lsblk
是一个用于列出系统中所有块设备的命令。它提供了有关设备的信息,包括设备名称、类型、大小、挂载点等。这个命令通常用于查看硬盘、分区、USB驱动器和其他存储设备的状态。
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cl-root 253:0 0 37G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─cl-root 253:0 0 37G 0 lvm /
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 10G 0 part
└─cl--test-mnt 253:2 0 9.9G 0 lvm
sr0 11:0 1 1024M 0 rom
# 输出说明
- NAME: 设备或分区的名称。
- MAJ:MIN: 设备的主设备号和次设备号,用于内核识别设备。
- RM: 表示设备是否可移动(1 表示可移动,0 表示不可移动)。
- SIZE: 设备或分区的总大小。
- RO: 表示设备是否为只读(1 表示只读,0 表示可读写)。
- TYPE: 设备的类型(disk 表示磁盘,part 表示分区,lvm 表示逻辑卷管理,rom 表示只读存储)。
- MOUNTPOINT: 设备或分区在文件系统中的挂载点。
### 详细解释
1. sda (20G):
- sda1 (1G):
- 类型: 分区
- 挂载点: /boot(用于存放启动相关文件)
- sda2 (19G):
- 类型: 分区
- 该分区未直接挂载,但它是 LVM 的一部分。
- cl-root (37G):
- 类型: LVM 逻辑卷
- 挂载点: /(根文件系统)
- cl-swap (2G):
- 类型: LVM 逻辑卷
- 用作交换空间(SWAP)。
2. sdb (20G):
- sdb1 (20G):
- 类型: 分区
- 该分区未直接挂载,但它也是 LVM 的一部分。
- cl-root (37G):
- 类型: LVM 逻辑卷
- 挂载点: /(与 sda2 共同构成根文件系统)。
3. sdc (10G):
- sdc1 (10G):
- 类型: 分区
- 该分区未直接挂载,但它是 LVM 的一部分。
- cl--test-mnt (9.9G):
- 类型: LVM 逻辑卷
- 可能用于测试或临时挂载。
4. sr0 (1024M):
- 类型: ROM 设备,通常表示 CD/DVD 驱动器。
df -hT
该命令将显示所有已挂载的文件系统的相关信息,包括文件系统的挂载点、总容量、已使用空间、可用空间以及使用百分比。-h选项以人类可读的格式显示磁盘空间, T 增加文件系统类型展示。
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/cl-root xfs 37G 1.2G 36G 4% /
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 139M 876M 14% /boot
tmpfs tmpfs 380M 0 380M 0% /run/user/1000
1. /dev/mapper/cl-root:
- Filesystem Type: xfs - 这是文件系统的类型。
- Size: 37G - 文件系统的总大小为37GB。
- Used: 1.2G - 已使用的空间为1.2GB。
- Avail: 36G - 可用空间为36GB。
- Use%: 4% - 已使用空间占总空间的4%。
- Mounted on: / - 该文件系统挂载在根目录。
2. devtmpfs:
- Filesystem Type: devtmpfs - 这是一个虚拟文件系统,用于管理设备文件。
- Size: 1.9G - 总大小为1.9GB。
- Used: 0 - 当前没有使用的空间。
- Avail: 1.9G - 可用空间为1.9GB。
- Use%: 0% - 没有使用空间。
- Mounted on: /dev - 挂载在/dev目录。
3. tmpfs (多个条目):
- Filesystem Type: tmpfs - 这是一个临时文件系统,通常用于存储临时数据。
- Size: 1.9G - 总大小为1.9GB。
- Used: 0 或 8.5M - 使用的空间,某些tmpfs没有使用,另一个使用了8.5MB。
- Avail: 1.9G - 可用空间为1.9GB。
- Use%: 0% 或 1% - 使用空间的百分比。
- Mounted on: /dev/shm, /run, /sys/fs/cgroup - 分别挂载在这些目录。
4. /dev/sda1:
- Filesystem Type: xfs - 文件系统类型。
- Size: 1014M - 总大小为1014MB。
- Used: 139M - 已使用的空间为139MB。
- Avail: 876M - 可用空间为876MB。
- Use%: 14% - 已使用空间占总空间的14%。
- Mounted on: /boot - 挂载在/boot目录,通常用于存放启动相关文件。
5. tmpfs (最后一个条目):
- Size: 380M - 总大小为380MB。
- Used: 0 - 当前没有使用的空间。
- Avail: 380M - 可用空间为380MB。
- Use%: 0% - 没有使用空间。
- Mounted on: /run/user/1000 - 挂载在该用户的运行目录。
除了以上三个命令,还有一些展示PV, VG, LV 信息的命令,分别是 pvdisplay
, vgdisplay
, lvdisplay
, 具体的信息不再赘述。
二、扩容
下面以CentOS虚拟机的文件系统扩容为例说明。虚拟机初始的文件系统配置为上面命令列出的信息。下面我们为虚拟机增加一个硬盘,并将展示为逻辑卷cl-root
扩容。以及新建分区sdd1
和逻辑卷cl--test-tmp
。
首先为虚拟机增加一个10GB的硬盘4:
此时虚拟机文件系统中还未显示新增的硬盘,需要进行扫描才能发现新硬盘。
扫描命令:echo "- - -" | tee /sys/class/scsi_host/host*/scan
扫描前:
$lsblk
[root@localhost /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cl-root 253:0 0 37G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─cl-root 253:0 0 37G 0 lvm /
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 10G 0 part
└─cl--test-mnt 253:2 0 9.9G 0 lvm
sr0 11:0 1 1024M 0 rom
扫描后:
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cl-root 253:0 0 37G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─cl-root 253:0 0 37G 0 lvm /
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 10G 0 part
└─cl--test-mnt 253:2 0 9.9G 0 lvm
sdd 8:48 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
可以看到有了sdd
的硬盘。
下面对其进行格式化:
[root@localhost /]# fdisk /dev/sdd
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 0x46a298f6.
Command (m for help): m
Command action
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)
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):
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 /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cl-root 253:0 0 37G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─cl-root 253:0 0 37G 0 lvm /
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 10G 0 part
└─cl--test-mnt 253:2 0 9.9G 0 lvm
sdd 8:48 0 10G 0 disk
└─sdd1 8:49 0 10G 0 part
sr0 11:0 1 1024M 0 rom
这里我们新建了一个分区sdd1
。
然后创建新的PV sdd1
[root@localhost /]# pvcreate /dev/sdd1
File descriptor 4 (/dev/urandom) leaked on pvcreate invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on pvcreate invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on pvcreate invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on pvcreate invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on pvcreate invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on pvcreate invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on pvcreate invocation. Parent PID 2562: bash
Physical volume "/dev/sdd1" successfully created.
[root@localhost /]# pvdisplay
File descriptor 4 (/dev/urandom) leaked on pvdisplay invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on pvdisplay invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on pvdisplay invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on pvdisplay invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on pvdisplay invocation. Parent PID 2562: bash
--- Physical volume ---
PV Name /dev/sdc1
VG Name cl-test
PV Size 10.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2559
Free PE 24
Allocated PE 2535
PV UUID U5vq2W-GfSt-jKKi-yQN7-pjFb-NAen-0g3yKH
--- Physical volume ---
PV Name /dev/sda2
VG Name cl
PV Size 19.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 4863
Free PE 0
Allocated PE 4863
PV UUID OHjtNV-nwVq-v4C0-20j1-4EuQ-WFEf-BIH3ji
--- Physical volume ---
PV Name /dev/sdb1
VG Name cl
PV Size 20.00 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5119
Free PE 0
Allocated PE 5119
PV UUID IVVgN0-ASbP-A9Vx-ZrTV-oJgP-P66Y-RRKy15
"/dev/sdd1" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdd1
VG Name
PV Size 10.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID QJdrmq-SrCH-TyqR-Os6E-ecxf-qsIh-lFEK24
现在可以用vgcreate
从新建的PV创建一个新的VG,也可以用vgextend
对原有的VG进行扩展,接下来我们创建一个新的VG cl-tmp
:
[root@localhost /]# vgcreate cl-tmp /dev/sdd1
File descriptor 4 (/dev/urandom) leaked on vgcreate invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on vgcreate invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on vgcreate invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on vgcreate invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on vgcreate invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on vgcreate invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on vgcreate invocation. Parent PID 2562: bash
Volume group "cl-tmp" successfully created
[root@localhost /]# vgs
File descriptor 4 (/dev/urandom) leaked on vgs invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on vgs invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on vgs invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on vgs invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on vgs invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on vgs invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on vgs invocation. Parent PID 2562: bash
VG #PV #LV #SN Attr VSize VFree
cl 2 2 0 wz--n- 38.99g 0
cl-test 1 1 0 wz--n- 10.00g 96.00m
cl-tmp 1 0 0 wz--n- 10.00g 10.00g
接下来使用cl-tmp
创建一个新的LV lv-tmp
:
[root@localhost /]# lvcreate -L 10G -n lv-tmp cl-tmp
File descriptor 4 (/dev/urandom) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on lvcreate invocation. Parent PID 2562: bash
Volume group "cl-tmp" has insufficient free space (2559 extents): 2560 required.
[root@localhost /]# lvcreate -L 9.99G -n lv-tmp cl-tmp
File descriptor 4 (/dev/urandom) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on lvcreate invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on lvcreate invocation. Parent PID 2562: bash
Rounding up size to full physical extent 9.99 GiB
Logical volume "lv-tmp" created.
[root@localhost /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cl-root 253:0 0 37G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─cl-root 253:0 0 37G 0 lvm /
sdc 8:32 0 10G 0 disk
└─sdc1 8:33 0 10G 0 part
└─cl--test-mnt 253:2 0 9.9G 0 lvm
sdd 8:48 0 10G 0 disk
└─sdd1 8:49 0 10G 0 part
└─cl--tmp-lv--tmp 253:3 0 10G 0 lvm
sr0 11:0 1 1024M 0 rom
也可以使用lvextend
对原有的LV进行扩容,不再演示。
如果需要将LV lv-tmp
挂载到目录/temp
, 并且设置重启后也不会失效,可以执行以下命令:
[root@localhost /]# mkfs.ext4 /dev/cl-tmp/lv-tmp
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
655360 inodes, 2619392 blocks
130969 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2151677952
80 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information:
done
[root@localhost /]#
[root@localhost /]# mount /dev/cl-tmp/lv-tmp /temp
[root@localhost /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 37G 1.2G 36G 4% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 139M 876M 14% /boot
tmpfs 380M 0 380M 0% /run/user/1000
/dev/mapper/cl--tmp-lv--tmp 9.8G 37M 9.2G 1% /temp
# Add below config at last line
# /dev/mapper/cl--tmp-lv--tmp /temp ext4 defaults 0 0
[root@localhost /]# vi /etc/fstab
值得一提的是这里显示的文件系统是/dev/mapper/cl--tmp-lv--tmp
,mapper与逻辑卷lv-tmp
之间是一种映射关系,通过将逻辑存储空间映射到物理存储空间上,实现了文件系统对存储设备的抽象和管理。这种映射关系使得操作系统可以更方便地管理复杂的存储结构,提高了系统的灵活性和可靠性。
至此,我们就对文件系统扩容了10GB,并挂载在 /temp
。
对于为逻辑卷LV cl-root
扩容,在创建一个新的PV后,可以使用命令
vgextend cl /dev/newPV
, 然后对LV使用命令 lvextend -r -l +100%free cl-root
例如这里的cl-root
就是由sdb1和sda2共同组成根文件系统
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 19G 0 part
├─cl-root 253:0 0 37G 0 lvm /
└─cl-swap 253:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 20G 0 disk
└─sdb1 8:17 0 20G 0 part
└─cl-root 253:0 0 37G 0 lvm /
三、 缩容
对于缩容,以上面的LV lv-tmp
为例进行缩容,缩容时需要先对文件系统进行缩容,然后对LV 缩容,文件系统缩容需要略大于LV缩容量
Caution:
缩容前需要先unmount
[root@localhost /]# umount /temp
[root@localhost /]# e2fsck -f /dev/cl-tmp/lv-tmp
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/cl-tmp/lv-tmp: 11/655360 files (0.0% non-contiguous), 83129/2619392 blocks
[root@localhost /]# resize2fs /dev/cl-tmp/lv-tmp 5G
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/cl-tmp/lv-tmp to 1310720 (4k) blocks.
The filesystem on /dev/cl-tmp/lv-tmp is now 1310720 blocks long.
[root@localhost /]# lvreduce -L -4.5G /dev/cl-tmp/lv-tmp
File descriptor 4 (/dev/urandom) leaked on lvreduce invocation. Parent PID 2562: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241101T023011/ptyhost.log) leaked on lvreduce invocation. Parent PID 2562: bash
File descriptor 22 (/dev/ptmx) leaked on lvreduce invocation. Parent PID 2562: bash
File descriptor 23 (/dev/ptmx) leaked on lvreduce invocation. Parent PID 2562: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241101T023011/remoteagent.log) leaked on lvreduce invocation. Parent PID 2562: bash
File descriptor 26 (/dev/urandom) leaked on lvreduce invocation. Parent PID 2562: bash
File descriptor 28 (socket:[18609]) leaked on lvreduce invocation. Parent PID 2562: bash
WARNING: Reducing active logical volume to 5.49 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce cl-tmp/lv-tmp? [y/n]: y
Size of logical volume cl-tmp/lv-tmp changed from 9.99 GiB (2558 extents) to 5.49 GiB (1406 extents).
Logical volume cl-tmp/lv-tmp successfully resized.
这里我们将文件系统缩容为5GB, 而LV 为5.49GB。如果LV < 文件系统
,则会在mount时出现错误:
[root@localhost /]# mount /dev/cl-tmp/lv-tmp /temp
mount: wrong fs type, bad option, bad superblock on /dev/mapper/cl--tmp-lv--tmp,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
[root@localhost /]# dmesg | tail
[ 5619.300562] ata2: soft resetting link
[ 5619.462099] ata2: EH complete
[ 5931.774523] sdd: sdd1
[ 5931.777442] sdd: sdd1
[ 7324.829841] EXT4-fs (dm-3): mounted filesystem with ordered data mode. Opts: (null)
[ 7327.422217] dm-3: WRITE SAME failed. Manually zeroing.
[10823.982042] EXT4-fs (dm-3): bad geometry: block count 2619392 exceeds size of device (1308672 blocks)
[10994.642010] EXT4-fs (dm-3): bad geometry: block count 2619392 exceeds size of device (1308672 blocks)
[11080.188047] EXT4-fs (dm-3): mounted filesystem with ordered data mode. Opts: (null)
[11377.754966] EXT4-fs (dm-3): bad geometry: block count 1310720 exceeds size of device (1308672 blocks)
此时的解决方式是我们需要将LV 的容量恢复回去,然后再进行缩容。
主要用的命令是:
lvextend -L +5G /dev/cl-tmp/lv-tmp
resize2fs /dev/cl-tmp/lv-tmp
接下来我们演示一下将一个PV的PE移到另一个PV,然后移除第一个PV,先查看哪些PV有空闲的PE:
[root@localhost guest]# pvdisplay | grep "PV Name\|Total\| Free"
File descriptor 4 (/dev/urandom) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241102T021905/ptyhost.log) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 21 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 22 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 23 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 24 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241102T021905/remoteagent.log) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 26 (/dev/urandom) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 28 (socket:[19598]) leaked on pvdisplay invocation. Parent PID 2567: bash
PV Name /dev/sdc1
Total PE 2559
Free PE 24
PV Name /dev/sda2
Total PE 4863
Free PE 0
PV Name /dev/sdb1
Total PE 5119
Free PE 0
PV Name /dev/sdd1
Total PE 2559
Free PE 519
PV Name /dev/sdd2
Total PE 1279
Free PE 889
PV Name /dev/sdd3
Total PE 3839
Free PE 3839
可以看到/dev/sdd3
有3839个PE空闲,而/dev/sdd2
总共有1279个PE,空闲的有889个,所以可以将sdd2
中的PE移到属于同一个VGcl-tmp
的sdd3
中。
接下来就可以移动PE了:
[root@localhost guest]# pvmove /dev/sdd2:0-1278 /dev/sdd3
File descriptor 4 (/dev/urandom) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241102T021905/ptyhost.log) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 21 (/dev/ptmx) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 22 (/dev/ptmx) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 23 (/dev/ptmx) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 24 (/dev/ptmx) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241102T021905/remoteagent.log) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 26 (/dev/urandom) leaked on pvmove invocation. Parent PID 2567: bash
File descriptor 28 (socket:[19598]) leaked on pvmove invocation. Parent PID 2567: bash
/dev/sdd2: Moved: 0.00%
/dev/sdd2: Moved: 4.36%
/dev/sdd2: Moved: 8.97%
/dev/sdd2: Moved: 12.82%
/dev/sdd2: Moved: 17.44%
/dev/sdd2: Moved: 21.28%
/dev/sdd2: Moved: 24.36%
/dev/sdd2: Moved: 28.46%
/dev/sdd2: Moved: 32.82%
/dev/sdd2: Moved: 36.92%
/dev/sdd2: Moved: 41.28%
/dev/sdd2: Moved: 45.13%
/dev/sdd2: Moved: 48.97%
/dev/sdd2: Moved: 53.33%
/dev/sdd2: Moved: 57.95%
/dev/sdd2: Moved: 61.79%
/dev/sdd2: Moved: 65.64%
/dev/sdd2: Moved: 69.74%
/dev/sdd2: Moved: 74.36%
/dev/sdd2: Moved: 78.46%
/dev/sdd2: Moved: 82.82%
/dev/sdd2: Moved: 86.15%
/dev/sdd2: Moved: 90.00%
/dev/sdd2: Moved: 93.59%
/dev/sdd2: Moved: 97.18%
/dev/sdd2: Moved: 100.00%
[root@localhost guest]# pvdisplay | grep "PV Name\|Total\| Free"
File descriptor 4 (/dev/urandom) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241102T021905/ptyhost.log) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 21 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 22 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 23 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 24 (/dev/ptmx) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241102T021905/remoteagent.log) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 26 (/dev/urandom) leaked on pvdisplay invocation. Parent PID 2567: bash
File descriptor 28 (socket:[19598]) leaked on pvdisplay invocation. Parent PID 2567: bash
PV Name /dev/sdc1
Total PE 2559
Free PE 24
PV Name /dev/sda2
Total PE 4863
Free PE 0
PV Name /dev/sdb1
Total PE 5119
Free PE 0
PV Name /dev/sdd1
Total PE 2559
Free PE 519
PV Name /dev/sdd2
Total PE 1279
Free PE 1279
PV Name /dev/sdd3
Total PE 3839
Free PE 3449
[root@localhost guest]#
现在sdd2
中的PE全部为空闲的,此时可以从VG cl-tmp
移除PV /dev/sdd2
:
[root@localhost guest]# vgreduce cl-tmp /dev/sdd2
File descriptor 4 (/dev/urandom) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241102T021905/ptyhost.log) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 21 (/dev/ptmx) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 22 (/dev/ptmx) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 23 (/dev/ptmx) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 24 (/dev/ptmx) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241102T021905/remoteagent.log) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 26 (/dev/urandom) leaked on vgreduce invocation. Parent PID 2567: bash
File descriptor 28 (socket:[19598]) leaked on vgreduce invocation. Parent PID 2567: bash
Removed "/dev/sdd2" from volume group "cl-tmp"
最后移除PV /dev/sdd2
:
[root@localhost guest]# pvremove /dev/sdd2
File descriptor 4 (/dev/urandom) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241102T021905/ptyhost.log) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 21 (/dev/ptmx) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 22 (/dev/ptmx) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 23 (/dev/ptmx) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 24 (/dev/ptmx) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241102T021905/remoteagent.log) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 26 (/dev/urandom) leaked on pvremove invocation. Parent PID 2567: bash
File descriptor 28 (socket:[19598]) leaked on pvremove invocation. Parent PID 2567: bash
Labels on physical volume "/dev/sdd2" successfully wiped.
[root@localhost guest]# pvs
File descriptor 4 (/dev/urandom) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 20 (/home/guest/.vscode-server/data/logs/20241102T021905/ptyhost.log) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 21 (/dev/ptmx) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 22 (/dev/ptmx) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 23 (/dev/ptmx) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 24 (/dev/ptmx) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 25 (/home/guest/.vscode-server/data/logs/20241102T021905/remoteagent.log) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 26 (/dev/urandom) leaked on pvs invocation. Parent PID 2567: bash
File descriptor 28 (socket:[19598]) leaked on pvs invocation. Parent PID 2567: bash
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 19.00g 0
/dev/sdb1 cl lvm2 a-- 20.00g 0
/dev/sdc1 cl-test lvm2 a-- 10.00g 96.00m
/dev/sdd1 cl-tmp lvm2 a-- 10.00g 2.03g
/dev/sdd3 cl-tmp lvm2 a-- 15.00g 13.47g
结语
以上就是部分扩容缩容操作示例,更多操作读者可以自行测试。
附上操作大全: