一直以来都是以vagrant+docker作为开发环境,可是久而久之,原Box自带的8G容量就捉襟见肘了。时不时需要手动删除一些东西。

Virtualbox 本身只支持vdi硬盘文件格式的扩容,对vmdk 格式的却不支持。但是却提供vmdk到vdi格式的转化,正好可以利用这一功能进行扩容。
因为要用到VBoxManage,所以要把D:\Program Files\Oracle\VirtualBox这个加入path。

  1. 关闭虚拟机, 从Virtualbox页面查看硬盘文件地址(选中虚拟机->右键->设置->存储)(如:C:\Users\Administrator\VirtualBox VMs\vagrant_default_1493690319341_4755)。进到文件所在目录后执行:
    1. $ VBoxManage clonehd box-disk1.vmdk box-disk1.vdi format VDI
  2. 给vdi格式硬盘文件扩容
    1. $ VBoxManage modifyhd box-disk1.vdi resize 40960
  3. 从Virutalbox存储界面删除原硬盘文件,然后再加入新的VDI格式硬盘文件

  4. vagrant up启动虚拟机,然后vagrant ssh进入

  5. 利用cfdisk工具创建主分区,谨记选择格式为Linux LVM (8e)

    1. $ sudo cfdisk /dev/sda
    2. cfdisk

    这里的具体操作如下:

    1. # vagrant ssh
    2. # sudo su -
    3. Find the name of the logical volume mapping the file-system is on (ie. /dev/mapper/VolGroupOS-lv_root).
    4. # df
    5. Find the name of the physical volume (or device) that all the partitions are created on (ie. /dev/sda).
    6. # fdisk -l
    7. Create a new primary partition for use as a Linux LVM
    8. # fdisk /dev/sda
    9. Press p to print the partition table to identify the number of partitions. By default there are two - sda1 and sda2.
    10. Press n to create a new primary partition.
    11. Press p for primary.
    12. Press 3 for the partition number, depending the output of the partition table print.
    13. Press Enter two times to accept the default First and Last cylinder.
    14. Press t to change the system's partition ID
    15. Press 3 to select the newly creation partition
    16. Type 8e to change the Hex Code of the partition for Linux LVM
    17. Press w to write the changes to the partition table.
    18. Reboot the machine, then ssh back in when it is up again and switch to the root user once more.
    19. # reboot
    20. # vagrant ssh
    21. # sudo su -
  6. 利用pvcreate命令给新的分区创建物理卷
    1. $ sudo pvcreate /dev/sda3
    2. successfully created
  7. 查看VG Name,我自己的VG Name是centos
    1. $ sudo pvdisplay | grep "VG Name"
    2. VG Name centos
  8. 将新分区扩展到centos这个组
    1. $ vgextend centos /dev/sda3
    2. Volume group "centos" sucessfully extended
  9. 扩展逻辑分区
    1. $ lvextend /dev/mapper/centos-root /dev/sda3
  10. resize并且生效
    1. $ resize2fs /dev/mapper/centos-root
    2. resize2fs 1.41.12 (17-May-2010)
    3. Filesystem at /dev/debian-605/root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 2
    4. Performing an on-line resize of /dev/debian-605/root to 4937728 (4k) blocks.
    5. The filesystem on /dev/debian-605/root is now 4937728 blocks long.
    在centos 7下,这一步会出错
    1. resize2fs 1.42.9 (28-Dec-2013)
    2. resize2fs: Bad magic number in super-block while trying to open /dev/mapper/centos-root
    3. Couldn't find valid filesystem superblock.
    4. resize2fs
    这时,只需要使用xfs_growfs命令替换就行了
    1. xfs_growfs /dev/mapper/centos-root
    至此,大分告成。vagrant reload重启虚拟机查看效果
    1. df -h
    于是乎,妈妈再也不用担心我的虚拟机磁盘没容量了。

参考:https://www.madcoder.cn/vagrant-box-resize.html
https://gist.github.com/christopher-hopper/9755310

分类: web

标签:   vagrant