每当在编译的时候看着满屏输出的编译信息,手里拿着一杯咖啡(虽然我不喝咖啡,但我吃零食)惬意地等着虚拟机里编译完成,舒服啊突然奇怪,虚拟机的编译怎么停止了啊?不对啊之前可以编译过的呀经过一翻倒腾的检查确认,发现磁盘空间不够了一开始设定的空间完全不够用了啊好烦还得先处理一下这个问题,今天小编就来聊一聊关于virtualbox怎么设置磁盘空间?接下来我们就一起去研究一下吧!

virtualbox怎么设置磁盘空间(攻克恼人的Virtualbox磁盘空间不足)

virtualbox怎么设置磁盘空间

令人抓狂的磁盘空间

每当在编译的时候看着满屏输出的编译信息,手里拿着一杯咖啡(虽然我不喝咖啡,但我吃零食)惬意地等着虚拟机里编译完成,舒服啊。突然!奇怪,虚拟机的编译怎么停止了啊?不对啊之前可以编译过的呀!!!经过一翻倒腾的检查确认,发现磁盘空间不够了!一开始设定的空间完全不够用了啊!!!!好烦还得先处理一下这个问题!

相信以上都是广大程序员经常遇到的问题,比如一开始设置的虚拟机空间太小,或者直接拿的是别人的虚拟机镜像,可能随着自己的使用空间都会变得不够用,等到出问题时,又中断了正在进行的编码,影响编码时间还好,特别影响心情!好了!今天就来介绍几种解决虚拟机空间不够用的办法!这里仅针对virtualbox进行说明。

解决办法外扩硬盘/共享空间

嗯。这也算得上是一个解决方案吧,直接创建一个新的磁盘后再直接挂载到原来的系统上或者挂载一个共享空间到虚拟机。可是这样有会一个问题,假设原来的编译工程很大的话,还得挪动一堆资料,相当不方便!想要更简单的解决办法?当然有!

gparted

使用这个gparted图形工具就可以准确,快速地对磁盘空间进行调整,堪比傲梅分区助手。

调整磁盘大小

在调整分区之前需要调大virtualbox磁盘的动态大小"C:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd D:\vm\vdi\16.04.vdi --resize 160000注意

    Virtualbox的路径要根据实际安装的位置相应的修改

    这里的默认单位是MB,所以160000 = 160000MB = 156.25GB

安装工具

别的先不说先安装需要用到的工具sudo apt install gpartedsudo gparted查看一下当前的容量大小

调整磁盘大小

D:>"C:\Program Files\Oracle\VirtualBox\VBoxManage" modifyhd "E:\vdi\ubuntu 20.04.vdi" --resize 2000000%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

磁盘空间分配

注意到这里的未使用空间已经增大

这里需要先选择把sda1的空间调大,才能再去调分区

再来调节实际分区的大小,有多少就用多少吧

最后再df确认一下空间,确实生效了

那万一,万一我们使用的系统完全没有图形,是一个server版本,那还怎么玩??没事继续往下看

parted

让我们换一台虚拟机来试试,这里只使用命令行,因为也没有图形界面可用。

现在的系统 只有18G的空间太少了!肯定不够用!使用命令行,就没法无脑直接配置了,那就一步步来,如下

查看分区状态

root@hlk:/home/hlk# partedGNU Parted 3.2Using /dev/sdaWelcome to GNU Parted! Type 'help' to view a list of commands.(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 21.5GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 19.3GB 19.3GB primary ext4 boot2 19.3GB 21.5GB 2145MB extended5 19.3GB 21.5GB 2145MB logical linux-swap(v1)

(parted)我们看到有三个分区,如果要调节分区的话,还是需要先把virtualbox的分区删除后再设置,有点麻烦。在这之前还是要先把virtualbox的磁盘空间调大,参见gparted部分。

root@hlk:/home/hlk# partedGNU Parted 3.2Using /dev/sdaWelcome to GNU Parted! Type 'help' to view a list of commands.(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 105GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 19.3GB 19.3GB primary ext4 boot2 19.3GB 21.5GB 2145MB extended5 19.3GB 21.5GB 2145MB logical linux-swap(v1)

(parted)可以看到空间确实不大。

删除分区

