磁盘的分区、格式化、挂载

使用fdisk -l列出系统上所有的磁盘分区信息。它会显示您系统中的硬盘设备以及每个设备上的分区情况,包括分区的起始扇区、大小、文件系统类型等。

 1fdisk -l
 2Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
 3Units: sectors of 1 * 512 = 512 bytes
 4Sector size (logical/physical): 512 bytes / 512 bytes
 5I/O size (minimum/optimal): 512 bytes / 512 bytes
 6Disklabel type: dos
 7Disk identifier: 0xa26fc47b
 8
 9Device     Boot   Start      End  Sectors Size Id Type
10/dev/vda1          2048  4194303  4192256   2G  e W95 FAT16 (LBA)
11/dev/vda2       4194304 83886079 79691776  38G 83 Linux
12
13
14Disk /dev/vdb: 8 GiB, 8589934592 bytes, 16777216 sectors
15Units: sectors of 1 * 512 = 512 bytes
16Sector size (logical/physical): 512 bytes / 512 bytes
17I/O size (minimum/optimal): 512 bytes / 512 bytes

使用df -h命令显示系统上各个文件系统的磁盘使用情况和剩余空间。它会显示文件系统的挂载点、总容量、已用空间、剩余空间和使用率等信息。

1df -h
2Filesystem      Size  Used Avail Use% Mounted on
3devtmpfs        4.0M     0  4.0M   0% /dev
4tmpfs           3.8G     0  3.8G   0% /dev/shm
5tmpfs           1.6G   17M  1.5G   2% /run
6tmpfs           4.0M     0  4.0M   0% /sys/fs/cgroup
7/dev/vda2        38G  1.2G   35G   4% /
8tmpfs           3.8G     0  3.8G   0% /tmp
9/dev/vda1       2.0G   95M  2.0G   5% /boot

磁盘/dev/vda上还有剩余空间,现在希望创建/dev/vda3分区,并将/dev/vda3挂载到/home。

备份/home中的内容到/root下:

1cp -r /home /root

创建分区/dev/vda3:

 1fdisk /dev/vda
 2Command (m for help): n
 3Partition type
 4   p   primary (2 primary, 0 extended, 2 free)
 5   e   extended (container for logical partitions)
 6Select (default p): p
 7Partition number (3,4, default 3): 3
 8First sector (83886080-209715199, default 83886080):
 9Last sector, +/-sectors or +/-size{K,M,G,T,P} (83886080-209715199, default 209715199):
10
11Created a new partition 3 of type 'Linux' and of size 60 GiB.
12
13Command (m for help): w
14The partition table has been altered.
15Syncing disks.

查看磁盘分区:

 1fdisk -l
 2Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
 3Units: sectors of 1 * 512 = 512 bytes
 4Sector size (logical/physical): 512 bytes / 512 bytes
 5I/O size (minimum/optimal): 512 bytes / 512 bytes
 6Disklabel type: dos
 7Disk identifier: 0xa26fc47b
 8
 9Device     Boot    Start       End   Sectors Size Id Type
10/dev/vda1           2048   4194303   4192256   2G  e W95 FAT16 (LBA)
11/dev/vda2        4194304  83886079  79691776  38G 83 Linux
12/dev/vda3       83886080 209715199 125829120  60G 83 Linux
13
14
15Disk /dev/vdb: 8 GiB, 8589934592 bytes, 16777216 sectors
16Units: sectors of 1 * 512 = 512 bytes
17Sector size (logical/physical): 512 bytes / 512 bytes
18I/O size (minimum/optimal): 512 bytes / 512 bytes

格式化磁盘分区/dev/vda3为ext4文件系统:

1mkfs.ext4 /dev/vda3

编辑/etc/fstab,添加下边的挂载记录:

1/dev/vda3   /home  ext4   defaults   0   0

