Difference between revisions of "Virtualbox"
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Install = | |||
<source lang=bash> | |||
echo "deb [arch=amd64] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee -a /etc/apt/sources.list | |||
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add - | |||
sudo apt-get update | |||
sudo apt-get install virtualbox-6.1 | |||
sudo apt-mark hold virtualbox-6.1 | |||
sudo apt-mark showhold | |||
sudo apt-mark unhold virtualbox-6.1 | |||
</source> | |||
= Resize disks in VirtualBox with Snapshots = | = Resize disks in VirtualBox with Snapshots = | ||
It is quite straightforward to resize a disk in VirtualBox as stated here and there. It becomes tricky though if the virtual machine,aka VM, has snapshots attached. The virtual disk thus is persisted across multiple VHD files, and the old trick will generally take not effect. This is also a [https://www.virtualbox.org/ticket/9103 known bug] hanging there for more than three years. | It is quite straightforward to resize a disk in VirtualBox as stated here and there. It becomes tricky though if the virtual machine,aka VM, has snapshots attached. The virtual disk thus is persisted across multiple VHD files, and the old trick will generally take not effect. This is also a [https://www.virtualbox.org/ticket/9103 known bug] hanging there for more than three years. | ||
Line 11: | Line 21: | ||
</source> | </source> | ||
Startup the VM, and you will see the unallocated space in the Disk Management utility. | Startup the VM, and you will see the unallocated space in the Disk Management utility. | ||
= Resize .vmdk disk on Linux = | = Resize .vmdk disk on Linux = | ||
Convert .vmdk format to .vdi and then resize. You can change format back after the resizing. | Convert .vmdk format to .vdi and then resize. You can change format back after the resizing. | ||
<source lang="bash"> | <source lang="bash"> | ||
VBoxManage clonehd "ubuntu-xenial-16.04-cloudimg.vmdk" "ubuntu-xenial-16.04-cloudimg.vdi" --format vdi # .vmdk -> .vdi | VBoxManage clonehd "ubuntu-xenial-16.04-cloudimg.vmdk" "ubuntu-xenial-16.04-cloudimg.vdi" --format vdi #.vmdk -> .vdi | ||
VBoxManage clonehd "ubuntu-xenial-16.04-cloudimg.vdi" "ubuntu-xenial-16.04-cloudimg.vmdk" --format vmdk #.vdi -> .vmdk | VBoxManage clonehd "ubuntu-xenial-16.04-cloudimg.vdi" "ubuntu-xenial-16.04-cloudimg.vmdk" --format vmdk #.vdi -> .vmdk | ||
</source> | </source> | ||
= Resize .vdi disk on Windows = | = Resize .vdi disk on Windows = | ||
<source> | |||
cd "C:\Program Files\Oracle\VirtualBox" | |||
VBoxManage.exe modifyhd "C:\Users\piotr\VirtualBox VMs\vm-ubuntu64\vm-ubuntu64.vdi" --resize 20480 | |||
</source> | |||
Note it also can resize VHD (Hyper-V) file formats. | |||
= Vagrant note = | = Vagrant note = | ||
* OS: Ubuntu 16.04 LTS | * OS: Ubuntu 16.04 LTS | ||
Line 32: | Line 46: | ||
# Converted .vmdk into .vdi | # Converted .vmdk into .vdi | ||
# Attached "ubuntu-xenial-16.04-cloudimg.vdi" making sure that | # Attached "ubuntu-xenial-16.04-cloudimg.vdi" making sure that | ||
* Controller: SCSI Controller | #* Controller: SCSI Controller | ||
* Hard disk is attached to: SCSI Port 0, otherwise may throw error "no bootable medium found" | #* Hard disk is attached to: SCSI Port 0, otherwise may throw error "no bootable medium found" | ||
= Shrink unused space on virtual drive = | |||
Hypervisor: Virtualbox, VMware | |||
Virtual disks can be shrink as long as they are ext3 or ext4 file systems. | |||
<source lang="bash"> | |||
$ vagrant ssh | |||
$ sudo dd if=/dev/zero of=wipefile bs=1024x1024; rm wipefile | |||
</source> | |||
The above command is simply writing zero bytes to the wipefile in chunks of 1024 bytes until there is no disk space left in your VM’s disk. Then it is removing the wipefile. This basically leaves all those excess bytes zero’d out. | |||
This is necessary because the shrink/compaction tools provided by either VMWare or VirtualBox both have no way of identifying space they can free up in the disks unless they are zero’d out. | |||
With VirtualBox the only way I was able to shrink the disk image was to clone it to a smaller copy using the following command: | |||
<source lang="bash"> | |||
$ VboxManage clonehd name-of-original-vm.vdi name-of-clone-vm.vdi | |||
</source> | |||
Once you have cloned the vdi you can then import it into the VM through VirtualBox and get rid of the original vdi. | |||
With VMware you can shrink the vmdk disk by doing the following: | |||
<source lang="bash"> | |||
$ vmware-vdiskmanager -d /path/to/main.vmdk | |||
$ vmware-vdiskmanager -k /path/to/main.vmdk | |||
</source> | |||
= Installing Virtualbox guest additions = | |||
Be sure to install DKMS(Dynamic Kernel Module System) before installing the Linux Guest Additions | |||
<source lang="bash"> | |||
sudo apt-get install dkms | |||
</source> | |||
Additional packages if ''dkms'' was not enough | |||
<source lang="bash"> | |||
sudo apt-get install linux-headers-$(uname -r) build-essential dkms #if above not work | |||
sudo apt-get install perl make gcc #was required for Ubuntu 18.04 LTS | |||
</source> | |||
In the "Devices" menu in the virtual machine's menu bar, VirtualBox has a handy menu item named "Insert Guest Additions CD image", which mounts the Guest Additions ISO file inside your virtual machine. Then change directory to your CD-ROM and issue following command: | |||
<source lang="bash"> | |||
sh ./VBoxLinuxAdditions.run | |||
</source> | |||
= Generalize Windows = | = Generalize Windows = | ||
If you wish to re use your Windows VM image it needs to be generalized: | If you wish to re use your Windows VM image it needs to be generalized: | ||
<source> | |||
C:\Windows\System32\Sysprep | |||
sysprep.exe /oobe /generalize /shutdown /mode:vm | |||
</source> |
Latest revision as of 00:36, 31 October 2021
Install
echo "deb [arch=amd64] https://download.virtualbox.org/virtualbox/debian $(lsb_release -cs) contrib" | sudo tee -a /etc/apt/sources.list wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | sudo apt-key add - sudo apt-get update sudo apt-get install virtualbox-6.1 sudo apt-mark hold virtualbox-6.1 sudo apt-mark showhold sudo apt-mark unhold virtualbox-6.1
Resize disks in VirtualBox with Snapshots
It is quite straightforward to resize a disk in VirtualBox as stated here and there. It becomes tricky though if the virtual machine,aka VM, has snapshots attached. The virtual disk thus is persisted across multiple VHD files, and the old trick will generally take not effect. This is also a known bug hanging there for more than three years.
The suggested approach is to delete all snapshots and wait patiently for VirtualBox Manager to merge all the VHD files for you. It is a painfully lengthy process, so I decide to take a shortcut.
- First, shutdown the VM and backup the whole virtual machine folder.
- Then modify the size of all .vdi files in the root of the VM and Snapshots subdirectory.
VBoxManage modifyhd "Windows 8.1.vdi" --resize 81920 for x in Snapshots/*.vdi ; do VBoxManage modifyhd $x --resize 81920 ; done
Startup the VM, and you will see the unallocated space in the Disk Management utility.
Resize .vmdk disk on Linux
Convert .vmdk format to .vdi and then resize. You can change format back after the resizing.
VBoxManage clonehd "ubuntu-xenial-16.04-cloudimg.vmdk" "ubuntu-xenial-16.04-cloudimg.vdi" --format vdi #.vmdk -> .vdi VBoxManage clonehd "ubuntu-xenial-16.04-cloudimg.vdi" "ubuntu-xenial-16.04-cloudimg.vmdk" --format vmdk #.vdi -> .vmdk
Resize .vdi disk on Windows
cd "C:\Program Files\Oracle\VirtualBox" VBoxManage.exe modifyhd "C:\Users\piotr\VirtualBox VMs\vm-ubuntu64\vm-ubuntu64.vdi" --resize 20480
Note it also can resize VHD (Hyper-V) file formats.
Vagrant note
- OS: Ubuntu 16.04 LTS
- Vagrant version 2.1.1
- VirtualBox: 5.1.34_Ubuntu
Steps I have taken to resize Vagrant Ubuntu disk
- Stopped VM
- In settings removed attached drive "ubuntu-xenial-16.04-cloudimg.vmdk"
- Converted .vmdk into .vdi
- Attached "ubuntu-xenial-16.04-cloudimg.vdi" making sure that
- Controller: SCSI Controller
- Hard disk is attached to: SCSI Port 0, otherwise may throw error "no bootable medium found"
Shrink unused space on virtual drive
Hypervisor: Virtualbox, VMware
Virtual disks can be shrink as long as they are ext3 or ext4 file systems.
$ vagrant ssh $ sudo dd if=/dev/zero of=wipefile bs=1024x1024; rm wipefile
The above command is simply writing zero bytes to the wipefile in chunks of 1024 bytes until there is no disk space left in your VM’s disk. Then it is removing the wipefile. This basically leaves all those excess bytes zero’d out.
This is necessary because the shrink/compaction tools provided by either VMWare or VirtualBox both have no way of identifying space they can free up in the disks unless they are zero’d out.
With VirtualBox the only way I was able to shrink the disk image was to clone it to a smaller copy using the following command:
$ VboxManage clonehd name-of-original-vm.vdi name-of-clone-vm.vdi
Once you have cloned the vdi you can then import it into the VM through VirtualBox and get rid of the original vdi.
With VMware you can shrink the vmdk disk by doing the following:
$ vmware-vdiskmanager -d /path/to/main.vmdk $ vmware-vdiskmanager -k /path/to/main.vmdk
Installing Virtualbox guest additions
Be sure to install DKMS(Dynamic Kernel Module System) before installing the Linux Guest Additions
sudo apt-get install dkms
Additional packages if dkms was not enough
sudo apt-get install linux-headers-$(uname -r) build-essential dkms #if above not work sudo apt-get install perl make gcc #was required for Ubuntu 18.04 LTS
In the "Devices" menu in the virtual machine's menu bar, VirtualBox has a handy menu item named "Insert Guest Additions CD image", which mounts the Guest Additions ISO file inside your virtual machine. Then change directory to your CD-ROM and issue following command:
sh ./VBoxLinuxAdditions.run
Generalize Windows
If you wish to re use your Windows VM image it needs to be generalized:
C:\Windows\System32\Sysprep sysprep.exe /oobe /generalize /shutdown /mode:vm