基于Esxi系统下对Ubuntu 22.04扩容LVM逻辑卷和文件系统

1,208次阅读
没有评论

共计 4601 个字符,预计需要花费 12 分钟才能阅读完成。

步骤

    1. 登录 Esxi 后

      对虚拟机进行扩容操作,我这里直接从 50G 调整到了 80G,更改完成后保存即可,接下来就要到机器内继续进行后续操作。基于 Esxi 系统下对 Ubuntu 22.04 扩容 LVM 逻辑卷和文件系统

    2. 登录虚拟机后

      使用 lsblk 命令检查发现,增加的空间到了硬盘,并没有到所需要的分区。其实这里可以重新对硬盘进行分区,创建 lvm 逻辑卷进行扩容,本着折腾为主,尝试直接对 /dev/nvme0n1 进行扩容。

      
      root@bot1:~# lsblk
      NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
      loop0                       7:0    0  63.4M  1 loop /snap/core20/1974
      loop1                       7:1    0  63.5M  1 loop /snap/core20/2015
      loop2                       7:2    0  79.9M  1 loop /snap/lxd/22923
      loop3                       7:3    0 111.9M  1 loop /snap/lxd/24322
      loop4                       7:4    0  53.3M  1 loop /snap/snapd/19457
      loop5                       7:5    0  40.9M  1 loop /snap/snapd/20290
      nvme0n1                   259:0    0    80G  0 disk 
      ├─nvme0n1p1               259:1    0     1M  0 part 
      ├─nvme0n1p2               259:2    0     2G  0 part /boot
      └─nvme0n1p3               259:3    0    48G  0 part 
        └─ubuntu--vg-ubuntu--lv 253:0    0    48G  0 lvm  /
      
    3. 使用 fdisk 命令对硬盘进行分区操作。(此步骤风险较高,不建议在实际生产环境中进行使用。)

      fdisk /dev/nvme0n1

      这里我先输入 p,查看扇区,记录下起始扇区,后面重新分区时,要保持一致

      
      root@bot1:~# fdisk /dev/nvme0n1
      
      Welcome to fdisk (util-linux 2.37.2).
      Changes will remain in memory only, until you decide to write them.
      Be careful before using the write command.
      
      This disk is currently in use - repartitioning is probably a bad idea.
      It's recommended to umount all file systems, and swapoff all swap
      partitions on this disk.
      
      
      Command (m for help): p  #查看扇区,记录 /dev/nvme0n1p3 起始扇区号
      
      Disk /dev/nvme0n1: 80 GiB, 85899345920 bytes, 167772160 sectors
      Disk model: Samsung SSD 960 128GB   
      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: 11C9A37D-A40C-4917-BE0F-0182387AF207
      
      Device           Start       End   Sectors Size Type
      /dev/nvme0n1p1    2048      4095      2048   1M BIOS boot
      /dev/nvme0n1p2    4096   4198399   4194304   2G Linux filesystem
      /dev/nvme0n1p3 4198400 104857566 100659167  48G Linux filesystem  #扇区起始号为 4198400
      

      删除分区 3

      
      Command (m for help): d  #删除分区
      Partition number (1-3, default 3): 3  #删除 3 号分区
      
      Partition 3 has been deleted.
      

      新建分区 3

      
      Command (m for help): n  #新建分区
      Partition number (3-128, default 3): 3  #分区号为 3,与之前的保持一致
      First sector (4198400-167772126, default 4198400): 4198400  #扇区起始号,与之前的保持一致
      Last sector, +/-sectors or +/-size{K,M,G,T,P} (4198400-167772126, default 167772126): 167772126  #结束扇区,这里直接回车默认使用所有
      
      Created a new partition 3 of type 'Linux filesystem' and of size 78 GiB.
      Partition #3 contains a LVM2_member signature.
      
      Do you want to remove the signature? [Y]es/[N]o: n  #这里一定要选 n,不然原来存在的 lvm 卷就会被干掉
      
      Command (m for help): w   #最后输入 w,保存退出
      
      The partition table has been altered.
      Syncing disks.
      

      然后执行 lsblk 查看 /dev/nvme0n1p3 的空间已经由原来的 48G 增加至 78G

      
      root@bot1:~# lsblk
      NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
      loop0                       7:0    0  63.4M  1 loop /snap/core20/1974
      loop1                       7:1    0  63.5M  1 loop /snap/core20/2015
      loop2                       7:2    0  79.9M  1 loop /snap/lxd/22923
      loop3                       7:3    0 111.9M  1 loop /snap/lxd/24322
      loop4                       7:4    0  53.3M  1 loop /snap/snapd/19457
      loop5                       7:5    0  40.9M  1 loop /snap/snapd/20290
      nvme0n1                   259:0    0    80G  0 disk 
      ├─nvme0n1p1               259:1    0     1M  0 part 
      ├─nvme0n1p2               259:2    0     2G  0 part /boot
      └─nvme0n1p3               259:3    0    78G  0 part 
        └─ubuntu--vg-ubuntu--lv 253:0    0    48G  0 lvm  /
      
    4. 此时虽然增加了 /dev/nvme0n1p3 的可用空间,但是 lvm 物理卷空间是没有增加的,执行 vgdisplay 查看

      
      root@bot1:~# vgdisplay
        --- Volume group ---
        VG Name               ubuntu-vg
        System ID             
        Format                lvm2
        Metadata Areas        1
        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                1
        Act PV                1
        VG Size               <48.00 GiB
        PE Size               4.00 MiB
        Total PE              12287
        Alloc PE / Size       12287 / <48.00 GiB
        Free  PE / Size       0 / 0   
        VG UUID               j09yxE-ZLni-dx1R-Zh1u-9hpZ-OcHd-Q59uBC
      
      
    5. 接着折腾 lvm 物理卷,使用 pvresize 命令对物理卷大小进行调整

      
      root@bot1:~# vgs  #查看空间
        VG        #PV #LV #SN Attr   VSize   VFree
        ubuntu-vg   1   1   0 wz--n- <48.00g    0 
      
      root@bot1:~# pvresize /dev/nvme0n1p3  #执行
        Physical volume "/dev/nvme0n1p3" changed
        1 physical volume(s) resized or updated / 0 physical volume(s) not resized
      
      root@bot1:~# vgdisplay ubuntu-vg  #检查可用空间是否增加
        --- Volume group ---
        VG Name               ubuntu-vg
        System ID             
        Format                lvm2
        Metadata Areas        1
        Metadata Sequence No  5
        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               <78.00 GiB
        PE Size               4.00 MiB
        Total PE              19967
        Alloc PE / Size       12287 / <48.00 GiB
        Free  PE / Size       7680 / 30.00 GiB  #这里可用空间已经增加
        VG UUID               j09yxE-ZLni-dx1R-Zh1u-9hpZ-OcHd-Q59uBC
      
      
    6. 还需要将可用的物理卷空间扩容至逻辑卷

      
      root@bot1:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv  #将可用空间全部给逻辑卷
        Size of logical volume ubuntu-vg/ubuntu-lv changed from <48.00 GiB (12287 extents) to <78.00 GiB (19967 extents).
        Logical volume ubuntu-vg/ubuntu-lv successfully resized.
      
      root@bot1:~# resize2fs /dev/ubuntu-vg/ubuntu-lv  #重新调整一下大小
      resize2fs 1.46.5 (30-Dec-2021)
      Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
      old_desc_blocks = 6, new_desc_blocks = 10
      The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 20446208 (4k) blocks long.
      
      
    7. 最后看看根目录空间是否已经增加

      
      root@bot1:~# lsblk
      NAME                      MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
      loop0                       7:0    0  63.4M  1 loop /snap/core20/1974
      loop1                       7:1    0  63.5M  1 loop /snap/core20/2015
      loop2                       7:2    0  79.9M  1 loop /snap/lxd/22923
      loop3                       7:3    0 111.9M  1 loop /snap/lxd/24322
      loop4                       7:4    0  53.3M  1 loop /snap/snapd/19457
      loop5                       7:5    0  40.9M  1 loop /snap/snapd/20290
      nvme0n1                   259:0    0    80G  0 disk 
      ├─nvme0n1p1               259:1    0     1M  0 part 
      ├─nvme0n1p2               259:2    0     2G  0 part /boot
      └─nvme0n1p3               259:3    0    78G  0 part 
        └─ubuntu--vg-ubuntu--lv 253:0    0    78G  0 lvm  /
      
      root@bot1:~# df -Th
      Filesystem                        Type   Size  Used Avail Use% Mounted on
      tmpfs                             tmpfs  1.6G  1.2M  1.6G   1% /run
      /dev/mapper/ubuntu--vg-ubuntu--lv ext4    77G   13G   61G  18% /
      tmpfs                             tmpfs  7.9G     0  7.9G   0% /dev/shm
      tmpfs                             tmpfs  5.0M     0  5.0M   0% /run/lock
      /dev/nvme0n1p2                    ext4   2.0G  252M  1.6G  14% /boot
      tmpfs                             tmpfs  1.6G  4.0K  1.6G   1% /run/user/0
      
      

完结

扩容结束,完结撒花

正文完
 1
评论(没有评论)
验证码