Virtual Box KVM and VMware How To

How to install windows paravirtualization network drivers with command line

reg add "HKCU\Software\Policies\Microsoft\Windows NT\Driver Signing" /v BehaviorOnFailedVerify /t reg_dword /d 00000000 /f
or

bcdedit.exe -set loadoptions DDISABLE_INTEGRITY_CHECKS
bcdedit.exe -set TESTSIGNING ON

or get redhats cert and
certutil -addstore "TrustedPublisher" cert_name.cer
pnputil /a netkvm.inf
accept it (somehow)
then

vboxmanage showvminfo modifyvm "host" --nictype1 virtio

How to move KVM VM

Copy the VM disk to the new location. You can see where it is in virt-manager "connection properties->storage"
save the VM xml file.
virsh dumpxml vmname > vm.xml
Edit it to change the location of the VM disk
Import the VM xml file at the destination
virsh define vm.xml

How to enable Hyper-V in KVM

Aka nested virtualization, useful for running windows vms (Docker) inside a KVM windows guest
Create a VM. Edit it's config
virsh edit vmname
You can get the list of vms by doing virsh list. Do not try to edit the xml directly, which is BTW in /etc/libvirt/qemu, as it is cached by libvirt
Add

<feature policy='require' name='vmx'/>
Inside the <cpu> block
Start the VM and install Hyper-V via 'windows features'
verify vmx is enabled by looking at the running instances' command line. Shoudl show something like
-cpu SandyBridge,vmx=on,hv_time,hv_relaxed,hv_vapic,hv_spinlocks=0x1fff
cat /sys/module/kvm_intel/parameters/nested

Should say 'yes'. Also make sure you have a kernel 4.10+, QEMU 2.7+, SeaBIOS 1.10+

Notes

You might need to switch to paravirtualized drives "virtio", create a virtual CD, mount and install guest virtio drivers from here. Documentation on installing these drivers is here. Also get the display mouse seamless integration drives from here (spice-guest-tools for windows guests).

References

Running HyperV inside KVM guest
Libvirt XML options

How to mount vmdk

This process will let you mount it even with lvm inside

First check if you have any snapshots on top of the vmdk. If so, stop and flatten the vmdk (delete the snapshot inside vmware/virtualbox) them. Mounting a vmdk that has snapshot on top is possible but will show old data and if you change anything you will likely damage the active filesystem and render the snapshot on top of it unusable.

Now run
fdisk -l disk.vmdk

If you see partitions then you have an old vmdk that is a flat disk you can mount it simply by mounting with the offset of the partition you need, just like a normal disk image. You can also use kpartx that will find all partitions in the image and create loopback devices for them automatically.

If nothing is shown you need, then it's in the new format and you need to use something else first to get to the raw image: affuse or vmdkmount from libvmdk-utils. Both are userland mounts, but run them as root since later kpartx/vgscan mounts require root permission and will not be able to access userland mounted files:

mkdir mount
sudo vmdkmount file.vmdk mount

Check if the partitions inside the raw file are lvm or normal partitions

sudo fdisk -l mount/vmdk1

