磁盘的分区、格式化、挂载
使用fdisk -l列出系统上所有的磁盘分区信息。它会显示您系统中的硬盘设备以及每个设备上的分区情况,包括分区的起始扇区、大小、文件系统类型等。
fdisk -l
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xa26fc47b
Device Boot Start End Sectors Size Id Type
/dev/vda1 2048 4194303 4192256 2G e W95 FAT16 (LBA)
/dev/vda2 4194304 83886079 79691776 38G 83 Linux
Disk /dev/vdb: 8 GiB, 8589934592 bytes, 16777216 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使用df -h命令显示系统上各个文件系统的磁盘使用情况和剩余空间。它会显示文件系统的挂载点、总容量、已用空间、剩余空间和使用率等信息。
df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 1.6G 17M 1.5G 2% /run
tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup
/dev/vda2 38G 1.2G 35G 4% /
tmpfs 3.8G 0 3.8G 0% /tmp
/dev/vda1 2.0G 95M 2.0G 5% /boot磁盘/dev/vda上还有剩余空间,现在希望创建/dev/vda3分区,并将/dev/vda3挂载到/home。
备份/home中的内容到/root下:
cp -r /home /root创建分区/dev/vda3:
fdisk /dev/vda
Command (m for help): n
Partition type
p primary (2 primary, 0 extended, 2 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 3
First sector (83886080-209715199, default 83886080):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (83886080-209715199, default 209715199):
Created a new partition 3 of type 'Linux' and of size 60 GiB.
Command (m for help): w
The partition table has been altered.
Syncing disks.查看磁盘分区:
fdisk -l
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: dos
Disk identifier: 0xa26fc47b
Device Boot Start End Sectors Size Id Type
/dev/vda1 2048 4194303 4192256 2G e W95 FAT16 (LBA)
/dev/vda2 4194304 83886079 79691776 38G 83 Linux
/dev/vda3 83886080 209715199 125829120 60G 83 Linux
Disk /dev/vdb: 8 GiB, 8589934592 bytes, 16777216 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格式化磁盘分区/dev/vda3为ext4文件系统:
mkfs.ext4 /dev/vda3编辑/etc/fstab,添加下边的挂载记录:
/dev/vda3 /home ext4 defaults 0 0在
/etc/fstab文件中,每一行都描述了一个要挂载的文件系统。其中,每个字段的含义如下:
- 设备路径(Device):这是要挂载的设备的路径,例如
/dev/vda3。 - 挂载点路径(Mount Point):这是你想要将设备挂载到的目标路径,例如
/home/new_home。 - 文件系统类型(File System Type):这是设备上的文件系统类型,例如
ext4。 - 选项(Options):这是一系列用逗号分隔的选项,用于控制挂载的行为。
defaults是一个常见的选项,它包含了一组默认的挂载选项,如读写权限、atime 更新等。你也可以使用其他选项,根据你的需求。常见选项包括:rw:以读写模式挂载。ro:以只读模式挂载。noatime:不更新文件访问时间,可以提高性能。errors=remount-ro:在发生错误时以只读模式重新挂载。discard:启用 TRIM 命令,用于固态硬盘的垃圾回收。
- Dump 标志:通常设置为
0,表示不需要备份。如果设置为1,则表示需要备份。 - Pass 标志:这是文件系统检查的顺序。通常设置为
2,表示在系统引导时会检查文件系统。当Pass设置为0时,表示文件系统在系统引导时不需要进行检查。在/etc/fstab中,Pass字段表示文件系统检查的顺序,可以设置为以下值:0: 表示不需要进行文件系统检查。这通常用于不需要定期检查的文件系统,例如非系统分区。1: 表示在引导时需要进行文件系统检查,但是会在检查时将根文件系统标记为“已检查”。这适用于根文件系统。2: 表示在引导时需要进行文件系统检查,而且不会将根文件系统标记为“已检查”。 选择正确的Pass值取决于文件系统的用途和特性。通常情况下,根文件系统(/)会设置为1,其他分区可以设置为0,以减少引导时间。
重新挂载:
mount -a将/root中备份的home目录拷贝会/home:
cp -r /root/home /查看文件系统的挂载情况:
df -h | grep /dev/vda
/dev/vda2 38G 1.2G 35G 4% /
/dev/vda1 2.0G 95M 2.0G 5% /boot
/dev/vda3 59G 52K 56G 1% /home最后重启一次系统,确认启动后一切正常
逻辑卷管理
在 Linux 中,逻辑卷管理器(Logical Volume Manager,LVM)是一种用于管理磁盘存储的技术,它引入了物理卷、卷组和逻辑卷等概念,以提供更灵活的存储管理。以下是它们之间的关系:
物理卷(Physical Volume,PV):物理卷是物理磁盘或分区的抽象,它可以是硬盘、SSD、RAID 阵列等。
卷组(Volume Group,VG):卷组是由一个或多个物理卷组成的逻辑单元,它是在物理卷上创建的。卷组可以看作是逻辑卷管理的容器,可以在其中创建和分配逻辑卷。卷组内的所有物理卷的存储空间累加形成卷组的总存储空间。
逻辑卷(Logical Volume,LV):逻辑卷是在卷组上创建的抽象卷。它是用户实际使用的卷,可以看作是格式化和挂载的“虚拟磁盘”。逻辑卷的大小和数量是可以根据需要调整的,这为动态存储管理提供了很大的灵活性。
关系示意图:
+-----------------+ +---------------------+
| Logical Volume| | Logical Volume|
| (LV) | | (LV) |
+-----------------+ +---------------------+
| |
| |
| |
+----------------+ +-----------------+
| Volume Group | | Volume Group |
| (VG) | | (VG) |
+----------------+ +-----------------+
| |
| |
| |
+----------------+ +-----------------+
| Physical Vol | | Physical Vol |
| (PV) | | (PV) |
+----------------+ +-----------------+
Disk 1 Disk 2总结来说,物理卷是磁盘或分区,卷组是物理卷的组合,逻辑卷是用户使用的逻辑单元,它们一起构成了 LVM 管理的层次结构,为存储管理提供了更高的灵活性和可管理性。
逻辑卷扩容
磁盘分区信息如下:
fdisk -l
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 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
Disklabel type: gpt
Disk identifier: 1BAB51F2-35A6-4997-9410-ECCD72C9FE8B
Device Start End Sectors Size Type
/dev/vda1 2048 204799 202752 99M EFI System
/dev/vda2 204800 2252799 2048000 1000M Linux filesystem
/dev/vda3 2252800 2260991 8192 4M PowerPC PReP boot
/dev/vda4 2260992 2263039 2048 1M BIOS boot
/dev/vda5 2265088 20969471 18704384 8.9G Linux LVM
Disk /dev/vdb: 8 GiB, 8589934592 bytes, 16777216 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/rocky-root: 8.9 GiB, 9575596032 bytes, 18702336 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文件系统信息如下:
df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs 3.8G 8.5M 3.8G 1% /run
tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/rocky-root 9.0G 1.3G 7.7G 14% /
/dev/vda2 994M 237M 758M 24% /boot
/dev/vda1 99M 5.8M 94M 6% /boot/efi
tmpfs 770M 0 770M 0% /run/user/1001查看物理卷信息:
pvdisplay
--- Physical volume ---
PV Name /dev/vda5
VG Name rocky
PV Size <8.92 GiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2283
Free PE 0
Allocated PE 2283
PV UUID etpBfN-XgGU-mzWh-TA5i-uk6C-n0ft-XAOzYn查看卷组信息:
vgdisplay
--- Volume group ---
VG Name rocky
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size <8.92 GiB
PE Size 4.00 MiB
Total PE 2283
Alloc PE / Size 2283 / <8.92 GiB
Free PE / Size 0 / 0
VG UUID zHG4uV-t2gx-fhCH-Gm8p-YzLZ-Fcto-Kvpz2H查看逻辑卷信息:
lvdisplay
--- Logical volume ---
LV Path /dev/rocky/root
LV Name root
VG Name rocky
LV UUID 4F5G28-hbpz-uJhP-l5VN-ceFn-KGUd-ZJAeBr
LV Write Access read/write
LV Status available
# open 1
LV Size <8.92 GiB
Current LE 2283
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0磁盘/dev/vda的分区/dev/vda5对应物理卷/dev/vda5带下为8.92GiB。物理卷/dev/vda5属于卷组rocky,卷组rocky当前的大小为8.92GiB。
从卷组rocky创建了了逻辑卷/dev/rocky/root大小为8.92GiB。
因为磁盘/dev/vda还有剩余空间,现在希望将逻辑卷/dev/rocky/root扩容。
将磁盘/dev/vda剩余空间创建为新的分区/dev/vda6:
fdisk /dev/vda
Command (m for help): n
Partition number (6-128, default 6): 6
First sector (20969472-209715166, default 20969472):
Last sector, +sectors or +size{K,M,G,T,P} (20969472-209715166, default 209715166):
Created a new partition 6 of type 'Linux filesystem' and of size 90 GiB.
Command (m for help): w
The partition table has been altered.
Syncing disks.查看磁盘分区:
fdisk -l | grep /dev/vda
Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
/dev/vda1 2048 204799 202752 99M EFI System
/dev/vda2 204800 2252799 2048000 1000M Linux filesystem
/dev/vda3 2252800 2260991 8192 4M PowerPC PReP boot
/dev/vda4 2260992 2263039 2048 1M BIOS boot
/dev/vda5 2265088 20969471 18704384 8.9G Linux LVM
/dev/vda6 20969472 209715166 188745695 90G Linux filesystem创建物理卷:
pvcreate /dev/vda6
Physical volume "/dev/vda6" successfully created.将pv /dev/vda6加入到卷组rocky中:
vgextend rocky /dev/vda6
Volume group "rocky" successfully extended对逻辑卷/dev/rocky/root进行扩容:
lvextend -l +100%FREE /dev/rocky/root
xfs_growfs /dev/rocky/root最后,查看并确认物理卷、卷组、逻辑卷和文件系统信息:
pvdisplay
--- Physical volume ---
PV Name /dev/vda5
VG Name rocky
PV Size <8.92 GiB / not usable 0
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 2283
Free PE 0
Allocated PE 2283
PV UUID etpBfN-XgGU-mzWh-TA5i-uk6C-n0ft-XAOzYn
--- Physical volume ---
PV Name /dev/vda6
VG Name rocky
PV Size 90.00 GiB / not usable 4.98 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 23039
Free PE 0
Allocated PE 23039
PV UUID BgcdSe-kAmp-fJRG-mG8H-WZa5-mcuC-r7R30U
vgdisplay
--- Volume group ---
VG Name rocky
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 98.91 GiB
PE Size 4.00 MiB
Total PE 25322
Alloc PE / Size 25322 / 98.91 GiB
Free PE / Size 0 / 0
VG UUID zHG4uV-t2gx-fhCH-Gm8p-YzLZ-Fcto-Kvpz2H
lvdisplay
--- Logical volume ---
LV Path /dev/rocky/root
LV Name root
VG Name rocky
LV UUID 4F5G28-hbpz-uJhP-l5VN-ceFn-KGUd-ZJAeBr
LV Write Access read/write
LV Status available
# open 1
LV Size 98.91 GiB
Current LE 25322
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
df -h | grep /dev/
tmpfs 3.8G 0 3.8G 0% /dev/shm
/dev/mapper/rocky-root 99G 1.9G 98G 2% /
/dev/vda2 994M 237M 758M 24% /boot
/dev/vda1 99M 5.8M 94M 6% /boot/efi