(parted) rm 5Warning: Partition /dev/sda5 is being used. Are you sure you want to continue?Yes/No? yesError: Partition(s) 5 on /dev/sda have been written, but we have been unable toinform the kernel of the change, probably because it/they are in use. As aresult, the old partition(s) will remain in use. You should reboot now beforemaking further changes.Ignore/Cancel? i(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 105GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 19.3GB 19.3GB primary ext4 boot2 19.3GB 21.5GB 2145MB extended

(parted) rm 2Error: Partition(s) 5 on /dev/sda have been written, but we have been unable toinform the kernel of the change, probably because it/they are in use. As aresult, the old partition(s) will remain in use. You should reboot now beforemaking further changes.Ignore/Cancel? i(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 105GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 19.3GB 19.3GB primary ext4 boot

(parted)

调节分区大小

(parted) resizepartPartition number? 1Warning: Partition /dev/sda1 is being used. Are you sure you want to continue?Yes/No? yEnd? [19.3GB]? 95%Error: Error informing the kernel about modifications to partition /dev/sda1 --Device or resource busy. This means Linux won't know about any changes you madeto /dev/sda1 until you reboot -- so you shouldn't mount it or use it in any waybefore rebooting.Ignore/Cancel? iError: Partition(s) 5 on /dev/sda have been written, but we have been unable toinform the kernel of the change, probably because it/they are in use. As aresult, the old partition(s) will remain in use. You should reboot now beforemaking further changes.Ignore/Cancel? i(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 105GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 99.6GB 99.6GB primary ext4 boot

(parted)这里发现并没有生效,没有关系,先继续。另外分区的比例可以自己更改,不一定是95%

创建扩展分区

(parted) mkpartPartition type? primary/extended? extStart? 95%End? 100%Error: Error informing the kernel about modifications to partition /dev/sda1 --Device or resource busy. This means Linux won't know about any changes you madeto /dev/sda1 until you reboot -- so you shouldn't mount it or use it in any waybefore rebooting.Ignore/Cancel? iError: Partition(s) 5 on /dev/sda have been written, but we have been unable toinform the kernel of the change, probably because it/they are in use. As aresult, the old partition(s) will remain in use. You should reboot now beforemaking further changes.Ignore/Cancel? i(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 105GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 99.6GB 99.6GB primary ext4 boot2 99.6GB 105GB 5242MB extended lba

(parted)

创建swap分区

(parted) mkpartPartition type? primary/logical? lFile system type? [ext2]? linux-swapStart? 95%End? 100%Error: Error informing the kernel about modifications to partition /dev/sda1 --Device or resource busy. This means Linux won't know about any changes you madeto /dev/sda1 until you reboot -- so you shouldn't mount it or use it in any waybefore rebooting.Ignore/Cancel? iError: Partition(s) 5 on /dev/sda have been written, but we have been unable toinform the kernel of the change, probably because it/they are in use. As aresult, the old partition(s) will remain in use. You should reboot now beforemaking further changes.Ignore/Cancel? i(parted) pModel: ATA VBOX HARDDISK (scsi)Disk /dev/sda: 105GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk Flags:

Number Start End Size Type File system Flags1 1049kB 99.6GB 99.6GB primary ext4 boot2 99.6GB 105GB 5242MB extended lba5 99.6GB 105GB 5241MB logical linux-swap(v1) lba

(parted)

重启并设置swap分区

root@hlk:/home/hlk# mkswap /dev/sda5mkswap: /dev/sda5: warning: wiping old swap signature.Setting up swapspace version 1, size = 4.9 GiB (5240778752 bytes)no label, UUID=deaf6ecb-927a-4ff2-8ade-26282e9ad917

然后装UUID替换进/etc/fstb里面的swap分区

修正文件系统大小

此时再检查下实际的文件系统root@hlk:/home/hlk# df -hFilesystem Size Used Avail Use% Mounted onudev 979M 0 979M 0% /devtmpfs 201M 3.4M 197M 2% /run/dev/sda1 18G 14G 3.2G 82% /tmpfs 1001M 0 1001M 0% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 1001M 0 1001M 0% /sys/fs/cgrouptmpfs 100K 0 100K 0% /run/lxcfs/controllerstmpfs 201M 0 201M 0% /run/user/1000root@hlk:/home/hlk#

怎么还是不对?不着急,再进行一次resize2fs就好了root@hlk:/home/hlk# resize2fs /dev/sda1resize2fs 1.42.13 (17-May-2015)Filesystem at /dev/sda1 is mounted on /; on-line resizing requiredold_desc_blocks = 2, new_desc_blocks = 6The filesystem on /dev/sda1 is now 24319744 (4k) blocks long.

root@hlk:/home/hlk# df -hFilesystem Size Used Avail Use% Mounted onudev 979M 0 979M 0% /devtmpfs 201M 3.4M 197M 2% /run/dev/sda1 92G 14G 74G 16% /tmpfs 1001M 0 1001M 0% /dev/shmtmpfs 5.0M 0 5.0M 0% /run/locktmpfs 1001M 0 1001M 0% /sys/fs/cgrouptmpfs 100K 0 100K 0% /run/lxcfs/controllerstmpfs 201M 0 201M 0% /run/user/1000root@hlk:/home/hlk#

大功告成

看来如果有图形界面的话,是非常一件简单的事,如果没有话,还是有那么一点麻烦的~

参考链接

http://askubuntu.com/questions/966394/how-do-i-create-a-swap-partition-using-parted

http://unix.stackexchange.com/questions/481651/df-not-recognizing-partition-being-resized