If they are lvm you need to use kpartx as it's able to parse out lvm
sudo kpartx -avs mount/vmdk1
Check /dev/mapper/* - several new partitions should show up under the names from the lvm file, or see groups with vgs:
sudo vgs
You will have /dev/mapper/entries to mount now

mkdir lvm
mount /dev/mapper/vg_igi-lv_root lvm

You could also use vgscan to scan the new loop0 disk, and vgchange -ay vg

If the partitions are not lvm mount it directly using
mount -o ro,loop=/dev/loop1,offset=$((512*start_sector)) vmdk1

kpartx -a file can also be user to autocreate loop devices for all partitions found in a disk image

Teardown

sudo umount lvm
rmdir lvm

drop to root (kpartx is buggy and does not work when run as a command to sudo)
sudo -s
kpartx -d mount/vmdk1
if that does not work you can try to deactivate the volume group
sudo vgchange -a n /dev/mapper/vg_igi
now unmount the raw file
sudo fusermount -u mount
rmdir mount


How to re-enable clipboard sharing on kvm whonix

edit
/etc/libvirt/qemu/Whonix-Workstation.xml
change
<clipboard copypaste="no">
to yes. reload libvird to re-read the xml
sudo systemctl reload libvirtd.service

How to forward kvm port

if user mode network stack is used
virsh qemu-monitor-command --hmp <vmname> 'hostfwd_add ::2222-:22'
more
virsh qemu-monitor-command --hmp <vmname> 'help hostfwd_add'

other methods:
http://wiki.libvirt.org/page/Networking#Forwarding_Incoming_Connections
https://www.linux-kvm.org/page/Networking
https://wiki.libvirt.org/page/VirtualNetworking

How to change Virtual Box guest Linux VM screen resolution without Virtual Box guest additions

Default possible resolutions will be 1600x1200, 1280x1024 and smaller. If you want a custom resolution run this:
vboxmanage setextradata "''VM-Name''" "CustomVideoMode1" "1440x900x32"

It will not take effect untill the reboot, but do the following before you reboot: Now create a conf file

sudo mkdir -p /etc/X11/xorg.conf.d
sudo vi /etc/X11/xorg.conf.d/00-vbox.conf

Set it to

Section "Device"
 Identifier "VirtualBox-Device"
 Option "Monitor-default" "VirtualBox-Monitor"
EndSection
Section "Monitor"
 Identifier "VirtualBox-Monitor"
 VendorName "VirtualBox"
 ModelName "VirtualBox"
 HorizSync 1.0 - 1000.0
 VertRefresh 1.0 - 1000.0
 Option "MinClock" "1.0"
 Option "MaxClock" "10000.0"
 Option "Primary" "True"
 Option "PreferredMode" "1440x900"
EndSection
Section "Screen"
 Identifier "MyScreen"
 Device "VirtualBox-Device"
 Monitor "VirtualBox-Monitor"
EndSection

Now reboot the VM. You could now set the custom mode in systemsettings in KDE.

How to reduce VDI disk size for a Virtual Box VM

  1. Boot into the VM
  2. Remove unnecessary files, get ccleaner, clean the system with ccleaner
  3. Run defragmentation at least couple of times.
  4. In ccleaner select tools, drive wiper, wipe free space with a simple overwrite. Even if the VDI is growable it will not grow during the wipe
  5. Shut down the VM
  6. Run

 VBoxManage modifyvdi ''your'' ''disk'' ''file.vdi'' compact

How to reduce VMDK disk size for a VMWare Fusion VM

  1. Boot into the VM
  2. Remove unnecessary files, get ccleaner, clean the system with ccleaner
  3. Run defragmentation at least couple of times.
  4. In ccleaner select tools, drive wiper, wipe free space with a simple overwrite. Even if the VMDK is growable it will not grow during the wipe
  5. Shut down the VM
  6. Run

/Applications/VMware Fusion.app/Contents/MacOS/diskTool -k 0 ''virtual'' ''disk'' ''image.vmdk''

How to consolidate all VMWare Fusion snapshots into one (aka combine or remove snaptshots)

/Applications/VMware Fusion.app/Contents/MacOS/diskCreate -C ''latest'' ''snapshort.vmdk'' ''new'' ''merged'' ''disk.vmdk''

How to share clipboard in vmware remote console

Aka enable Copy and Paste Operations Between the Guest Operating System and Remote Console
Log into a vCenter Server system using the vSphere Client and select the virtual machine.
On the Summary tab, click Edit Settings.
Select Options > Advanced > General and click Configuration Parameters.
Click Add Row and type the following values in the Name and Value columns.

isolation.tools.copy.disable = false
isolation.tools.paste.disable = false

Note: These options override any settings made in the guest operating system’s VMware Tools control panel.
Click OK to close the Configuration Parameters dialog box, and click OK again to close the Virtual Machine Properties dialog box.
Restart the virtual machine.

How to change default Virtual Box NAT network

Take the VM down and do the following:
VBoxManage modifyvm "VM name" --natnet1 "192.168.5/24"

How to change VirtualBox guest resolution via RDP

Assuming you are using VirtualBox's built-in RDP you have to submit the video mode change "hint" manually to the guest VM:
VBoxManage controlvm "Machinename" setvideomodehint 1280 800 16

How to improve VirtualBox guest performance

Use SATA disks, not IDE. For XP guest you might need to install the Intel Matrix Drivers for SATA. For others it is out of the box.

How to increase VirtualBox VDI disk size

First, make sure it is not a static disk
VBoxManage showhdinfo <disk.vdi>
Look for "format variant". If it says 'static' then convert it to a dynamic disk first, by cloning it:
VBoxManage clonehd <disk.vdi> <diskclone.vdi>
Verify the new format by running VBoxManage showhdinfo again and reconnect your cloned to the VM:
VBoxManage storageattach <vmname> --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium <diskclone.vdi>
Now expand it:
VBoxManage modifyhd <disk.vdi> --resize 40960
Get partedmagic in an iso format and mount it to the VM

VBoxManage storageattach ''<vmname>'' --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium pmagic_2011_12_30.iso 

Now start the VM, press F12 and choose CD-ROM to boot from the iso, start gparted and resize your partition to fill the drive.

How to configure VirtualBox VM RDP authentication

Here is how to authenticate against credentials stored in inside the VM's own XML definition file vm_name/vm_name.vbox:
VBoxManage setproperty vrdeauthlibrary "VBoxAuthSimple"
Make sure your VM is down, then
VBoxManage modifyvm ''<vm_name>'' --vrdeauthtype external
Generate a password hash
VBoxManage internalcommands passwordhash "''<password>''"
Set the user and the password
VBoxManage setextradata ''<vm_name>'' "VBoxAuthSimple/users/user" ''<password_hash>''
Then use the -u and -p options for rdesktop to connect (you can put password in quotes and it should still work)

How to configure VirtualBox VM RDP encryption

Do the following while in the folder where your VM located. This forces the TLS encryption, be wary of potential performance impacts. Create a self signed CA for your vm:

openssl req -new -x509 -days 3650 -extensions v3_ca -keyout vm_ca_key_private.pem -out vm_ca_cert.pem

When asked, create a passphrase for the CA. Create the VM server private key:

openssl genrsa -out vm_server_key_private.pem

Create a request to the CA for signing the VM server key:

openssl req -new -key vm_server_key_private.pem -out vm_server_req.pem

Sign the request and issue the server public key:

openssl x509 -req -days 3650 -in vm_server_req.pem -CA vm_ca_cert.pem -CAkey vm_ca_key_private.pem -set_serial 01 -out vm_server_cert.pem

Make sure that your VM is stopped and configure it for TLS

VBoxManage modifyvm ''<vm_name>'' --vrdeproperty "Security/Method=TLS"
VBoxManage modifyvm ''<vm_name>'' --vrdeproperty "Security/CACertificate=vm_ca_cert.pem"
VBoxManage modifyvm ''<vm_name>'' --vrdeproperty "Security/ServerCertificate=vm_server_cert.pem"
VBoxManage modifyvm ''<vm_name>'' --vrdeproperty "Security/ServerPrivateKey=vm_server_key_private.pem"

Try connecting. No changes are required on the client.

How to connect USB remotely to a VirtualBox VM on Ubuntu

For some reason the tool to connect USB over RDP (rdesktop-vrdp) is not included in the VirtualBox distros from the PPA. If you had installed it this way you'd need to do the following to get rdesktop-vrdp:

Download a debian pack from https://www.virtualbox.org/wiki/Linux_Downloads. extract it somewhere with 7zip or dpkg. Now do the following as root:

cp unpacked/usr/bin/rdesktop-vrdp /usr/bin/
chmod 755 /usr/bin/rdesktop-vrdp
cp -R unpacked/usr/share/virtualbox/rdesktop-vrdp-keymaps /usr/share/virtualbox/
chmod 755 /usr/share/virtualbox/rdesktop-vrdp-keymaps
chmod 644 /usr/share/virtualbox/rdesktop-vrdp-keymaps/*

Now, configure your vm to accept remote USB. First make sure that your VM is down, then run

VBoxManage showvminfo VMNAME

To enable an existing USB device filter for remote use:

VBoxManage usbfilter modify 0 --target VMNAME --remote yes

or to catch all usbs

VBoxManage usbfilter add 0 --name "remoteUSB" --remote yes --target VMNAME

or to connect iPhone 4 remotely:

VBoxManage usbfilter add 0 --name "aiPhone" --vendorid 05ac --productid 1297 --revision 0001 -manufacturer "Apple Inc." --product iPhone --remote yes --target VMNAME

Note: --remote "any", for both local and remote connections does NOT work from the command line, but you can use GUI to set it up

Make sure you have a vboxusers group. Run

groups

If vboxusers is not listed run

sudo usermod -a -G vboxusers user

And re-login

Start the VM and connect with the remote USB:

rdesktop-vrdp -5 -r usb -x l -k en-us -N server

Convert VMDK to VDI

Trivial

VBoxManage clonehd source.vmdk destination.vdi -format VDI

How to mount a VMWare VMDK disk image on Linux

Get the list of partitions
vmware-mount ''[path'' ''to'' ''disk]'' -l
Mount the partition you need
vmware-mount ''[path'' ''to'' ''disk]'' ''[partition'' ''number]'' ''[mount'' ''folder]''
Or mount all partitions
vmware-mount ''[path'' ''to'' ''disk]''  ''[mount'' ''folder]''

How to mount a VirtualBox VDI disk image on Linux

There used to be virtualbox-fuse, but it's no more after virtual box v4.1, so we have to resort to the qemu method
sudo apt-get install qemu-kvm
sudo modprobe nbd
sudo qemu-nbd -c /dev/nbd0 ./yourfile.vdi
sudo mount /dev/nbd0p1 /media/temp

Once you are done do the following

sudo umount /media/temp
sudo qemu-nbd -d /dev/nbd0

For history - the virtualbox-fuse method. Create two folders /media/temp and /media/vms and install the fuse module

sudo apt-get install virtualbox-fuse

Then mount sequentially

sudo vdfuse -r -f ''win7.vdi'' /media/temp
sudo mount /media/temp/Partition1 /media/vms

vdfuse -r is for "read-only". After you are done unmount by the normal umount

sudo umount /media/vms
sudo umount /media/temp

The manual way. Calculate the partition offset and mount with offset:

OFFSET1=`VBoxManage internalcommands dumphdinfo ''[path'' ''to'' ''disk]'' |grep "offData="|sed "s#.*offData=$[0-9]\+$#\1#"`
OFFSET=$((32256+$OFFSET1))
sudo mount -o loop,offset=$OFFSET ''[path'' ''to'' ''disk]''  ''[mount'' ''folder]''

  • Pick the appropriate file system for the -t parameter and the appropriate mount point for the partition.

How to increase a VMDK disk size

Shutdown the vmware image (it could be just paused for expansion but Win2K will not notice the Physical disk increase untill reboot) Do the following for the 377byte file with the disk descriptor (not the actual disk)

"C:\Program Files\VMware\VMware Workstation\"vmware-vdiskmanager.exe -x 8Gb "Windows Server 2003 Enterprise Edition.vmdk"

Fire it back up

Download Knoppix live cd. Boot from it into the vmware. Start QTParted, perform disk expansion. Alternatively you could install Acronis Disk Director Suite on the vmware image do the disk expansion

How to map vmware disks directly to the host

To mount the disk download howtomapvmwaredisks directly to the host run

"C:\Program Files\VMware\VMware DiskMount Utility\"vmware-mount.exe /p "Windows Server 2003 Standard Edition.vmdk"

to get the list of partitions vmdk must be the small text file with a disk description. not the actual multigig disk file - however the text file might be inside of the disk file if not present in the folder. Look at the disk file - it should be at the beginning. in this case yo uneed to point the vmware-vdisk to the big disk file then mount them

"C:\Program Files\VMware\VMware DiskMount Utility\"vmware-mount.exe v: /v:1 "Windows Server 2003 Standard Edition.vmdk"

How to shrink a preallocated disk - convert to growable

"C:\Program Files\VMware\VMWare Workstation\"vmware-vdiskmanager.exe -r "Windows Server 2003 Standard Edition.vmdk" -t 0 growable.vmdk

after it is complete reconfigure the vmware image - delete old hdd and create a new one pointing to the growable.vmdk it might be already small so that you would not have to shrink it

  • make a clean partition
  • power of a vm

How to shrink disk

Either power on the vmware and go to control panel -> vmware tools -> shrink or mount using
vmware-mount.ext v: /v:1 ....vmdk
then prepare for shrinking
vmware-vdiskmanager.exe -p V:
then shrink
vmware-vdiskmanager -k ....vmdk

How to virtualize windows partition

Directions for VMWARE:

  • first dump it to a file
  • boot under linux
apt-get install ntfsprogs
ntfsclone -o file /dev/sda1 (ntfs partition)
or use
dd if=/dev/sda1 of=file bs=1024
ntfs clone is just better feedback (percentage complete) and possibly speed you could then nuke the partition with gparted
  1. create a virtual disk
  2. create a new disk in a new vm or an existing, of the same size and preallocate it.
  3. boot the vm with a partedmagic or gparted ISO mounted to be able to partition the new vmdk
  4. create the partition table and the ntfs partition on the new vmdk. Mark it as bootable.
  5. shut down the vm
  6. copy the image to vmdk
  7. create a mounting-point for the vmdk
  8. mount vmdk with

vmdk-mount disk 1 mounting-point

where 1 is the number of the ntfs partition in vmdk

  1. now, we're gonna use the block device, not the mount point
  2. run "mount" to see what block device is in use for the mounting-point
  3. copy the image content

dd if=image of=/dev/vm-block-device bs=1024

  1. restart vmware. vmdk-mount gets its noodles screwed up by using dd so a simple unmount with

vmdk-mount -X 

may not work.

boot and enjoy and BTW if your boot record is screwed-up, boot with a Windows bootable floppy mounted or try to restore MBR and the boot sector using Windows recovery console (did not work for me)

How to zero out linux or cygwin disk

cat /dev/zero > /zero.dat; sync; sleep 1; sync; rm -f /zero.dat
or
dd if=/dev/zero of=/empty_file
rm /empty_file
then shrink -
vmware-vdiskmanager -k MyDisk.vmdk

How to clean disk space and reduce disk size

on windows get and run sdelete -z
dd if=/dev/zero of=/zerofill
rm /zerofill
dd if=/dev/zero of=/boot/zerofill
rm /boot/zerofill
sync

VBoxManage modifyhd somedisk.vdi --compact


How to convert between fixed and dynamic size vdi

You need to clone it in and then link to the VM to the new disk:

Fixed to dynamic:
VBoxManage clonehd [old] [new] --variant Standard

Dynamic to fixed:
VBoxManage clonehd [old] [new] --variant Fixed

@HowTo @Virtualization