VMware之Ubuntu20.04擴充根目錄磁碟空間

Alicute發表於2024-04-26

1、改變虛擬磁碟大小

  • 關閉Ubuntu
  • 右鍵—>設定—>硬碟—->實用工具—>擴充套件
  • 填入擴充套件後的分割槽大小
  • 確定

2、檢視磁碟使用情況

  • 開啟Ubuntu

  • 輸入:df -h

cl@cl:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            955M     0  955M   0% /dev
tmpfs           198M  1.3M  196M   1% /run
/dev/sda2        30G   14G   15G  48% /
tmpfs           986M     0  986M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           986M     0  986M   0% /sys/fs/cgroup
/dev/loop0       98M   98M     0 100% /snap/core/10185
/dev/loop1      205M  205M     0 100% /snap/microk8s/1710
/dev/loop2       98M   98M     0 100% /snap/core/10126
/dev/loop3      205M  205M     0 100% /snap/microk8s/1769
tmpfs           198M     0  198M   0% /run/user/1000

3、在Ubuntu shell進行分割槽擴容操作

cl@cl:~$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
  • 列印檢視分割槽劃分情況
Command (m for help): p
Disk /dev/sda: 40 GiB, 42949672960 bytes, 83886080 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: BB1002CF-D749-4A1F-8DF7-EF0945D4DF0E

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 83886046 83881951  40G Linux filesystem
  • 刪除需要擴容的分割槽,這裡是2,完成後不要執行 w 寫入
Command (m for help): d
Partition number (1,2, default 2): 2


Command (m for help): p
Disk /dev/sda: 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: 0976B291-839E-463D-BD05-936253587234

Device     Start   End Sectors Size Type
/dev/sda1   2048  4095    2048   1M BIOS boot
  • 建立新分割槽
# 建立新分割槽 ,First和Last sector直接回車預設值,因為我是把剩下所有空閒的空間全部分配到擴容的新分割槽內
# 若是部分分配,請在Last sector輸入對應的值
Command (m for help): n
Partition number (2-128, default 2):
First sector (4096-209715166, default 4096):
Last sector, +sectors or +size{K,M,G,T,P} (4096-209715166, default 209715166):

Created a new partition 2 of type 'Linux filesystem' and of size 100 GiB.
Partition '#2' contains a ext4 signature.

# No
Do you want to remove the signature? [Y]es/[N]o: n
  • 檢視新分割槽
Command (m for help): p

Disk /dev/sda: 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: 0976B291-839E-463D-BD05-936253587234

Device     Start       End   Sectors  Size Type
/dev/sda1   2048      4095      2048    1M BIOS boot
/dev/sda2   4096 209715166 209711071  100G Linux filesystem
  • 確認無誤後 w 寫入操作
Command (m for help): w

The partition table has been altered.
Syncing disks.
  • 重啟機器
cl@cl:~$ sudo reboot

4、執行擴容操作

# 執行擴容操作
root@cl:~# pvs

root@cl:~# pvresize /dev/sda2          //擴充套件物理分割槽

root@cl:~# lvdisplay

root@cl:~# lvextend -r -l +100%FREE /dev/ubuntu-vg/ubuntu-lv     

5、參考文件
https://blog.csdn.net/qq32048487/article/details/136944155
https://kleinlsl.github.io/2020/11/13/VM%E8%99%9A%E6%8B%9F%E6%9C%BA%E7%BB%99Ubuntu%E6%A0%B9%E5%88%86%E5%8C%BA%E6%89%A9%E5%AE%B9/#4%E3%80%81%E6%89%A7%E8%A1%8C%E6%89%A9%E5%AE%B9%E6%93%8D%E4%BD%9C

相關文章