VirtualBox虚拟机的linux系统磁盘满了,可行的扩容操作

752 阅读4分钟

VirtualBox虚拟机的linux系统磁盘满了,可行的扩容操作。

查了很多资料,方法很多钟,只有fdisk是可行的,所以记录下来。如:gparted磁盘管理的UI操作(我是纯CLI界面)、xfs_growfs(这个试了不行啊,也不知道为啥)、fdisk(成功的是这个)

系统环境

chunpat@hostname:~$ uname -a
Linux hostname.newdomain.local 5.4.0-99-generic #112-Ubuntu SMP Thu Feb 3 13:50:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

磁盘格式是ext4

虚拟机分配磁盘

这个没啥难度,关机状态下,找到对应的vdi文件,然后执行下面扩容,扩容到20G(这里只是增加,实际使用还要挂载)。

VBoxManage.exe modifyhd "D:\ubuntu18.vdi" --resize 20000

扩容操作

需要注意,这里有抹掉已存在的磁盘操作,要小心

//查看磁盘,ext4格式
chunpat@hostname:~$ sudo df -hT
Filesystem                         Type       Size  Used Avail Use% Mounted on
udev                               devtmpfs   445M     0  445M   0% /dev
tmpfs                              tmpfs       98M  1.1M   97M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  ext4       8.8G  8.8G     0 100% /
tmpfs                              tmpfs      489M     0  489M   0% /dev/shm
tmpfs                              tmpfs      5.0M     0  5.0M   0% /run/lock
tmpfs                              tmpfs      489M     0  489M   0% /sys/fs/cgroup
/dev/loop0                         squashfs    33M   33M     0 100% /snap/snapd/12704
/dev/loop1                         squashfs    56M   56M     0 100% /snap/core18/2128
/dev/loop2                         squashfs    71M   71M     0 100% /snap/lxd/21029
/dev/sda2                          ext4       976M  108M  801M  12% /boot
tmpfs                              tmpfs       98M     0   98M   0% /run/user/1000

//查看磁盘情况,看到磁盘名字叫/dev/sda
chunpat@chunpatserver:~$ sudo fdisk -l
Disk /dev/loop0: 32.3 MiB, 33865728 bytes, 66144 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/loop1: 70.32 MiB, 73728000 bytes, 144000 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/loop2: 55.45 MiB, 58130432 bytes, 113536 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/sda: 19.54 GiB, 20971520000 bytes, 40960000 sectors
Disk model: VBOX HARDDISK
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: 3EF29170-AB9B-4BFD-B16B-F89AAA9FE6E8

Device        Start      End  Sectors  Size Type
/dev/sda1      2048     4095     2048    1M BIOS boot
/dev/sda2      4096  2101247  2097152    1G Linux filesystem
/dev/sda3   2101248 20969471 18868224    9G Linux filesystem


//对/dev/sda操作,通过引导
chunpat@chunpatserver:~$ sudo 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): m

Help:

  GPT
   M   enter protective/hybrid MBR

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


// 打印分区表情况
Command (m for help): p
Disk /dev/sda: 19.54 GiB, 20971520000 bytes, 40960000 sectors
Disk model: VBOX HARDDISK
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: 3EF29170-AB9B-4BFD-B16B-F89AAA9FE6E8

Device        Start      End  Sectors  Size Type
/dev/sda1      2048     4095     2048    1M BIOS boot
/dev/sda2      4096  2101247  2097152    1G Linux filesystem
/dev/sda3   2101248 20969471 18868224    9G Linux filesystem


//删分区,重新划分,执行后不要保存,危险操作
Command (m for help): d
Partition number (1-4, default 4): 3

Partition 3 has been deleted.


//分区
Command (m for help): n
Partition number (3-128, default 3): 3


//将所有的空间都扩容到/dev/sda
First sector (2101248-40959966, default 2101248):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2101248-40959966, default 40959966):

Created a new partition 3 of type 'Linux filesystem' and of size 18.5 GiB.
Partition #3 contains a LVM2_member signature.


// 因为原来的/dev/sda3有数据,所有有个签名,输入N
Do you want to remove the signature? [Y]es/[N]o: N


//保存
Command (m for help): w

The partition table has been altered.
Syncing disks.


//再次查看分区情况,/dev/sda3已经20G了,但是还不行/dev/mapper/ubuntu--vg-ubuntu--lv这个系统还没20G
chunpat@hostname:~$ sudo fdisk -l /dev/sda
Disk /dev/sda: 19.54 GiB, 20971520000 bytes, 40960000 sectors
Disk model: VBOX HARDDISK   
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: 3EF29170-AB9B-4BFD-B16B-F89AAA9FE6E8

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  2101247  2097152    1G Linux filesystem
/dev/sda3  2101248 40959966 38858719 18.5G Linux filesystem
chunpat@hostname:~$ sudo pvs
  PV         VG        Fmt  Attr PSize  PFree
  /dev/sda3  ubuntu-vg lvm2 a--  <9.00g    0
chunpat@hostname:~$ sudo pvresize /dev/sda3
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized or updated / 0 physical volume(s) not resized
chunpat@hostname:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               445M     0  445M   0% /dev
tmpfs                               98M  1.1M   97M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  8.8G  8.8G     0 100% /
tmpfs                              489M     0  489M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              489M     0  489M   0% /sys/fs/cgroup
/dev/loop0                          33M   33M     0 100% /snap/snapd/12704
/dev/loop1                          56M   56M     0 100% /snap/core18/2128
/dev/loop2                          71M   71M     0 100% /snap/lxd/21029
/dev/sda2                          976M  108M  801M  12% /boot
tmpfs                               98M     0   98M   0% /run/user/1000


//执行这个
chunpat@hostname:~$ sudo lvextend -r -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <9.00 GiB (2303 extents) to <18.53 GiB (4743 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.
resize2fs 1.45.5 (07-Jan-2020)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 4856832 (4k) blocks long.


//查看效果,成功了!!!
chunpat@hostname:~$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               445M     0  445M   0% /dev
tmpfs                               98M  1.1M   97M   2% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   19G  8.8G  8.6G  51% /
tmpfs                              489M     0  489M   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
tmpfs                              489M     0  489M   0% /sys/fs/cgroup
/dev/loop0                          33M   33M     0 100% /snap/snapd/12704
/dev/loop1                          56M   56M     0 100% /snap/core18/2128
/dev/loop2                          71M   71M     0 100% /snap/lxd/21029
/dev/sda2                          976M  108M  801M  12% /boot
tmpfs                               98M     0   98M   0% /run/user/1000

参考