/etc/fstab 文件中,每一行都描述了一个要挂载的文件系统。其中,每个字段的含义如下:

  1. 设备路径(Device):这是要挂载的设备的路径,例如 /dev/vda3
  2. 挂载点路径(Mount Point):这是你想要将设备挂载到的目标路径,例如 /home/new_home
  3. 文件系统类型(File System Type):这是设备上的文件系统类型,例如 ext4
  4. 选项(Options):这是一系列用逗号分隔的选项,用于控制挂载的行为。defaults 是一个常见的选项,它包含了一组默认的挂载选项,如读写权限、atime 更新等。你也可以使用其他选项,根据你的需求。常见选项包括:
    • rw:以读写模式挂载。
    • ro:以只读模式挂载。
    • noatime:不更新文件访问时间,可以提高性能。
    • errors=remount-ro:在发生错误时以只读模式重新挂载。
    • discard:启用 TRIM 命令,用于固态硬盘的垃圾回收。
  5. Dump 标志:通常设置为 0,表示不需要备份。如果设置为 1,则表示需要备份。
  6. Pass 标志:这是文件系统检查的顺序。通常设置为 2,表示在系统引导时会检查文件系统。当 Pass 设置为 0 时,表示文件系统在系统引导时不需要进行检查。在 /etc/fstab 中,Pass 字段表示文件系统检查的顺序,可以设置为以下值:
    • 0: 表示不需要进行文件系统检查。这通常用于不需要定期检查的文件系统,例如非系统分区。
    • 1: 表示在引导时需要进行文件系统检查,但是会在检查时将根文件系统标记为“已检查”。这适用于根文件系统。
    • 2: 表示在引导时需要进行文件系统检查,而且不会将根文件系统标记为“已检查”。 选择正确的 Pass 值取决于文件系统的用途和特性。通常情况下,根文件系统(/)会设置为 1,其他分区可以设置为 0,以减少引导时间。

重新挂载:

1mount -a

将/root中备份的home目录拷贝会/home:

1cp -r /root/home /

查看文件系统的挂载情况:

1df -h | grep /dev/vda
2/dev/vda2        38G  1.2G   35G   4% /
3/dev/vda1       2.0G   95M  2.0G   5% /boot
4/dev/vda3        59G   52K   56G   1% /home

最后重启一次系统,确认启动后一切正常

逻辑卷管理

在 Linux 中,逻辑卷管理器(Logical Volume Manager,LVM)是一种用于管理磁盘存储的技术,它引入了物理卷、卷组和逻辑卷等概念,以提供更灵活的存储管理。以下是它们之间的关系:

  1. 物理卷(Physical Volume,PV):物理卷是物理磁盘或分区的抽象,它可以是硬盘、SSD、RAID 阵列等。

  2. 卷组(Volume Group,VG):卷组是由一个或多个物理卷组成的逻辑单元,它是在物理卷上创建的。卷组可以看作是逻辑卷管理的容器,可以在其中创建和分配逻辑卷。卷组内的所有物理卷的存储空间累加形成卷组的总存储空间。

  3. 逻辑卷(Logical Volume,LV):逻辑卷是在卷组上创建的抽象卷。它是用户实际使用的卷,可以看作是格式化和挂载的“虚拟磁盘”。逻辑卷的大小和数量是可以根据需要调整的,这为动态存储管理提供了很大的灵活性。

关系示意图:

 1    +-----------------+  +---------------------+
 2    |   Logical Volume|  |       Logical Volume|
 3    |      (LV)       |  |          (LV)        |
 4    +-----------------+  +---------------------+
 5            |                       |
 6            |                       |
 7            |                       |
 8    +----------------+     +-----------------+
 9    |  Volume Group |     |  Volume Group   |
10    |      (VG)      |     |      (VG)       |
11    +----------------+     +-----------------+
12            |                       |
13            |                       |
14            |                       |
15    +----------------+     +-----------------+
16    |  Physical Vol  |     |  Physical Vol   |
17    |      (PV)      |     |      (PV)       |
18    +----------------+     +-----------------+
19           Disk 1                  Disk 2

总结来说,物理卷是磁盘或分区,卷组是物理卷的组合,逻辑卷是用户使用的逻辑单元,它们一起构成了 LVM 管理的层次结构,为存储管理提供了更高的灵活性和可管理性。

