VirtualBox磁盘扩容

2023-12-27  本文已影响0人  Lyudmilalala

df -h获取当前文件系统的使用情况

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              794M  1.2M  793M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  9.8G  8.8G  488M  95% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  251M  1.4G  16% /boot
shared                             439G  409G   31G  94% /opt/shared        
tmpfs                              794M  4.0K  794M   1% /run/user/1000  
shared                             439G  409G   31G  94% /media/sf_shared     

物理磁盘扩容

关闭虚拟机 -> 右上角【管理】 -> 虚拟介质管理,在页面上选择对应的虚拟机磁盘,拖动浮标修改大小

NOTICE:目前无法将大磁盘改小,暂未实现缩减功能

虚拟介质管理.png

查看新添加的磁盘,下图中的/dev/sda即为挂载的磁盘,同时我们可以看到对应Linux文件系统根目录的虚拟磁盘/dev/mapper/ubuntu--vg-ubuntu--lv的容量并没有增加

$ sudo fdisk -l
[sudo] password for spinq: 
Disk /dev/loop0: 63.46 MiB, 66547712 bytes, 129976 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: 63.91 MiB, 67014656 bytes, 130888 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: 79.95 MiB, 83832832 bytes, 163736 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/loop3: 111.95 MiB, 117387264 bytes, 229272 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/loop4: 40.86 MiB, 42840064 bytes, 83672 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
GPT PMBR size mismatch (31457279 != 104857599) will be corrected by write.  
The backup GPT table is not on the end of the device.


Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: 0F7F3C0E-834C-4462-AB38-63129ECAD7E6

Device       Start      End  Sectors  Size Type
/dev/sda1     2048     4095     2048    1M BIOS boot
/dev/sda2     4096  3674111  3670016  1.8G Linux filesystem
/dev/sda3  3674112 31455231 27781120 13.2G Linux filesystem


Disk /dev/mapper/ubuntu--vg-ubuntu--lv: 10 GiB, 10737418240 bytes, 20971520 
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

此时df -h

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              794M  1.2M  793M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  9.8G  8.2G  1.2G  89% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  251M  1.4G  16% /boot
shared                             439G  409G   30G  94% /opt/shared        
tmpfs                              794M  4.0K  794M   1% /run/user/1000

Q1: 为什么``变大了一点

Q2: 为什么第二个shared消失了

扩展物理分区

如果仔细看,我们可以发现/dev/sda的几个分区加起来并没有达到50G的大小

这说明在磁盘尾部还留有一大片未被分配的区域,为了使用它们,我们需要将这些空间划分到我们希望挂载的分区中去。我们使用parted来实现

sudo parted /dev/sda
(parted) resizepart 3
End? [31456.2MB]? 100%
(parted) quit

再次查看磁盘空间,发现分区/dev/sda3变大

$ sudo fdisk -l /dev/sda
Disk /dev/sda: 50 GiB, 53687091200 bytes, 104857600 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: 0F7F3C0E-834C-4462-AB38-63129ECAD7E6

Device       Start       End   Sectors  Size Type
/dev/sda1     2048      4095      2048    1M BIOS boot
/dev/sda2     4096   3674111   3670016  1.8G Linux filesystem
/dev/sda3  3674112 104857566 101183455 48.2G Linux filesystem

挂载物理卷轴到虚拟卷轴

若上一步修改了磁盘分区大小,需要同步更新物理卷轴大小

sudo pvresize /dev/sda3

查看可用的物理卷轴大小,Free PE即为可增加的空间,数据为卷轴数量,每个卷轴4MB

$ sudo pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda3
  VG Name               ubuntu-vg
  PV Size               <48.25 GiB / not usable 16.50 KiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              12351
  Free PE               8960
  Allocated PE          3391
  PV UUID               NXUaFx-dgOz-nxvG-1Dlx-T6w6-9lte-Yyfh4a

将这些空间全部用来扩展 LVM 逻辑卷

$ sudo lvextend -l +100%FREE /dev/mapper/ubuntu--vg-ubuntu--lv
  Size of logical volume ubuntu-vg/ubuntu-lv changed from <10.00 GiB (2560 extents) to <48.25 GiB (12351 extents).
  Logical volume ubuntu-vg/ubuntu-lv successfully resized.

命令文件系统重新分配空间

$ sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/mapper/ubuntu--vg-ubuntu--lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 7
The filesystem on /dev/mapper/ubuntu--vg-ubuntu--lv is now 12647424 (4k) blocks long.

再查看df -h,发现空间已经增加

$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
tmpfs                              794M  1.2M  793M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv   48G  8.2G   38G  18% /
tmpfs                              3.9G     0  3.9G   0% /dev/shm
tmpfs                              5.0M     0  5.0M   0% /run/lock
/dev/sda2                          1.7G  251M  1.4G  16% /boot
shared                             439G  409G   30G  94% /opt/shared        
tmpfs                              794M  4.0K  794M   1% /run/user/1000   
上一篇 下一篇

猜你喜欢

热点阅读