虚拟机linux扩容

虚拟机ubuntu 扩容

ubuntu2004

原文:https://blog.csdn.net/changqing_xu/article/details/129286088

1.使用fdisk命令对硬盘进行分区操作。

fdisk /dev/sda

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

root@0-125:~# fdisk /dev/sda

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p   #查看扇区,记录sda3起始扇区号

Disk /dev/sda: 80 GiB, 85899345920 bytes, 167772160 sectors
Disk model: Virtual disk
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: 7F13220A-DDDB-44AD-BC0D-E3F369DDE708

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3149823  3145728  1.5G Linux filesystem
/dev/sda3  3149824 83884031 80734208 38.5G Linux filesystem    #扇区起始号为3149824

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

Partition 3 has been deleted.

Command (m for help): n   #新建分区
Partition number (3-128, default 3): 3   #分区号为3,与之前的保持一致
First sector (3149824-167772126, default 3149824): 3149824    #扇区起始号,与之前的保持一致
Last sector, +/-sectors or +/-size{K,M,G,T,P} (3149824-167772126, default 167772126):   #结束扇区,这里直接回车默认使用所有

Created a new partition 3 of type 'Linux filesystem' and of size 78.5 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/sda3的空间已经由原来的38.5G增加至78.5G

root@0-125:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0 63.3M  1 loop /snap/core20/1778
loop1                       7:1    0 63.3M  1 loop /snap/core20/1822
loop2                       7:2    0 67.2M  1 loop /snap/lxd/21835
loop3                       7:3    0 49.8M  1 loop /snap/snapd/17950
loop4                       7:4    0 91.9M  1 loop /snap/lxd/24061
loop5                       7:5    0 49.9M  1 loop /snap/snapd/18357
sda                         8:0    0   80G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0  1.5G  0 part /boot
└─sda3                      8:3    0 78.5G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0 38.5G  0 lvm  /
sr0                        11:0    1 1024M  0 rom

4、此时虽然增加了/dev/sda3的可用空间,但是lvm物理卷空间是没有增加的

lvm> vgdisplay
  --- Volume group ---
  VG Name               ubuntu-vg
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  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               <38.50 GiB
  PE Size               4.00 MiB
  Total PE              9855
  Alloc PE / Size       9855 / <38.50 GiB
  Free  PE / Size       0 / 0              #可用空间为0
  VG UUID               que55V-3g38-XqKx-yKdZ-QjQZ-Sy8S-JyQ6bn

接着折腾lvm物理卷,使用pvresize命令对物理卷大小进行调整

lvm> vgs     #查看空间
  VG        #PV #LV #SN Attr   VSize   VFree
  ubuntu-vg   1   1   0 wz--n- <38.50g    0
  
lvm> pvresize -t /dev/sda3     #先测试一下
  TEST MODE: Metadata will NOT be updated and volumes will not be (de)activated.
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  
lvm> pvresize  /dev/sda3     #测试成功去掉 -t 执行
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
  
lvm> pvs         #再次查看空间大小已经更改
  PV         VG        Fmt  Attr PSize   PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <78.50g 40.00g

稳妥起见,退出检查可用空间是否增加

root@0-125:~# vgdisplay ubuntu-vg
  --- 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               <78.50 GiB
  PE Size               4.00 MiB
  Total PE              20095
  Alloc PE / Size       9855 / <38.50 GiB
  Free  PE / Size       10240 / 40.00 GiB       #这里可用空间已经增加
  VG UUID               que55V-3g38-XqKx-yKdZ-QjQZ-Sy8S-JyQ6bn

这里还需要将可用的物理卷空间扩容至逻辑卷

root@0-125:~# lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv      #将可用空间全部给逻辑卷
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <38.50 GiB (9855 extents) to <78.50 GiB (20095 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

root@0-125:~# resize2fs /dev/ubuntu-vg/ubuntu-lv    #重新调整一下大小
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 5, new_desc_blocks = 10
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 20577280 (4k) blocks long.

最后看看根目录空间是否已经增加

root@0-125:~# lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0                       7:0    0 63.3M  1 loop /snap/core20/1778
loop1                       7:1    0 63.3M  1 loop /snap/core20/1822
loop2                       7:2    0 67.2M  1 loop /snap/lxd/21835
loop3                       7:3    0 49.8M  1 loop /snap/snapd/17950
loop4                       7:4    0 91.9M  1 loop /snap/lxd/24061
loop5                       7:5    0 49.9M  1 loop /snap/snapd/18357
sda                         8:0    0   80G  0 disk
├─sda1                      8:1    0    1M  0 part
├─sda2                      8:2    0  1.5G  0 part /boot
└─sda3                      8:3    0 78.5G  0 part
  └─ubuntu--vg-ubuntu--lv 253:0    0 78.5G  0 lvm  /
sr0                        11:0    1 1024M  0 rom
root@0-125:~# df -Th
Filesystem                        Type      Size  Used Avail Use% Mounted on
udev                              devtmpfs  7.8G     0  7.8G   0% /dev
tmpfs                             tmpfs     1.6G  1.3M  1.6G   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4       78G  8.6G   66G  12% /
tmpfs                             tmpfs     7.9G     0  7.9G   0% /dev/shm
tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs     7.9G     0  7.9G   0% /sys/fs/cgroup
/dev/loop1                        squashfs   64M   64M     0 100% /snap/core20/1822
/dev/loop2                        squashfs   68M   68M     0 100% /snap/lxd/21835
/dev/loop0                        squashfs   64M   64M     0 100% /snap/core20/1778
/dev/sda2                         ext4      1.5G  303M  1.1G  22% /boot
/dev/loop3                        squashfs   50M   50M     0 100% /snap/snapd/17950
/dev/loop4                        squashfs   92M   92M     0 100% /snap/lxd/24061
/dev/loop5                        squashfs   50M   50M     0 100% /snap/snapd/18357
tmpfs                             tmpfs     1.6G     0  1.6G   0% /run/user/0

结束。

posted @   年华似水゛  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示