逻辑卷扩容

磁盘分区信息如下:

 1fdisk -l
 2Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
 3Units: sectors of 1 * 512 = 512 bytes
 4Sector size (logical/physical): 512 bytes / 512 bytes
 5I/O size (minimum/optimal): 512 bytes / 512 bytes
 6Disklabel type: gpt
 7Disk identifier: 1BAB51F2-35A6-4997-9410-ECCD72C9FE8B
 8
 9Device       Start      End  Sectors  Size Type
10/dev/vda1     2048   204799   202752   99M EFI System
11/dev/vda2   204800  2252799  2048000 1000M Linux filesystem
12/dev/vda3  2252800  2260991     8192    4M PowerPC PReP boot
13/dev/vda4  2260992  2263039     2048    1M BIOS boot
14/dev/vda5  2265088 20969471 18704384  8.9G Linux LVM
15
16
17Disk /dev/vdb: 8 GiB, 8589934592 bytes, 16777216 sectors
18Units: sectors of 1 * 512 = 512 bytes
19Sector size (logical/physical): 512 bytes / 512 bytes
20I/O size (minimum/optimal): 512 bytes / 512 bytes
21
22
23Disk /dev/mapper/rocky-root: 8.9 GiB, 9575596032 bytes, 18702336 sectors
24Units: sectors of 1 * 512 = 512 bytes
25Sector size (logical/physical): 512 bytes / 512 bytes
26I/O size (minimum/optimal): 512 bytes / 512 bytes

文件系统信息如下:

 1df -h
 2Filesystem              Size  Used Avail Use% Mounted on
 3devtmpfs                3.8G     0  3.8G   0% /dev
 4tmpfs                   3.8G     0  3.8G   0% /dev/shm
 5tmpfs                   3.8G  8.5M  3.8G   1% /run
 6tmpfs                   3.8G     0  3.8G   0% /sys/fs/cgroup
 7/dev/mapper/rocky-root  9.0G  1.3G  7.7G  14% /
 8/dev/vda2               994M  237M  758M  24% /boot
 9/dev/vda1                99M  5.8M   94M   6% /boot/efi
10tmpfs                   770M     0  770M   0% /run/user/1001

查看物理卷信息:

 1 pvdisplay
 2  --- Physical volume ---
 3  PV Name               /dev/vda5
 4  VG Name               rocky
 5  PV Size               <8.92 GiB / not usable 0
 6  Allocatable           yes (but full)
 7  PE Size               4.00 MiB
 8  Total PE              2283
 9  Free PE               0
10  Allocated PE          2283
11  PV UUID               etpBfN-XgGU-mzWh-TA5i-uk6C-n0ft-XAOzYn

查看卷组信息:

 1vgdisplay
 2  --- Volume group ---
 3  VG Name               rocky
 4  System ID
 5  Format                lvm2
 6  Metadata Areas        1
 7  Metadata Sequence No  2
 8  VG Access             read/write
 9  VG Status             resizable
10  MAX LV                0
11  Cur LV                1
12  Open LV               1
13  Max PV                0
14  Cur PV                1
15  Act PV                1
16  VG Size               <8.92 GiB
17  PE Size               4.00 MiB
18  Total PE              2283
19  Alloc PE / Size       2283 / <8.92 GiB
20  Free  PE / Size       0 / 0
21  VG UUID               zHG4uV-t2gx-fhCH-Gm8p-YzLZ-Fcto-Kvpz2H

查看逻辑卷信息:

 1lvdisplay
 2  --- Logical volume ---
 3  LV Path                /dev/rocky/root
 4  LV Name                root
 5  VG Name                rocky
 6  LV UUID                4F5G28-hbpz-uJhP-l5VN-ceFn-KGUd-ZJAeBr
 7  LV Write Access        read/write
 8  LV Status              available
 9  # open                 1
10  LV Size                <8.92 GiB
11  Current LE             2283
12  Segments               1
13  Allocation             inherit
14  Read ahead sectors     auto
15  - currently set to     8192
16  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:

 1fdisk /dev/vda
 2
 3Command (m for help): n
 4Partition number (6-128, default 6): 6
 5First sector (20969472-209715166, default 20969472):
 6Last sector, +sectors or +size{K,M,G,T,P} (20969472-209715166, default 209715166):
 7
 8Created a new partition 6 of type 'Linux filesystem' and of size 90 GiB.
 9
10Command (m for help): w
11The partition table has been altered.
12Syncing disks.

查看磁盘分区:

1fdisk -l | grep /dev/vda
2Disk /dev/vda: 100 GiB, 107374182400 bytes, 209715200 sectors
3/dev/vda1      2048    204799    202752   99M EFI System
4/dev/vda2    204800   2252799   2048000 1000M Linux filesystem
5/dev/vda3   2252800   2260991      8192    4M PowerPC PReP boot
6/dev/vda4   2260992   2263039      2048    1M BIOS boot
7/dev/vda5   2265088  20969471  18704384  8.9G Linux LVM
8/dev/vda6  20969472 209715166 188745695   90G Linux filesystem

创建物理卷:

1pvcreate /dev/vda6
2 Physical volume "/dev/vda6" successfully created.

将pv /dev/vda6加入到卷组rocky中:

1vgextend rocky /dev/vda6
2  Volume group "rocky" successfully extended

对逻辑卷/dev/rocky/root进行扩容:

1lvextend -l +100%FREE /dev/rocky/root
2xfs_growfs /dev/rocky/root

最后,查看并确认物理卷、卷组、逻辑卷和文件系统信息:

 1pvdisplay
 2  --- Physical volume ---
 3  PV Name               /dev/vda5
 4  VG Name               rocky
 5  PV Size               <8.92 GiB / not usable 0
 6  Allocatable           yes (but full)
 7  PE Size               4.00 MiB
 8  Total PE              2283
 9  Free PE               0
10  Allocated PE          2283
11  PV UUID               etpBfN-XgGU-mzWh-TA5i-uk6C-n0ft-XAOzYn
12
13  --- Physical volume ---
14  PV Name               /dev/vda6
15  VG Name               rocky
16  PV Size               90.00 GiB / not usable 4.98 MiB
17  Allocatable           yes (but full)
18  PE Size               4.00 MiB
19  Total PE              23039
20  Free PE               0
21  Allocated PE          23039
22  PV UUID               BgcdSe-kAmp-fJRG-mG8H-WZa5-mcuC-r7R30U
23
24
25vgdisplay
26  --- Volume group ---
27  VG Name               rocky
28  System ID
29  Format                lvm2
30  Metadata Areas        2
31  Metadata Sequence No  4
32  VG Access             read/write
33  VG Status             resizable
34  MAX LV                0
35  Cur LV                1
36  Open LV               1
37  Max PV                0
38  Cur PV                2
39  Act PV                2
40  VG Size               98.91 GiB
41  PE Size               4.00 MiB
42  Total PE              25322
43  Alloc PE / Size       25322 / 98.91 GiB
44  Free  PE / Size       0 / 0
45  VG UUID               zHG4uV-t2gx-fhCH-Gm8p-YzLZ-Fcto-Kvpz2H
46
47
48lvdisplay
49  --- Logical volume ---
50  LV Path                /dev/rocky/root
51  LV Name                root
52  VG Name                rocky
53  LV UUID                4F5G28-hbpz-uJhP-l5VN-ceFn-KGUd-ZJAeBr
54  LV Write Access        read/write
55  LV Status              available
56  # open                 1
57  LV Size                98.91 GiB
58  Current LE             25322
59  Segments               2
60  Allocation             inherit
61  Read ahead sectors     auto
62  - currently set to     8192
63  Block device           253:0
64
65df -h | grep /dev/
66tmpfs                   3.8G     0  3.8G   0% /dev/shm
67/dev/mapper/rocky-root   99G  1.9G   98G   2% /
68/dev/vda2               994M  237M  758M  24% /boot
69/dev/vda1                99M  5.8M   94M   6% /boot/efi